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_Page
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: * Page layout helper
29: *
30: * @category Mage
31: * @package Mage_Page
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Page_Helper_Layout extends Mage_Core_Helper_Abstract
35: {
36: /**
37: * Apply page layout handle
38: *
39: * @param string $pageLayout
40: * @return Mage_Page_Helper_Layout
41: */
42: public function applyHandle($pageLayout)
43: {
44: $pageLayout = $this->_getConfig()->getPageLayout($pageLayout);
45:
46: if (!$pageLayout) {
47: return $this;
48: }
49:
50: $this->getLayout()
51: ->getUpdate()
52: ->addHandle($pageLayout->getLayoutHandle());
53:
54: return $this;
55: }
56:
57: /**
58: * Apply page layout template
59: * (for old design packages)
60: *
61: * @param string $pageLayout
62: * @return Mage_Page_Helper_Layout
63: */
64: public function applyTemplate($pageLayout = null)
65: {
66: if ($pageLayout === null) {
67: $pageLayout = $this->getCurrentPageLayout();
68: } else {
69: $pageLayout = $this->_getConfig()->getPageLayout($pageLayout);
70: }
71:
72: if (!$pageLayout) {
73: return $this;
74: }
75:
76: if ($this->getLayout()->getBlock('root') &&
77: !$this->getLayout()->getBlock('root')->getIsHandle()) {
78: // If not applied handle
79: $this->getLayout()
80: ->getBlock('root')
81: ->setTemplate($pageLayout->getTemplate());
82: }
83:
84: return $this;
85: }
86:
87: /**
88: * Retrieve current applied page layout
89: *
90: * @return Varien_Object|boolean
91: */
92: public function getCurrentPageLayout()
93: {
94: if ($this->getLayout()->getBlock('root') &&
95: $this->getLayout()->getBlock('root')->getLayoutCode()) {
96: return $this->_getConfig()->getPageLayout($this->getLayout()->getBlock('root')->getLayoutCode());
97: }
98:
99: // All loaded handles
100: $handles = $this->getLayout()->getUpdate()->getHandles();
101: // Handles used in page layouts
102: $pageLayoutHandles = $this->_getConfig()->getPageLayoutHandles();
103: // Applied page layout handles
104: $appliedHandles = array_intersect($handles, $pageLayoutHandles);
105:
106: if (empty($appliedHandles)) {
107: return false;
108: }
109:
110: $currentHandle = array_pop($appliedHandles);
111:
112: $layoutCode = array_search($currentHandle, $pageLayoutHandles, true);
113:
114: return $this->_getConfig()->getPageLayout($layoutCode);
115: }
116:
117: /**
118: * Retrieve page config
119: *
120: * @return Mage_Page_Model_Config
121: */
122: protected function _getConfig()
123: {
124: return Mage::getSingleton('page/config');
125: }
126: }
127: