Example #1
0
 public function newAction()
 {
     parent::newAction();
     $timeCookiesTimeout = Mage::helper('newsletterpopup')->timeCookiesTimeout();
     //Set or remove cookie //
     if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
         $email = (string) $this->getRequest()->getPost('email');
         $period = $timeCookiesTimeout * 86400;
         Mage::getModel('core/cookie')->set('email_subscribed', $email, $period);
     } else {
         if (isset($_COOKIE['email_subscribed'])) {
             setcookie('email_subscribed', $email, time() - $timeCookiesTimeout * 86400, '/');
         }
     }
     // get poll result to send email
     if ($pollResult = $this->getRequest()->getPost('poll')) {
         //if (isset($status) && $status != Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
         $email = (string) $this->getRequest()->getPost('email');
         $poll = ['1' => 'I only buy it to get out of trouble', '2' => 'It looks good on my Pinterest page', '3' => 'I buy seasonal pieces at walmart', '4' => 'I love it! Please keep me informed...'];
         $body = '<table' . '<tr><td colspan="2">Subscriber Info</td></tr>' . '<tr><td>Email</td><td>Poll Result</td></tr>' . '<tr><td>' . $email . '</td><td>' . $poll[$pollResult] . '</td></tr>' . '</table>';
         $mail = Mage::getModel('core/email')->setToName('Debbi')->setToEmail('*****@*****.**')->setBody($body)->setSubject('There is a new subscriber - Gemz.Gallery')->setFromEmail('*****@*****.**')->setFromName('GEMZ.GALLERY')->setType('html');
         try {
             $mail->send();
         } catch (Exception $e) {
             Mage::log($e->getMessage());
             Mage::getSingleton('core/session')->addError($e->getMessage());
         }
         //}
     }
 }
 public function massUnsubscribeAction()
 {
     $session = Mage::getSingleton('core/session');
     Mage::helper('campaignmonitor')->log("massUnsubscribeAction");
     $subscribersIds = $this->getRequest()->getParam('subscriber');
     if (!is_array($subscribersIds)) {
         Mage::getSingleton('adminhtml/session')->addError(Mage::helper('newsletter')->__('Please select subscriber(s)'));
         $this->_redirect('*/*/index');
     } else {
         try {
             if (Mage::helper('campaignmonitor')->isOAuth()) {
                 $accessToken = Mage::getModel('campaignmonitor/auth')->getAccessToken();
                 $refreshToken = Mage::getModel('campaignmonitor/auth')->getRefreshToken();
                 $auth = array('access_token' => $accessToken, 'refresh_token' => $refreshToken);
             } else {
                 $auth = Mage::helper('campaignmonitor')->getApiKey();
             }
             $listID = Mage::helper('campaignmonitor')->getListId();
             try {
                 $client = new CS_REST_Subscribers($listID, $auth);
             } catch (Exception $e) {
                 Mage::helper('campaignmonitor')->log("Error connecting to CampaignMonitor server: " . $e->getMessage());
                 $session->addException($e, $this->__('There was a problem with the subscription'));
                 $this->_redirectReferer();
             }
             foreach ($subscribersIds as $subscriberId) {
                 $subscriber = Mage::getModel('newsletter/subscriber')->load($subscriberId);
                 $email = $subscriber->getEmail();
                 Mage::helper('campaignmonitor')->log($this->__("Unsubscribing: %s", $email));
                 try {
                     $result = $client->unsubscribe($email);
                     if (!$result->was_successful()) {
                         // If you receive '121: Expired OAuth Token', refresh the access token
                         if ($result->response->Code == 121) {
                             // Refresh the token
                             Mage::helper('campaignmonitor')->refreshToken();
                         }
                         // Make the call again
                         $client->unsubscribe($email);
                     }
                 } catch (Exception $e) {
                     Mage::helper('campaignmonitor')->log("Error in CampaignMonitor SOAP call: " . $e->getMessage());
                 }
             }
         } catch (Exception $e) {
             Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
         }
     }
     parent::massUnsubscribeAction();
 }
 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 newAction()
 {
     if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
         $session = Mage::getSingleton('core/session');
         $email = (string) $this->getRequest()->getPost('email');
         Mage::log("DigitalPianism_CampaignMonitor: Adding newsletter subscription via frontend 'Sign up' block for {$email}");
         if (Mage::helper('campaignmonitor')->isOAuth()) {
             $accessToken = Mage::getModel('campaignmonitor/auth')->getAccessToken();
             $refreshToken = Mage::getModel('campaignmonitor/auth')->getRefreshToken();
             $auth = array('access_token' => $accessToken, 'refresh_token' => $refreshToken);
         } else {
             $auth = Mage::helper('campaignmonitor')->getApiKey();
         }
         $listID = Mage::helper('campaignmonitor')->getListId();
         if ($auth && $listID) {
             try {
                 $client = new CS_REST_Subscribers($listID, $auth);
             } catch (Exception $e) {
                 Mage::helper('campaignmonitor')->log("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 = Mage::helper('campaignmonitor')->generateCustomFields($customer);
                 try {
                     $result = $client->add(array("EmailAddress" => $email, "Name" => $name, "CustomFields" => $customFields, "Resubscribe" => true));
                     if (!$result->was_successful()) {
                         // If you receive '121: Expired OAuth Token', refresh the access token
                         if ($result->response->Code == 121) {
                             // Refresh the token
                             Mage::helper('campaignmonitor')->refreshToken();
                         }
                         // Make the call again
                         $client->add(array("EmailAddress" => $email, "Name" => $name, "CustomFields" => $customFields, "Resubscribe" => true));
                     }
                 } catch (Exception $e) {
                     Mage::helper('campaignmonitor')->log("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->add(array("EmailAddress" => $email, "Name" => "(Guest)", "Resubscribe" => true));
                     if (!$result->was_successful()) {
                         // If you receive '121: Expired OAuth Token', refresh the access token
                         if ($result->response->Code == 121) {
                             // Refresh the token
                             Mage::helper('campaignmonitor')->refreshToken();
                         }
                         // Make the call again
                         $client->add(array("EmailAddress" => $email, "Name" => "(Guest)", "Resubscribe" => true));
                     }
                 } catch (Exception $e) {
                     Mage::helper('campaignmonitor')->log("Error in CampaignMonitor SOAP call: " . $e->getMessage());
                     $session->addException($e, $this->__('There was a problem with the subscription'));
                     $this->_redirectReferer();
                 }
             }
         } else {
             Mage::helper('campaignmonitor')->log("Error: Campaign Monitor API key and/or list ID not set in Magento Newsletter options.");
         }
     }
     parent::newAction();
 }