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_Core_Model_App_Emulation extends Varien_Object
35: {
36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: public function startEnvironmentEmulation($storeId, $area = Mage_Core_Model_App_Area::AREA_FRONTEND, $emulateSroreInlineTranslation = false)
48: {
49: if (is_null($area)) {
50: $area = Mage_Core_Model_App_Area::AREA_FRONTEND;
51: }
52: if ($emulateSroreInlineTranslation) {
53: $initialTranslateInline = $this->_emulateInlineTranslation($storeId, $area);
54: } else {
55: $initialTranslateInline = $this->_emulateInlineTranslation();
56: }
57: $initialDesign = $this->_emulateDesign($storeId, $area);
58:
59: Mage::app()->setCurrentStore($storeId);
60: $initialLocaleCode = $this->_emulateLocale($storeId, $area);
61:
62: $initialEnvironmentInfo = new Varien_Object();
63: $initialEnvironmentInfo->setInitialTranslateInline($initialTranslateInline)
64: ->setInitialDesign($initialDesign)
65: ->setInitialLocaleCode($initialLocaleCode);
66:
67: return $initialEnvironmentInfo;
68: }
69:
70: 71: 72: 73: 74: 75: 76: 77: 78:
79: public function stopEnvironmentEmulation(Varien_Object $initialEnvironmentInfo)
80: {
81: $this->_restoreInitialInlineTranslation($initialEnvironmentInfo->getInitialTranslateInline());
82: $initialDesign = $initialEnvironmentInfo->getInitialDesign();
83: $this->_restoreInitialDesign($initialDesign);
84:
85: Mage::app()->setCurrentStore($initialDesign['store']);
86: $this->_restoreInitialLocale($initialEnvironmentInfo->getInitialLocaleCode(), $initialDesign['area']);
87: return $this;
88: }
89:
90: 91: 92: 93: 94: 95: 96: 97: 98: 99:
100: protected function _emulateInlineTranslation($storeId = null, $area = Mage_Core_Model_App_Area::AREA_FRONTEND)
101: {
102: if (is_null($storeId)) {
103: $newTranslateInline = false;
104: } else {
105: if ($area == Mage_Core_Model_App_Area::AREA_ADMINHTML) {
106: $newTranslateInline = Mage::getStoreConfigFlag('dev/translate_inline/active_admin', $storeId);
107: } else {
108: $newTranslateInline = Mage::getStoreConfigFlag('dev/translate_inline/active', $storeId);
109: }
110: }
111: $translateModel = Mage::getSingleton('core/translate');
112: $initialTranslateInline = $translateModel->getTranslateInline();
113: $translateModel->setTranslateInline($newTranslateInline);
114: return $initialTranslateInline;
115: }
116:
117: 118: 119: 120: 121: 122: 123: 124:
125: protected function _emulateDesign($storeId, $area = Mage_Core_Model_App_Area::AREA_FRONTEND)
126: {
127: $initialDesign = Mage::getDesign()->setAllGetOld(array(
128: 'package' => Mage::getStoreConfig('design/package/name', $storeId),
129: 'store' => $storeId,
130: 'area' => $area
131: ));
132: Mage::getDesign()->setTheme('');
133: Mage::getDesign()->setPackageName('');
134: return $initialDesign;
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144:
145: protected function _emulateLocale($storeId, $area = Mage_Core_Model_App_Area::AREA_FRONTEND)
146: {
147: $initialLocaleCode = Mage::app()->getLocale()->getLocaleCode();
148: $newLocaleCode = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $storeId);
149: Mage::app()->getLocale()->setLocaleCode($newLocaleCode);
150: Mage::getSingleton('core/translate')->setLocale($newLocaleCode)->init($area, true);
151: return $initialLocaleCode;
152: }
153:
154: 155: 156: 157: 158: 159: 160:
161: protected function _restoreInitialInlineTranslation($initialTranslateInline)
162: {
163: $translateModel = Mage::getSingleton('core/translate');
164: $translateModel->setTranslateInline($initialTranslateInline);
165: return $this;
166: }
167:
168: 169: 170: 171: 172: 173: 174:
175: protected function _restoreInitialDesign(array $initialDesign)
176: {
177: Mage::getDesign()->setAllGetOld($initialDesign);
178: Mage::getDesign()->setTheme('');
179: Mage::getDesign()->setPackageName('');
180: return $this;
181: }
182:
183: 184: 185: 186: 187: 188: 189: 190:
191: protected function _restoreInitialLocale($initialLocaleCode, $initialArea = Mage_Core_Model_App_Area::AREA_ADMINHTML)
192: {
193: Mage::app()->getLocale()->setLocaleCode($initialLocaleCode);
194: Mage::getSingleton('core/translate')->setLocale($initialLocaleCode)->init($initialArea, true);
195: return $this;
196: }
197: }
198: