Skip to content

Commit

Permalink
Updates and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
colintucker committed May 1, 2018
1 parent 075ab99 commit ff3e90d
Show file tree
Hide file tree
Showing 14 changed files with 1,091 additions and 3 deletions.
Binary file added admin/client/dist/images/icons/SubscribePage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added admin/client/dist/images/icons/UnsubscribePage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added admin/client/src/images/icons/SubscribePage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added admin/client/src/images/icons/UnsubscribePage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/API/MailChimpAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ public function put($method, $params = [])
return $this->request('put', $method, $params);
}

/**
* Answers an MD5 hash for the given email address (used for API actions).
*
* @param string $email
*
* @return string
*/
public function hash($email)
{
return md5(strtolower(trim($email)));
}

/**
* Answers true if the last request was successful.
*
Expand Down
16 changes: 13 additions & 3 deletions src/Components/MailChimpSignupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public function Form()

$form->restoreFormState();

// Extend Form Object:

$this->extend('updateForm', $form);

// Answer Form Object:

return $form;
Expand Down Expand Up @@ -187,7 +191,7 @@ public function doSubscribe($data, $form)

// Obtain API Response:

$response = $this->api->post(
$response = $this->api->put(
$this->getSubscribeMethod(),
[
'email_address' => $data['Email'],
Expand Down Expand Up @@ -276,10 +280,16 @@ public function doSubscribe($data, $form)
/**
* Answers the API method for adding the subscriber.
*
* @param array $data
*
* @return string
*/
protected function getSubscribeMethod()
protected function getSubscribeMethod($data)
{
return sprintf('lists/%s/members', $this->ListID);
return sprintf(
'lists/%s/members/%s',
$this->ListID,
$this->api->hash($data['Email'])
);
}
}
297 changes: 297 additions & 0 deletions src/Pages/SubscribePage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
<?php

/**
* This file is part of SilverWare.
*
* PHP version >=5.6.0
*
* For full copyright and license information, please view the
* LICENSE.md file that was distributed with this source code.
*
* @package SilverWare\MailChimp\Pages
* @author Colin Tucker <[email protected]>
* @copyright 2018 Praxis Interactive
* @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @link https://github.com/praxisnetau/silverware-mailchimp
*/

namespace SilverWare\MailChimp\Pages;

use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\TextField;
use SilverWare\Forms\FieldSection;
use SilverWare\MailChimp\Forms\MailChimpListField;
use Page;

