|
| 1 | +# Mailchimp |
| 2 | + |
| 3 | +## Content |
| 4 | + |
| 5 | +- [Configuration](#configuration) |
| 6 | +- [Services available in DI container](#services-available-in-di-container) |
| 7 | +- [Usage](#usage) |
| 8 | + |
| 9 | +## Configuration |
| 10 | + |
| 11 | +You have to register this extension at first. |
| 12 | + |
| 13 | +```yaml |
| 14 | +extensions: |
| 15 | + trejjam.mailchimp: Trejjam\MailChimp\DI\MailChimpExtension |
| 16 | +``` |
| 17 | +
|
| 18 | +List of all options: |
| 19 | +
|
| 20 | +```yaml |
| 21 | +trejjam.mailchimp: |
| 22 | + findDataCenter: true/false # enabled by default, if false apiUrl mus be filled in compatible way with apiKey |
| 23 | + apiUrl: 'https://%s.api.mailchimp.com/%s/' #default, first placeholder is data center extracted from apiKey, second is for API version |
| 24 | + apiKey: 'someApiKey123-us11' |
| 25 | + lists: |
| 26 | + newsletter: 'foo123' |
| 27 | + segments: |
| 28 | + newsletter: |
| 29 | + bar: 123 |
| 30 | +``` |
| 31 | +
|
| 32 | +Minimal production configuration: |
| 33 | +
|
| 34 | +```yaml |
| 35 | +extensions: |
| 36 | + trejjam.mailchimp: Trejjam\MailChimp\DI\MailChimpExtension |
| 37 | + |
| 38 | +trejjam.mailchimp: |
| 39 | + apiKey: 'someApiKey123-us11' |
| 40 | +``` |
| 41 | +
|
| 42 | +## Services available in DI container |
| 43 | +
|
| 44 | +- [`Trejjam\MailChimp\Request`](https://github.com/trejjam/mailchimp/blob/master/src/Request.php) |
| 45 | + This class is supposed to perform http requests with API token through `GuzzleHttp\Client` |
| 46 | +- [`Trejjam\MailChimp\Context`](https://github.com/trejjam/mailchimp/blob/master/src/Context.php) |
| 47 | + Kind of root node, contains getter for existing implementation of API scopes |
| 48 | +- [`Trejjam\MailChimp\Group\Root`](https://github.com/trejjam/mailchimp/blob/master/src/Group/Root.php) |
| 49 | + The simplest API scope, provides only one `get` method returning object with user info (related to API token) |
| 50 | +- [`Trejjam\MailChimp\Group\Lists`](https://github.com/trejjam/mailchimp/blob/master/src/Group/Lists.php) |
| 51 | + API scope containing methods to manipulate with entities in Mailchimp lists. See class content to check available operations. |
| 52 | +- [`Trejjam\MailChimp\Lists`](https://github.com/trejjam/mailchimp/blob/master/src/Lists.php) |
| 53 | + Configuration object supposed to provide mapping between lists in Mailchimp environment and local friendly names |
| 54 | +- [`Trejjam\MailChimp\Segments`](https://github.com/trejjam/mailchimp/blob/master/src/Segments.php) |
| 55 | + Configuration object supposed to provide mapping between segments (part of a list) in Mailchimp environment and local friendly names |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +Example user subscribe component |
| 60 | + |
| 61 | +`OrderPresenter.php`: |
| 62 | +```php |
| 63 | +/** |
| 64 | + * @method onNewsletterSubscribe(Trejjam\MailChimp\Entity\Lists\Member\MemberItem $memberItem) |
| 65 | + * @method onNewsletterSubscribeError(Trejjam\MailChimp\Entity\Lists\Member\MemberItem $memberItem) |
| 66 | + */ |
| 67 | +final class MessageFactory extends UI\Component |
| 68 | +{ |
| 69 | + /** |
| 70 | + * @var Trejjam\MailChimp\Lists |
| 71 | + */ |
| 72 | + private $mailchimpListDictionary; |
| 73 | + /** |
| 74 | + * @var Trejjam\MailChimp\Group\Lists |
| 75 | + */ |
| 76 | + private $mailchimpList; |
| 77 | +
|
| 78 | + /** |
| 79 | + * @var callable[] |
| 80 | + */ |
| 81 | + public $onNewsletterSubscribe = []; |
| 82 | + /** |
| 83 | + * @var callable[] |
| 84 | + */ |
| 85 | + public $onNewsletterSubscribeError = []; |
| 86 | +
|
| 87 | + public function __construct( |
| 88 | + Trejjam\MailChimp\Lists $mailchimpListDictionary, |
| 89 | + Trejjam\MailChimp\Group\Lists $mailchimpList |
| 90 | + ) { |
| 91 | + parent::__construct(); |
| 92 | +
|
| 93 | + $this->mailchimpListDictionary = $mailchimpListDictionary; |
| 94 | + $this->mailchimpList = $mailchimpList; |
| 95 | + } |
| 96 | +
|
| 97 | + protected function createComponentNewsletter() |
| 98 | + { |
| 99 | + $form = new Nette\Application\UI\Form; |
| 100 | +
|
| 101 | + $form->addEmail('email', 'email') |
| 102 | + ->setRequired() |
| 103 | + ->addRule($form::EMAIL); |
| 104 | +
|
| 105 | + $form->addSubmit('submit', 'submit'); |
| 106 | + $form->onSuccess[] = [$this, 'processNewsletterSubscribe']; |
| 107 | +
|
| 108 | + return $form; |
| 109 | + } |
| 110 | +
|
| 111 | + public function processNewsletterSubscribe(Nette\Application\UI\Form $form, \stdClass $values) : void |
| 112 | + { |
| 113 | + $memberItem = Trejjam\MailChimp\Entity\Lists\Member\MemberItem::create( |
| 114 | + $values->email, |
| 115 | + $this->mailchimpListDictionary->getListByName('newsletter'), |
| 116 | + Trejjam\MailChimp\Entity\Lists\Member\MemberItem::STATUS_PENDING |
| 117 | + ); |
| 118 | +
|
| 119 | + try { |
| 120 | + $this->mailchimpList->addMember($memberItem); |
| 121 | +
|
| 122 | + $this->onNewsletterSubscribe($memberItem); |
| 123 | + } catch (Trejjam\MailChimp\Exception\MemberNotFoundException $e) { |
| 124 | + $this->onNewsletterSubscribeError($memberItem); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
0 commit comments