Mage_Newsletter_Model_Subscriber Class Reference

Inheritance diagram for Mage_Newsletter_Model_Subscriber:

Varien_Object

List of all members.

Public Member Functions

 getId ()
 setId ($value)
 getCode ()
 getConfirmationLink ()
 getUnsubscriptionLink ()
 setCode ($value)
 getStatus ()
 setStatus ($value)
 setMessagesScope ($scope)
 getEmail ()
 setEmail ($value)
 setIsStatusChanged ($value)
 getIsStatusChanged ()
 isSubscribed ()
 getResource ()
 load ($subscriberId)
 loadByEmail ($subscriberEmail)
 loadByCustomer (Mage_Customer_Model_Customer $customer)
 save ()
 delete ()
 randomSequence ($length=32)
 subscribe ($email)
 unsubscribe ()
 subscribeCustomer ($customer)
 confirm ($code)
 received (Mage_Newsletter_Model_Queue $queue)
 sendConfirmationRequestEmail ()
 sendConfirmationSuccessEmail ()
 sendUnsubscriptionEmail ()

Public Attributes

const STATUS_SUBSCRIBED = 1
const STATUS_NOT_ACTIVE = 2
const STATUS_UNSUBSCRIBED = 3
const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'newsletter/subscription/confirm_email_template'
const XML_PATH_CONFIRM_EMAIL_IDENTITY = 'newsletter/subscription/confirm_email_identity'
const XML_PATH_SUCCESS_EMAIL_TEMPLATE = 'newsletter/subscription/success_email_template'
const XML_PATH_SUCCESS_EMAIL_IDENTITY = 'newsletter/subscription/success_email_identity'
const XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE = 'newsletter/subscription/un_email_template'
const XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY = 'newsletter/subscription/un_email_identity'
const XML_PATH_CONFIRMATION_FLAG = 'newsletter/subscription/confirm'
const XML_PATH_SENDING_SET_RETURN_PATH = 'newsletter/sending/set_return_path'

Protected Attributes

 $_isStatusChanged = false


Detailed Description

Definition at line 34 of file Subscriber.php.


Member Function Documentation

confirm ( code  ) 

Confirms subscriber newsletter

Parameters:
string $code
Returns:
boolean

Definition at line 417 of file Subscriber.php.

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     }

delete (  ) 

Deletes subscriber data

Definition at line 259 of file Subscriber.php.

00260     {
00261         $this->getResource()->delete($this->getId());
00262         $this->setId(null);
00263     }

getCode (  ) 

Alias for getSubscriberConfirmCode()

Returns:
string

Definition at line 77 of file Subscriber.php.

00078     {
00079         return $this->getSubscriberConfirmCode();
00080     }

getConfirmationLink (  ) 

Return link for confirmation of subscription

Returns:
string

Definition at line 87 of file Subscriber.php.

00087                                           {
00088         return Mage::helper('newsletter')->getConfirmationUrl($this);
00089     }

getEmail (  ) 

Alias for getSubscriberEmail()

Returns:
string

Definition at line 144 of file Subscriber.php.

00145     {
00146         return $this->getSubscriberEmail();
00147     }

getId (  ) 

Alias for getSubscriberId()

Returns:
int

Reimplemented from Varien_Object.

Definition at line 57 of file Subscriber.php.

00058     {
00059         return $this->getSubscriberId();
00060     }

getIsStatusChanged (  ) 

Return status change flag value

Returns:
boolean

Definition at line 175 of file Subscriber.php.

00176     {
00177         return $this->_isStatusChanged;
00178     }

getResource (  ) 

Return resource model

Returns:
Mage_Subscriber_Model_Mysql4_Subscriber

Definition at line 199 of file Subscriber.php.

00200     {
00201         return Mage::getResourceSingleton('newsletter/subscriber');
00202     }

getStatus (  ) 

Alias for getSubscriberStatus()

Returns:
int

Definition at line 111 of file Subscriber.php.

00112     {
00113         return $this->getSubscriberStatus();
00114     }

getUnsubscriptionLink (  ) 

Definition at line 91 of file Subscriber.php.

00091                                             {
00092         return Mage::helper('newsletter')->getUnsubscribeUrl($this);
00093     }

isSubscribed (  ) 

Return customer subscription status

Returns:
bool

Definition at line 185 of file Subscriber.php.

00186     {
00187         if($this->getId() && $this->getStatus()==self::STATUS_SUBSCRIBED) {
00188             return true;
00189         }
00190 
00191         return false;
00192     }

load ( subscriberId  ) 

Load subscriber data from resource model

Parameters:
int $subscriberId

Definition at line 209 of file Subscriber.php.

00210     {
00211         $this->addData($this->getResource()->load($subscriberId));
00212         return $this;
00213     }

loadByCustomer ( Mage_Customer_Model_Customer customer  ) 

Load subscriber info by customer

Parameters:
Mage_Customer_Model_Customer $customer
Returns:
Mage_Newsletter_Model_Subscriber

Definition at line 232 of file Subscriber.php.

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     }

loadByEmail ( subscriberEmail  ) 

Load subscriber data from resource model by email

Parameters:
int $subscriberId

Definition at line 220 of file Subscriber.php.

00221     {
00222         $this->addData($this->getResource()->loadByEmail($subscriberEmail));
00223         return $this;
00224     }

randomSequence ( length = 32  ) 

Definition at line 265 of file Subscriber.php.

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     }

received ( Mage_Newsletter_Model_Queue queue  ) 

Mark receiving subscriber of queue newsletter

Parameters:
Mage_Newsletter_Model_Queue $queue
Returns:
boolean

Definition at line 435 of file Subscriber.php.

00436     {
00437         $this->getResource()->received($this,$queue);
00438         return $this;
00439     }

save (  ) 

Save subscriber data to resource model

Definition at line 251 of file Subscriber.php.

00252     {
00253         return $this->getResource()->save($this);
00254     }

sendConfirmationRequestEmail (  ) 

Definition at line 441 of file Subscriber.php.

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         /* @var $translate Mage_Core_Model_Translate */
00453         $translate->setTranslateInline(false);
00454 
00455         $email = Mage::getModel('core/email_template');
00456         /* @var $email Mage_Core_Model_Email_Template */
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     }

sendConfirmationSuccessEmail (  ) 

Definition at line 473 of file Subscriber.php.

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         /* @var $translate Mage_Core_Model_Translate */
00485         $translate->setTranslateInline(false);
00486 
00487         $email = Mage::getModel('core/email_template');
00488         /* @var $email Mage_Core_Model_Email_Template */
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     }

sendUnsubscriptionEmail (  ) 

Definition at line 505 of file Subscriber.php.

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         /* @var $translate Mage_Core_Model_Translate */
00516         $translate->setTranslateInline(false);
00517 
00518         $email = Mage::getModel('core/email_template');
00519         /* @var $email Mage_Core_Model_Email_Template */
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     }

setCode ( value  ) 

Alias for setSubscriberConfirmCode()

Parameters:
string $value

Definition at line 101 of file Subscriber.php.

00102     {
00103         return $this->setSubscriberConfirmCode($value);
00104     }

setEmail ( value  ) 

Alias for setSubscriberEmail()

Parameters:
string $value

Definition at line 154 of file Subscriber.php.

00155     {
00156         return $this->setSubscriberEmail($value);
00157     }

setId ( value  ) 

Alias for setSubscriberId()

Parameters:
int $value

Reimplemented from Varien_Object.

Definition at line 67 of file Subscriber.php.

00068     {
00069         return $this->setSubscriberId($value);
00070     }

setIsStatusChanged ( value  ) 

Set for status change flag

Parameters:
boolean $value

Definition at line 164 of file Subscriber.php.

00165     {
00166         $this->_isStatusChanged = (boolean) $value;
00167            return $this;
00168     }

setMessagesScope ( scope  ) 

Set the error messages scope for subscription

Parameters:
boolean $scope
Returns:
unknown

Definition at line 133 of file Subscriber.php.

00134     {
00135         $this->getResource()->setMessagesScope($scope);
00136         return $this;
00137     }

setStatus ( value  ) 

Alias for setSubscriberStatus()

Parameters:
int 

Definition at line 121 of file Subscriber.php.

00122     {
00123         return $this->setSubscriberStatus($value);
00124     }

subscribe ( email  ) 

Definition at line 279 of file Subscriber.php.

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 //        if(($this->getCustomerId() && !$customerSession->isLoggedIn())
00294 //           || ($this->getCustomerId()
00295 //               && $customerSession->getCustomerId() != $this->getCustomerId()
00296 //               )) {
00297 //            return $this->getSubscriberStatus();
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     }

subscribeCustomer ( customer  ) 

Saving customer cubscription status

Parameters:
Mage_Customer_Model_Customer $customer
Returns:
Mage_Newsletter_Model_Subscriber

Definition at line 358 of file Subscriber.php.

00359     {
00360         $this->loadByCustomer($customer);
00361 
00362         if ($customer->getImportMode()) {
00363             $this->setImportMode(true);
00364         }
00365 
00366         if (!$customer->getIsSubscribed() && !$this->getId()) {
00367             // If subscription flag not seted or customer not subscriber
00368             // and no subscribe bellow
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     }

unsubscribe (  ) 

Definition at line 340 of file Subscriber.php.

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     }


Member Data Documentation

$_isStatusChanged = false [protected]

Definition at line 50 of file Subscriber.php.

const STATUS_NOT_ACTIVE = 2

Definition at line 37 of file Subscriber.php.

const STATUS_SUBSCRIBED = 1

Definition at line 36 of file Subscriber.php.

Definition at line 38 of file Subscriber.php.

const XML_PATH_CONFIRM_EMAIL_IDENTITY = 'newsletter/subscription/confirm_email_identity'

Definition at line 41 of file Subscriber.php.

const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'newsletter/subscription/confirm_email_template'

Definition at line 40 of file Subscriber.php.

const XML_PATH_CONFIRMATION_FLAG = 'newsletter/subscription/confirm'

Definition at line 46 of file Subscriber.php.

const XML_PATH_SENDING_SET_RETURN_PATH = 'newsletter/sending/set_return_path'

Definition at line 48 of file Subscriber.php.

const XML_PATH_SUCCESS_EMAIL_IDENTITY = 'newsletter/subscription/success_email_identity'

Definition at line 43 of file Subscriber.php.

const XML_PATH_SUCCESS_EMAIL_TEMPLATE = 'newsletter/subscription/success_email_template'

Definition at line 42 of file Subscriber.php.

const XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY = 'newsletter/subscription/un_email_identity'

Definition at line 45 of file Subscriber.php.

const XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE = 'newsletter/subscription/un_email_template'

Definition at line 44 of file Subscriber.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:30 2009 for Magento by  doxygen 1.5.8