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: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
49: class Mage_Newsletter_Model_Subscriber extends Mage_Core_Model_Abstract
50: {
51: const STATUS_SUBSCRIBED = 1;
52: const STATUS_NOT_ACTIVE = 2;
53: const STATUS_UNSUBSCRIBED = 3;
54: const STATUS_UNCONFIRMED = 4;
55:
56: const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'newsletter/subscription/confirm_email_template';
57: const XML_PATH_CONFIRM_EMAIL_IDENTITY = 'newsletter/subscription/confirm_email_identity';
58: const XML_PATH_SUCCESS_EMAIL_TEMPLATE = 'newsletter/subscription/success_email_template';
59: const XML_PATH_SUCCESS_EMAIL_IDENTITY = 'newsletter/subscription/success_email_identity';
60: const XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE = 'newsletter/subscription/un_email_template';
61: const XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY = 'newsletter/subscription/un_email_identity';
62: const XML_PATH_CONFIRMATION_FLAG = 'newsletter/subscription/confirm';
63: const XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG = 'newsletter/subscription/allow_guest_subscribe';
64:
65: 66: 67:
68: const XML_PATH_SENDING_SET_RETURN_PATH = Mage_Core_Model_Email_Template::XML_PATH_SENDING_SET_RETURN_PATH;
69:
70: 71: 72: 73: 74:
75: protected $_eventPrefix = 'newsletter_subscriber';
76:
77: 78: 79: 80: 81: 82: 83:
84: protected $_eventObject = 'subscriber';
85:
86: 87: 88: 89: 90:
91: protected $_isStatusChanged = false;
92:
93: 94: 95:
96: protected function _construct()
97: {
98: $this->_init('newsletter/subscriber');
99: }
100:
101: 102: 103: 104: 105:
106: public function getId()
107: {
108: return $this->getSubscriberId();
109: }
110:
111: 112: 113: 114: 115:
116: public function setId($value)
117: {
118: return $this->setSubscriberId($value);
119: }
120:
121: 122: 123: 124: 125:
126: public function getCode()
127: {
128: return $this->getSubscriberConfirmCode();
129: }
130:
131: 132: 133: 134: 135:
136: public function getConfirmationLink() {
137: return Mage::helper('newsletter')->getConfirmationUrl($this);
138: }
139:
140: 141: 142: 143: 144:
145: public function getUnsubscriptionLink() {
146: return Mage::helper('newsletter')->getUnsubscribeUrl($this);
147: }
148:
149: 150: 151: 152: 153:
154: public function setCode($value)
155: {
156: return $this->setSubscriberConfirmCode($value);
157: }
158:
159: 160: 161: 162: 163:
164: public function getStatus()
165: {
166: return $this->getSubscriberStatus();
167: }
168:
169: 170: 171: 172: 173:
174: public function setStatus($value)
175: {
176: return $this->setSubscriberStatus($value);
177: }
178:
179: 180: 181: 182: 183: 184:
185:
186: public function setMessagesScope($scope)
187: {
188: $this->getResource()->setMessagesScope($scope);
189: return $this;
190: }
191:
192: 193: 194: 195: 196:
197: public function getEmail()
198: {
199: return $this->getSubscriberEmail();
200: }
201:
202: 203: 204: 205: 206:
207: public function setEmail($value)
208: {
209: return $this->setSubscriberEmail($value);
210: }
211:
212: 213: 214: 215: 216:
217: public function setIsStatusChanged($value)
218: {
219: $this->_isStatusChanged = (boolean) $value;
220: return $this;
221: }
222:
223: 224: 225: 226: 227:
228: public function getIsStatusChanged()
229: {
230: return $this->_isStatusChanged;
231: }
232:
233: 234: 235: 236: 237:
238: public function isSubscribed()
239: {
240: if($this->getId() && $this->getStatus()==self::STATUS_SUBSCRIBED) {
241: return true;
242: }
243:
244: return false;
245: }
246:
247:
248: 249: 250: 251: 252:
253: public function loadByEmail($subscriberEmail)
254: {
255: $this->addData($this->getResource()->loadByEmail($subscriberEmail));
256: return $this;
257: }
258:
259: 260: 261: 262: 263: 264:
265: public function loadByCustomer(Mage_Customer_Model_Customer $customer)
266: {
267: $data = $this->getResource()->loadByCustomer($customer);
268: $this->addData($data);
269: if (!empty($data) && $customer->getId() && !$this->getCustomerId()) {
270: $this->setCustomerId($customer->getId());
271: $this->setSubscriberConfirmCode($this->randomSequence());
272: if ($this->getStatus()==self::STATUS_NOT_ACTIVE) {
273: $this->setStatus($customer->getIsSubscribed() ? self::STATUS_SUBSCRIBED : self::STATUS_UNSUBSCRIBED);
274: }
275: $this->save();
276: }
277: return $this;
278: }
279:
280: 281: 282: 283: 284: 285:
286: public function randomSequence($length=32)
287: {
288: $id = '';
289: $par = array();
290: $char = array_merge(range('a','z'),range(0,9));
291: $charLen = count($char)-1;
292: for ($i=0;$i<$length;$i++){
293: $disc = mt_rand(0, $charLen);
294: $par[$i] = $char[$disc];
295: $id = $id.$char[$disc];
296: }
297: return $id;
298: }
299:
300: 301: 302: 303: 304: 305: 306:
307: public function subscribe($email)
308: {
309: $this->loadByEmail($email);
310: $customerSession = Mage::getSingleton('customer/session');
311:
312: if(!$this->getId()) {
313: $this->setSubscriberConfirmCode($this->randomSequence());
314: }
315:
316: $isConfirmNeed = (Mage::getStoreConfig(self::XML_PATH_CONFIRMATION_FLAG) == 1) ? true : false;
317: $isOwnSubscribes = false;
318: $ownerId = Mage::getModel('customer/customer')
319: ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
320: ->loadByEmail($email)
321: ->getId();
322: $isSubscribeOwnEmail = $customerSession->isLoggedIn() && $ownerId == $customerSession->getId();
323:
324: if (!$this->getId() || $this->getStatus() == self::STATUS_UNSUBSCRIBED
325: || $this->getStatus() == self::STATUS_NOT_ACTIVE
326: ) {
327: if ($isConfirmNeed === true) {
328:
329: $isOwnSubscribes = $isSubscribeOwnEmail;
330: if ($isOwnSubscribes == true){
331: $this->setStatus(self::STATUS_SUBSCRIBED);
332: } else {
333: $this->setStatus(self::STATUS_NOT_ACTIVE);
334: }
335: } else {
336: $this->setStatus(self::STATUS_SUBSCRIBED);
337: }
338: $this->setSubscriberEmail($email);
339: }
340:
341: if ($isSubscribeOwnEmail) {
342: $this->setStoreId($customerSession->getCustomer()->getStoreId());
343: $this->setCustomerId($customerSession->getCustomerId());
344: } else {
345: $this->setStoreId(Mage::app()->getStore()->getId());
346: $this->setCustomerId(0);
347: }
348:
349: $this->setIsStatusChanged(true);
350:
351: try {
352: $this->save();
353: if ($isConfirmNeed === true
354: && $isOwnSubscribes === false
355: ) {
356: $this->sendConfirmationRequestEmail();
357: } else {
358: $this->sendConfirmationSuccessEmail();
359: }
360:
361: return $this->getStatus();
362: } catch (Exception $e) {
363: throw new Exception($e->getMessage());
364: }
365: }
366:
367: 368: 369: 370:
371: public function unsubscribe()
372: {
373: if ($this->hasCheckCode() && $this->getCode() != $this->getCheckCode()) {
374: Mage::throwException(Mage::helper('newsletter')->__('Invalid subscription confirmation code.'));
375: }
376:
377: $this->setSubscriberStatus(self::STATUS_UNSUBSCRIBED)
378: ->save();
379: $this->sendUnsubscriptionEmail();
380: return $this;
381: }
382:
383: 384: 385: 386: 387: 388:
389: public function subscribeCustomer($customer)
390: {
391: $this->loadByCustomer($customer);
392:
393: if ($customer->getImportMode()) {
394: $this->setImportMode(true);
395: }
396:
397: if (!$customer->getIsSubscribed() && !$this->getId()) {
398:
399:
400: return $this;
401: }
402:
403: if(!$this->getId()) {
404: $this->setSubscriberConfirmCode($this->randomSequence());
405: }
406:
407: 408: 409:
410: $confirmation = null;
411: if ($customer->isConfirmationRequired() && ($customer->getConfirmation() != $customer->getPassword())) {
412: $confirmation = $customer->getConfirmation();
413: }
414:
415: $sendInformationEmail = false;
416: if ($customer->hasIsSubscribed()) {
417: $status = $customer->getIsSubscribed()
418: ? (!is_null($confirmation) ? self::STATUS_UNCONFIRMED : self::STATUS_SUBSCRIBED)
419: : self::STATUS_UNSUBSCRIBED;
420: 421: 422:
423: if ($status != self::STATUS_UNCONFIRMED && $status != $this->getStatus()) {
424: $sendInformationEmail = true;
425: }
426: } elseif (($this->getStatus() == self::STATUS_UNCONFIRMED) && (is_null($confirmation))) {
427: $status = self::STATUS_SUBSCRIBED;
428: $sendInformationEmail = true;
429: } else {
430: $status = ($this->getStatus() == self::STATUS_NOT_ACTIVE ? self::STATUS_UNSUBSCRIBED : $this->getStatus());
431: }
432:
433: if($status != $this->getStatus()) {
434: $this->setIsStatusChanged(true);
435: }
436:
437: $this->setStatus($status);
438:
439: if(!$this->getId()) {
440: $storeId = $customer->getStoreId();
441: if ($customer->getStoreId() == 0) {
442: $storeId = Mage::app()->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
443: }
444: $this->setStoreId($storeId)
445: ->setCustomerId($customer->getId())
446: ->setEmail($customer->getEmail());
447: } else {
448: $this->setStoreId($customer->getStoreId())
449: ->setEmail($customer->getEmail());
450: }
451:
452: $this->save();
453: $sendSubscription = $customer->getData('sendSubscription') || $sendInformationEmail;
454: if (is_null($sendSubscription) xor $sendSubscription) {
455: if ($this->getIsStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
456: $this->sendUnsubscriptionEmail();
457: } elseif ($this->getIsStatusChanged() && $status == self::STATUS_SUBSCRIBED) {
458: $this->sendConfirmationSuccessEmail();
459: }
460: }
461: return $this;
462: }
463:
464: 465: 466: 467: 468: 469:
470: public function confirm($code)
471: {
472: if($this->getCode()==$code) {
473: $this->setStatus(self::STATUS_SUBSCRIBED)
474: ->setIsStatusChanged(true)
475: ->save();
476: return true;
477: }
478:
479: return false;
480: }
481:
482: 483: 484: 485: 486: 487:
488: public function received(Mage_Newsletter_Model_Queue $queue)
489: {
490: $this->getResource()->received($this,$queue);
491: return $this;
492: }
493:
494: 495: 496: 497: 498:
499: public function sendConfirmationRequestEmail()
500: {
501: if ($this->getImportMode()) {
502: return $this;
503: }
504:
505: if(!Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_TEMPLATE)
506: || !Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY)
507: ) {
508: return $this;
509: }
510:
511: $translate = Mage::getSingleton('core/translate');
512:
513: $translate->setTranslateInline(false);
514:
515: $email = Mage::getModel('core/email_template');
516:
517: $email->sendTransactional(
518: Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_TEMPLATE),
519: Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY),
520: $this->getEmail(),
521: $this->getName(),
522: array('subscriber'=>$this)
523: );
524:
525: $translate->setTranslateInline(true);
526:
527: return $this;
528: }
529:
530: 531: 532: 533: 534:
535: public function sendConfirmationSuccessEmail()
536: {
537: if ($this->getImportMode()) {
538: return $this;
539: }
540:
541: if(!Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE)
542: || !Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY)
543: ) {
544: return $this;
545: }
546:
547: $translate = Mage::getSingleton('core/translate');
548:
549: $translate->setTranslateInline(false);
550:
551: $email = Mage::getModel('core/email_template');
552:
553: $email->sendTransactional(
554: Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE),
555: Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY),
556: $this->getEmail(),
557: $this->getName(),
558: array('subscriber'=>$this)
559: );
560:
561: $translate->setTranslateInline(true);
562:
563: return $this;
564: }
565:
566: 567: 568: 569: 570:
571: public function sendUnsubscriptionEmail()
572: {
573: if ($this->getImportMode()) {
574: return $this;
575: }
576: if(!Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE)
577: || !Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY)
578: ) {
579: return $this;
580: }
581:
582: $translate = Mage::getSingleton('core/translate');
583:
584: $translate->setTranslateInline(false);
585:
586: $email = Mage::getModel('core/email_template');
587:
588: $email->sendTransactional(
589: Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE),
590: Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY),
591: $this->getEmail(),
592: $this->getName(),
593: array('subscriber'=>$this)
594: );
595:
596: $translate->setTranslateInline(true);
597:
598: return $this;
599: }
600:
601: 602: 603: 604: 605:
606: public function getSubscriberFullName()
607: {
608: $name = null;
609: if ($this->hasCustomerFirstname() || $this->hasCustomerLastname()) {
610: $name = $this->getCustomerFirstname() . ' ' . $this->getCustomerLastname();
611: }
612: return $name;
613: }
614: }
615: