1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Core
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Template model class
30: *
31: * @category Mage
32: * @package Mage_Core
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: abstract class Mage_Core_Model_Template extends Mage_Core_Model_Abstract
36: {
37: /**
38: * Types of template
39: */
40: const TYPE_TEXT = 1;
41: const TYPE_HTML = 2;
42:
43: /**
44: * Default design area for emulation
45: */
46: const DEFAULT_DESIGN_AREA = 'frontend';
47:
48: /**
49: * Configuration of desing package for template
50: *
51: * @var Varien_Object
52: */
53: protected $_designConfig;
54:
55:
56: /**
57: * Configuration of emulated desing package.
58: *
59: * @var Varien_Object|boolean
60: */
61: protected $_emulatedDesignConfig = false;
62:
63: /**
64: * Initial environment information
65: * @see self::_applyDesignConfig()
66: *
67: * @var Varien_Object|null
68: */
69: protected $_initialEnvironmentInfo = null;
70:
71: /**
72: * Applying of design config
73: *
74: * @return Mage_Core_Model_Template
75: */
76: protected function _applyDesignConfig()
77: {
78: $designConfig = $this->getDesignConfig();
79: $store = $designConfig->getStore();
80: $storeId = is_object($store) ? $store->getId() : $store;
81: $area = $designConfig->getArea();
82: if (!is_null($storeId)) {
83: $appEmulation = Mage::getSingleton('core/app_emulation');
84: $this->_initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId, $area);
85: }
86: return $this;
87: }
88:
89: /**
90: * Revert design settings to previous
91: *
92: * @return Mage_Core_Model_Template
93: */
94: protected function _cancelDesignConfig()
95: {
96: if (!empty($this->_initialEnvironmentInfo)) {
97: $appEmulation = Mage::getSingleton('core/app_emulation');
98: $appEmulation->stopEnvironmentEmulation($this->_initialEnvironmentInfo);
99: $this->_initialEnvironmentInfo = null;
100: }
101: return $this;
102: }
103:
104: /**
105: * Get design configuration data
106: *
107: * @return Varien_Object
108: */
109: protected function getDesignConfig()
110: {
111: if(is_null($this->_designConfig)) {
112: $store = Mage::getDesign()->getStore();
113: $storeId = is_object($store) ? $store->getId() : $store;
114: $this->_designConfig = new Varien_Object(array(
115: 'area' => Mage::getDesign()->getArea(),
116: 'store' => $storeId
117: ));
118: }
119: return $this->_designConfig;
120: }
121:
122: /**
123: * Initialize design information for template processing
124: *
125: * @param array $config
126: * @return Mage_Core_Model_Template
127: */
128: public function setDesignConfig(array $config)
129: {
130: $this->getDesignConfig()->setData($config);
131: return $this;
132: }
133:
134: /**
135: * Save current design config and replace with design config from specified store
136: * Event is not dispatched.
137: *
138: * @param int|string $storeId
139: */
140: public function emulateDesign($storeId, $area=self::DEFAULT_DESIGN_AREA)
141: {
142: if ($storeId) {
143: // save current design settings
144: $this->_emulatedDesignConfig = clone $this->getDesignConfig();
145: if ($this->getDesignConfig()->getStore() != $storeId) {
146: $this->setDesignConfig(array('area' => $area, 'store' => $storeId));
147: $this->_applyDesignConfig();
148: }
149: } else {
150: $this->_emulatedDesignConfig = false;
151: }
152: }
153:
154: /**
155: * Revert to last design config, used before emulation
156: *
157: */
158: public function revertDesign()
159: {
160: if ($this->_emulatedDesignConfig) {
161: $this->setDesignConfig($this->_emulatedDesignConfig->getData());
162: $this->_cancelDesignConfig();
163: $this->_emulatedDesignConfig = false;
164: }
165: }
166:
167: /**
168: * Return true if template type eq text
169: *
170: * @return boolean
171: */
172: public function isPlain()
173: {
174: return $this->getType() == self::TYPE_TEXT;
175: }
176:
177: /**
178: * Getter for template type
179: *
180: * @return int|string
181: */
182: abstract public function getType();
183: }
184: