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: abstract class Mage_XmlConnect_Controller_Action extends Mage_Core_Controller_Front_Action
35: {
36: 37: 38:
39: const MESSAGE_STATUS_ERROR = 'error';
40:
41: 42: 43:
44: const MESSAGE_STATUS_WARNING = 'warning';
45:
46: 47: 48:
49: const MESSAGE_STATUS_SUCCESS = 'success';
50:
51: 52: 53:
54: const MESSAGE_TYPE_ALERT = 'alert';
55:
56: 57: 58:
59: const MESSAGE_TYPE_PROMPT = 'prompt';
60:
61: 62: 63: 64: 65: 66:
67: public function preDispatch()
68: {
69: parent::preDispatch();
70:
71: $this->getResponse()->setHeader('Content-type', 'text/xml; charset=UTF-8');
72: 73: 74:
75: $cookieName = Mage_XmlConnect_Model_Application::APP_CODE_COOKIE_NAME;
76: $appCode = isset($_COOKIE[$cookieName]) ? (string) $_COOKIE[$cookieName] : '';
77: $screenSizeCookieName = Mage_XmlConnect_Model_Application::APP_SCREEN_SIZE_NAME;
78: $screenSize = isset($_COOKIE[$screenSizeCookieName]) ? (string) $_COOKIE[$screenSizeCookieName] : '';
79: if (!$appCode) {
80: $this->_message(
81: Mage::helper('xmlconnect')->__('Specified invalid app code.'), self::MESSAGE_STATUS_ERROR
82: );
83: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
84: return;
85: }
86: 87: 88:
89: if ((int)Mage::getStoreConfig('general/restriction/is_active')
90: && (int)Mage::getStoreConfig('general/restriction/mode') == 0
91: ) {
92: $this->_message(
93: Mage::helper('xmlconnect')->__('Website is offline.'), self::MESSAGE_STATUS_SUCCESS
94: );
95: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
96: return;
97: }
98:
99: $appModel = Mage::getModel('xmlconnect/application')->loadByCode($appCode);
100: $appModel->setScreenSize($screenSize);
101: if ($appModel && $appModel->getId()) {
102: Mage::app()->setCurrentStore(
103: Mage::app()->getStore($appModel->getStoreId())->getCode()
104: );
105: Mage::getSingleton('core/locale')->emulate($appModel->getStoreId());
106: Mage::register('current_app', $appModel);
107: } else {
108: $this->_message(
109: Mage::helper('xmlconnect')->__('Specified invalid app code.'), self::MESSAGE_STATUS_ERROR
110: );
111: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
112: return;
113: }
114: }
115:
116: 117: 118: 119: 120:
121: public function postDispatch()
122: {
123: parent::postDispatch();
124: $body = $this->getResponse()->getBody();
125: if (empty($body)) {
126: $this->_message(
127: Mage::helper('xmlconnect')->__('An error occurred while processing your request.'),
128: self::MESSAGE_STATUS_ERROR
129: );
130: }
131: }
132:
133: 134: 135: 136: 137: 138: 139: 140:
141: protected function _message($text, $status, $children = array())
142: {
143:
144: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
145: $message->addCustomChild('status', $status);
146: $message->addCustomChild('text', $text);
147:
148: foreach ($children as $node => $value) {
149: $message->addCustomChild($node, $value);
150: }
151:
152: $this->getResponse()->setBody($message->asNiceXml());
153: }
154: }
155: