1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Newsletter
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Newsletter queue model.
29: *
30: * @method Mage_Newsletter_Model_Resource_Queue _getResource()
31: * @method Mage_Newsletter_Model_Resource_Queue getResource()
32: * @method int getTemplateId()
33: * @method Mage_Newsletter_Model_Queue setTemplateId(int $value)
34: * @method int getNewsletterType()
35: * @method Mage_Newsletter_Model_Queue setNewsletterType(int $value)
36: * @method string getNewsletterText()
37: * @method Mage_Newsletter_Model_Queue setNewsletterText(string $value)
38: * @method string getNewsletterStyles()
39: * @method Mage_Newsletter_Model_Queue setNewsletterStyles(string $value)
40: * @method string getNewsletterSubject()
41: * @method Mage_Newsletter_Model_Queue setNewsletterSubject(string $value)
42: * @method string getNewsletterSenderName()
43: * @method Mage_Newsletter_Model_Queue setNewsletterSenderName(string $value)
44: * @method string getNewsletterSenderEmail()
45: * @method Mage_Newsletter_Model_Queue setNewsletterSenderEmail(string $value)
46: * @method int getQueueStatus()
47: * @method Mage_Newsletter_Model_Queue setQueueStatus(int $value)
48: * @method string getQueueStartAt()
49: * @method Mage_Newsletter_Model_Queue setQueueStartAt(string $value)
50: * @method string getQueueFinishAt()
51: * @method Mage_Newsletter_Model_Queue setQueueFinishAt(string $value)
52: *
53: * @category Mage
54: * @package Mage_Newsletter
55: * @author Magento Core Team <core@magentocommerce.com>
56: */
57: class Mage_Newsletter_Model_Queue extends Mage_Core_Model_Template
58: {
59: /**
60: * Newsletter Template object
61: *
62: * @var Mage_Newsletter_Model_Template
63: */
64: protected $_template;
65:
66: /**
67: * Subscribers collection
68: * @var Varien_Data_Collection_Db
69: */
70: protected $_subscribersCollection = null;
71:
72: /**
73: * save template flag
74: *
75: * @var boolean
76: * @deprecated since 1.4.0.1
77: */
78: protected $_saveTemplateFlag = false;
79:
80: /**
81: * Save stores flag.
82: *
83: * @var boolean
84: */
85: protected $_saveStoresFlag = false;
86:
87: /**
88: * Stores assigned to queue.
89: *
90: * @var array
91: */
92: protected $_stores = array();
93:
94: const STATUS_NEVER = 0;
95: const STATUS_SENDING = 1;
96: const STATUS_CANCEL = 2;
97: const STATUS_SENT = 3;
98: const STATUS_PAUSE = 4;
99:
100: /**
101: * Initialize resource model
102: */
103: protected function _construct()
104: {
105: $this->_init('newsletter/queue');
106: }
107:
108: /**
109: * Return: is this queue newly created or not.
110: *
111: * @return boolean
112: */
113: public function isNew()
114: {
115: return (is_null($this->getQueueStatus()));
116: }
117:
118: /**
119: * Returns subscribers collection for this queue
120: *
121: * @return Varien_Data_Collection_Db
122: */
123: public function getSubscribersCollection()
124: {
125: if (is_null($this->_subscribersCollection)) {
126: $this->_subscribersCollection = Mage::getResourceModel('newsletter/subscriber_collection')
127: ->useQueue($this);
128: }
129:
130: return $this->_subscribersCollection;
131: }
132:
133: /**
134: * Add template data to queue.
135: *
136: * @param Varien_Object $data
137: * @return Mage_Newsletter_Model_Queue
138: * @deprecated since 1.4.0.1
139: */
140: public function addTemplateData($data)
141: {
142: $template = $this->getTemplate();
143: if ($data->getTemplateId() && $data->getTemplateId() != $template->getId()) {
144: $template->load($data->getTemplateId());
145: }
146:
147: return $this;
148: }
149:
150: /**
151: * Set $_data['queue_start'] based on string from backend, which based on locale.
152: *
153: * @param string|null $startAt start date of the mailing queue
154: * @return Mage_Newsletter_Model_Queue
155: */
156: public function setQueueStartAtByString($startAt)
157: {
158: if(is_null($startAt) || $startAt == '') {
159: $this->setQueueStartAt(null);
160: } else {
161: $locale = Mage::app()->getLocale();
162: $format = $locale->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
163: $time = $locale->date($startAt, $format)->getTimestamp();
164: $this->setQueueStartAt(Mage::getModel('core/date')->gmtDate(null, $time));
165: }
166: return $this;
167: }
168:
169: /**
170: * Send messages to subscribers for this queue
171: *
172: * @param int $count
173: * @param array $additionalVariables
174: * @return Mage_Newsletter_Model_Queue
175: */
176: public function sendPerSubscriber($count=20, array $additionalVariables=array())
177: {
178: if($this->getQueueStatus()!=self::STATUS_SENDING
179: && ($this->getQueueStatus()!=self::STATUS_NEVER && $this->getQueueStartAt())
180: ) {
181: return $this;
182: }
183:
184: if ($this->getSubscribersCollection()->getSize() == 0) {
185: $this->_finishQueue();
186: return $this;
187: }
188:
189: $collection = $this->getSubscribersCollection()
190: ->useOnlyUnsent()
191: ->showCustomerInfo()
192: ->setPageSize($count)
193: ->setCurPage(1)
194: ->load();
195:
196: /* @var $sender Mage_Core_Model_Email_Template */
197: $sender = Mage::getModel('core/email_template');
198: $sender->setSenderName($this->getNewsletterSenderName())
199: ->setSenderEmail($this->getNewsletterSenderEmail())
200: ->setTemplateType(self::TYPE_HTML)
201: ->setTemplateSubject($this->getNewsletterSubject())
202: ->setTemplateText($this->getNewsletterText())
203: ->setTemplateStyles($this->getNewsletterStyles())
204: ->setTemplateFilter(Mage::helper('newsletter')->getTemplateProcessor());
205:
206: foreach($collection->getItems() as $item) {
207: $email = $item->getSubscriberEmail();
208: $name = $item->getSubscriberFullName();
209:
210: $sender->emulateDesign($item->getStoreId());
211: $successSend = $sender->send($email, $name, array('subscriber' => $item));
212: $sender->revertDesign();
213:
214: if($successSend) {
215: $item->received($this);
216: } else {
217: $problem = Mage::getModel('newsletter/problem');
218: $notification = Mage::helper('newsletter')->__('Please refer to exeption.log');
219: $problem->addSubscriberData($item)
220: ->addQueueData($this)
221: ->addErrorData(new Exception($notification))
222: ->save();
223: $item->received($this);
224: }
225: }
226:
227: if(count($collection->getItems()) < $count-1 || count($collection->getItems()) == 0) {
228: $this->_finishQueue();
229: }
230: return $this;
231: }
232:
233: /**
234: * Finish queue: set status SENT and update finish date
235: *
236: * @return Mage_Newsletter_Model_Queue
237: */
238: protected function _finishQueue()
239: {
240: $this->setQueueFinishAt(Mage::getSingleton('core/date')->gmtDate());
241: $this->setQueueStatus(self::STATUS_SENT);
242: $this->save();
243:
244: return $this;
245: }
246:
247: /**
248: * Getter data for saving
249: *
250: * @return array
251: */
252: public function getDataForSave()
253: {
254: $data = array();
255: $data['template_id'] = $this->getTemplateId();
256: $data['queue_status'] = $this->getQueueStatus();
257: $data['queue_start_at'] = $this->getQueueStartAt();
258: $data['queue_finish_at'] = $this->getQueueFinishAt();
259: return $data;
260: }
261:
262: /**
263: * Add subscribers to queue.
264: *
265: * @param array $subscriberIds
266: * @return Mage_Newsletter_Model_Queue
267: */
268: public function addSubscribersToQueue(array $subscriberIds)
269: {
270: $this->_getResource()->addSubscribersToQueue($this, $subscriberIds);
271: return $this;
272: }
273:
274: /**
275: * Setter for save template flag.
276: *
277: * @param boolean|integer|string $value
278: * @return Mage_Newsletter_Model_Queue
279: * @deprecated since 1.4.0.1
280: */
281: public function setSaveTemplateFlag($value)
282: {
283: $this->_saveTemplateFlag = (boolean)$value;
284: return $this;
285: }
286:
287: /**
288: * Getter for save template flag.
289: *
290: * @param void
291: * @return boolean
292: * @deprecated since 1.4.0.1
293: */
294: public function getSaveTemplateFlag()
295: {
296: return $this->_saveTemplateFlag;
297: }
298:
299: /**
300: * Setter for save stores flag.
301: *
302: * @param boolean|integer|string $value
303: * @return Mage_Newsletter_Model_Queue
304: */
305: public function setSaveStoresFlag($value)
306: {
307: $this->_saveStoresFlag = (boolean)$value;
308: return $this;
309: }
310:
311: /**
312: * Getter for save stores flag.
313: *
314: * @param void
315: * @return boolean
316: */
317: public function getSaveStoresFlag()
318: {
319: return $this->_saveStoresFlag;
320: }
321:
322: /**
323: * Setter for stores of queue.
324: *
325: * @param array
326: * @return Mage_Newsletter_Model_Queue
327: */
328: public function setStores(array $storesIds)
329: {
330: $this->setSaveStoresFlag(true);
331: $this->_stores = $storesIds;
332: return $this;
333: }
334:
335: /**
336: * Getter for stores of queue.
337: *
338: * @return array
339: */
340: public function getStores()
341: {
342: if(!$this->_stores) {
343: $this->_stores = $this->_getResource()->getStores($this);
344: }
345:
346: return $this->_stores;
347: }
348:
349: /**
350: * Retrieve Newsletter Template object
351: *
352: * @return Mage_Newsletter_Model_Template
353: */
354: public function getTemplate()
355: {
356: if (is_null($this->_template)) {
357: $this->_template = Mage::getModel('newsletter/template')
358: ->load($this->getTemplateId());
359: }
360: return $this->_template;
361: }
362:
363: /**
364: * Getter for template type
365: *
366: * @return int|string
367: */
368: public function getType(){
369: return $this->getNewsletterType();
370: }
371:
372: }
373: