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: 33:
34: class Mage_XmlConnect_Block_Catalog_Product extends Mage_XmlConnect_Block_Catalog
35: {
36: 37: 38: 39: 40: 41: 42:
43: public function productToXmlObject(Mage_Catalog_Model_Product $product, $itemNodeName = 'item')
44: {
45:
46: $item = Mage::getModel('xmlconnect/simplexml_element', '<' . $itemNodeName . '></' . $itemNodeName . '>');
47: if ($product && $product->getId()) {
48: $item->addChild('entity_id', $product->getId());
49: $item->addChild('name', $item->escapeXml($product->getName()));
50: $item->addChild('entity_type', $product->getTypeId());
51: $item->addChild('short_description', $item->escapeXml($product->getShortDescription()));
52: $description = Mage::helper('xmlconnect')->htmlize($item->xmlentities($product->getDescription()));
53: $item->addChild('description', $description);
54: $item->addChild('link', $product->getProductUrl());
55:
56: if ($itemNodeName == 'item') {
57: $imageToResize = Mage::helper('xmlconnect/image')->getImageSizeForContent('product_small');
58: $propertyToResizeName = 'small_image';
59: } else {
60: $imageToResize = Mage::helper('xmlconnect/image')->getImageSizeForContent('product_big');
61: $propertyToResizeName = 'image';
62: }
63:
64: $icon = clone Mage::helper('catalog/image')->init($product, $propertyToResizeName)->resize($imageToResize);
65:
66: $iconXml = $item->addChild('icon', $icon);
67:
68: $file = Mage::helper('xmlconnect')->urlToPath($icon);
69: $iconXml->addAttribute('modification_time', filemtime($file));
70:
71: $item->addChild('in_stock', (int)$product->getIsInStock());
72: $item->addChild('is_salable', (int)$product->isSalable());
73: 74: 75:
76: $hasGallery = 1;
77: if ($product->getMediaGalleryImages()) {
78: $hasGallery = sizeof($product->getMediaGalleryImages()) > 0 ? 1 : 0;
79: }
80: $item->addChild('has_gallery', $hasGallery);
81: 82: 83:
84: if ($product->getTypeId() == Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE
85: || $product->getTypeId() == Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE) {
86: $product->setHasOptions(true);
87: }
88: $item->addChild('has_options', (int)$product->getHasOptions());
89:
90: if ($minSaleQty = $this->_getMinimalQty($product)) {
91: $item->addChild('min_sale_qty', (int) $minSaleQty);
92: }
93:
94: if (!$product->getRatingSummary()) {
95: Mage::getModel('review/review')->getEntitySummary($product, Mage::app()->getStore()->getId());
96: }
97:
98: $item->addChild('rating_summary', round((int)$product->getRatingSummary()->getRatingSummary() / 10));
99: $item->addChild('reviews_count', $product->getRatingSummary()->getReviewsCount());
100:
101: if ($this->getChild('product_price')) {
102: $this->getChild('product_price')->setProduct($product)->setProductXmlObj($item)
103: ->collectProductPrices();
104: }
105:
106: if ($this->getChild('additional_info')) {
107: $this->getChild('additional_info')->addAdditionalData($product, $item);
108: }
109: }
110:
111: return $item;
112: }
113:
114: 115: 116: 117: 118: 119:
120: protected function _getMinimalQty($product)
121: {
122: if ($stockItem = $product->getStockItem()) {
123: if ($stockItem->getMinSaleQty() && $stockItem->getMinSaleQty() > 0) {
124: return ($stockItem->getMinSaleQty() * 1);
125: }
126: }
127: return null;
128: }
129:
130: 131: 132: 133: 134: 135:
136: protected function _toHtml()
137: {
138:
139: $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())
140: ->load($this->getRequest()->getParam('id', 0));
141:
142: if (!$product) {
143: Mage::throwException($this->__('Selected product is unavailable.'));
144: } else {
145: $this->setProduct($product);
146: $productXmlObj = $this->productToXmlObject($product, 'product');
147:
148: $relatedProductsBlock = $this->getChild('related_products');
149: if ($relatedProductsBlock) {
150: $relatedXmlObj = $relatedProductsBlock->getRelatedProductsXmlObj();
151: $productXmlObj->appendChild($relatedXmlObj);
152: }
153: }
154:
155: $productOptions = $this->getChild('xmlconnect.catalog.product.options')
156: ->getProductOptionsXmlObject($product);
157: if ($productOptions instanceof Mage_XmlConnect_Model_Simplexml_Element) {
158: $productXmlObj->appendChild($productOptions);
159: }
160:
161: return $productXmlObj->asNiceXml();
162: }
163: }
164: