Permalink
Browse files

ENHANCEMENT sync fields can now be set up in SiteConfig or Hardcoded.…

… ENHANCEMENT MailCHimp will now detect some of the data-types and set up the correct merge tag type (text, date, & number).

Signed-off-by: Cam Findlay <cam@camfindlay.com>
  • Loading branch information...
1 parent a3b2ff5 commit 130387199161d27796ced699663cea8b7e5fa834 @camfindlay camfindlay committed Nov 14, 2011
Showing with 138 additions and 61 deletions.
  1. +4 −1 _config.php
  2. +49 −7 code/APES.php
  3. +26 −20 code/mailchimp/MailChimp.php
  4. +59 −33 code/mailchimp/MailChimpSiteConfig.php
View
@@ -1,2 +1,5 @@
<?php
-Object::add_extension('Member', 'APES');
+/*Uncomment or move to /mysite/_config.php for MailChimp List Service*/
+//Object::add_extension('Member', 'MailChimp');
+//Object::add_extension('SiteConfig', 'MailChimpSiteConfig');
+
View
@@ -10,22 +10,64 @@
* @author Cam Findlay <cam@camfindlay.com>
* @copyright 2011 Cam Findlay Consulting
* @version SVN: $Id$
- * @uses Member
*/
class APES extends DataObjectDecorator {
-
-
- public static $syncFields = array(
- );
-
+
+
+
+ public static $syncFields = array();
+
+
+
+
+
+ /**
+ * Sets the sync fields either with hardcoded array or sets up SiteConfig to accept comma separated list
+ */
public static function setSyncFields($fields){
- self::$syncFields = $fields;
+ switch($fields) {
+
+ case 'SiteConfig':
+ self::$syncFields = null;
+ Object::add_extension('SiteConfig', 'APESConfig');
+ break;
+ default: self::$syncFields = $fields;
+ }
+
+
+
}
+ /**
+ * Returns either the hardcoded or SiteCOnfig set SyncFields
+ * @return array the fields to be sync'd
+ */
+ public static function getSyncFields(){
+ $siteconfig = SiteConfig::current_site_config();
+ if(self::$syncFields <> null && is_array(self::$syncFields)){
+ $array = self::$syncFields;
+ } else {
+ $array = explode(',',$siteconfig->APESSyncFields);
+ }
+
+ if(empty($array[0])){
+ return false;
+ }else{
+ return $array;
+ }
+ }
+
+
+
+
+
+
+
+
}
@@ -46,12 +46,10 @@ public function updateCMSFields(FieldSet &$fields) {}
public function updateFrontEndFields($fields) {}
- public function onBeforeWrite(){}
+ public function onBeforeWrite(){
- public function onAfterWrite(){
-
- $siteconfig = SiteConfig::current_site_config();
+ $siteconfig = SiteConfig::current_site_config();
if($siteconfig->MailchimpInstalled){
@@ -62,37 +60,45 @@ public function onAfterWrite(){
);
-
- foreach(APES::$syncFields as $field){
- $tag = strtoupper(substr($field,0,8));
-
-
+ //setup the fields to sync if they have been entered in to SiteConfig
+ if(APES::getSyncFields()){
+ $fields = APES::getSyncFields();
+ foreach($fields as $field){
+ $tag = strtoupper(substr($field,0,8));
$merge_vars[$tag] = $this->owner->$field;
-
-
- }
+ }
+ }
-
-
- //add custom syncfields here
- //@todo use statics to set the double optin etc
- if(!$this->isUnsubscribed($this->owner->Email)){
- $api->listSubscribe( $siteconfig->MailchimpListId, $this->owner->Email,$merge_vars,'html',false,true,true,false);
+ //make sure the member did't unsubscribe
+ if(!$this->isUnsubscribed($this->owner->Email)){
- $memberinfo = $api->listMemberInfo($siteconfig->MailchimpListId, $this->owner->Email);
+ $doubleoptin = ($siteconfig->DoubleOptIn == 1) ? true : false;
+ //add them or update them in the list.
+ $api->listSubscribe( $siteconfig->MailchimpListId, $this->owner->Email,$merge_vars,'html',$doubleoptin,true,true,false);
+ //return the unique MC id for this member - may look at using this later to do 2 way sync.
+ $memberinfo = $api->listMemberInfo($siteconfig->MailchimpListId, $this->owner->Email);
$this->owner->MailChimpID = $memberinfo['id'];
+
}
}
-}
+
+
+
+ }
+
+
+ public function onAfterWrite(){
+
+ }
public function onBeforeDelete(){
//remove the member from mailchimp if they ask to be deleted
@@ -23,45 +23,30 @@ public function extraStatics(){
'MailchimpListId' => 'Text',
'MailchimpInstalled' => 'Boolean',
- ),
-
- 'has_one' => array(
- ),
-
- 'has_many' => array(
- ),
-
- 'summary_fields' => array(
- ),
-
- 'searchable_fields' => array(
- ),
-
- 'field_labels' => array(
- )
-
-
+ )
);
}
- public function updateCMSFields(FieldSet &$fields) {
-
+public function updateCMSFields(FieldSet &$fields) {
+ $siteconfig = $this->owner;
+
+ $fields->addFieldToTab('Root.APES', new HeaderField('MailChimp Setup'));
- /*Custom Mailchimp Settings
- *
- */
- $fields->addFieldToTab("Root.MailChimp", new TextField("MailchimpApiKey", 'MailChimp API Key'));
- $fields->addFieldToTab("Root.MailChimp", new TextField("MailchimpListId", 'MailChimp List ID'));
+ $fields->addFieldToTab("Root.APES", new TextField("MailchimpApiKey", 'MailChimp API Key'));
+ $fields->addFieldToTab("Root.APES", new TextField("MailchimpListId", 'MailChimp List ID'));
- $installed = new TextField("MailchimpInstalled",'Is MailChimp Installed?');
+ $installed = new CheckboxField("MailchimpInstalled",'Is MailChimp Installed?');
$installed = $installed->transform(new ReadonlyTransformation());
- $fields->addFieldToTab('Root.MailChimp',$installed);
-
+
+
+ $fields->addFieldToTab('Root.APES',$installed);
+
}
+
- public function updateFrontEndFields($fields) {}
+
public function onBeforeWrite(){
$siteconfig = $this->owner;
@@ -76,14 +61,20 @@ public function onBeforeWrite(){
public function onAfterWrite(){
- //only run the mailchimp extensions if they have set a API ket and List ID
+ //only run the mailchimp extensions if they have set a API key, List ID and some sync fields
$siteconfig = $this->owner;
- if($siteconfig->MailchimpApiKey && $siteconfig->MailchimpListId){
+ if($siteconfig->MailchimpApiKey && $siteconfig->MailchimpListId && APES::getSyncFields()){
$api = new MCAPI($siteconfig->MailchimpApiKey);
- $fields = APES::$syncFields;
+
+
+ $fields = APES::getSyncFields();
+
+
+
+
$mergefields = array();
@@ -95,8 +86,43 @@ public function onAfterWrite(){
foreach($fields as $field){
if(!in_array($field, $mergefields)){
$tag = strtoupper(substr($field,0,8));
+
+ //find the type of data in the field - text, number, radio, dropdown, date, address, phone, url, imageurl
+ $datatype = DataObject::database_fields('Member');
+
+ switch($datatype[$field]){
+
+ case 'Date':
+ $field_type = 'date';
+ break;
+
+ case 'SS_Datetime':
+ $field_type = 'date';
+ break;
+
+ case 'Int':
+ $field_type = 'number';
+ break;
+
+ case 'Currency':
+ $field_type = 'number';
+ break;
+
+ case 'Decimal':
+ $field_type = 'number';
+ break;
+
+ case 'Percentage':
+ $field_type = 'number';
+ break;
+
+ default:
+ $field_type = 'text';
+ break;
+
+ }
- $api->listMergeVarAdd($siteconfig->MailchimpListId, $tag, $field, array('field_type'=>'text') );
+ $api->listMergeVarAdd($siteconfig->MailchimpListId, $tag, $field, array('field_type'=>$field_type) );

0 comments on commit 1303871

Please sign in to comment.