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_Adminhtml_MobileController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40: 41: 42:
43: protected function _initApp($paramName = 'application_id', $type = false)
44: {
45: $id = (int) $this->getRequest()->getParam($paramName);
46: $app = Mage::getModel('xmlconnect/application');
47: if ($id) {
48: $app->load($id);
49: if ($app->getId()) {
50: $app->loadConfiguration();
51: }
52: } else {
53: $app->setType($type);
54: Mage::register('current_app', $app);
55: $app->loadDefaultConfiguration();
56: Mage::unregister('current_app');
57: }
58: Mage::register('current_app', $app);
59: return $app;
60: }
61:
62: 63: 64: 65: 66: 67:
68: protected function _restoreSessionFilesFormData($data)
69: {
70: $filesData = Mage::getSingleton('adminhtml/session')->getUploadedFilesFormData(true);
71: if (!empty($filesData) && is_array($filesData)) {
72: if (!is_array($data)) {
73: $data = array();
74: }
75: foreach ($filesData as $filePath => $fileName) {
76: $target =& $data;
77: Mage::helper('xmlconnect')->_injectFieldToArray($target, $filePath, $fileName);
78: }
79: }
80: return $data;
81: }
82:
83: 84: 85: 86: 87:
88: public function indexAction()
89: {
90: $this->loadLayout();
91: $this->_setActiveMenu('xmlconnect/mobile');
92: $this->renderLayout();
93: }
94:
95: 96: 97: 98: 99:
100: public function newAction()
101: {
102: Mage::getSingleton('adminhtml/session')->setData('new_application', true);
103: $this->loadLayout();
104: $this->_setActiveMenu('xmlconnect/mobile');
105: $this->renderLayout();
106: }
107:
108: 109: 110: 111: 112:
113: public function submissionAction()
114: {
115: try {
116: $app = $this->_initApp();
117: if (!$app->getId()) {
118: $this->_getSession()->addError($this->__('App does not exist.'));
119: $this->_redirect('*/*/');
120: return;
121: }
122: $app->loadSubmit();
123: if ((bool) Mage::getSingleton('adminhtml/session')->getLoadSessionFlag(true)) {
124: $data = $this->_restoreSessionFilesFormData(
125: Mage::getSingleton('adminhtml/session')->getFormSubmissionData(true)
126: );
127: if (!empty($data)) {
128: $app->setData(Mage::helper('xmlconnect')->arrayMergeRecursive($app->getData(), $data));
129: }
130: }
131:
132: $this->loadLayout();
133: $this->_setActiveMenu('xmlconnect/mobile');
134: $this->renderLayout();
135: } catch (Mage_Core_Exception $e) {
136: $this->_getSession()->addError($e->getMessage());
137: if (isset($app)) {
138: $this->_redirect('*/*/edit', array('application_id' => $app->getId()));
139: } else {
140: $this->_redirect('*/*/');
141: }
142: } catch (Exception $e) {
143: $this->_getSession()->addException($e, $this->__('Can\'t open submission form.'));
144: if (isset($app)) {
145: $this->_redirect('*/*/edit', array('application_id' => $app->getId()));
146: } else {
147: $this->_redirect('*/*/');
148: }
149: }
150: }
151:
152: 153: 154: 155: 156:
157: public function editAction()
158: {
159: $redirectBack = false;
160: try {
161: $id = (int) $this->getRequest()->getParam('application_id');
162: $type = $this->getRequest()->getParam('type');
163: $app = $this->_initApp('application_id', $type);
164:
165: if (!$app->getId() && $id) {
166: $this->_getSession()->addError($this->__('App does not exist.'));
167: $this->_redirect('*/*/');
168: return;
169: }
170:
171: $newAppData = $this->_restoreSessionFilesFormData(
172: Mage::getSingleton('adminhtml/session')->getFormData(true)
173: );
174:
175: if (!empty($newAppData)) {
176: $app->setData(Mage::helper('xmlconnect')->arrayMergeRecursive($app->getData(), $newAppData));
177: }
178:
179: if ($app->getId() || $app->getType()) {
180: Mage::getSingleton('adminhtml/session')->setData('new_application', false);
181: } else {
182: $this->_redirect('*/*/new');
183: }
184:
185: $devArray = Mage::helper('xmlconnect')->getSupportedDevices();
186: if (array_key_exists($app->getType(), $devArray)) {
187: $deviceTitle = $devArray[$app->getType()];
188: }
189: $deviceTitle = isset($deviceTitle) ? $deviceTitle : $app->getType();
190: $app->setDevtype($deviceTitle);
191: $app->loadSubmit();
192: $this->loadLayout();
193: $this->_setActiveMenu('xmlconnect/mobile');
194: $this->renderLayout();
195: } catch (Mage_Core_Exception $e) {
196: $this->_getSession()->addError($e->getMessage());
197: $redirectBack = true;
198: } catch (Exception $e) {
199: $this->_getSession()->addError($this->__('Unable to load application form.'));
200: $redirectBack = true;
201: Mage::logException($e);
202: }
203: if ($redirectBack) {
204: $this->_redirect('*/*/');
205: return;
206: }
207: }
208:
209: 210: 211: 212: 213:
214: public function submissionPostAction()
215: {
216: $data = $this->getRequest()->getPost();
217: try {
218: $isError = false;
219: if (!empty($data)) {
220: Mage::getSingleton('adminhtml/session')->setFormSubmissionData($this->_filterFormDataForSession($data));
221: }
222:
223: $app = $this->_initApp('key');
224: $app->loadSubmit();
225: $newAppData = $this->_processUploadedFiles($app->getData(), true);
226: if (!empty($newAppData)) {
227: $app->setData(Mage::helper('xmlconnect')->arrayMergeRecursive($app->getData(), $newAppData));
228: }
229: $params = $app->prepareSubmitParams($data);
230: $errors = $app->validateSubmit($params);
231: if ($errors !== true) {
232: foreach ($errors as $err) {
233: $this->_getSession()->addError($err);
234: }
235: $isError = true;
236: }
237: if (!$isError) {
238: $this->_processPostRequest();
239: $history = Mage::getModel('xmlconnect/history');
240: $history->setData(array(
241: 'params' => $params, 'application_id' => $app->getId(),
242: 'created_at' => Mage::getModel('core/date')->date(), 'store_id' => $app->getStoreId(),
243: 'title' => isset($params['title']) ? $params['title'] : '', 'name' => $app->getName(),
244: 'code' => $app->getCode(), 'activation_key' => isset($params['resubmission_activation_key'])
245: ? $params['resubmission_activation_key'] : $params['key'],
246: ));
247: $history->save();
248: $app->getResource()->updateApplicationStatus(
249: $app->getId(), Mage_XmlConnect_Model_Application::APP_STATUS_SUCCESS
250: );
251: $this->_getSession()->addSuccess($this->__('App has been submitted.'));
252: $this->_clearSessionData();
253: $this->_redirect('*/*/edit', array('application_id' => $app->getId()));
254: } else {
255: Mage::getSingleton('adminhtml/session')->setLoadSessionFlag(true);
256: $this->_redirect('*/*/submission', array('application_id' => $app->getId()));
257: }
258: } catch (Mage_Core_Exception $e) {
259: $this->_getSession()->addError($e->getMessage());
260: if (isset($app)) {
261: Mage::getSingleton('adminhtml/session')->setLoadSessionFlag(true);
262: $this->_redirect('*/*/submission', array('application_id' => $app->getId()));
263: } else {
264: $this->_redirect('*/*/');
265: }
266: } catch (Exception $e) {
267: $this->_getSession()->addException($e, $this->__('Can\'t submit application.'));
268: Mage::logException($e);
269: if (isset($app)) {
270: Mage::getSingleton('adminhtml/session')->setLoadSessionFlag(true);
271: $this->_redirect('*/*/submission', array('application_id' => $app->getId()));
272: } else {
273: $this->_redirect('*/*/');
274: }
275: }
276: }
277:
278: 279: 280: 281: 282: 283:
284: protected function _filterFormDataForSession($data)
285: {
286: $params = null;
287: if (isset($data['conf']) && is_array($data['conf'])) {
288: if (isset($data['conf']['submit_text']) && is_array($data['conf']['submit_text'])) {
289: $params = &$data['conf']['submit_text'];
290: }
291: }
292: if (isset($params['country']) && is_array($params['country'])) {
293: $data['conf']['submit_text']['country'] = implode(',', $params['country']);
294: }
295: return $data;
296: }
297:
298: 299: 300: 301: 302: 303:
304: protected function _clearSessionData()
305: {
306: Mage::getSingleton('adminhtml/session')->unsFormData();
307: Mage::getSingleton('adminhtml/session')->unsFormSubmissionData();
308: Mage::getSingleton('adminhtml/session')->unsUploadedFilesFormData();
309: return $this;
310: }
311:
312: 313: 314: 315: 316:
317: protected function _processPostRequest()
318: {
319: try {
320:
321: $app = Mage::helper('xmlconnect')->getApplication();
322: $params = $app->getSubmitParams();
323:
324: $appConnectorUrl = Mage::getStoreConfig('xmlconnect/mobile_application/magentocommerce_url');
325:
326: $curl = new Varien_Http_Adapter_Curl();
327: $verifyPeerValue = Mage::getStoreConfig('xmlconnect/mobile_application/curl_ssl_verifypeer');
328: $curl->setConfig(array(
329: 'timeout' => Mage_XmlConnect_Helper_Data::CURLOPT_DEFAULT_TIMEOUT,
330: 'verifypeer' => $verifyPeerValue, 'verifyhost' => 2, 'header' => false
331: ));
332:
333: $mCommerceUrl = $appConnectorUrl . $params['key'];
334: $curl->write(Zend_Http_Client::POST, $mCommerceUrl, CURL_HTTP_VERSION_1_1, array(), $params);
335:
336: $result = $curl->read();
337: if(false === $result) {
338: Mage::log('Curl error: ' . $curl->getError());
339: $curl->close();
340: Mage::throwException($this->__('Request internal error.'));
341: }
342: $curl->close();
343:
344:
345: $resultArray = json_decode($result, true);
346:
347: $app->setResult($result);
348: $success = isset($resultArray['success']) && $resultArray['success'] === true;
349:
350: $app->setSuccess($success);
351: if (!$app->getSuccess()) {
352: $message = isset($resultArray['message']) ? $resultArray['message']: '';
353: if (is_array($message)) {
354: $message = implode(' ,', $message);
355: }
356: Mage::throwException($this->__('Submit App failure. %s', $message));
357: }
358: } catch (Exception $e) {
359: throw $e;
360: }
361: }
362:
363: 364: 365: 366: 367:
368: public function saveAction()
369: {
370: $data = $this->getRequest()->getPost();
371: $redirectSubmit = $this->getRequest()->getParam('submitapp', false);
372: $app = false;
373: $isError = false;
374: if ($data) {
375: Mage::getSingleton('adminhtml/session')->setFormData($data);
376: try {
377: $id = (int) $this->getRequest()->getParam('application_id');
378:
379: if (!$id && isset($data['devtype'])) {
380: $devArray = Mage::helper('xmlconnect')->getSupportedDevices();
381: if (!array_key_exists($data['devtype'], $devArray)) {
382: $this->_getSession()->addError($this->__('Wrong device type.'));
383: $isError = true;
384: }
385: }
386:
387: $app = $this->_initApp('application_id', $data['devtype']);
388: if (!$app->getId() && $id) {
389: $this->_getSession()->addError($this->__('App does not exist.'));
390: $this->_redirect('*/*/');
391: return;
392: }
393: $app->addData($this->_preparePostData($data));
394: $app->addData($this->_processUploadedFiles($app->getData()));
395: $errors = $app->validate();
396:
397: if ($errors !== true) {
398: foreach ($errors as $err) {
399: $this->_getSession()->addError($err);
400: }
401: $isError = true;
402: }
403:
404: if (!$isError) {
405: $this->_saveThemeAction($data, 'current_theme');
406: $app->save();
407: $this->_getSession()->addSuccess($this->__('App has been saved.'));
408: $this->_clearSessionData();
409: }
410: } catch (Mage_Core_Exception $e) {
411: $this->_getSession()->addException($e, $e->getMessage());
412: $isError = true;
413: } catch (Exception $e) {
414: $this->_getSession()->addException($e, $this->__('Unable to save app.'));
415: $isError = true;
416: Mage::logException($e);
417: }
418: }
419: $isApplication = $app instanceof Mage_XmlConnect_Model_Application;
420: if (!$isError && $isApplication && $app->getId() && $redirectSubmit) {
421: $this->_redirect('*/*/submission', array('application_id' => $app->getId()));
422: } else if ($isError && $isApplication && $app->getId()) {
423: Mage::getSingleton('adminhtml/session')->setLoadSessionFlag(true);
424: $this->_redirect('*/*/edit', array('application_id' => $app->getId()));
425: } else if ($isError && $isApplication && !$app->getId() && $app->getType()) {
426: $this->_redirect('*/*/edit', array('type' => $app->getType()));
427: } else if ($this->getRequest()->getParam('back') && $isApplication) {
428: $this->_redirect('*/*/edit', array('application_id' => $app->getId()));
429: } else {
430: $this->_redirect('*/*/');
431: }
432: }
433:
434: 435: 436: 437: 438: 439:
440: protected function _saveThemeAction($data, $paramId = 'saveTheme')
441: {
442: $themeName = trim($this->getRequest()->getParam($paramId, false));
443: if ($themeName) {
444:
445: $themesHelper = Mage::helper('xmlconnect/theme');
446: try {
447: if (array_key_exists($themeName, $themesHelper->getAllThemes())) {
448:
449: $theme = $themesHelper->getThemeByName($themeName);
450: if ($theme instanceof Mage_XmlConnect_Model_Theme) {
451: if ($paramId == 'saveTheme') {
452: $convertedConf = $this->_convertPost($data);
453: } else {
454: if (isset($data['conf'])) {
455: $convertedConf = $data['conf'];
456: } else {
457: $response = array(
458: 'error' => true,
459: 'message' => $this->__('Cannot save theme "%s". Incorrect data received', $themeName)
460: );
461: }
462: }
463: if (!isset($response)) {
464: $theme->importAndSaveData($convertedConf);
465: $response = array(
466: 'message' => $this->__('Changes have been saved to theme.'),
467: 'themes' => $themesHelper->getAllThemesArray(true),
468: 'themeSelector' => $themesHelper->getThemesSelector($themeName),
469: 'selectedTheme' => $themeName
470: );
471: }
472: } else {
473: $response = array('error' => true, 'message' => $this->__('Cannot load theme "%s".', $themeName));
474: }
475: } else {
476: $convertedConf = $this->_convertPost($data);
477: $newTheme = $themesHelper->createNewTheme($themeName, $convertedConf);
478: $response = array(
479: 'message' => $this->__('Theme has been created.'),
480: 'themes' => $themesHelper->getAllThemesArray(true),
481: 'themeSelector' => $themesHelper->getThemesSelector($newTheme->getName()),
482: 'selectedTheme' => $newTheme->getName()
483: );
484: }
485: } catch (Mage_Core_Exception $e) {
486: $response = array('error' => true, 'message' => $e->getMessage());
487: } catch (Exception $e) {
488: $response = array('error' => true, 'message' => $this->__('Can\'t save theme.'));
489: }
490: } else {
491: $response = array('error' => true, 'message' => $this->__('Theme name is not set.'));
492: }
493: if (is_array($response)) {
494: $response = Mage::helper('core')->jsonEncode($response);
495: $this->getResponse()->setBody($response);
496: }
497: }
498:
499: 500: 501: 502: 503: 504: 505: 506:
507: protected function _convertPost($data)
508: {
509: $conf = array();
510: foreach ($data as $key => $val) {
511: $parts = explode('_', $key);
512:
513: if (is_array($parts) && count($parts) == 4) {
514: list(, $key1, $key2, $key3) = $parts;
515: if (!isset($conf[$key1])) {
516: $conf[$key1] = array();
517: }
518: if (!isset($conf[$key1][$key2])) {
519: $conf[$key1][$key2] = array();
520: }
521: $conf[$key1][$key2][$key3] = $val;
522: }
523: }
524: return $conf;
525: }
526:
527: 528: 529: 530: 531:
532: public function deleteThemeAction()
533: {
534: $themeId = $this->getRequest()->getParam('theme_id', false);
535: if ($themeId) {
536: try {
537:
538: $themesHelper = Mage::helper('xmlconnect/theme');
539: $result = $themesHelper->deleteTheme($themeId);
540: if ($result) {
541: $response = array(
542: 'message' => $this->__('Theme has been delete.'),
543: 'themes' => $themesHelper->getAllThemesArray(true),
544: 'themeSelector' => $themesHelper->getThemesSelector(),
545: 'selectedTheme' => $themesHelper->getDefaultThemeName()
546: );
547: } else {
548: $response = array(
549: 'error' => true,
550: 'message' => $this->__('Can\'t delete "%s" theme.', $themeId)
551: );
552: }
553: } catch (Mage_Core_Exception $e) {
554: $response = array('error' => true, 'message' => $e->getMessage());
555: } catch (Exception $e) {
556: $response = array(
557: 'error' => true,
558: 'message' => $this->__('Can\'t delete "%s" theme.', $themeId)
559: );
560: }
561: } else {
562: $response = array('error' => true, 'message' => $this->__('Theme name is not set.'));
563: }
564: if (is_array($response)) {
565: $response = Mage::helper('core')->jsonEncode($response);
566: $this->getResponse()->setBody($response);
567: }
568: }
569:
570: 571: 572: 573: 574:
575: public function saveThemeAction()
576: {
577: $data = $this->getRequest()->getPost();
578: $this->_saveThemeAction($data);
579: }
580:
581: 582: 583: 584: 585:
586: public function resetThemeAction()
587: {
588: try {
589: $theme = $this->getRequest()->getPost('theme', null);
590: Mage::helper('xmlconnect/theme')->resetTheme($theme);
591: $response = Mage::helper('xmlconnect/theme')->getAllThemesArray(true);
592: } catch (Mage_Core_Exception $e) {
593: $response = array('error' => true, 'message' => $e->getMessage());
594: } catch (Exception $e) {
595: $response = array('error' => true, 'message' => $this->__('Can\'t reset theme.'));
596: }
597: if (is_array($response)) {
598: $response = Mage::helper('core')->jsonEncode($response);
599: $this->getResponse()->setBody($response);
600: }
601: }
602:
603: 604: 605: 606: 607:
608: public function previewHomeAction()
609: {
610: $this->_previewAction('preview_home_content');
611: }
612:
613: 614: 615:
616: public function previewHomeHorAction()
617: {
618: $this->_previewAction('preview_home_hor_content');
619: }
620:
621: 622: 623: 624: 625:
626: public function previewCatalogAction()
627: {
628: $this->_previewAction('preview_catalog_content');
629: }
630:
631: 632: 633:
634: public function previewCatalogHorAction()
635: {
636: $this->_previewAction('preview_catalog_hor_content');
637: }
638:
639: 640: 641:
642: public function previewProductinfoAction()
643: {
644: $this->_previewAction('preview_productinfo_content');
645: }
646:
647: 648: 649:
650: public function previewQueueAction()
651: {
652: $message = $this->_initMessage();
653: if ($message->getId()) {
654: $this->getRequest()->setParam('queue_preview', $message->getId());
655: }
656: $this->_forward('previewTemplate');
657: }
658:
659: 660: 661:
662: public function previewTemplateAction()
663: {
664: $this->loadLayout('adminhtml_mobile_template_preview');
665: $this->renderLayout();
666: }
667:
668: 669: 670: 671: 672:
673: protected function _previewAction($block)
674: {
675: $redirectBack = false;
676: try {
677: $deviceType = $this->getRequest()->getParam('devtype');
678: $app = $this->_initApp('application_id', $deviceType);
679: if (!$this->getRequest()->getParam('submission_action')) {
680: $app->addData($this->_preparePostData($this->getRequest()->getPost()));
681: }
682:
683: $appConf = $app->getRenderConf();
684: try {
685:
686: $dataUploaded = $this->_processUploadedFiles($app->getData());
687: $app->addData($dataUploaded);
688:
689: $appConf = $app->getRenderConf();
690: } catch (Exception $e) {
691:
692: $jsErrorMessage = addslashes($e->getMessage());
693: }
694:
695: $this->loadLayout(false);
696: $preview = $this->getLayout()->getBlock($block);
697:
698: if (isset($jsErrorMessage)) {
699: $preview->setJsErrorMessage($jsErrorMessage);
700: }
701: $preview->setConf($appConf);
702: Mage::helper('xmlconnect')->getPreviewModel()->setConf($appConf);
703: $this->renderLayout();
704: return;
705: } catch (Mage_Core_Exception $e) {
706: $this->_getSession()->addException($e, $e->getMessage());
707: $redirectBack = true;
708: } catch (Exception $e) {
709: $this->_getSession()->addException($e, $this->__('Unable to process preview.'));
710: $redirectBack = true;
711: }
712: if (isset($app) && $app instanceof Mage_XmlConnect_Model_Application && $redirectBack) {
713: $this->_redirect('*/*/edit', array('application_id' => $app->getId()));
714: } else {
715: $this->_redirect('*/*/');
716: }
717: }
718:
719: 720: 721: 722: 723:
724: public function deleteAction()
725: {
726: try {
727: $app = $this->_initApp();
728: if (!$app->getIsSubmitted()) {
729: $app->delete();
730: $this->_getSession()->addSuccess($this->__('App has been deleted.'));
731: } else {
732: Mage::throwException($this->__('It\'s not allowed to delete submitted application.'));
733: }
734: } catch (Mage_Core_Exception $e) {
735: $this->_getSession()->addException($e, $e->getMessage());
736: } catch (Exception $e) {
737: $this->_getSession()->addException($e, $this->__('Unable to find an app to delete.'));
738: }
739: $this->_redirect('*/*/');
740: }
741:
742: 743: 744:
745: public function deleteTemplateAction()
746: {
747:
748: if ($id = $this->getRequest()->getParam('id')) {
749: try {
750:
751: Mage::getModel('xmlconnect/template')->load($id)->delete();
752:
753:
754: Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Template has been deleted.'));
755:
756:
757: $this->_redirect('*/*/template');
758: return;
759:
760: } catch (Exception $e) {
761:
762: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
763:
764: $this->_redirect('*/*/template', array('id' => $id));
765: return;
766: }
767: }
768:
769:
770: Mage::getSingleton('adminhtml/session')->addError($this->__('Unable to find template to delete.'));
771: }
772:
773: 774: 775: 776: 777:
778: protected function _isAllowed()
779: {
780: return Mage::getSingleton('admin/session')->isAllowed('xmlconnect');
781: }
782:
783: 784: 785:
786: public function historyAction()
787: {
788: $this->loadLayout();
789: $this->_setActiveMenu('xmlconnect/history');
790: $this->renderLayout();
791: }
792:
793: 794: 795: 796: 797:
798: public function gridAction()
799: {
800: $this->loadLayout(false);
801: $this->_setActiveMenu('xmlconnect/mobile');
802: $this->renderLayout();
803: }
804:
805: 806: 807: 808: 809: 810: 811: 812:
813: protected function _processUploadedFiles($data, $restore = false)
814: {
815: if ($restore === true) {
816: $this->_uploadedFiles = Mage::getSingleton('adminhtml/session')->getUploadedFilesFormDataSubmit();
817: }
818: if (!isset($this->_uploadedFiles) || !is_array($this->_uploadedFiles)) {
819: $this->_uploadedFiles = array();
820: }
821:
822: if (!empty($_FILES)) {
823: foreach ($_FILES as $field => $file) {
824: if (!empty($file['name']) && is_scalar($file['name'])) {
825: $uploadedFileName = Mage::helper('xmlconnect/image')->handleUpload($field);
826: if (!empty($uploadedFileName)) {
827: $this->_uploadedFiles[$field] = $uploadedFileName;
828: }
829: }
830: }
831: }
832:
833: foreach ($this->_uploadedFiles as $fieldPath => $fileName) {
834: Mage::helper('xmlconnect')->_injectFieldToArray($data, $fieldPath, $fileName);
835: }
836: Mage::getSingleton('adminhtml/session')->setUploadedFilesFormData($this->_uploadedFiles);
837: if ($restore === true) {
838: Mage::getSingleton('adminhtml/session')->setUploadedFilesFormDataSubmit($this->_uploadedFiles);
839: }
840: return $data;
841: }
842:
843: 844: 845: 846: 847: 848: 849:
850: public function _preparePostData(array $arr)
851: {
852: unset($arr['code']);
853: if (!empty($arr['conf']['native']['pages'])) {
854: 855: 856:
857: $pages = array();
858: foreach ($arr['conf']['native']['pages'] as $_page) {
859: if (!empty($_page['id']) && !empty($_page['label'])) {
860: $pages[] = $_page;
861: }
862: }
863: $arr['conf']['native']['pages'] = $pages;
864: }
865: if (isset($arr['conf']['new_pages']['ids']) && isset($arr['conf']['new_pages']['labels'])) {
866: $newPages = array();
867: $cmsPages = &$arr['conf']['new_pages'];
868: $idx = 0;
869: foreach ($cmsPages['ids'] as $key => $value) {
870: if (!empty($value) && !empty($cmsPages['labels'][$key])) {
871: $newPages[$idx]['id'] = trim($value);
872: $newPages[$idx++]['label'] = trim($cmsPages['labels'][$key]);
873: }
874: }
875: if (!isset($arr['conf']['native']['pages'])) {
876: $arr['conf']['native']['pages'] = array();
877: }
878: if (!empty($newPages)) {
879: $arr['conf']['native']['pages'] = array_merge($arr['conf']['native']['pages'], $newPages);
880: }
881: unset($arr['conf']['new_pages']);
882: }
883:
884: 885: 886:
887: $lifetime = &$arr['conf']['native']['cacheLifetime'];
888: $lifetime = $lifetime <= 0 ? '' : (int)$lifetime;
889:
890: 891: 892:
893: if (isset($arr['current_theme'])) {
894: $arr['conf']['extra']['theme'] = $arr['current_theme'];
895: }
896: if (!isset($arr['conf']['defaultCheckout'])) {
897: $arr['conf']['defaultCheckout'] = array();
898: }
899: if (!isset($arr['conf']['defaultCheckout']['isActive'])) {
900: $arr['conf']['defaultCheckout']['isActive'] = 0;
901: }
902:
903: if (!isset($arr['conf']['paypal'])) {
904: $arr['conf']['paypal'] = array();
905: }
906: if (!isset($arr['conf']['paypal']['isActive'])) {
907: $arr['conf']['paypal']['isActive'] = 0;
908: }
909: return $arr;
910: }
911:
912: 913: 914:
915: public function submissionHistoryGridAction()
916: {
917: $this->_initApp();
918: $this->loadLayout();
919: $this->renderLayout();
920: }
921:
922: 923: 924: 925: 926: 927:
928: protected function _initMessage($paramName = 'id')
929: {
930: $id = (int) $this->getRequest()->getParam($paramName);
931: $message = Mage::getModel('xmlconnect/queue')->load($id);
932: Mage::unregister('current_message');
933: Mage::register('current_message', $message);
934: return $message;
935: }
936:
937: 938: 939: 940: 941: 942:
943: protected function _initTemplate($paramName = 'id')
944: {
945: $id = (int) $this->getRequest()->getParam($paramName);
946: $template = Mage::getModel('xmlconnect/template')->load($id);
947: Mage::unregister('current_template');
948: Mage::register('current_template', $template);
949: return $template;
950: }
951:
952: 953: 954: 955: 956:
957: public function queueAction()
958: {
959: $this->loadLayout();
960: $this->_setActiveMenu('xmlconnect/queue');
961: $this->renderLayout();
962: }
963:
964: 965: 966: 967: 968:
969: public function editQueueAction()
970: {
971: $message = $this->_initMessage();
972: if ($message->getId()) {
973: $this->getRequest()->setParam('template_id', $message->getTemplateId());
974: $this->_initTemplate('template_id');
975: }
976: $this->_forward('queueMessage');
977: }
978:
979: 980: 981: 982: 983: 984:
985: protected function _filterPostData($data)
986: {
987: $data = $this->_filterDateTime($data, array('exec_time'));
988: return $data;
989: }
990:
991: 992: 993: 994: 995:
996: public function cancelQueueAction()
997: {
998: try {
999: $id = $this->getRequest()->getParam('id');
1000: $message = $this->_initMessage();
1001: if (!$message->getId() && $id) {
1002: $this->_getSession()->addError($this->__('Queue does not exist.'));
1003: $this->_redirect('*/*/');
1004: return;
1005: }
1006: $message->setStatus(Mage_XmlConnect_Model_Queue::STATUS_CANCELED);
1007: $message->save();
1008: } catch (Mage_Core_Exception $e) {
1009: $this->_getSession()->addException($e, $e->getMessage());
1010: } catch (Exception $e) {
1011: $this->_getSession()->addException($e, $this->__('Unable to cancel queue.'));
1012: Mage::logException($e);
1013: }
1014:
1015: $this->_redirect('*/*/queue');
1016: }
1017:
1018: 1019: 1020: 1021: 1022:
1023: public function deleteQueueAction()
1024: {
1025: try {
1026: $id = $this->getRequest()->getParam('id');
1027: $message = $this->_initMessage();
1028: if (!$message->getId() && $id) {
1029: $this->_getSession()->addError($this->__('Queue does not exist.'));
1030: $this->_redirect('*/*/');
1031: return;
1032: }
1033: $message->setStatus(Mage_XmlConnect_Model_Queue::STATUS_DELETED);
1034: $message->save();
1035: } catch (Mage_Core_Exception $e) {
1036: $this->_getSession()->addException($e, $e->getMessage());
1037: } catch (Exception $e) {
1038: $this->_getSession()->addException($e, $this->__('Unable to delete queue.'));
1039: Mage::logException($e);
1040: }
1041:
1042: $this->_redirect('*/*/queue');
1043: }
1044:
1045: 1046: 1047: 1048: 1049:
1050: public function massCancelQueueAction()
1051: {
1052: $queueIds = $this->getRequest()->getParam('queue');
1053: if(!is_array($queueIds)) {
1054: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select message(s).'));
1055: } else {
1056: try {
1057: $queue = Mage::getModel('xmlconnect/queue');
1058: foreach ($queueIds as $queueId) {
1059: $queue->reset()->load((int)$queueId)->setStatus(Mage_XmlConnect_Model_Queue::STATUS_CANCELED)
1060: ->save();
1061: }
1062: Mage::getSingleton('adminhtml/session')->addSuccess(
1063: Mage::helper('adminhtml')->__('Total of %d record(s) were canceled.', count($queueIds))
1064: );
1065: } catch (Exception $e) {
1066: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
1067: }
1068: }
1069:
1070: $this->_redirect('*/*/queue');
1071: }
1072:
1073: 1074: 1075: 1076: 1077:
1078: public function massDeleteQueueAction()
1079: {
1080: $queueIds = $this->getRequest()->getParam('queue');
1081: if(!is_array($queueIds)) {
1082: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select message(s).'));
1083: } else {
1084: try {
1085: $queue = Mage::getModel('xmlconnect/queue');
1086: foreach ($queueIds as $queueId) {
1087: $queue->reset()->load($queueId)->setStatus(Mage_XmlConnect_Model_Queue::STATUS_DELETED)->save();
1088: }
1089: Mage::getSingleton('adminhtml/session')->addSuccess(
1090: Mage::helper('adminhtml')->__('Total of %d record(s) were deleted.', count($queueIds))
1091: );
1092: } catch (Exception $e) {
1093: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
1094: }
1095: }
1096:
1097: $this->_redirect('*/*/queue');
1098: }
1099:
1100: 1101: 1102: 1103: 1104:
1105: public function saveMessageAction()
1106: {
1107: $data = $this->_filterPostData($this->getRequest()->getPost());
1108: $isError = false;
1109: $message = false;
1110:
1111: if ($data) {
1112: try {
1113: $data = Mage::getModel('core/input_filter_maliciousCode')->filter($data);
1114: $template = $this->_initTemplate('template_id');
1115: $message = $this->_initMessage();
1116:
1117: if (!$template->getId() && !$message->getTemplateId()) {
1118: $this->_getSession()->addError($this->__('Template for new AirMail Message does not exist.'));
1119: $this->_redirect('*/*/queue');
1120: return;
1121: }
1122:
1123:
1124: $app = Mage::getModel('xmlconnect/application')->loadByCode($template->getAppCode());
1125: $deviceType = Mage::helper('xmlconnect')->getDeviceType($app);
1126:
1127: if ($deviceType == Mage_XmlConnect_Helper_Data::DEVICE_TYPE_ANDROID
1128: && $data['type'] == Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL
1129: ) {
1130: $this->_getSession()->addError($this->__('Android doesn\'t support AirMail message type.'));
1131: $redirectParams = $this->_getQueueMessageParams($message);
1132: $action = $message->getId() ? 'editQueue' : 'queueMessage';
1133: $this->_redirect('*/*/' . $action, $redirectParams);
1134: return;
1135: }
1136:
1137: $temporaryObject = new Varien_Object();
1138: $temporaryObject->setData($data);
1139:
1140: if ($temporaryObject->getTemplateId()) {
1141: $message->setTemplateId($temporaryObject->getTemplateId());
1142: } else {
1143: $message->setTemplateId($template->getId());
1144: }
1145:
1146: if (!$message->getId()) {
1147:
1148: $message->setStatus(Mage_XmlConnect_Model_Queue::STATUS_IN_QUEUE);
1149: } elseif ($message->getStatus() != Mage_XmlConnect_Model_Queue::STATUS_IN_QUEUE) {
1150: $this->_getSession()->addError(
1151: $this->__('Message can be edited when status of the message is "In Queue" only.')
1152: );
1153: $this->_redirect('*/*/queue');
1154: return;
1155: }
1156:
1157: switch ($temporaryObject->getType()) {
1158: case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL:
1159: $message->setData('type', Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL);
1160: break;
1161:
1162: case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH:
1163: default:
1164: $message->setData('type', Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH);
1165: break;
1166: }
1167: if ($temporaryObject->getExecTime()) {
1168: $execTime = Mage::getSingleton('core/date')->gmtDate(null, $temporaryObject->getExecTime());
1169: $message->setExecTime($execTime ? $execTime : Mage::getSingleton('core/date')->gmtDate());
1170: } else {
1171: $message->setExecTime(new Zend_Db_Expr('NULL'));
1172: }
1173: if ($template->getId()) {
1174: $message->setAppCode($template->getAppCode());
1175: }
1176: $message->setPushTitle($temporaryObject->getPushTitle());
1177: $message->setMessageTitle($temporaryObject->getMessageTitle());
1178: $message->setContent($temporaryObject->getContent());
1179: $message->save();
1180: } catch (Mage_Core_Exception $e) {
1181: $this->_getSession()->addException($e, $e->getMessage());
1182: $isError = true;
1183: } catch (Exception $e) {
1184: $this->_getSession()->addException($e, $this->__('Unable to save message.'));
1185: $isError = true;
1186: Mage::logException($e);
1187: }
1188: }
1189:
1190: if ($isError) {
1191: if ($isError) {
1192: Mage::getSingleton('adminhtml/session')->setLoadSessionFlag(true);
1193: }
1194: $redirectParams = $this->_getQueueMessageParams($message);
1195: $this->_redirect('*/*/queueMessage', $redirectParams);
1196: } else {
1197: $this->_redirect('*/*/queue');
1198: }
1199: }
1200:
1201: 1202: 1203: 1204: 1205: 1206:
1207: protected function _getQueueMessageParams(Mage_XmlConnect_Model_Queue $message)
1208: {
1209: $redirectParams = array();
1210: if ($message && $message->getId()) {
1211: $redirectParams['id'] = $message->getId();
1212: } else {
1213: $redirectParams['template_id'] = (int) $this->getRequest()->getParam('template_id');
1214: }
1215: return $redirectParams;
1216: }
1217:
1218: 1219: 1220: 1221: 1222:
1223: public function templateAction()
1224: {
1225: $this->loadLayout();
1226: $this->_setActiveMenu('xmlconnect/template');
1227: $this->renderLayout();
1228: }
1229:
1230: 1231: 1232: 1233: 1234:
1235: public function newTemplateAction()
1236: {
1237: $this->_forward('editTemplate');
1238: }
1239:
1240: 1241: 1242: 1243: 1244:
1245: public function editTemplateAction()
1246: {
1247: $template = $this->_initTemplate();
1248:
1249: $applicationsFound = Mage::helper('xmlconnect')->getApplicationOptions();
1250: if (!$template->getId() && empty($applicationsFound)) {
1251: $this->_getSession()->addError($this->__('At last one application has to be created.'));
1252: $this->_redirect('*/*/template');
1253: return;
1254: }
1255:
1256: $this->loadLayout();
1257: $this->_setActiveMenu('xmlconnect/templates');
1258: $this->renderLayout();
1259: }
1260:
1261: 1262: 1263: 1264: 1265:
1266: public function saveTemplateAction()
1267: {
1268: $data = $this->getRequest()->getPost();
1269: $template = false;
1270: $isError = false;
1271: if ($data) {
1272: $data = Mage::getModel('core/input_filter_maliciousCode')->filter($data);
1273: Mage::getSingleton('adminhtml/session')->setTemplateFormData($data);
1274: try {
1275: $id = $this->getRequest()->getParam('id');
1276: $template = $this->_initTemplate();
1277: if (!$template->getId() && $id) {
1278: $this->_getSession()->addError($this->__('Template does not exist.'));
1279: $this->_redirect('*/*/');
1280: return;
1281: }
1282: $template->addData($data);
1283: $template->save();
1284: } catch (Mage_Core_Exception $e) {
1285: $this->_getSession()->addException($e, $e->getMessage());
1286: $isError = true;
1287: } catch (Exception $e) {
1288: $this->_getSession()->addException($e, $this->__('Unable to save template.'));
1289: $isError = true;
1290: Mage::logException($e);
1291: }
1292: }
1293:
1294: if ($isError && ($template && $template->getId())) {
1295: Mage::getSingleton('adminhtml/session')->setLoadSessionFlag(true);
1296: $this->_redirect('*/*/editTemplate', array('id' => $template->getId()));
1297: } else {
1298: $this->_redirect('*/*/template');
1299: }
1300: }
1301:
1302: 1303: 1304: 1305: 1306:
1307: public function queueMessageAction()
1308: {
1309: $message = $this->_initMessage();
1310: if (!$message->getId()) {
1311: $template = $this->_initTemplate('template_id');
1312: if (!$template->getId()) {
1313: $this->_getSession()->addError($this->__('Template for new AirMail Message does not exist.'));
1314: $this->_redirect('*/*/template');
1315: }
1316: }
1317:
1318: if (isset($template)) {
1319: $applicationId = $template->getApplicationId();
1320: } else {
1321: $applicationId = Mage::getModel('xmlconnect/template')->load($message->getTemplateId())->getApplicationId();
1322: }
1323:
1324:
1325: $app = Mage::getModel('xmlconnect/application')->load($applicationId);
1326:
1327: if(!$app->isNotificationsActive()) {
1328: $this->_getSession()->addError(
1329: $this->__('Queue is allowed only for applications with enabled Push Notification.')
1330: );
1331: $action = $message->getId() ? 'queue' : 'template';
1332: $this->_redirect('*/*/' . $action);
1333: return;
1334: }
1335:
1336: $this->loadLayout();
1337: if ($message->getId()) {
1338: $title = $this->__('Edit AirMail Message');
1339: } else {
1340: $title = $this->__('New AirMail Message');
1341: }
1342: $this->_addBreadcrumb($this->__('AirMail Message Queue'), $this->__('AirMail Message Queue'),
1343: $this->getUrl('*/*/queue')
1344: );
1345: $this->_addBreadcrumb($title, $title);
1346:
1347: $this->_setActiveMenu('xmlconnect/queue');
1348: $this->renderLayout();
1349: }
1350:
1351: 1352: 1353: 1354: 1355:
1356: public function editMessageAction()
1357: {
1358: $this->_forward('queueMessage');
1359: }
1360: }
1361: