From efcd6dd02e023a5e7777d7d65993466efe562dc3 Mon Sep 17 00:00:00 2001 From: Georg Ringer Date: Mon, 6 Jun 2016 21:14:12 +0200 Subject: [PATCH] [FEATURE] Finish interest groups --- Classes/Domain/Model/Dto/FormDto.php | 19 ++++++ Classes/Service/ApiService.php | 58 ++++++++++------ Readme.md | 15 +++-- Resources/Private/Templates/Form/Index.html | 74 ++++++++++++++++----- Tests/Unit/Domain/Model/Dto/FormDtoTest.php | 12 ++++ Tests/Unit/Service/ApiServiceTest.php | 34 ++++++++++ composer.json | 1 + ext_emconf.php | 2 +- 8 files changed, 173 insertions(+), 42 deletions(-) create mode 100644 Tests/Unit/Service/ApiServiceTest.php diff --git a/Classes/Domain/Model/Dto/FormDto.php b/Classes/Domain/Model/Dto/FormDto.php index 01b60c9..e6c6abf 100644 --- a/Classes/Domain/Model/Dto/FormDto.php +++ b/Classes/Domain/Model/Dto/FormDto.php @@ -20,6 +20,9 @@ class FormDto { /** @var array */ protected $interests; + /** @var string */ + protected $interest; + /** * @return string */ @@ -83,6 +86,22 @@ public function setInterests($interests) { $this->interests = $interests; } + + /** + * @return string + */ + public function getInterest() + { + return $this->interest; + } + + /** + * @param string $interest + */ + public function setInterest($interest) + { + $this->interest = $interest; + } } \ No newline at end of file diff --git a/Classes/Service/ApiService.php b/Classes/Service/ApiService.php index eba7ebc..3370598 100644 --- a/Classes/Service/ApiService.php +++ b/Classes/Service/ApiService.php @@ -2,6 +2,7 @@ namespace Sup7\Mailchimp\Service; +use DrewM\MailChimp\MailChimp; use Sup7\Mailchimp\Domain\Model\Dto\FormDto; use Sup7\Mailchimp\Exception\GeneralException; use Sup7\Mailchimp\Exception\MemberExistsException; @@ -11,7 +12,7 @@ class ApiService { - + /** @var MailChimp */ protected $api; /** @var $logger Logger */ @@ -25,7 +26,7 @@ public function __construct() $extensionConfiguration = GeneralUtility::makeInstance('Sup7\\Mailchimp\\Domain\\Model\\Dto\\ExtensionConfiguration'); $apiKey = $extensionConfiguration->getApiKey(); - $this->api = new \DrewM\MailChimp\MailChimp($apiKey); + $this->api = new MailChimp($apiKey); $this->logger = GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')->getLogger(__CLASS__); } @@ -49,7 +50,8 @@ public function getLists() * @param string $list * @return array|false */ - public function getList($list) { + public function getList($list) + { return $this->api->get('lists/' . $list); } @@ -78,17 +80,18 @@ public function getInterestLists($listId) */ public function getCategories($listId, $interestId) { - $groupData = $this->api->get('lists/' . $listId . '/interest-categories/' . $interestId .'/'); + $groupData = $this->api->get('lists/' . $listId . '/interest-categories/' . $interestId . '/'); $result = array( 'title' => $groupData['title'], 'type' => $groupData['type'] ); $list = $this->api->get('lists/' . $listId . '/interest-categories/' . $interestId . '/interests'); - foreach ($list['interests'] as $group) { - $result['options'][$group['id']] = $group['name']; + if (isset($list['interests']) && is_array($list['interests'])) { + foreach ($list['interests'] as $group) { + $result['options'][$group['id']] = $group['name']; + } } - return $result; } @@ -109,19 +112,11 @@ public function register($listId, FormDto $form) 'FNAME' => $form->getFirstName(), 'LNAME' => $form->getLastName(), ), - 'intxerests' => array(), + 'interests' => array(), ); - $interests = $form->getInterests(); - if ($interests) { - $interestData = array(); - foreach ($interests as $id => $state) { - if ($state) { - $interestData[$id] = true; - } - } - if ($interestData) { - $data['interests'] = $interestData; - } + $interestData = $this->getInterests($form); + if ($interestData) { + $data['interests'] = $interestData; } $response = $this->api->post("lists/$listId/members", $data); @@ -136,4 +131,29 @@ public function register($listId, FormDto $form) throw new GeneralException($response['detail']); } } + + /** + * @param FormDto $form + * @return array + */ + protected function getInterests(FormDto $form) + { + $interestData = array(); + // multi interests + $interests = $form->getInterests(); + if ($interests) { + foreach ($interests as $id => $state) { + if ($state) { + $interestData[$id] = true; + } + } + } + // single interests + $interest = $form->getInterest(); + if ($interests) { + $interestData[$interest] = true; + return $interestData; + } + return $interestData; + } } \ No newline at end of file diff --git a/Readme.md b/Readme.md index 7da746c..a25b2bb 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,4 @@ -# TYPO3 CMS Extension mailchimp +# TYPO3 CMS Extension ``mailchimp` [![Build Status](https://travis-ci.org/sup7even/mailchimp.svg?branch=master)](https://travis-ci.org/sup7even/mailchimp) @@ -6,7 +6,8 @@ This extension implements the most important feature of MailChimp: Let the users ## Requirements -- TYPO3 CMS 6.2+ +- TYPO3 CMS 6.2+ - 8.2 +- PHP 5.6 - 7 - MailChimp API key - License: GPL 2 @@ -14,11 +15,11 @@ This extension implements the most important feature of MailChimp: Let the users 1) Install the extension as any other extension. 2) Add the Mailchimp API key in the Extension Manager configuration. -3) Create a new plugin `MailChimp`on any page -4) Select a list you want the users register to and press *Save* +3) Create a new plugin `MailChimp` on any page. +4) Select a list you want the users register to and press *Save*. 5) Optional: Select an interest group and save again. -### Integration in EXT:formhandler` +### Integration in ``EXT:formhandler` You can integrate `formhandler` by using the following finisher: @@ -64,11 +65,11 @@ plugin.tx_mailchimp { } ``` -## Roadmap +## Roadmap & Development This extension is in its early beginnings. It is not yet defined if more features will be added or not! -## Tests +### UnitTests Unit Tests can be started by using diff --git a/Resources/Private/Templates/Form/Index.html b/Resources/Private/Templates/Form/Index.html index c4d14b1..aaacf9e 100644 --- a/Resources/Private/Templates/Form/Index.html +++ b/Resources/Private/Templates/Form/Index.html @@ -43,21 +43,21 @@ - -
- -
- -
- -
-
-
-
+ + + + + + + + + + + + + + +
@@ -70,6 +70,50 @@ + +
+ +
+ +
+ +
+
+
+
+
+ + +
+ +
+ +
+ +
+
+
+
+
+ + +
+ +
+ +
+
+
+ diff --git a/Tests/Unit/Domain/Model/Dto/FormDtoTest.php b/Tests/Unit/Domain/Model/Dto/FormDtoTest.php index 1b32994..a5cb8e7 100644 --- a/Tests/Unit/Domain/Model/Dto/FormDtoTest.php +++ b/Tests/Unit/Domain/Model/Dto/FormDtoTest.php @@ -50,4 +50,16 @@ public function interestsCanBeSet() $domainModelInstance->setInterests($subject); $this->assertEquals($subject, $domainModelInstance->getInterests()); } + + + /** + * @test + */ + public function interestCanBeSet() + { + $domainModelInstance = new FormDto(); + $subject = '12345'; + $domainModelInstance->setInterest($subject); + $this->assertEquals($subject, $domainModelInstance->getInterest()); + } } \ No newline at end of file diff --git a/Tests/Unit/Service/ApiServiceTest.php b/Tests/Unit/Service/ApiServiceTest.php new file mode 100644 index 0000000..8b672af --- /dev/null +++ b/Tests/Unit/Service/ApiServiceTest.php @@ -0,0 +1,34 @@ +getAccessibleMock(ApiService::class, array('dummy'), [], '', false); + $form = new FormDto(); + $interests = [ + '123' => 0, + '456' => true, + '789' => false, + '012' => 1 + ]; + $form->setInterests($interests); + $form->setInterest('345'); + $expected = [ + '456' => true, + '012' => true, + '345' => true + ]; + $this->assertEquals($expected, $mockedApiService->_call('getInterests', $form)); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index c51eb97..1bfe502 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "name": "sup7even/mailchimp", "type": "typo3-cms-extension", + "version": "1.1.0", "description": "Simple mailchimp integration to let users register to a specific list", "keywords": [ "TYPO3", diff --git a/ext_emconf.php b/ext_emconf.php index afd1e24..aa3261d 100644 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -11,7 +11,7 @@ 'uploadfolder' => '1', 'createDirs' => '', 'clearCacheOnLoad' => 0, - 'version' => '1.0.0', + 'version' => '1.1.0', 'constraints' => array( 'depends' => array( 'typo3' => '6.2.0-7.6.99'