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_ConfigurationController extends Mage_Core_Controller_Front_Action
35: {
36: 37: 38: 39: 40:
41: public function preDispatch()
42: {
43: parent::preDispatch();
44: $this->getResponse()->setHeader('Content-type', 'text/xml; charset=UTF-8');
45: }
46:
47: 48: 49: 50: 51: 52:
53: protected function _initApp()
54: {
55: $cookieName = Mage_XmlConnect_Model_Application::APP_CODE_COOKIE_NAME;
56: $code = $this->getRequest()->getParam($cookieName);
57: $screenSize = (string) $this->getRequest()->getParam(
58: Mage_XmlConnect_Model_Application::APP_SCREEN_SIZE_NAME
59: );
60:
61: $app = Mage::getModel('xmlconnect/application');
62: if ($app) {
63: $app->loadByCode($code);
64: Mage::app()->setCurrentStore(
65: Mage::app()->getStore($app->getStoreId())->getCode()
66: );
67: Mage::getSingleton('core/locale')->emulate($app->getStoreId());
68: $app->setScreenSize($screenSize);
69:
70: if (!$app->getId()) {
71: Mage::throwException($this->__('App with specified code does not exist.'));
72: }
73:
74: $app->loadConfiguration();
75: } else {
76: Mage::throwException($this->__('App code required.'));
77: }
78: Mage::register('current_app', $app);
79: return $app;
80: }
81:
82: 83: 84: 85: 86: 87: 88: 89:
90: protected function _initCookies(Mage_XmlConnect_Model_Application $app)
91: {
92: $cookieToSetArray = array(
93: array(
94: 'cookieName' => Mage_XmlConnect_Model_Application::APP_CODE_COOKIE_NAME,
95: 'paramName' => Mage_XmlConnect_Model_Application::APP_CODE_COOKIE_NAME,
96: 'value' => $app->getCode()
97: ),
98: array(
99: 'cookieName' => Mage_XmlConnect_Model_Application::APP_SCREEN_SIZE_NAME,
100: 'paramName' => Mage_XmlConnect_Model_Application::APP_SCREEN_SIZE_NAME,
101: 'value' => $app->getScreenSize()
102: ));
103:
104: foreach ($cookieToSetArray as $item) {
105: if (!isset($_COOKIE[$item['cookieName']])
106: || $_COOKIE[$item['cookieName']] != $this->getRequest()->getParam($item['paramName'])
107: ) {
108: 109: 110:
111: $cookieExpireOffset = 3600 * 24 * 30;
112: Mage::getSingleton('core/cookie')->set(
113: $item['cookieName'], $item['value'], $cookieExpireOffset, '/', null, null, true
114: );
115: }
116: }
117: }
118:
119: 120: 121: 122: 123:
124: public function indexAction()
125: {
126: try {
127:
128: $app = $this->_initApp();
129: $this->_initCookies($app);
130:
131: if ($this->getRequest()->getParam('updated_at')) {
132: $updatedAt = strtotime($app->getUpdatedAt());
133: $loadedAt = (int) $this->getRequest()->getParam('updated_at');
134: if ($loadedAt >= $updatedAt) {
135: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
136: $message->addChild('status', Mage_XmlConnect_Controller_Action::MESSAGE_STATUS_SUCCESS);
137: $message->addChild('no_changes', '1');
138: $this->getResponse()->setBody($message->asNiceXml());
139: return;
140: }
141: }
142: $this->loadLayout(false);
143: $this->renderLayout();
144: } catch (Mage_Core_Exception $e) {
145: $this->_message($e->getMessage(), Mage_XmlConnect_Controller_Action::MESSAGE_STATUS_ERROR);
146: } catch (Exception $e) {
147: $this->_message(
148: $this->__('Can\'t show configuration.'), Mage_XmlConnect_Controller_Action::MESSAGE_STATUS_ERROR
149: );
150: Mage::logException($e);
151: }
152: }
153:
154: 155: 156: 157: 158: 159: 160:
161: protected function _message($text, $status)
162: {
163:
164: $message = Mage::getModel('xmlconnect/simplexml_element', '<message></message>');
165: $message->addCustomChild('status', $status);
166: $message->addCustomChild('text', $text);
167: $this->getResponse()->setBody($message->asNiceXml());
168: }
169: }
170: