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_System_Config_System_StorageController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected function _getSyncSingleton()
42: {
43: return Mage::getSingleton('core/file_storage');
44: }
45:
46: 47: 48: 49: 50:
51: protected function _getSyncFlag()
52: {
53: return $this->_getSyncSingleton()->getSyncFlag();
54: }
55:
56: 57: 58: 59: 60:
61: public function synchronizeAction()
62: {
63: session_write_close();
64:
65: if (!isset($_REQUEST['storage'])) {
66: return;
67: }
68:
69: $flag = $this->_getSyncFlag();
70: if ($flag && $flag->getState() == Mage_Core_Model_File_Storage_Flag::STATE_RUNNING
71: && $flag->getLastUpdate()
72: && time() <= (strtotime($flag->getLastUpdate()) + Mage_Core_Model_File_Storage_Flag::FLAG_TTL)
73: ) {
74: return;
75: }
76:
77: $flag->setState(Mage_Core_Model_File_Storage_Flag::STATE_RUNNING)->save();
78: Mage::getSingleton('admin/session')->setSyncProcessStopWatch(false);
79:
80: $storage = array('type' => (int) $_REQUEST['storage']);
81: if (isset($_REQUEST['connection']) && !empty($_REQUEST['connection'])) {
82: $storage['connection'] = $_REQUEST['connection'];
83: }
84:
85: try {
86: $this->_getSyncSingleton()->synchronize($storage);
87: } catch (Exception $e) {
88: Mage::logException($e);
89: $flag->passError($e);
90: }
91:
92: $flag->setState(Mage_Core_Model_File_Storage_Flag::STATE_FINISHED)->save();
93: }
94:
95: 96: 97: 98: 99:
100: public function statusAction()
101: {
102: $result = array();
103: $flag = $this->_getSyncFlag();
104:
105: if ($flag) {
106: $state = $flag->getState();
107:
108: switch ($state) {
109: case Mage_Core_Model_File_Storage_Flag::STATE_INACTIVE:
110: $flagData = $flag->getFlagData();
111: if (is_array($flagData)) {
112: if (isset($flagData['destination']) && !empty($flagData['destination'])) {
113: $result['destination'] = $flagData['destination'];
114: }
115: }
116:
117: $state = Mage_Core_Model_File_Storage_Flag::STATE_INACTIVE;
118: break;
119: case Mage_Core_Model_File_Storage_Flag::STATE_RUNNING:
120: if (!$flag->getLastUpdate()
121: || time() <= (strtotime($flag->getLastUpdate()) + Mage_Core_Model_File_Storage_Flag::FLAG_TTL)
122: ) {
123: $flagData = $flag->getFlagData();
124: if (is_array($flagData)
125: && isset($flagData['source']) && !empty($flagData['source'])
126: && isset($flagData['destination']) && !empty($flagData['destination'])
127: ) {
128: $result['message'] = Mage::helper('adminhtml')->__('Synchronizing %s to %s', $flagData['source'], $flagData['destination']);
129: } else {
130: $result['message'] = Mage::helper('adminhtml')->__('Synchronizing...');
131: }
132:
133: break;
134: } else {
135: $flagData = $flag->getFlagData();
136: if (is_array($flagData)
137: && !(isset($flagData['timeout_reached']) && $flagData['timeout_reached'])
138: ) {
139: Mage::logException(new Mage_Exception(
140: Mage::helper('adminhtml')->__('Timeout limit for response from synchronize process was reached.')
141: ));
142:
143: $state = Mage_Core_Model_File_Storage_Flag::STATE_FINISHED;
144:
145: $flagData['has_errors'] = true;
146: $flagData['timeout_reached'] = true;
147:
148: $flag->setState($state)
149: ->setFlagData($flagData)
150: ->save();
151: }
152: }
153: case Mage_Core_Model_File_Storage_Flag::STATE_FINISHED:
154: Mage::dispatchEvent('add_synchronize_message');
155:
156: $state = Mage_Core_Model_File_Storage_Flag::STATE_NOTIFIED;
157: case Mage_Core_Model_File_Storage_Flag::STATE_NOTIFIED:
158: $block = Mage::getSingleton('core/layout')
159: ->createBlock('adminhtml/notification_toolbar')
160: ->setTemplate('notification/toolbar.phtml');
161: $result['html'] = $block->toHtml();
162:
163: $flagData = $flag->getFlagData();
164: if (is_array($flagData)) {
165: if (isset($flagData['has_errors']) && $flagData['has_errors']) {
166: $result['has_errors'] = true;
167: }
168: }
169:
170: break;
171: default:
172: $state = Mage_Core_Model_File_Storage_Flag::STATE_INACTIVE;
173: break;
174: }
175: } else {
176: $state = Mage_Core_Model_File_Storage_Flag::STATE_INACTIVE;
177: }
178: $result['state'] = $state;
179:
180: $result = Mage::helper('core')->jsonEncode($result);
181: Mage::app()->getResponse()->setBody($result);
182: }
183: }
184: