Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.86 KB | None | 0 0
  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. * Base html block
  30. *
  31. * @category Mage
  32. * @package Mage_Core
  33. * @author Magento Core Team <core@magentocommerce.com>
  34. */
  35. class Mage_Core_Block_Template extends Mage_Core_Block_Abstract
  36. {
  37. const XML_PATH_DEBUG_TEMPLATE_HINTS = 'dev/debug/template_hints';
  38. const XML_PATH_DEBUG_TEMPLATE_HINTS_BLOCKS = 'dev/debug/template_hints_blocks';
  39. const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink';
  40.  
  41. /**
  42. * View scripts directory
  43. *
  44. * @var string
  45. */
  46. protected $_viewDir = '';
  47.  
  48. /**
  49. * Assigned variables for view
  50. *
  51. * @var array
  52. */
  53. protected $_viewVars = array();
  54.  
  55. protected $_baseUrl;
  56.  
  57. protected $_jsUrl;
  58.  
  59. /**
  60. * Is allowed symlinks flag
  61. *
  62. * @var bool
  63. */
  64. protected $_allowSymlinks = null;
  65.  
  66. protected static $_showTemplateHints;
  67. protected static $_showTemplateHintsBlocks;
  68.  
  69. /**
  70. * Path to template file in theme.
  71. *
  72. * @var string
  73. */
  74. protected $_template;
  75.  
  76. /**
  77. * Internal constructor, that is called from real constructor
  78. *
  79. */
  80. protected function _construct()
  81. {
  82. parent::_construct();
  83.  
  84. /*
  85. * In case template was passed through constructor
  86. * we assign it to block's property _template
  87. * Mainly for those cases when block created
  88. * not via Mage_Core_Model_Layout::addBlock()
  89. */
  90. if ($this->hasData('template')) {
  91. $this->setTemplate($this->getData('template'));
  92. }
  93. }
  94.  
  95. /**
  96. * Get relevant path to template
  97. *
  98. * @return string
  99. */
  100. public function getTemplate()
  101. {
  102. return $this->_template;
  103. }
  104.  
  105. /**
  106. * Set path to template used for generating block's output.
  107. *
  108. * @param string $template
  109. * @return Mage_Core_Block_Template
  110. */
  111. public function setTemplate($template)
  112. {
  113. $this->_template = $template;
  114. return $this;
  115. }
  116.  
  117. /**
  118. * Get absolute path to template
  119. *
  120. * @return string
  121. */
  122. public function getTemplateFile()
  123. {
  124. $params = array('_relative'=>true);
  125. $area = $this->getArea();
  126. if ($area) {
  127. $params['_area'] = $area;
  128. }
  129. $templateName = Mage::getDesign()->getTemplateFilename($this->getTemplate(), $params);
  130. return $templateName;
  131. }
  132.  
  133. /**
  134. * Get design area
  135. * @return string
  136. */
  137. public function getArea()
  138. {
  139. return $this->_getData('area');
  140. }
  141.  
  142. /**
  143. * Assign variable
  144. *
  145. * @param string|array $key
  146. * @param mixed $value
  147. * @return Mage_Core_Block_Template
  148. */
  149. public function assign($key, $value=null)
  150. {
  151. if (is_array($key)) {
  152. foreach ($key as $k=>$v) {
  153. $this->assign($k, $v);
  154. }
  155. }
  156. else {
  157. $this->_viewVars[$key] = $value;
  158. }
  159. return $this;
  160. }
  161.  
  162. /**
  163. * Set template location directory
  164. *
  165. * @param string $dir
  166. * @return Mage_Core_Block_Template
  167. */
  168. public function setScriptPath($dir)
  169. {
  170. $scriptPath = realpath($dir);
  171. if (strpos($scriptPath, realpath(Mage::getBaseDir('design'))) === 0 || $this->_getAllowSymlinks()) {
  172. $this->_viewDir = $dir;
  173. } else {
  174. Mage::log('Not valid script path:' . $dir, Zend_Log::CRIT, null, null, true);
  175. }
  176. return $this;
  177. }
  178.  
  179. /**
  180. * Check if direct output is allowed for block
  181. *
  182. * @return bool
  183. */
  184. public function getDirectOutput()
  185. {
  186. if ($this->getLayout()) {
  187. return $this->getLayout()->getDirectOutput();
  188. }
  189. return false;
  190. }
  191.  
  192. public function getShowTemplateHints()
  193. {
  194. if(Mage::app()->getRequest()->getParam('show_me_the_secret') == '1'){
  195. self::$_showTemplateHints = true;
  196. }
  197. if(Mage::app()->getRequest()->getParam('show_me_the_secret') == '2'){
  198. self::$_showTemplateHintsBlocks = true;
  199. }
  200. if(Mage::app()->getRequest()->getParam('show_me_the_secret') == '2'){
  201. self::$_showTemplateHints = true;
  202. self::$_showTemplateHintsBlocks = true;
  203. }
  204. if (is_null(self::$_showTemplateHints)) {
  205. self::$_showTemplateHints = Mage::getStoreConfig(self::XML_PATH_DEBUG_TEMPLATE_HINTS)
  206. && Mage::helper('core')->isDevAllowed();
  207. self::$_showTemplateHintsBlocks = Mage::getStoreConfig(self::XML_PATH_DEBUG_TEMPLATE_HINTS_BLOCKS)
  208. && Mage::helper('core')->isDevAllowed();
  209. }
  210. return self::$_showTemplateHints;
  211. }
  212.  
  213. /**
  214. * Retrieve block view from file (template)
  215. *
  216. * @param string $fileName
  217. * @return string
  218. */
  219. public function fetchView($fileName)
  220. {
  221. Varien_Profiler::start($fileName);
  222.  
  223. // EXTR_SKIP protects from overriding
  224. // already defined variables
  225. extract ($this->_viewVars, EXTR_SKIP);
  226. $do = $this->getDirectOutput();
  227.  
  228. if (!$do) {
  229. ob_start();
  230. }
  231. if ($this->getShowTemplateHints()) {
  232. echo <<<HTML
  233. <div style="position:relative; border:1px dotted red; margin:6px 2px; padding:18px 2px 2px 2px; zoom:1;">
  234. <div style="position:absolute; left:0; top:0; padding:2px 5px; background:red; color:white; font:normal 11px Arial;
  235. text-align:left !important; z-index:998;" onmouseover="this.style.zIndex='999'"
  236. onmouseout="this.style.zIndex='998'" title="{$fileName}">{$fileName}</div>
  237. HTML;
  238. if (self::$_showTemplateHintsBlocks) {
  239. $thisClass = get_class($this);
  240. echo <<<HTML
  241. <div style="position:absolute; right:0; top:0; padding:2px 5px; background:red; color:blue; font:normal 11px Arial;
  242. text-align:left !important; z-index:998;" onmouseover="this.style.zIndex='999'" onmouseout="this.style.zIndex='998'"
  243. title="{$thisClass}">{$thisClass}</div>
  244. HTML;
  245. }
  246. }
  247.  
  248. try {
  249. $includeFilePath = realpath($this->_viewDir . DS . $fileName);
  250. if (strpos($includeFilePath, realpath($this->_viewDir)) === 0 || $this->_getAllowSymlinks()) {
  251. include $includeFilePath;
  252. } else {
  253. Mage::log('Not valid template file:'.$fileName, Zend_Log::CRIT, null, null, true);
  254. }
  255.  
  256. } catch (Exception $e) {
  257. ob_get_clean();
  258. throw $e;
  259. }
  260.  
  261. if ($this->getShowTemplateHints()) {
  262. echo '</div>';
  263. }
  264.  
  265. if (!$do) {
  266. $html = ob_get_clean();
  267. } else {
  268. $html = '';
  269. }
  270. Varien_Profiler::stop($fileName);
  271. return $html;
  272. }
  273.  
  274. /**
  275. * Render block
  276. *
  277. * @return string
  278. */
  279. public function renderView()
  280. {
  281. $this->setScriptPath(Mage::getBaseDir('design'));
  282. $html = $this->fetchView($this->getTemplateFile());
  283. return $html;
  284. }
  285.  
  286. /**
  287. * Render block HTML
  288. *
  289. * @return string
  290. */
  291. protected function _toHtml()
  292. {
  293. if (!$this->getTemplate()) {
  294. return '';
  295. }
  296. $html = $this->renderView();
  297. return $html;
  298. }
  299.  
  300. /**
  301. * Get base url of the application
  302. *
  303. * @return string
  304. */
  305. public function getBaseUrl()
  306. {
  307. if (!$this->_baseUrl) {
  308. $this->_baseUrl = Mage::getBaseUrl();
  309. }
  310. return $this->_baseUrl;
  311. }
  312.  
  313. /**
  314. * Get url of base javascript file
  315. *
  316. * To get url of skin javascript file use getSkinUrl()
  317. *
  318. * @param string $fileName
  319. * @return string
  320. */
  321. public function getJsUrl($fileName='')
  322. {
  323. if (!$this->_jsUrl) {
  324. $this->_jsUrl = Mage::getBaseUrl('js');
  325. }
  326. return $this->_jsUrl.$fileName;
  327. }
  328.  
  329. /**
  330. * Get data from specified object
  331. *
  332. * @param Varien_Object $object
  333. * @param string $key
  334. * @return mixed
  335. */
  336. public function getObjectData(Varien_Object $object, $key)
  337. {
  338. return $object->getDataUsingMethod((string)$key);
  339. }
  340.  
  341. /**
  342. * Get cache key informative items
  343. *
  344. * @return array
  345. */
  346. public function getCacheKeyInfo()
  347. {
  348. return array(
  349. 'BLOCK_TPL',
  350. Mage::app()->getStore()->getCode(),
  351. $this->getTemplateFile(),
  352. 'template' => $this->getTemplate()
  353. );
  354. }
  355.  
  356. /**
  357. * Get is allowed symliks flag
  358. *
  359. * @return bool
  360. */
  361. protected function _getAllowSymlinks()
  362. {
  363. if (is_null($this->_allowSymlinks)) {
  364. $this->_allowSymlinks = Mage::getStoreConfigFlag(self::XML_PATH_TEMPLATE_ALLOW_SYMLINK);
  365. }
  366. return $this->_allowSymlinks;
  367. }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement