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_Subscriber extends Varien_Object
00035 {
00036 const STATUS_SUBSCRIBED = 1;
00037 const STATUS_NOT_ACTIVE = 2;
00038 const STATUS_UNSUBSCRIBED = 3;
00039
00040 const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'newsletter/subscription/confirm_email_template';
00041 const XML_PATH_CONFIRM_EMAIL_IDENTITY = 'newsletter/subscription/confirm_email_identity';
00042 const XML_PATH_SUCCESS_EMAIL_TEMPLATE = 'newsletter/subscription/success_email_template';
00043 const XML_PATH_SUCCESS_EMAIL_IDENTITY = 'newsletter/subscription/success_email_identity';
00044 const XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE = 'newsletter/subscription/un_email_template';
00045 const XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY = 'newsletter/subscription/un_email_identity';
00046 const XML_PATH_CONFIRMATION_FLAG = 'newsletter/subscription/confirm';
00047
00048 const XML_PATH_SENDING_SET_RETURN_PATH = 'newsletter/sending/set_return_path';
00049
00050 protected $_isStatusChanged = false;
00051
00052
00053
00054
00055
00056
00057 public function getId()
00058 {
00059 return $this->getSubscriberId();
00060 }
00061
00062
00063
00064
00065
00066
00067 public function setId($value)
00068 {
00069 return $this->setSubscriberId($value);
00070 }
00071
00072
00073
00074
00075
00076
00077 public function getCode()
00078 {
00079 return $this->getSubscriberConfirmCode();
00080 }
00081
00082
00083
00084
00085
00086
00087 public function getConfirmationLink() {
00088 return Mage::helper('newsletter')->getConfirmationUrl($this);
00089 }
00090
00091 public function getUnsubscriptionLink() {
00092 return Mage::helper('newsletter')->getUnsubscribeUrl($this);
00093 }
00094
00095
00096
00097
00098
00099
00100
00101 public function setCode($value)
00102 {
00103 return $this->setSubscriberConfirmCode($value);
00104 }
00105
00106
00107
00108
00109
00110
00111 public function getStatus()
00112 {
00113 return $this->getSubscriberStatus();
00114 }
00115
00116
00117
00118
00119
00120
00121 public function setStatus($value)
00122 {
00123 return $this->setSubscriberStatus($value);
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133 public function setMessagesScope($scope)
00134 {
00135 $this->getResource()->setMessagesScope($scope);
00136 return $this;
00137 }
00138
00139
00140
00141
00142
00143
00144 public function getEmail()
00145 {
00146 return $this->getSubscriberEmail();
00147 }
00148
00149
00150
00151
00152
00153
00154 public function setEmail($value)
00155 {
00156 return $this->setSubscriberEmail($value);
00157 }
00158
00159
00160
00161
00162
00163
00164 public function setIsStatusChanged($value)
00165 {
00166 $this->_isStatusChanged = (boolean) $value;
00167 return $this;
00168 }
00169
00170
00171
00172
00173
00174
00175 public function getIsStatusChanged()
00176 {
00177 return $this->_isStatusChanged;
00178 }
00179
00180
00181
00182
00183
00184
00185 public function isSubscribed()
00186 {
00187 if($this->getId() && $this->getStatus()==self::STATUS_SUBSCRIBED) {
00188 return true;
00189 }
00190
00191 return false;
00192 }
00193
00194
00195
00196
00197
00198
00199 public function getResource()
00200 {
00201 return Mage::getResourceSingleton('newsletter/subscriber');
00202 }
00203
00204
00205
00206
00207
00208
00209 public function load($subscriberId)
00210 {
00211 $this->addData($this->getResource()->load($subscriberId));
00212 return $this;
00213 }
00214
00215
00216
00217
00218
00219
00220 public function loadByEmail($subscriberEmail)
00221 {
00222 $this->addData($this->getResource()->loadByEmail($subscriberEmail));
00223 return $this;
00224 }
00225
00226
00227
00228
00229
00230
00231
00232 public function loadByCustomer(Mage_Customer_Model_Customer $customer)
00233 {
00234 $data = $this->getResource()->loadByCustomer($customer);
00235 $this->addData($data);
00236 if (!empty($data) && $customer->getId() && !$this->getCustomerId()) {
00237 $this->setCustomerId($customer->getId());
00238 $this->setSubscriberConfirmCode($this->randomSequence());
00239 if ($this->getStatus()==self::STATUS_NOT_ACTIVE) {
00240 $this->setStatus($customer->getIsSubscribed() ? self::STATUS_SUBSCRIBED : self::STATUS_UNSUBSCRIBED);
00241 }
00242 $this->save();
00243 }
00244 return $this;
00245 }
00246
00247
00248
00249
00250
00251 public function save()
00252 {
00253 return $this->getResource()->save($this);
00254 }
00255
00256
00257
00258
00259 public function delete()
00260 {
00261 $this->getResource()->delete($this->getId());
00262 $this->setId(null);
00263 }
00264
00265 public function randomSequence($length=32)
00266 {
00267 $id = '';
00268 $par = array();
00269 $char = array_merge(range('a','z'),range(0,9));
00270 $charLen = count($char)-1;
00271 for ($i=0;$i<$length;$i++){
00272 $disc = mt_rand(0, $charLen);
00273 $par[$i] = $char[$disc];
00274 $id = $id.$char[$disc];
00275 }
00276 return $id;
00277 }
00278
00279 public function subscribe($email)
00280 {
00281 $this->loadByEmail($email);
00282 $customer = Mage::getModel('customer/customer')
00283 ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
00284 ->loadByEmail($email);
00285 $isNewSubscriber = false;
00286
00287 $customerSession = Mage::getSingleton('customer/session');
00288
00289 if(!$this->getId()) {
00290 $this->setSubscriberConfirmCode($this->randomSequence());
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300 if (!$this->getId() || $this->getStatus()==self::STATUS_UNSUBSCRIBED || $this->getStatus()==self::STATUS_NOT_ACTIVE) {
00301 if (Mage::getStoreConfig(self::XML_PATH_CONFIRMATION_FLAG) == 1) {
00302 $this->setStatus(self::STATUS_NOT_ACTIVE);
00303 } else {
00304 $this->setStatus(self::STATUS_SUBSCRIBED);
00305 }
00306 $this->setSubscriberEmail($email);
00307 }
00308
00309 if ($customerSession->isLoggedIn()) {
00310 $this->setStoreId($customerSession->getCustomer()->getStoreId());
00311 $this->setStatus(self::STATUS_SUBSCRIBED);
00312 $this->setCustomerId($customerSession->getCustomerId());
00313 } else if ($customer->getId()) {
00314 $this->setStoreId($customer->getStoreId());
00315 $this->setSubscriberStatus(self::STATUS_SUBSCRIBED);
00316 $this->setCustomerId($customer->getId());
00317 } else {
00318 $this->setStoreId(Mage::app()->getStore()->getId());
00319 $this->setCustomerId(0);
00320 $isNewSubscriber = true;
00321 }
00322
00323 $this->setIsStatusChanged(true);
00324
00325 try {
00326 $this->save();
00327 if (Mage::getStoreConfig(self::XML_PATH_CONFIRMATION_FLAG) == 1
00328 && $this->getSubscriberStatus()==self::STATUS_NOT_ACTIVE) {
00329 $this->sendConfirmationRequestEmail();
00330 } else {
00331 $this->sendConfirmationSuccessEmail();
00332 }
00333
00334 return $this->getStatus();
00335 } catch (Exception $e) {
00336 throw new Exception($e->getMessage());
00337 }
00338 }
00339
00340 public function unsubscribe()
00341 {
00342 if ($this->hasCheckCode() && $this->getCode() != $this->getCheckCode()) {
00343 Mage::throwException(Mage::helper('newsletter')->__('Invalid subscription confirmation code'));
00344 }
00345
00346 $this->setSubscriberStatus(self::STATUS_UNSUBSCRIBED)
00347 ->save();
00348 $this->sendUnsubscriptionEmail();
00349 return $this;
00350 }
00351
00352
00353
00354
00355
00356
00357
00358 public function subscribeCustomer($customer)
00359 {
00360 $this->loadByCustomer($customer);
00361
00362 if ($customer->getImportMode()) {
00363 $this->setImportMode(true);
00364 }
00365
00366 if (!$customer->getIsSubscribed() && !$this->getId()) {
00367
00368
00369 return $this;
00370 }
00371
00372 if(!$this->getId()) {
00373 $this->setSubscriberConfirmCode($this->randomSequence());
00374 }
00375
00376 if($customer->hasIsSubscribed()) {
00377 $status = $customer->getIsSubscribed() ? self::STATUS_SUBSCRIBED : self::STATUS_UNSUBSCRIBED;
00378 } else {
00379 $status = ($this->getStatus() == self::STATUS_NOT_ACTIVE ? self::STATUS_UNSUBSCRIBED : $this->getStatus());
00380 }
00381
00382
00383 if($status != $this->getStatus()) {
00384 $this->setIsStatusChanged(true);
00385 }
00386
00387 $this->setStatus($status);
00388
00389
00390
00391 if(!$this->getId()) {
00392 $this->setStoreId($customer->getStoreId())
00393 ->setCustomerId($customer->getId())
00394 ->setEmail($customer->getEmail());
00395 } else {
00396 $this->setEmail($customer->getEmail());
00397 }
00398
00399 $this->save();
00400 $sendSubscription = $customer->getData('sendSubscription');
00401 if (is_null($sendSubscription) xor $sendSubscription) {
00402 if ($this->getIsStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
00403 $this->sendUnsubscriptionEmail();
00404 } elseif ($this->getIsStatusChanged() && $status == self::STATUS_SUBSCRIBED) {
00405 $this->sendConfirmationSuccessEmail();
00406 }
00407 }
00408 return $this;
00409 }
00410
00411
00412
00413
00414
00415
00416
00417 public function confirm($code)
00418 {
00419 if($this->getCode()==$code) {
00420 $this->setStatus(self::STATUS_SUBSCRIBED)
00421 ->setIsStatusChanged(true)
00422 ->save();
00423 return true;
00424 }
00425
00426 return false;
00427 }
00428
00429
00430
00431
00432
00433
00434
00435 public function received(Mage_Newsletter_Model_Queue $queue)
00436 {
00437 $this->getResource()->received($this,$queue);
00438 return $this;
00439 }
00440
00441 public function sendConfirmationRequestEmail()
00442 {
00443 if ($this->getImportMode()) {
00444 return $this;
00445 }
00446
00447 if(!Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_TEMPLATE) || !Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY)) {
00448 return $this;
00449 }
00450
00451 $translate = Mage::getSingleton('core/translate');
00452
00453 $translate->setTranslateInline(false);
00454
00455 $email = Mage::getModel('core/email_template');
00456
00457 if (Mage::getStoreConfigFlag(self::XML_PATH_SENDING_SET_RETURN_PATH)) {
00458 $email->setReturnPath(Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY));
00459 }
00460 $email->sendTransactional(
00461 Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_TEMPLATE),
00462 Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY),
00463 $this->getEmail(),
00464 $this->getName(),
00465 array('subscriber'=>$this)
00466 );
00467
00468 $translate->setTranslateInline(true);
00469
00470 return $this;
00471 }
00472
00473 public function sendConfirmationSuccessEmail()
00474 {
00475 if ($this->getImportMode()) {
00476 return $this;
00477 }
00478
00479 if(!Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE) || !Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY)) {
00480 return $this;
00481 }
00482
00483 $translate = Mage::getSingleton('core/translate');
00484
00485 $translate->setTranslateInline(false);
00486
00487 $email = Mage::getModel('core/email_template');
00488
00489 if (Mage::getStoreConfigFlag(self::XML_PATH_SENDING_SET_RETURN_PATH)) {
00490 $email->setReturnPath(Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY));
00491 }
00492 $email->sendTransactional(
00493 Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE),
00494 Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY),
00495 $this->getEmail(),
00496 $this->getName(),
00497 array('subscriber'=>$this)
00498 );
00499
00500 $translate->setTranslateInline(true);
00501
00502 return $this;
00503 }
00504
00505 public function sendUnsubscriptionEmail()
00506 {
00507 if ($this->getImportMode()) {
00508 return $this;
00509 }
00510 if(!Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE) || !Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY)) {
00511 return $this;
00512 }
00513
00514 $translate = Mage::getSingleton('core/translate');
00515
00516 $translate->setTranslateInline(false);
00517
00518 $email = Mage::getModel('core/email_template');
00519
00520 if (Mage::getStoreConfigFlag(self::XML_PATH_SENDING_SET_RETURN_PATH)) {
00521 $email->setReturnPath(Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY));
00522 }
00523 $email->sendTransactional(
00524 Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE),
00525 Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY),
00526 $this->getEmail(),
00527 $this->getName(),
00528 array('subscriber'=>$this)
00529 );
00530
00531 $translate->setTranslateInline(true);
00532
00533 return $this;
00534 }
00535 }