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_XmlConnect_Model_Queue extends Mage_Core_Model_Template
35: {
36: 37: 38:
39: const STATUS_IN_QUEUE = 0;
40:
41: 42: 43:
44: const STATUS_CANCELED = 1;
45:
46: 47: 48:
49: const STATUS_COMPLETED = 2;
50:
51: 52: 53:
54: const STATUS_DELETED = 3;
55:
56: 57: 58:
59: const MESSAGE_TYPE_AIRMAIL = 'airmail';
60:
61: 62: 63:
64: const MESSAGE_TYPE_PUSH = 'push';
65:
66: 67: 68:
69: const XML_PATH_NOTIFICATION_TYPE = 'xmlconnect/devices/%s/notification_type';
70:
71: 72: 73: 74:
75: const XML_PATH_CRON_MESSAGES_COUNT = 'xmlconnect/mobile_application/cron_send_messages_count';
76:
77: 78: 79: 80: 81:
82: protected $_appType = null;
83:
84: 85: 86: 87: 88:
89: protected function _construct()
90: {
91: $this->_init('xmlconnect/queue');
92: }
93:
94: 95: 96: 97: 98: 99: 100:
101: public function load($id, $field = null)
102: {
103: parent::load($id, $field);
104:
105: if ($this->getTemplateId()) {
106: $this->setName(Mage::getModel('xmlconnect/template')->load($this->getTemplateId())->getName());
107: }
108: return $this;
109: }
110:
111: 112: 113: 114: 115:
116: public function getType()
117: {
118: return self::TYPE_HTML;
119: }
120:
121: 122: 123: 124:
125: public function getApplicationType()
126: {
127: if (empty($this->_appType) && $this->getAppCode()) {
128: $app = Mage::getModel('xmlconnect/application')->loadByCode($this->getAppCode());
129: $this->_appType = $app->getId() ? $app->getType() : null;
130: }
131:
132: return $this->_appType;
133: }
134:
135: 136: 137: 138: 139:
140: public function getAppName()
141: {
142: if ($this->getApplicationName()) {
143: return $this->getApplicationName();
144: } else {
145: return Mage::helper('xmlconnect')->getApplicationName($this->getAppCode());
146: }
147: }
148:
149: 150: 151: 152: 153:
154: public function getTplName()
155: {
156: if ($this->getTemplateName()) {
157: return $this->getTemplateName();
158: } else {
159: return Mage::helper('xmlconnect')->getTemplateName($this->getTemplateId());
160: }
161: }
162:
163: 164: 165: 166: 167: 168:
169: public function getProcessedTemplate(array $variables = array())
170: {
171:
172: $processor = Mage::getModel('widget/template_filter');
173:
174: $variables['this'] = $this;
175:
176: if (Mage::app()->isSingleStoreMode()) {
177: $processor->setStoreId(Mage::app()->getStore());
178: } else {
179: $processor->setStoreId(1);
180: }
181:
182: $htmlDescription = <<<EOT
183: <div style="font-size: 0.8em; text-decoration: underline; margin-top: 1.5em; line-height: 2em;">%s:</div>
184: EOT;
185:
186: switch ($this->getData('type')) {
187: case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL:
188: $html = sprintf($htmlDescription, Mage::helper('xmlconnect')->__('Push title'))
189: . $this->getPushTitle()
190: . sprintf($htmlDescription, Mage::helper('xmlconnect')->__('Message title'))
191: . $this->getMessageTitle()
192: . sprintf($htmlDescription, Mage::helper('xmlconnect')->__('Message content'))
193: . $processor->filter($this->getContent());
194: break;
195: case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH:
196: default:
197: $html = sprintf($htmlDescription, Mage::helper('xmlconnect')->__('Push title'))
198: . $this->getPushTitle();
199: break;
200: }
201: return $html;
202: }
203:
204: 205: 206: 207: 208:
209: public function reset()
210: {
211: $this->setData(array());
212: $this->setOrigData();
213:
214: return $this;
215: }
216:
217:
218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235:
236: public function getAirmailBroadcastParams()
237: {
238: $notificationType = Mage::getStoreConfig(
239: sprintf(Mage_XmlConnect_Model_Queue::XML_PATH_NOTIFICATION_TYPE, $this->getApplicationType())
240: );
241:
242: $payload = array(
243: 'push' => array($notificationType => array('alert' => $this->getPushTitle())),
244: 'title' => $this->getMessageTitle(),
245: 'message' => $this->getContent(),
246: );
247: return Mage::helper('core')->jsonEncode($payload);
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266:
267: public function getPushBroadcastParams()
268: {
269: $notificationType = Mage::getStoreConfig(
270: sprintf(Mage_XmlConnect_Model_Queue::XML_PATH_NOTIFICATION_TYPE, $this->getApplicationType())
271: );
272:
273: $payload = array(
274: $notificationType => array(
275:
276: 'alert' => $this->getPushTitle(),
277: 'sound' => 'default'
278: )
279: );
280: return Mage::helper('core')->jsonEncode($payload);
281: }
282:
283: 284: 285: 286: 287:
288: public function save()
289: {
290: if (!$this->getIsSent() && $this->getStatus() == self::STATUS_IN_QUEUE) {
291: try {
292: Mage::dispatchEvent('before_save_message_queue', array('queueMessage' => $this));
293: } catch (Exception $e) {
294: Mage::logException($e);
295: }
296: }
297: return parent::save();
298: }
299: }
300: