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_Model_Mysql4_Subscriber
00035 {
00036
00037
00038
00039
00040
00041 protected $_read;
00042
00043
00044
00045
00046
00047
00048 protected $_write;
00049
00050
00051
00052
00053
00054
00055 protected $_subscriberTable;
00056
00057
00058
00059
00060
00061
00062 protected $_subscriberLinkTable;
00063
00064
00065
00066
00067
00068
00069 protected $_messagesScope = 'newsletter/session';
00070
00071
00072
00073
00074
00075
00076 public function __construct()
00077 {
00078 $this->_subscriberTable = Mage::getSingleton('core/resource')->getTableName("newsletter/subscriber");
00079 $this->_subscriberLinkTable = Mage::getSingleton('core/resource')->getTableName("newsletter/queue_link");
00080 $this->_read = Mage::getSingleton('core/resource')->getConnection('newsletter_read');
00081 $this->_write = Mage::getSingleton('core/resource')->getConnection('newsletter_write');
00082 }
00083
00084
00085
00086
00087
00088
00089
00090 public function load($subscriberId)
00091 {
00092 $select = $this->_read->select()
00093 ->from($this->_subscriberTable)
00094 ->where('subscriber_id=?',$subscriberId);
00095
00096 return $this->_read->fetchRow($select);
00097 }
00098
00099
00100
00101
00102
00103
00104 public function setMessagesScope($scope)
00105 {
00106 $this->_messagesScope = $scope;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115 public function loadByEmail($subscriberEmail)
00116 {
00117 $select = $this->_read->select()
00118 ->from($this->_subscriberTable)
00119 ->where('subscriber_email=?',$subscriberEmail);
00120
00121 $result = $this->_read->fetchRow($select);
00122
00123 if(!$result) {
00124 return array();
00125 }
00126
00127 return $result;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 public function loadByCustomer(Mage_Customer_Model_Customer $customer)
00139 {
00140 $select = $this->_read->select()
00141 ->from($this->_subscriberTable)
00142 ->where('customer_id=?',$customer->getId());
00143
00144 $result = $this->_read->fetchRow($select);
00145
00146 if ($result) {
00147 return $result;
00148 }
00149
00150 $select = $this->_read->select()
00151 ->from($this->_subscriberTable)
00152 ->where('subscriber_email=?',$customer->getEmail());
00153
00154 $result = $this->_read->fetchRow($select);
00155
00156 if ($result) {
00157 return $result;
00158 }
00159
00160 return array();
00161 }
00162
00163
00164
00165
00166
00167
00168
00169 public function save(Mage_Newsletter_Model_Subscriber $subscriber)
00170 {
00171 $this->_write->beginTransaction();
00172 try {
00173 $data = $this->_prepareSave($subscriber);
00174 if ($subscriber->getId()) {
00175 $this->_write->update($this->_subscriberTable, $data,
00176 $this->_write->quoteInto('subscriber_id=?',$subscriber->getId()));
00177 } else {
00178 $this->_write->insert($this->_subscriberTable, $data);
00179 $subscriber->setId($this->_write->lastInsertId($this->_subscriberTable));
00180 }
00181 $this->_write->commit();
00182 }
00183 catch(Exception $e) {
00184 $this->_write->rollBack();
00185 Mage::throwException(Mage::helper('newsletter')->__('Cannot save your subscription: %s', $e->getMessage()));
00186 }
00187
00188 return $subscriber;
00189 }
00190
00191
00192
00193
00194
00195
00196 protected function _generateRandomCode()
00197 {
00198 return md5(microtime() + rand());
00199 }
00200
00201
00202
00203
00204
00205
00206
00207 protected function _prepareSave(Mage_Newsletter_Model_Subscriber $subscriber)
00208 {
00209 $data = array();
00210 $data['customer_id'] = $subscriber->getCustomerId();
00211 $data['store_id'] = $subscriber->getStoreId() ? $subscriber->getStoreId() : 0;
00212 $data['subscriber_status'] = $subscriber->getStatus();
00213 $data['subscriber_email'] = $subscriber->getEmail();
00214 $data['subscriber_confirm_code'] = $subscriber->getCode();
00215
00216 if($subscriber->getIsStatusChanged()) {
00217 $data['change_status_at'] = Mage::getSingleton('core/date')->gmtDate();
00218 }
00219
00220 $validators = array('subscriber_email' => 'EmailAddress');
00221 $filters = array();
00222 $input = new Zend_Filter_Input($filters, $validators, $data);
00223 $session = Mage::getSingleton($this->_messagesScope);
00224 if ($input->hasInvalid() || $input->hasMissing()) {
00225 foreach ($input->getMessages() as $message) {
00226 if(is_array($message)) {
00227 foreach( $message as $error ) {
00228 $session->addError($error);
00229 }
00230 } else {
00231 $session->addError($message);
00232 }
00233 }
00234 Mage::throwException(Mage::helper('newsletter')->__('Form was filled incorrectly'));
00235 }
00236
00237 return $data;
00238 }
00239
00240
00241
00242
00243
00244
00245 public function delete($subscriberId)
00246 {
00247 if(!(int)$subscriberId) {
00248 Mage::throwException(Mage::helper('newsletter')->__('Invalid subscriber ID'));
00249 }
00250
00251 $this->_write->beginTransaction();
00252 try {
00253 $this->_write->delete($this->_subscriberTable,
00254 $this->_write->quoteInto('subscriber_id=?', $subscriberId));
00255 $this->_write->commit();
00256 }
00257 catch (Exception $e) {
00258 $this->_write->rollBack();
00259 Mage::throwException(Mage::helper('newsletter')->__('Cannot delete subscriber'));
00260 }
00261 }
00262
00263 public function received(Mage_Newsletter_Model_Subscriber $subscriber, Mage_Newsletter_Model_Queue $queue)
00264 {
00265 $this->_write->beginTransaction();
00266 try {
00267 $data['letter_sent_at'] = now();
00268 $this->_write->update($this->_subscriberLinkTable,
00269 $data,
00270 array($this->_write->quoteInto('subscriber_id=?', $subscriber->getId()),
00271 $this->_write->quoteInto('queue_id=?', $queue->getId())));
00272 $this->_write->commit();
00273 }
00274 catch (Exception $e) {
00275 $this->_write->rollBack();
00276 Mage::throwException(Mage::helper('newsletter')->__('Cannot mark as received subscriber'));
00277 }
00278 return $this;
00279 }
00280 }