Skip to content

Commit

Permalink
[FEATURE] Finish interest groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Georg Ringer committed Jun 6, 2016
1 parent 1865a28 commit efcd6dd
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 42 deletions.
19 changes: 19 additions & 0 deletions Classes/Domain/Model/Dto/FormDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class FormDto {
/** @var array */
protected $interests;

/** @var string */
protected $interest;

/**
* @return string
*/
Expand Down Expand Up @@ -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;
}


}
58 changes: 39 additions & 19 deletions Classes/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,7 +12,7 @@

class ApiService
{

/** @var MailChimp */
protected $api;

/** @var $logger Logger */
Expand All @@ -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__);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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;
}
}
15 changes: 8 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# 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)

This extension implements the most important feature of MailChimp: Let the users register for a specific list, including interest groups!

## Requirements

- TYPO3 CMS 6.2+
- TYPO3 CMS 6.2+ - 8.2
- PHP 5.6 - 7
- MailChimp API key
- License: GPL 2

## Installation + Configuration

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:

Expand Down Expand Up @@ -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

Expand Down
74 changes: 59 additions & 15 deletions Resources/Private/Templates/Form/Index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@
</div>
</div>

<f:if condition="{interests.options}">
<div class="form-group">
<label class="col-sm-3 control-label">{interests.title}</label>
<div class="col-sm-9">
<f:for each="{interests.options}" as="interest" key="id">
<div class="checkbox">
<label>
<f:form.checkbox property="interests.{id}" value="1"/>
<f:translate key="interest.{interest -> mc:simplifyLabel()}"
default="{interest -> f:format.htmlspecialchars()}"/>
</label>
</div>
</f:for>
</div>
</div>
<f:if condition="{interests}">
<f:switch expression="{interests.type}">
<f:case value="checkboxes">
<f:render section="interestType-checkboxes" arguments="{_all}"/>
</f:case>
<f:case value="radio">
<f:render section="interestType-radio" arguments="{_all}"/>
</f:case>
<f:case value="dropdown">
<f:render section="interestType-dropdown" arguments="{_all}"/>
</f:case>
<f:case value="hidden">
<f:comment><!-- do nothing --></f:comment>
</f:case>
</f:switch>
</f:if>

<div class="form-group">
Expand All @@ -70,6 +70,50 @@
</f:form>
</f:section>

<f:section name="interestType-checkboxes">
<div class="form-group">
<label class="col-sm-3 control-label">{interests.title}</label>
<div class="col-sm-9">
<f:for each="{interests.options}" as="interest" key="id">
<div class="checkbox">
<label>
<f:form.checkbox property="interests.{id}" value="1"/>
<f:translate key="interest.{interest -> mc:simplifyLabel()}"
default="{interest -> f:format.htmlspecialchars()}"/>
</label>
</div>
</f:for>
</div>
</div>
</f:section>

<f:section name="interestType-radio">
<div class="form-group">
<label class="col-sm-3 control-label">{interests.title}</label>
<div class="col-sm-9">
<f:for each="{interests.options}" as="interest" key="id">
<div class="checkbox">
<label>
<f:form.radio property="interest" value="{id}"/>
<f:translate key="interest.{interest -> mc:simplifyLabel()}"
default="{interest -> f:format.htmlspecialchars()}"/>
</label>
</div>
</f:for>
</div>
</div>
</f:section>

<f:section name="interestType-dropdown">
<div class="form-group">
<label class="col-sm-3 control-label">{interests.title}</label>
<div class="col-sm-9">
<f:form.select class="form-control" property="interest" options="{interests.options}"
prependOptionLabel="" prependOptionValue=""/>
</div>
</div>
</f:section>

<f:section name="formUrl">
<mc:ajaxEnabled isEnabled="{settings.useAjax}">
<f:render partial="FormAjaxUrl"/>
Expand Down
12 changes: 12 additions & 0 deletions Tests/Unit/Domain/Model/Dto/FormDtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
34 changes: 34 additions & 0 deletions Tests/Unit/Service/ApiServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Sup7\Mailchimp\Tests\Unit\Service;

use Sup7\Mailchimp\Domain\Model\Dto\FormDto;
use Sup7\Mailchimp\Service\ApiService;
use TYPO3\CMS\Core\Tests\UnitTestCase;

class ApiServiceTest extends UnitTestCase
{

/**
* @test
*/
public function properInterestsAreReturned()
{
$mockedApiService = $this->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));
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit efcd6dd

Please sign in to comment.