1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Adminhtml_Newsletter_QueueController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38:
39: public function indexAction()
40: {
41: $this->_title($this->__('Newsletter'))->_title($this->__('Newsletter Queue'));
42:
43: if ($this->getRequest()->getQuery('ajax')) {
44: $this->_forward('grid');
45: return;
46: }
47:
48: $this->loadLayout();
49:
50: $this->_setActiveMenu('newsletter/queue');
51:
52: $this->_addContent(
53: $this->getLayout()->createBlock('adminhtml/newsletter_queue', 'queue')
54: );
55:
56: $this->_addBreadcrumb(Mage::helper('newsletter')->__('Newsletter Queue'), Mage::helper('newsletter')->__('Newsletter Queue'));
57:
58: $this->renderLayout();
59: }
60:
61:
62: 63: 64:
65: public function dropAction ()
66: {
67: $this->loadLayout('newsletter_queue_preview');
68: $this->renderLayout();
69: }
70:
71: 72: 73:
74: public function previewAction()
75: {
76: $this->loadLayout();
77: $data = $this->getRequest()->getParams();
78: if (empty($data) || !isset($data['id'])) {
79: $this->_forward('noRoute');
80: return $this;
81: }
82:
83:
84: $data['preview_store_id'] = Mage::app()->getDefaultStoreView()->getId();
85:
86: $this->getLayout()->getBlock('preview_form')->setFormData($data);
87: $this->renderLayout();
88: }
89:
90: 91: 92:
93: public function gridAction()
94: {
95: $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/newsletter_queue_grid')->toHtml());
96: }
97:
98: public function startAction()
99: {
100: $queue = Mage::getModel('newsletter/queue')
101: ->load($this->getRequest()->getParam('id'));
102: if ($queue->getId()) {
103: if (!in_array($queue->getQueueStatus(),
104: array(Mage_Newsletter_Model_Queue::STATUS_NEVER,
105: Mage_Newsletter_Model_Queue::STATUS_PAUSE))) {
106: $this->_redirect('*/*');
107: return;
108: }
109:
110: $queue->setQueueStartAt(Mage::getSingleton('core/date')->gmtDate())
111: ->setQueueStatus(Mage_Newsletter_Model_Queue::STATUS_SENDING)
112: ->save();
113: }
114:
115: $this->_redirect('*/*');
116: }
117:
118: public function pauseAction()
119: {
120: $queue = Mage::getSingleton('newsletter/queue')
121: ->load($this->getRequest()->getParam('id'));
122:
123: if (!in_array($queue->getQueueStatus(),
124: array(Mage_Newsletter_Model_Queue::STATUS_SENDING))) {
125: $this->_redirect('*/*');
126: return;
127: }
128:
129: $queue->setQueueStatus(Mage_Newsletter_Model_Queue::STATUS_PAUSE);
130: $queue->save();
131:
132: $this->_redirect('*/*');
133: }
134:
135: public function resumeAction()
136: {
137: $queue = Mage::getSingleton('newsletter/queue')
138: ->load($this->getRequest()->getParam('id'));
139:
140: if (!in_array($queue->getQueueStatus(),
141: array(Mage_Newsletter_Model_Queue::STATUS_PAUSE))) {
142: $this->_redirect('*/*');
143: return;
144: }
145:
146: $queue->setQueueStatus(Mage_Newsletter_Model_Queue::STATUS_SENDING);
147: $queue->save();
148:
149: $this->_redirect('*/*');
150: }
151:
152: public function cancelAction()
153: {
154: $queue = Mage::getSingleton('newsletter/queue')
155: ->load($this->getRequest()->getParam('id'));
156:
157: if (!in_array($queue->getQueueStatus(),
158: array(Mage_Newsletter_Model_Queue::STATUS_SENDING))) {
159: $this->_redirect('*/*');
160: return;
161: }
162:
163: $queue->setQueueStatus(Mage_Newsletter_Model_Queue::STATUS_CANCEL);
164: $queue->save();
165:
166: $this->_redirect('*/*');
167: }
168:
169: public function sendingAction()
170: {
171:
172: $countOfQueue = 3;
173: $countOfSubscritions = 20;
174:
175: $collection = Mage::getResourceModel('newsletter/queue_collection')
176: ->setPageSize($countOfQueue)
177: ->setCurPage(1)
178: ->addOnlyForSendingFilter()
179: ->load();
180:
181: $collection->walk('sendPerSubscriber', array($countOfSubscritions));
182: }
183:
184: public function editAction()
185: {
186: $this->_title($this->__('Newsletter'))->_title($this->__('Newsletter Queue'));
187:
188: Mage::register('current_queue', Mage::getSingleton('newsletter/queue'));
189:
190: $id = $this->getRequest()->getParam('id');
191: $templateId = $this->getRequest()->getParam('template_id');
192:
193: if ($id) {
194: $queue = Mage::registry('current_queue')->load($id);
195: } elseif ($templateId) {
196: $template = Mage::getModel('newsletter/template')->load($templateId);
197: $queue = Mage::registry('current_queue')->setTemplateId($template->getId());
198: }
199:
200: $this->_title($this->__('Edit Queue'));
201:
202: $this->loadLayout();
203:
204: $this->_setActiveMenu('newsletter/queue');
205:
206: $this->_addBreadcrumb(
207: Mage::helper('newsletter')->__('Newsletter Queue'),
208: Mage::helper('newsletter')->__('Newsletter Queue'),
209: $this->getUrl('*/newsletter_queue')
210: );
211: $this->_addBreadcrumb(Mage::helper('newsletter')->__('Edit Queue'), Mage::helper('newsletter')->__('Edit Queue'));
212:
213: $this->renderLayout();
214: }
215:
216: public function saveAction()
217: {
218: try {
219:
220: $queue = Mage::getModel('newsletter/queue');
221:
222: $templateId = $this->getRequest()->getParam('template_id');
223: if ($templateId) {
224:
225: $template = Mage::getModel('newsletter/template')->load($templateId);
226:
227: if (!$template->getId() || $template->getIsSystem()) {
228: Mage::throwException($this->__('Wrong newsletter template.'));
229: }
230:
231: $queue->setTemplateId($template->getId())
232: ->setQueueStatus(Mage_Newsletter_Model_Queue::STATUS_NEVER);
233: } else {
234: $queue->load($this->getRequest()->getParam('id'));
235: }
236:
237: if (!in_array($queue->getQueueStatus(),
238: array(Mage_Newsletter_Model_Queue::STATUS_NEVER,
239: Mage_Newsletter_Model_Queue::STATUS_PAUSE))
240: ) {
241: $this->_redirect('*/*');
242: return;
243: }
244:
245: if ($queue->getQueueStatus() == Mage_Newsletter_Model_Queue::STATUS_NEVER) {
246: $queue->setQueueStartAtByString($this->getRequest()->getParam('start_at'));
247: }
248:
249: $queue->setStores($this->getRequest()->getParam('stores', array()))
250: ->setNewsletterSubject($this->getRequest()->getParam('subject'))
251: ->setNewsletterSenderName($this->getRequest()->getParam('sender_name'))
252: ->setNewsletterSenderEmail($this->getRequest()->getParam('sender_email'))
253: ->setNewsletterText($this->getRequest()->getParam('text'))
254: ->setNewsletterStyles($this->getRequest()->getParam('styles'));
255:
256: if ($queue->getQueueStatus() == Mage_Newsletter_Model_Queue::STATUS_PAUSE
257: && $this->getRequest()->getParam('_resume', false)) {
258: $queue->setQueueStatus(Mage_Newsletter_Model_Queue::STATUS_SENDING);
259: }
260:
261: $queue->save();
262: $this->_redirect('*/*');
263: }
264: catch (Mage_Core_Exception $e) {
265: $this->_getSession()->addError($e->getMessage());
266: $id = $this->getRequest()->getParam('id');
267: if ($id) {
268: $this->_redirect('*/*/edit', array('id' => $id));
269: } else {
270: $this->_redirectReferer();
271: }
272: }
273: }
274:
275: protected function _isAllowed()
276: {
277: return Mage::getSingleton('admin/session')->isAllowed('newsletter/queue');
278: }
279: }
280: