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: class Mage_Catalog_Helper_Product_View extends Mage_Core_Helper_Abstract
33: {
34:
35: public $ERR_NO_PRODUCT_LOADED = 1;
36: public $ERR_BAD_CONTROLLER_INTERFACE = 2;
37:
38: 39: 40: 41: 42: 43: 44: 45:
46: public function initProductLayout($product, $controller)
47: {
48: $design = Mage::getSingleton('catalog/design');
49: $settings = $design->getDesignSettings($product);
50:
51: if ($settings->getCustomDesign()) {
52: $design->applyCustomDesign($settings->getCustomDesign());
53: }
54:
55: $update = $controller->getLayout()->getUpdate();
56: $update->addHandle('default');
57: $controller->addActionLayoutHandles();
58:
59: $update->addHandle('PRODUCT_TYPE_' . $product->getTypeId());
60: $update->addHandle('PRODUCT_' . $product->getId());
61: $controller->loadLayoutUpdates();
62:
63:
64: $layoutUpdates = $settings->getLayoutUpdates();
65: if ($layoutUpdates) {
66: if (is_array($layoutUpdates)) {
67: foreach($layoutUpdates as $layoutUpdate) {
68: $update->addUpdate($layoutUpdate);
69: }
70: }
71: }
72:
73: $controller->generateLayoutXml()->generateLayoutBlocks();
74:
75:
76: if ($settings->getPageLayout()) {
77: $controller->getLayout()->helper('page/layout')->applyTemplate($settings->getPageLayout());
78: }
79:
80: $currentCategory = Mage::registry('current_category');
81: $root = $controller->getLayout()->getBlock('root');
82: if ($root) {
83: $controllerClass = $controller->getFullActionName();
84: if ($controllerClass != 'catalog-product-view') {
85: $root->addBodyClass('catalog-product-view');
86: }
87: $root->addBodyClass('product-' . $product->getUrlKey());
88: if ($currentCategory instanceof Mage_Catalog_Model_Category) {
89: $root->addBodyClass('categorypath-' . $currentCategory->getUrlPath())
90: ->addBodyClass('category-' . $currentCategory->getUrlKey());
91: }
92: }
93:
94: return $this;
95: }
96:
97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111:
112: public function prepareAndRender($productId, $controller, $params = null)
113: {
114:
115: $productHelper = Mage::helper('catalog/product');
116: if (!$params) {
117: $params = new Varien_Object();
118: }
119:
120:
121: $product = $productHelper->initProduct($productId, $controller, $params);
122: if (!$product) {
123: throw new Mage_Core_Exception($this->__('Product is not loaded'), $this->ERR_NO_PRODUCT_LOADED);
124: }
125:
126: $buyRequest = $params->getBuyRequest();
127: if ($buyRequest) {
128: $productHelper->prepareProductOptions($product, $buyRequest);
129: }
130:
131: if ($params->hasConfigureMode()) {
132: $product->setConfigureMode($params->getConfigureMode());
133: }
134:
135: Mage::dispatchEvent('catalog_controller_product_view', array('product' => $product));
136:
137: if ($params->getSpecifyOptions()) {
138: $notice = $product->getTypeInstance(true)->getSpecifyOptionMessage();
139: Mage::getSingleton('catalog/session')->addNotice($notice);
140: }
141:
142: Mage::getSingleton('catalog/session')->setLastViewedProductId($product->getId());
143:
144: $this->initProductLayout($product, $controller);
145:
146: $controller->initLayoutMessages(array('catalog/session', 'tag/session', 'checkout/session'))
147: ->renderLayout();
148:
149: return $this;
150: }
151: }
152: