Source of file SmartChimpSignupPage.php
Size: 10,581 Bytes - Last Modified: 2021-12-23T10:46:39+00:00
/var/www/docs.ssmods.com/process/src/code/SmartChimpSignupPage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 | <?php class SmartChimpSignupPage extends Page { public static $icon = "smartchimp/images/treeicons/SmartChimpSignupPage"; public static $db = array( // @todo: provide optional dropdown for entering username/password?? 'MCApiKey' => 'Varchar(50)', // api_key 'MCListKey' => 'Varchar(50)', // list_unique_id 'MCSuccessContent' => 'HTMLText', "DoubleOptin" => "Boolean", "SendWelcomeMail" => "Boolean", "SendGoodbey" => "Boolean", "SendDeleteNotification" => "Boolean", "IsDefaultList" => "Boolean", "FirstFieldRequired" => "Boolean", "LastFieldRequired" => "Boolean" ); public static $has_many = array( "SmartChimpNewsletters" => "SmartChimpNewsletter" ); public static $defaults = array( "DoubleOptin" => 1, "SendWelcomeMail" => 0, "SendGoodbey" => 0, "IsDefaultList" => 1 ); public static $mc_api_version = '1.2.1'; protected static $api = null; public function getAPI() { if ($this->MCApiKey && $this->MCListKey) { if (!(self::$api instanceof MCAPI)) { require_once(Director::baseFolder().'/smartchimp/thirdparty/mcapi/'.self::$mc_api_version.'/MCAPI.class.php'); self::$api = new MCAPI("$this->MCApiKey"); } return self::$api; } } public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldsToTab('Root.Content.SentNewsletters', array( new LiteralField('HowToRetrieve', '<p>To retrieve sent newsletters, simply save this page or <a href="'.$this->Link("update").'?flush=1">click here</a>.</p>'), $this->SmartChimpNewslettersTable() )); $fields->addFieldsToTab('Root.Content.MailChimpConfig', array( new CheckboxField('IsDefaultList', 'This is the default newsletter'), new TextField('MCApiKey', _t('SmartChimp.MCAPIKEY', 'API Key')), new TextField('MCListKey', _t('SmartChimp.MCLISTKEY', 'Unique ID for List')), new HTMLEditorField('MCSuccessContent', _t('SmartChimp.MCSuccessContent', 'Signup Success Content')) )); $fields->addFieldsToTab('Root.Content.Subscribe', array( new CheckboxField('DoubleOptin', "Double Opt-In Process (send email to confirm registration)"), new CheckboxField('SendWelcomeMail', "Send Welcome Mail") )); $fields->addFieldsToTab('Root.Content.Unsubscribe', array( new CheckboxField('SendGoodbey', "Send Goodbey Email"), new CheckboxField('SendDeleteNotification', "Send notification of unsubscribe") )); $fields->addFieldsToTab("Root.Content.RequiredFields", array( new CheckboxField('FirstRequired', "First name is required"), new CheckboxField('LastRequired', "Last name is required") )); $this->extend('updateSmartChimpCMSFields'); return $fields; } public function SmartChimpNewslettersTable() { $table = new HasManyComplexTableField( $controller = $this, $name = "SmartChimpNewsletters", $sourceClass = "SmartChimpNewsletter", $fieldList = null, $detailFormFields = null, $sourceFilter = "ParentID = ".$this->ID ); $table->setPageSize(100); $table->setPermissions(array('export', 'show', 'edit', 'delete')); return $table; } public function SmartChimpNewslettersShow() { return DataObject::get("SmartChimpNewsletter", "\"ParentID\" = ".$this->ID." AND \"Hide\" <> 1"); } public function RetrieveCampaigns() { $dos = new DataObjectSet(); SmartChimpNewsletter::clean_up_characters(); $api = $this->getAPI(); if ($api && $this->ID) { $campaignArray = $api->campaigns(array("list_id" => $this->MCListKey)); if (is_array($campaignArray) && count($campaignArray)) { foreach ($campaignArray as $key => $campaign) { if ($campaign["status"] == "sent") { $obj = DataObject::get_one("SmartChimpNewsletter", "`ParentID` = ".$this->ID." AND `CampaignID` = '".$campaign["id"]."'"); if ($obj) { //do nothing } else { $content = $api->campaignContent($campaign["id"]); if ($content) { $obj = new SmartChimpNewsletter(); $obj->ParentID = $this->ID; $obj->Date = $campaign["send_time"];// $obj->Title = $campaign["title"];// $obj->Subject = $campaign["subject"];// $obj->PermaLink = $campaign["archive_url"];// $obj->CampaignID = $campaign["id"];// $obj->WebID = $campaign["web_id"];// $obj->Status = $campaign["status"];//should be sent! //$obj->TextContent = $content["text"] ;//html //$obj->HTMLContent = $content["html"];//text } else { user_error(" could not retrieve content for newsletter with subject: ".$campaign["subject"]." AND ID".$campaign["id"], E_USER_NOTICE); } $obj->write(); } } } } } } public function subscribe($email, $firstname, $lastname) { $api = $this->getAPI(); if ($api) { $mergeVars = array( 'FNAME' => $firstname, 'LNAME' => $lastname ); //NOTE: update existing is set to false to not accidentally resubscribe someone. if (true === $api->listSubscribe($id = $this->MCListKey, $email, $mergeVars, $email_type='html', $this->DoubleOptin, $update_existing=false, $replace_interests=true, $this->SendWelcomeMail)) { return true; } else { return $api->errorMessage; } } } public function unsubscribe($email) { $api = $this->getAPI(); if ($api) { if (true === $api->listUnsubscribe($id = $this->MCListKey, $email, $delete_member=false, $this->SendGoodbey, $this->SendDeleteNotification)) { return true; } else { return $api->errorMessage; } } } public function onBeforeWrite() { parent::onBeforeWrite(); if ($this->IsDefaultList && $this->ID) { $others = DataObject::get("SmartChimpSignupPage", "`SmartChimpSignupPage`.`ID` <> ".intval($this->ID)." AND `SmartChimpSignupPage`.`IsDefaultList` = 1"); if ($others) { foreach ($others as $other) { $other->IsDefaultList = 0; $other->writeToStage('Stage'); $other->publish('Stage', 'Live'); } } } } public function onAfterWrite() { parent::onAfterWrite(); $this->RetrieveCampaigns(); } public function requireDefaultRecords() { parent::requireDefaultRecords(); $pages = DataObject::get("SmartChimpSignupPage"); if ($pages) { if ($pages->count() == 1) { foreach ($pages as $page) { if (!$page->IsDefaultList) { $page->IsDefaultList = 1; $page->writeToStage('Stage'); $page->publish('Stage', 'Live'); Database::alteration_message($page->ClassName.' created/updated: added IsDefaultList = true setting as there is only one SmartChimpSignupPage', 'edited'); } } } } } } class SmartChimpSignupPage_Controller extends Page_Controller { public static $allowed_actions = array("Form", "update"); public function init() { parent::init(); } public function update() { $this->RetrieveCampaigns(); return array(); } public function Form() { if (Session::get('SmartChimp.SUCCESS')) { Session::clear('SmartChimp.SUCCESS'); return false; } $requiredFields = new RequiredFields('email'); if ($this->FirstFieldRequired) { $requiredFields->appendRequiredFields(array('fname')); } if ($this->LastFieldRequired) { $requiredFields->appendRequiredFields(array('lname')); } $form = new Form($this, 'Form', new FieldSet( new TextField('fname', 'First name'), new TextField('lname', 'Last name'), new TextField('email', 'Email address') ), new FieldSet( new FormAction('SignupAction', 'Sign up') ), $requiredFields ); $this->extend('updateSmartChimpForm', $form); return $form; } public function ShortForm() { if (Session::get('SmartChimp.SUCCESS')) { Session::clear('SmartChimp.SUCCESS'); return false; } $form = new Form($this, 'Form', new FieldSet( new TextField('email', 'Email Address') ), new FieldSet( new FormAction('SignupAction', 'Sign up') ), new RequiredFields('email') ); $this->extend('updateSmartChimpForm', $form); return $form; } public function mcsuccess() { if (Session::get('SmartChimp.SUCCESS')) { $this->Content = $this->MCSuccessContent; } return array(); } public function SignupAction($raw_data, $form) { $data = Convert::raw2sql($raw_data); $outcome = $this->subscribe($data['email'], $data['fname'], $data['lname']); if (true === $outcome) { Session::set('SmartChimp.SUCCESS', true); return $this->mcsuccess(); } else { $form->sessionMessage($outcome, 'warning'); Director::redirectBack(); } } } |