00001 <?php
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 class Mage_Newsletter_SubscriberController extends Mage_Core_Controller_Front_Action
00035 {
00036 
00037 
00038 
00039     public function newAction()
00040     {
00041         if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
00042             $session   = Mage::getSingleton('core/session');
00043             $email     = (string) $this->getRequest()->getPost('email');
00044 
00045             try {
00046                 if (!Zend_Validate::is($email, 'EmailAddress')) {
00047                     Mage::throwException($this->__('Please enter a valid email address'));
00048                 }
00049 
00050                 $status = Mage::getModel('newsletter/subscriber')->subscribe($email);
00051                 if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
00052                     $session->addSuccess($this->__('Confirmation request has been sent'));
00053                 }
00054                 else {
00055                     $session->addSuccess($this->__('Thank you for your subscription'));
00056                 }
00057             }
00058             catch (Mage_Core_Exception $e) {
00059                 $session->addException($e, $this->__('There was a problem with the subscription: %s', $e->getMessage()));
00060             }
00061             catch (Exception $e) {
00062                 $session->addException($e, $this->__('There was a problem with the subscription'));
00063             }
00064         }
00065         $this->_redirectReferer();
00066     }
00067 
00068 
00069 
00070 
00071     public function confirmAction()
00072     {
00073         $id    = (int) $this->getRequest()->getParam('id');
00074         $code  = (string) $this->getRequest()->getParam('code');
00075 
00076         if ($id && $code) {
00077             $subscriber = Mage::getModel('newsletter/subscriber')->load($id);
00078             $session = Mage::getSingleton('core/session');
00079 
00080             if($subscriber->getId() && $subscriber->getCode()) {
00081                 if($subscriber->confirm($code)) {
00082                     $session->addSuccess($this->__('Your subscription was successfully confirmed'));
00083                 } else {
00084                     $session->addError($this->__('Invalid subscription confirmation code'));
00085                 }
00086             } else {
00087                 $session->addError($this->__('Invalid subscription ID'));
00088             }
00089         }
00090 
00091         $this->_redirectUrl(Mage::getBaseUrl());
00092     }
00093 
00094 
00095 
00096 
00097     public function unsubscribeAction()
00098     {
00099         $id    = (int) $this->getRequest()->getParam('id');
00100         $code  = (string) $this->getRequest()->getParam('code');
00101 
00102         if ($id && $code) {
00103             $session = Mage::getSingleton('core/session');
00104             try {
00105                 Mage::getModel('newsletter/subscriber')->load($id)
00106                     ->setCheckCode($code)
00107                     ->unsubscribe();
00108                 $session->addSuccess($this->__('You have been successfully unsubscribed.'));
00109             }
00110             catch (Mage_Core_Exception $e) {
00111                 $session->addException($e, $e->getMessage());
00112             }
00113             catch (Exception $e) {
00114                 $session->addException($e, $this->__('There was a problem with the un-subscription.'));
00115             }
00116         }
00117         $this->_redirectReferer();
00118     }
00119 }