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_Queue extends Mage_Core_Model_Abstract
00035 {
00036
00037
00038
00039
00040
00041 protected $_template;
00042
00043
00044
00045
00046
00047 protected $_subscribersCollection = null;
00048
00049 protected $_saveTemplateFlag = false;
00050
00051 protected $_saveStoresFlag = false;
00052
00053 protected $_stores = false;
00054
00055 const STATUS_NEVER = 0;
00056 const STATUS_SENDING = 1;
00057 const STATUS_CANCEL = 2;
00058 const STATUS_SENT = 3;
00059 const STATUS_PAUSE = 4;
00060
00061
00062 protected function _construct()
00063 {
00064 $this->_init('newsletter/queue');
00065 }
00066
00067
00068
00069
00070
00071
00072 public function getSubscribersCollection()
00073 {
00074 if (is_null($this->_subscribersCollection)) {
00075 $this->_subscribersCollection = Mage::getResourceModel('newsletter/subscriber_collection')
00076 ->useQueue($this);
00077 }
00078
00079 return $this->_subscribersCollection;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089 public function addTemplateData( $data )
00090 {
00091 $template = $this->getTemplate();
00092 if ($data->getTemplateId() && $data->getTemplateId() != $template->getId()) {
00093 $template->load($data->getTemplateId());
00094 }
00095
00096 return $this;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105 public function sendPerSubscriber($count=20, array $additionalVariables=array())
00106 {
00107 if($this->getQueueStatus()!=self::STATUS_SENDING && ($this->getQueueStatus()!=self::STATUS_NEVER && $this->getQueueStartAt()) ) {
00108 return $this;
00109 }
00110
00111 if($this->getSubscribersCollection()->getSize()==0) {
00112 return $this;
00113 }
00114
00115 $collection = $this->getSubscribersCollection()
00116 ->useOnlyUnsent()
00117 ->showCustomerInfo()
00118 ->setPageSize($count)
00119 ->setCurPage(1)
00120 ->load();
00121
00122 if(!$this->getTemplate()) {
00123 $this->addTemplateData($this);
00124 if(!$this->getTemplate()->isPreprocessed()) {
00125 $this->getTemplate()->preproccess();
00126 }
00127 }
00128
00129 foreach($collection->getItems() as $item) {
00130 $this->getTemplate()->send($item, array('subscriber'=>$item), null, $this);
00131 }
00132
00133 if(count($collection->getItems()) < $count-1 || count($collection->getItems()) == 0) {
00134 $this->setQueueFinishAt(now());
00135 $this->setQueueStatus(self::STATUS_SENT);
00136 $this->save();
00137 }
00138 return $this;
00139 }
00140
00141 public function getDataForSave() {
00142 $data = array();
00143 $data['template_id'] = $this->getTemplateId();
00144 $data['queue_status'] = $this->getQueueStatus();
00145 $data['queue_start_at'] = $this->getQueueStartAt();
00146 $data['queue_finish_at'] = $this->getQueueFinishAt();
00147 return $data;
00148 }
00149
00150 public function addSubscribersToQueue(array $subscriberIds)
00151 {
00152 $this->_getResource()->addSubscribersToQueue($this, $subscriberIds);
00153 return $this;
00154 }
00155
00156 public function setSaveTemplateFlag($value)
00157 {
00158 $this->_saveTemplateFlag = (boolean)$value;
00159 return $this;
00160 }
00161
00162 public function getSaveTemplateFlag()
00163 {
00164 return $this->_saveTemplateFlag;
00165 }
00166
00167 public function setSaveStoresFlag($value)
00168 {
00169 $this->_saveStoresFlag = (boolean)$value;
00170 return $this;
00171 }
00172
00173 public function getSaveStoresFlag()
00174 {
00175 return $this->_saveStoresFlag;
00176 }
00177
00178 public function setStores(array $storesIds)
00179 {
00180 $this->setSaveStoresFlag(true);
00181 $this->_stores = $storesIds;
00182 return $this;
00183 }
00184
00185 public function getStores()
00186 {
00187 if(!$this->_stores) {
00188 $this->_stores = $this->_getResource()->getStores($this);
00189 }
00190
00191 return $this->_stores;
00192 }
00193
00194
00195
00196
00197
00198
00199 public function getTemplate()
00200 {
00201 if (is_null($this->_template)) {
00202 $this->_template = Mage::getModel('newsletter/template')
00203 ->load($this->getTemplateId());
00204 }
00205 return $this->_template;
00206 }
00207 }