1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Newsletter
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Newsletter subscriber resource model
30: *
31: * @category Mage
32: * @package Mage_Newsletter
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Newsletter_Model_Resource_Subscriber extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * DB read connection
39: *
40: * @var Zend_Db_Adapter_Abstract
41: */
42: protected $_read;
43:
44: /**
45: * DB write connection
46: *
47: * @var Zend_Db_Adapter_Abstract
48: */
49: protected $_write;
50:
51: /**
52: * Name of subscriber link DB table
53: *
54: * @var string
55: */
56: protected $_subscriberLinkTable;
57:
58: /**
59: * Name of scope for error messages
60: *
61: * @var string
62: */
63: protected $_messagesScope = 'newsletter/session';
64:
65: /**
66: * Initialize resource model
67: * Get tablename from config
68: *
69: */
70: protected function _construct()
71: {
72: $this->_init('newsletter/subscriber', 'subscriber_id');
73: $this->_subscriberLinkTable = $this->getTable('newsletter/queue_link');
74: $this->_read = $this->_getReadAdapter();
75: $this->_write = $this->_getWriteAdapter();
76: }
77:
78: /**
79: * Set error messages scope
80: *
81: * @param string $scope
82: */
83: public function setMessagesScope($scope)
84: {
85: $this->_messagesScope = $scope;
86: }
87:
88: /**
89: * Load subscriber from DB by email
90: *
91: * @param string $subscriberEmail
92: * @return array
93: */
94: public function loadByEmail($subscriberEmail)
95: {
96: $select = $this->_read->select()
97: ->from($this->getMainTable())
98: ->where('subscriber_email=:subscriber_email');
99:
100: $result = $this->_read->fetchRow($select, array('subscriber_email'=>$subscriberEmail));
101:
102: if (!$result) {
103: return array();
104: }
105:
106: return $result;
107: }
108:
109: /**
110: * Load subscriber by customer
111: *
112: * @param Mage_Customer_Model_Customer $customer
113: * @return array
114: */
115: public function loadByCustomer(Mage_Customer_Model_Customer $customer)
116: {
117: $select = $this->_read->select()
118: ->from($this->getMainTable())
119: ->where('customer_id=:customer_id');
120:
121: $result = $this->_read->fetchRow($select, array('customer_id'=>$customer->getId()));
122:
123: if ($result) {
124: return $result;
125: }
126:
127: $select = $this->_read->select()
128: ->from($this->getMainTable())
129: ->where('subscriber_email=:subscriber_email');
130:
131: $result = $this->_read->fetchRow($select, array('subscriber_email'=>$customer->getEmail()));
132:
133: if ($result) {
134: return $result;
135: }
136:
137: return array();
138: }
139:
140: /**
141: * Generates random code for subscription confirmation
142: *
143: * @return string
144: */
145: protected function _generateRandomCode()
146: {
147: return Mage::helper('core')->uniqHash();
148: }
149:
150: /**
151: * Updates data when subscriber received
152: *
153: * @param Mage_Newsletter_Model_Subscriber $subscriber
154: * @param Mage_Newsletter_Model_Queue $queue
155: * @return Mage_Newsletter_Model_Resource_Subscriber
156: */
157: public function received(Mage_Newsletter_Model_Subscriber $subscriber, Mage_Newsletter_Model_Queue $queue)
158: {
159: $this->_write->beginTransaction();
160: try {
161: $data['letter_sent_at'] = Mage::getSingleton('core/date')->gmtDate();
162: $this->_write->update($this->_subscriberLinkTable, $data, array(
163: 'subscriber_id = ?' => $subscriber->getId(),
164: 'queue_id = ?' => $queue->getId()
165: ));
166: $this->_write->commit();
167: }
168: catch (Exception $e) {
169: $this->_write->rollBack();
170: Mage::throwException(Mage::helper('newsletter')->__('Cannot mark as received subscriber.'));
171: }
172: return $this;
173: }
174: }
175: