Mage_Newsletter_Model_Mysql4_Subscriber Class Reference

List of all members.

Public Member Functions

 __construct ()
 load ($subscriberId)
 setMessagesScope ($scope)
 loadByEmail ($subscriberEmail)
 loadByCustomer (Mage_Customer_Model_Customer $customer)
 save (Mage_Newsletter_Model_Subscriber $subscriber)
 delete ($subscriberId)
 received (Mage_Newsletter_Model_Subscriber $subscriber, Mage_Newsletter_Model_Queue $queue)

Protected Member Functions

 _generateRandomCode ()
 _prepareSave (Mage_Newsletter_Model_Subscriber $subscriber)

Protected Attributes

 $_read
 $_write
 $_subscriberTable
 $_subscriberLinkTable
 $_messagesScope = 'newsletter/session'


Detailed Description

Definition at line 34 of file Subscriber.php.


Constructor & Destructor Documentation

__construct (  ) 

Constructor

Set read and write connection, get tablename from config

Definition at line 76 of file Subscriber.php.

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     }


Member Function Documentation

_generateRandomCode (  )  [protected]

Generates random code for subscription confirmation

Returns:
string

Definition at line 196 of file Subscriber.php.

00197     {
00198         return md5(microtime() + rand());
00199     }

_prepareSave ( Mage_Newsletter_Model_Subscriber subscriber  )  [protected]

Preapares data for saving of subscriber

Parameters:
Mage_Newsletter_Model_Subscriber $subscriber
Returns:
array

Definition at line 207 of file Subscriber.php.

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     }

delete ( subscriberId  ) 

Delete subscriber from DB

Parameters:
int $subscriberId

Definition at line 245 of file Subscriber.php.

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     }

load ( subscriberId  ) 

Load subscriber from DB

Parameters:
int $subscriberId
Returns:
array

Definition at line 90 of file Subscriber.php.

00091     {
00092         $select = $this->_read->select()
00093             ->from($this->_subscriberTable)
00094             ->where('subscriber_id=?',$subscriberId);
00095 
00096         return $this->_read->fetchRow($select);
00097     }

loadByCustomer ( Mage_Customer_Model_Customer customer  ) 

Load subscriber by customer

Parameters:
Mage_Customer_Model_Customer $customer
Returns:
array

Definition at line 138 of file Subscriber.php.

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     }

loadByEmail ( subscriberEmail  ) 

Load subscriber from DB by email

Parameters:
string $subscriberEmail
Returns:
array

Definition at line 115 of file Subscriber.php.

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     }

received ( Mage_Newsletter_Model_Subscriber subscriber,
Mage_Newsletter_Model_Queue queue 
)

Definition at line 263 of file Subscriber.php.

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     }

save ( Mage_Newsletter_Model_Subscriber subscriber  ) 

Save subscriber info from it model.

Parameters:
Mage_Newsletter_Model_Subscriber $subscriber
Returns:
Mage_Newsletter_Model_Subscriber

Definition at line 169 of file Subscriber.php.

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     }

setMessagesScope ( scope  ) 

Set error messages scope

Parameters:
string $scope

Definition at line 104 of file Subscriber.php.

00105     {
00106         $this->_messagesScope = $scope;
00107     }


Member Data Documentation

$_messagesScope = 'newsletter/session' [protected]

Definition at line 69 of file Subscriber.php.

$_read [protected]

Definition at line 41 of file Subscriber.php.

$_subscriberLinkTable [protected]

Definition at line 62 of file Subscriber.php.

$_subscriberTable [protected]

Definition at line 55 of file Subscriber.php.

$_write [protected]

Definition at line 48 of file Subscriber.php.


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

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