/**
* An extension of the page class for a MailChimp subscribe page.
*
* @package SilverWare\MailChimp\Pages
* @author Colin Tucker <[email protected]>
* @copyright 2018 Praxis Interactive
* @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @link https://github.com/praxisnetau/silverware-ma
*/
class SubscribePage extends Page
{
/**
* Human-readable singular name.
*
* @var string
* @config
*/
private static $singular_name = 'Subscribe Page';

/**
* Human-readable plural name.
*
* @var string
* @config
*/
private static $plural_name = 'Subscribe Pages';

/**
* Description of this object.
*
* @var string
* @config
*/
private static $description = 'Allows a user to subscribe to a MailChimp mailing list';

/**
* Icon file for this object.
*
* @var string
* @config
*/
private static $icon = 'silverware/mailchimp: admin/client/dist/images/icons/SubscribePage.png';

/**
* Defines the table name to use for this object.
*
* @var string
* @config
*/
private static $table_name = 'SilverWare_MailChimp_SubscribePage';

/**
* Maps field names to field types for this object.
*
* @var array
* @config
*/
private static $db = [
'ListID' => 'Varchar(32)',
'ShowFirstName' => 'Boolean',
'ShowLastName' => 'Boolean',
'RequireFirstName' => 'Boolean',
'RequireLastName' => 'Boolean',
'OnErrorMessage' => 'Varchar(255)',
'OnSubscribeMessage' => 'Varchar(255)',
'OnAlreadySubscribedMessage' => 'Varchar(255)'
];

/**
* Defines the default values for the fields of this object.
*
* @var array
* @config
*/
private static $defaults = [
'ShowFirstName' => 1,
'RequireFirstName' => 1
];

/**
* Answers a list of field objects for the CMS interface.
*
* @return FieldList
*/
public function getCMSFields()
{
// Obtain Field Objects (from parent):

$fields = parent::getCMSFields();

// Add Status Message (if exists):

$fields->addStatusMessage($this->getSiteConfig()->getMailChimpStatusMessage());

// Define Placeholder:

$placeholder = _t(__CLASS__ . '.DROPDOWNSELECT', 'Select');

// Create Main Fields:

$fields->addFieldToTab(
'Root.Main',
MailChimpListField::create(
'ListID',
$this->fieldLabel('ListID')
)->setEmptyString(' ')->setAttribute('data-placeholder', $placeholder),
'Content'
);

// Create Options Fields:

$fields->addFieldsToTab(
'Root.Options',
[
FieldSection::create(
'FieldOptions',
$this->fieldLabel('Fields'),
[
FieldGroup::create(
$this->fieldLabel('ShowFields'),
[
CheckboxField::create(
'ShowFirstName',
$this->fieldLabel('FirstName')
),
CheckboxField::create(
'ShowLastName',
$this->fieldLabel('LastName')
)
]
),
FieldGroup::create(
$this->fieldLabel('RequireFields'),
[
CheckboxField::create(
'RequireFirstName',
$this->fieldLabel('FirstName')
),
CheckboxField::create(
'RequireLastName',
$this->fieldLabel('LastName')
)
]
)
]
),
FieldSection::create(
'MessageOptions',
$this->fieldLabel('Messages'),
[
TextField::create(
'OnSubscribeMessage',
$this->fieldLabel('OnSubscribeMessage')
)->setRightTitle(
_t(
__CLASS__ . '.ONSUBSCRIBEMESSAGERIGHTTITLE',
'Shown to the user after subscribing.'
)
),
TextField::create(
'OnAlreadySubscribedMessage',
$this->fieldLabel('OnAlreadySubscribedMessage')
)->setRightTitle(
_t(
__CLASS__ . '.ONALREADYSUBSCRIBEDMESSAGERIGHTTITLE',
'Shown to the user if they have already subscribed.'
)
),
TextField::create(
'OnErrorMessage',
$this->fieldLabel('OnErrorMessage')
)->setRightTitle(
_t(
__CLASS__ . '.ONERRORMESSAGERIGHTTITLE',
'Shown to the user when an error occurs.'
)
)
]
)
]
);

// Answer Field Objects:

return $fields;
}

/**
* Answers the labels for the fields of the receiver.
*
* @param boolean $includerelations Include labels for relations.
*
* @return array
*/
public function fieldLabels($includerelations = true)
{
// Obtain Field Labels (from parent):

$labels = parent::fieldLabels($includerelations);

// Define Field Labels:

$labels['ListID'] = _t(__CLASS__ . '.MAILINGLIST', 'Mailing List');
$labels['Fields'] = _t(__CLASS__ . '.FIELDS', 'Fields');
$labels['Messages'] = _t(__CLASS__ . '.MESSAGES', 'Messages');
$labels['LastName'] = _t(__CLASS__ . '.REQUIRELASTNAME', 'Last name');
$labels['FirstName'] = _t(__CLASS__ . '.REQUIREFIRSTNAME', 'First name');
$labels['ShowFields'] = _t(__CLASS__ . '.SHOWFIELDS', 'Show fields');
$labels['RequireFields'] = _t(__CLASS__ . '.REQUIREFIELDS', 'Require fields');

// Define Message Field Labels:

$labels['OnErrorMessage'] = _t(
__CLASS__ . '.ONERRORMESSAGE',
'On Error message'
);

$labels['OnSubscribeMessage'] = _t(
__CLASS__ . '.ONSUBSCRIBEMESSAGE',
'On Subscribe message'
);

$labels['OnAlreadySubscribedMessage'] = _t(
__CLASS__ . '.ONALREADYSUBSCRIBEDMESSAGE',
'On Already Subscribed message'
);

// Answer Field Labels:

return $labels;
}

/**
* Populates the default values for the fields of the receiver.
*
* @return void
*/
public function populateDefaults()
{
// Populate Defaults (from parent):

parent::populateDefaults();

// Populate Defaults:

$this->OnErrorMessage = _t(
__CLASS__ . '.DEFAULTONERRORMESSAGE',
'Sorry, an error has occurred. Please try again later.'
);

$this->OnSubscribeMessage = _t(
__CLASS__ . '.DEFAULTONSUBSCRIBEMESSAGE',
'Thank you for subscribing to our mailing list.'
);

$this->OnAlreadySubscribedMessage = _t(
__CLASS__ . '.DEFAULTONALREADYSUBSCRIBEDMESSAGE',
'You have already subscribed to our mailing list.'
);
}

/**
* Answers a message string to be shown when no list is available.
*
* @return string
*/
public function getNoListMessage()
{
return _t(__CLASS__ . '.NOLISTAVAILABLE', 'No list available.');
}
}
Loading

0 comments on commit ff3e90d

Please sign in to comment.