1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Newsletter_SubscriberController extends Mage_Core_Controller_Front_Action
35: {
36: 37: 38:
39: public function newAction()
40: {
41: if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
42: $session = Mage::getSingleton('core/session');
43: $customerSession = Mage::getSingleton('customer/session');
44: $email = (string) $this->getRequest()->getPost('email');
45:
46: try {
47: if (!Zend_Validate::is($email, 'EmailAddress')) {
48: Mage::throwException($this->__('Please enter a valid email address.'));
49: }
50:
51: if (Mage::getStoreConfig(Mage_Newsletter_Model_Subscriber::XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG) != 1 &&
52: !$customerSession->isLoggedIn()) {
53: Mage::throwException($this->__('Sorry, but administrator denied subscription for guests. Please <a href="%s">register</a>.', Mage::helper('customer')->getRegisterUrl()));
54: }
55:
56: $ownerId = Mage::getModel('customer/customer')
57: ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
58: ->loadByEmail($email)
59: ->getId();
60: if ($ownerId !== null && $ownerId != $customerSession->getId()) {
61: Mage::throwException($this->__('This email address is already assigned to another user.'));
62: }
63:
64: $status = Mage::getModel('newsletter/subscriber')->subscribe($email);
65: if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
66: $session->addSuccess($this->__('Confirmation request has been sent.'));
67: }
68: else {
69: $session->addSuccess($this->__('Thank you for your subscription.'));
70: }
71: }
72: catch (Mage_Core_Exception $e) {
73: $session->addException($e, $this->__('There was a problem with the subscription: %s', $e->getMessage()));
74: }
75: catch (Exception $e) {
76: $session->addException($e, $this->__('There was a problem with the subscription.'));
77: }
78: }
79: $this->_redirectReferer();
80: }
81:
82: 83: 84:
85: public function confirmAction()
86: {
87: $id = (int) $this->getRequest()->getParam('id');
88: $code = (string) $this->getRequest()->getParam('code');
89:
90: if ($id && $code) {
91: $subscriber = Mage::getModel('newsletter/subscriber')->load($id);
92: $session = Mage::getSingleton('core/session');
93:
94: if($subscriber->getId() && $subscriber->getCode()) {
95: if($subscriber->confirm($code)) {
96: $session->addSuccess($this->__('Your subscription has been confirmed.'));
97: } else {
98: $session->addError($this->__('Invalid subscription confirmation code.'));
99: }
100: } else {
101: $session->addError($this->__('Invalid subscription ID.'));
102: }
103: }
104:
105: $this->_redirectUrl(Mage::getBaseUrl());
106: }
107:
108: 109: 110:
111: public function unsubscribeAction()
112: {
113: $id = (int) $this->getRequest()->getParam('id');
114: $code = (string) $this->getRequest()->getParam('code');
115:
116: if ($id && $code) {
117: $session = Mage::getSingleton('core/session');
118: try {
119: Mage::getModel('newsletter/subscriber')->load($id)
120: ->setCheckCode($code)
121: ->unsubscribe();
122: $session->addSuccess($this->__('You have been unsubscribed.'));
123: }
124: catch (Mage_Core_Exception $e) {
125: $session->addException($e, $e->getMessage());
126: }
127: catch (Exception $e) {
128: $session->addException($e, $this->__('There was a problem with the un-subscription.'));
129: }
130: }
131: $this->_redirectReferer();
132: }
133: }
134: