public function newAction()
 {
     if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
         $session = Mage::getSingleton('core/session');
         $email = (string) $this->getRequest()->getPost('email');
         Mage::log("Fontis_CampaignMonitor: Adding newsletter subscription via frontend 'Sign up' block for {$email}");
         $apiKey = trim(Mage::getStoreConfig('newsletter/campaignmonitor/api_key'));
         $listID = trim(Mage::getStoreConfig('newsletter/campaignmonitor/list_id'));
         if ($apiKey && $listID) {
             try {
                 $client = new SoapClient("http://api.createsend.com/api/api.asmx?wsdl", array("trace" => true));
             } catch (Exception $e) {
                 Mage::log("Fontis_CampaignMonitor: Error connecting to CampaignMonitor server: " . $e->getMessage());
                 $session->addException($e, $this->__('There was a problem with the subscription'));
                 $this->_redirectReferer();
             }
             // if a user is logged in, fill in the Campaign Monitor custom
             // attributes with the data for the logged-in user
             $customerHelper = Mage::helper('customer');
             if ($customerHelper->isLoggedIn()) {
                 $customer = $customerHelper->getCustomer();
                 $name = $customer->getFirstname() . " " . $customer->getLastname();
                 $customFields = Fontis_CampaignMonitor_Model_Customer_Observer::generateCustomFields($customer);
                 try {
                     $result = $client->AddAndResubscribeWithCustomFields(array("ApiKey" => $apiKey, "ListID" => $listID, "Email" => $email, "Name" => $name, "CustomFields" => $customFields));
                 } catch (Exception $e) {
                     Mage::log("Fontis_CampaignMonitor: Error in CampaignMonitor SOAP call: " . $e->getMessage());
                     $session->addException($e, $this->__('There was a problem with the subscription'));
                     $this->_redirectReferer();
                 }
             } else {
                 // otherwise if nobody's logged in, ignore the custom
                 // attributes and just set the name to '(Guest)'
                 try {
                     $result = $client->AddAndResubscribe(array("ApiKey" => $apiKey, "ListID" => $listID, "Email" => $email, "Name" => "(Guest)"));
                 } catch (Exception $e) {
                     Mage::log("Fontis_CampaignMonitor: Error in CampaignMonitor SOAP call: " . $e->getMessage());
                     $session->addException($e, $this->__('There was a problem with the subscription'));
                     $this->_redirectReferer();
                 }
             }
         } else {
             Mage::log("Fontis_CampaignMonitor: Error: Campaign Monitor API key and/or list ID not set in Magento Newsletter options.");
         }
     }
     parent::newAction();
 }
 public function check_subscription_status($observer)
 {
     $event = $observer->getEvent();
     $customer = $event->getCustomer();
     $apiKey = trim(Mage::getStoreConfig('newsletter/campaignmonitor/api_key'));
     $listID = trim(Mage::getStoreConfig('newsletter/campaignmonitor/list_id'));
     $name = $customer->getFirstname() . " " . $customer->getLastname();
     $newEmail = $customer->getEmail();
     $subscribed = $customer->getIsSubscribed();
     $oldEmail = Mage::getModel('customer/customer')->load($customer->getId())->getEmail();
     // if subscribed is NULL (i.e. because the form didn't set it one way
     // or the other), get the existing value from the database
     if ($subscribed === NULL) {
         $subscribed = Mage::getModel('newsletter/subscriber')->loadByCustomer($customer)->isSubscribed();
         //print "\$subscribed is NULL, using old value: $subscribed\n<br />";
     }
     //print "Name: $name, New email: $newEmail, Subscribed: $subscribed, Old email: $oldEmail<br />\n";
     if ($apiKey and $listID) {
         $customFields = Fontis_CampaignMonitor_Model_Customer_Observer::generateCustomFields($customer);
         try {
             $client = new SoapClient("http://api.createsend.com/api/api.asmx?wsdl", array("trace" => true));
         } catch (Exception $e) {
             Mage::log("Fontis_CampaignMonitor: Error connecting to CampaignMonitor server: " . $e->getMessage());
             return;
         }
         if ($subscribed) {
             /* If the customer:
                
                1) Already exists (i.e. has an old email address)
                2) Has changed their email address
                 
                unsubscribe their old address. */
             if ($oldEmail and $newEmail != $oldEmail) {
                 Mage::log("Fontis_CampaignMonitor: Unsubscribing old email address: {$oldEmail}");
                 try {
                     $result = $client->Unsubscribe(array("ApiKey" => $apiKey, "ListID" => $listID, "Email" => $oldEmail));
                 } catch (Exception $e) {
                     Mage::log("Fontis_CampaignMonitor: Error in SOAP call: " . $e->getMessage());
                     return;
                 }
             }
             // Using 'add and resubscribe' rather than just 'add', otherwise
             // somebody who unsubscribes and resubscribes won't be put back
             // on the active list
             Mage::log("Fontis_CampaignMonitor: Subscribing new email address: {$newEmail}");
             try {
                 $result = $client->AddAndResubscribeWithCustomFields(array("ApiKey" => $apiKey, "ListID" => $listID, "Email" => $newEmail, "Name" => $name, "CustomFields" => $customFields));
             } catch (Exception $e) {
                 Mage::log("Fontis_CampaignMonitor: Error in SOAP call: " . $e->getMessage());
                 return;
             }
         } else {
             Mage::log("Fontis_CampaignMonitor: Unsubscribing: {$oldEmail}");
             try {
                 $result = $client->Unsubscribe(array("ApiKey" => $apiKey, "ListID" => $listID, "Email" => $oldEmail));
             } catch (Exception $e) {
                 Mage::log("Fontis_CampaignMonitor: Error in SOAP call: " . $e->getMessage());
                 return;
             }
         }
     }
 }