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