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_XmlConnect
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: * Related products block
29: *
30: * @category Mage
31: * @package Mage_XmlConnect
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_XmlConnect_Block_Catalog_Product_Related extends Mage_XmlConnect_Block_Catalog_Product_List
35: {
36: /**
37: * Retrieve related products xml object based on current product
38: *
39: * @return Mage_XmlConnect_Model_Simplexml_Element
40: * @see $this->getProduct()
41: */
42: public function getRelatedProductsXmlObj()
43: {
44: /** @var $relatedXmlObj Mage_XmlConnect_Model_Simplexml_Element */
45: $relatedXmlObj = Mage::getModel('xmlconnect/simplexml_element', '<related_products></related_products>');
46:
47: $productObj = $this->getParentBlock()->getProduct();
48:
49: if (is_object(Mage::getConfig()->getNode('modules/Enterprise_TargetRule'))) {
50: Mage::register('product', $productObj);
51:
52: $productBlock = $this->getLayout()->addBlock(
53: 'enterprise_targetrule/catalog_product_list_related', 'relatedProducts'
54: );
55:
56: $collection = $productBlock->getItemCollection();
57: } else {
58: if ($productObj->getId() > 0) {
59: $collection = $this->_getProductCollection();
60: if (!$collection) {
61: return $relatedXmlObj;
62: }
63: $collection = $collection->getItems();
64: }
65: }
66:
67: $this->_addProductXmlObj($relatedXmlObj, $collection);
68:
69: return $relatedXmlObj;
70: }
71:
72: /**
73: * Add related products info to xml object
74: *
75: * @param Mage_XmlConnect_Model_Simplexml_Element $relatedXmlObj
76: * @param array $collection
77: * @return Mage_XmlConnect_Model_Simplexml_Element
78: */
79: protected function _addProductXmlObj(Mage_XmlConnect_Model_Simplexml_Element $relatedXmlObj, $collection)
80: {
81: foreach ($collection as $product) {
82: $productXmlObj = $this->productToXmlObject($product);
83:
84: if (!$productXmlObj) {
85: continue;
86: }
87:
88: if ($this->getParentBlock()->getChild('product_price')) {
89: $this->getParentBlock()->getChild('product_price')->setProduct($product)
90: ->setProductXmlObj($productXmlObj)->collectProductPrices();
91: }
92: $relatedXmlObj->appendChild($productXmlObj);
93: }
94: return $relatedXmlObj;
95: }
96:
97: /**
98: * Generate related products xml
99: *
100: * @return string
101: */
102: protected function _toHtml()
103: {
104: return $this->getRelatedProductsXmlObj()->asNiceXml();
105: }
106:
107: /**
108: * Retrieve product collection with all prepared data
109: *
110: * @return Mage_Eav_Model_Entity_Collection_Abstract
111: */
112: protected function _getProductCollection()
113: {
114: if (is_null($this->_productCollection)) {
115: $collection = $this->getParentBlock()->getProduct()->getRelatedProductCollection();
116: Mage::getSingleton('catalog/layer')->prepareProductCollection($collection);
117: /**
118: * Add rating and review summary, image attribute, apply sort params
119: */
120: $this->_prepareCollection($collection);
121: $this->_productCollection = $collection;
122: }
123: return $this->_productCollection;
124: }
125: }
126: