Mage_Newsletter_Model_Mysql4_Queue Class Reference

Inheritance diagram for Mage_Newsletter_Model_Mysql4_Queue:

Mage_Core_Model_Mysql4_Abstract Mage_Core_Model_Resource_Abstract

List of all members.

Public Member Functions

 addSubscribersToQueue (Mage_Newsletter_Model_Queue $queue, array $subscriberIds)
 removeSubscribersFromQueue (Mage_Newsletter_Model_Queue $queue)
 setStores (Mage_Newsletter_Model_Queue $queue)
 getStores (Mage_Newsletter_Model_Queue $queue)

Protected Member Functions

 _construct ()
 _afterSave (Mage_Core_Model_Abstract $queue)


Detailed Description

Definition at line 34 of file Queue.php.


Member Function Documentation

_afterSave ( Mage_Core_Model_Abstract queue  )  [protected]

Saving template after saving queue action

Parameters:
Mage_Core_Model_Abstract $queue
Returns:
Mage_Core_Model_Mysql4_Abstract

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 165 of file Queue.php.

00166     {
00167         if($queue->getSaveTemplateFlag()) {
00168             $queue->getTemplate()->save();
00169         }
00170         
00171         if($queue->getSaveStoresFlag()) {
00172             $this->setStores($queue);           
00173         }
00174         
00175         return $this;
00176     }

_construct (  )  [protected]

Resource initialization

Reimplemented from Mage_Core_Model_Resource_Abstract.

Definition at line 37 of file Queue.php.

00038     {
00039         $this->_init('newsletter/queue', 'queue_id');
00040     }

addSubscribersToQueue ( Mage_Newsletter_Model_Queue queue,
array subscriberIds 
)

Add subscribers to queue

Parameters:
Mage_Newsletter_Model_Queue $queue
array $subscriberIds

Definition at line 48 of file Queue.php.

00049     {
00050         if (count($subscriberIds)==0) {
00051             Mage::throwException(Mage::helper('newsletter')->__('No subscribers selected'));
00052         }
00053         
00054         if (!$queue->getId() && $queue->getQueueStatus()!=Mage_Newsletter_Model_Queue::STATUS_NEVER) {
00055             Mage::throwException(Mage::helper('newsletter')->__('Invalid queue selected'));
00056         }
00057         
00058         $select = $this->_getWriteAdapter()->select();
00059         $select->from($this->getTable('queue_link'),'subscriber_id')
00060             ->where('queue_id = ?', $queue->getId())
00061             ->where('subscriber_id in (?)', $subscriberIds);
00062         
00063         $usedIds = $this->_getWriteAdapter()->fetchCol($select);
00064         $this->_getWriteAdapter()->beginTransaction();
00065         try {
00066             foreach($subscriberIds as $subscriberId) {
00067                 if(in_array($subscriberId, $usedIds)) {
00068                     continue;
00069                 }
00070                 $data = array();
00071                 $data['queue_id'] = $queue->getId();
00072                 $data['subscriber_id'] = $subscriberId;
00073                 $this->_getWriteAdapter()->insert($this->getTable('queue_link'), $data);
00074             }
00075             $this->_getWriteAdapter()->commit();
00076         } 
00077         catch (Exception $e) {
00078             $this->_getWriteAdapter()->rollBack();
00079         }
00080         
00081     }

getStores ( Mage_Newsletter_Model_Queue queue  ) 

Definition at line 146 of file Queue.php.

00147     {
00148         $select = $this->_getReadAdapter()->select()
00149             ->from($this->getTable('queue_store_link'), 'store_id')
00150             ->where('queue_id = ?', $queue->getId());
00151         
00152         if(!($result = $this->_getReadAdapter()->fetchCol($select))) {
00153             $result = array();
00154         }
00155         
00156         return $result;
00157     }

removeSubscribersFromQueue ( Mage_Newsletter_Model_Queue queue  ) 

Definition at line 83 of file Queue.php.

00084     {
00085         try {
00086             $this->_getWriteAdapter()->delete(
00087                 $this->getTable('queue_link'), 
00088                 array(
00089                     $this->_getWriteAdapter()->quoteInto('queue_id = ?', $queue->getId()),
00090                     'letter_sent_at IS NULL'
00091                 )
00092             );
00093             
00094             $this->_getWriteAdapter()->commit();
00095         } 
00096         catch (Exception $e) {
00097             $this->_getWriteAdapter()->rollBack();
00098         }
00099         
00100     }

setStores ( Mage_Newsletter_Model_Queue queue  ) 

Definition at line 102 of file Queue.php.

00103     {
00104         $this->_getWriteAdapter()
00105             ->delete(
00106                 $this->getTable('queue_store_link'), 
00107                 $this->_getWriteAdapter()->quoteInto('queue_id = ?', $queue->getId())
00108             );
00109         
00110         if (!is_array($queue->getStores())) { 
00111             $stores = array(); 
00112         } else {
00113             $stores = $queue->getStores();
00114         }
00115         
00116         foreach ($stores as $storeId) {
00117             $data = array();
00118             $data['store_id'] = $storeId;
00119             $data['queue_id'] = $queue->getId();
00120             $this->_getWriteAdapter()->insert($this->getTable('queue_store_link'), $data);
00121         }
00122          
00123         $this->removeSubscribersFromQueue($queue);
00124 
00125         if(count($stores)==0) {
00126             return $this;
00127         }
00128         $subscribers = Mage::getResourceSingleton('newsletter/subscriber_collection')
00129             ->addFieldToFilter('store_id', array('in'=>$stores))
00130             ->useOnlySubscribed()
00131             ->load();
00132          
00133         $subscriberIds = array();
00134         
00135         foreach ($subscribers as $subscriber) {
00136             $subscriberIds[] = $subscriber->getId();
00137         }
00138         
00139         if (count($subscriberIds) > 0) {
00140             $this->addSubscribersToQueue($queue, $subscriberIds);
00141         }
00142         
00143         return $this;
00144     }


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