PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/XmlConnect/Block/Catalog/Product.php

https://github.com/itfcfan/magento-mirror
PHP | 161 lines | 86 code | 19 blank | 56 comment | 18 complexity | 4e2254849151b499ac8d33fcaee697a7 MD5 | raw file
  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) 2010 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. * Product data xml renderer
  28. *
  29. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_Block_Catalog_Product extends Mage_XmlConnect_Block_Catalog
  34. {
  35. /**
  36. * Retrieve product attributes as xml object
  37. *
  38. * @param Mage_Catalog_Model_Product $product
  39. * @param string $itemNodeName
  40. *
  41. * @return Mage_XmlConnect_Model_Simplexml_Element
  42. */
  43. public function productToXmlObject(Mage_Catalog_Model_Product $product, $itemNodeName = 'item')
  44. {
  45. $item = new Mage_XmlConnect_Model_Simplexml_Element('<' . $itemNodeName . '></' . $itemNodeName . '>');
  46. if ($product && $product->getId()) {
  47. $item->addChild('entity_id', $product->getId());
  48. $item->addChild('name', $item->xmlentities(strip_tags($product->getName())));
  49. $item->addChild('entity_type', $product->getTypeId());
  50. $item->addChild('short_description', $item->xmlentities(strip_tags($product->getShortDescription())));
  51. $item->addChild('description', Mage::helper('xmlconnect')->htmlize($item->xmlentities($product->getDescription())));
  52. if ($itemNodeName == 'item') {
  53. $imageToResize = Mage::helper('xmlconnect/image')->getImageSizeForContent('product_small');
  54. $propertyToResizeName = 'small_image';
  55. } else {
  56. $imageToResize = Mage::helper('xmlconnect/image')->getImageSizeForContent('product_big');
  57. $propertyToResizeName = 'image';
  58. }
  59. $icon = clone Mage::helper('catalog/image')->init($product, $propertyToResizeName)
  60. ->resize($imageToResize);
  61. $iconXml = $item->addChild('icon', $icon);
  62. $file = Mage::helper('xmlconnect')->urlToPath($icon);
  63. $iconXml->addAttribute('modification_time', filemtime($file));
  64. $item->addChild('in_stock', (int)$product->isSalable());
  65. $item->addChild('is_salable', (int)$product->isSalable());
  66. /**
  67. * By default all products has gallery (because of collection not load gallery attribute)
  68. */
  69. $hasGallery = 1;
  70. if ($product->getMediaGalleryImages()) {
  71. $hasGallery = sizeof($product->getMediaGalleryImages()) > 0 ? 1 : 0;
  72. }
  73. $item->addChild('has_gallery', $hasGallery);
  74. /**
  75. * If product type is grouped than it has options as its grouped items
  76. */
  77. if ($product->getTypeId() == Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE) {
  78. $product->setHasOptions(true);
  79. }
  80. $item->addChild('has_options', (int)$product->getHasOptions());
  81. if ($minSaleQty = $this->_getMinimalQty($product)) {
  82. $item->addChild('min_sale_qty', (int) $minSaleQty);
  83. }
  84. if (!$product->getRatingSummary()) {
  85. Mage::getModel('review/review')
  86. ->getEntitySummary($product, Mage::app()->getStore()->getId());
  87. }
  88. $item->addChild('rating_summary', round((int)$product->getRatingSummary()->getRatingSummary() / 10));
  89. $item->addChild('reviews_count', $product->getRatingSummary()->getReviewsCount());
  90. if ($this->getChild('product_price')) {
  91. $this->getChild('product_price')->setProduct($product)
  92. ->setProductXmlObj($item)
  93. ->collectProductPrices();
  94. }
  95. if ($this->getChild('additional_info')) {
  96. $this->getChild('additional_info')->addAdditionalData($product, $item);
  97. }
  98. }
  99. return $item;
  100. }
  101. /**
  102. * Get MinSaleQty for product
  103. *
  104. * @param Mage_Catalog_Model_Product $product
  105. * @return int|null
  106. */
  107. protected function _getMinimalQty($product)
  108. {
  109. if ($stockItem = $product->getStockItem()) {
  110. return ($stockItem->getMinSaleQty() && $stockItem->getMinSaleQty() > 0 ? $stockItem->getMinSaleQty() * 1 : null);
  111. }
  112. return null;
  113. }
  114. /**
  115. * Render product info xml
  116. *
  117. * @return string
  118. */
  119. protected function _toHtml()
  120. {
  121. $product = Mage::getModel('catalog/product')
  122. ->setStoreId(Mage::app()->getStore()->getId())
  123. ->load($this->getRequest()->getParam('id', 0));
  124. if (!$product) {
  125. throw new Mage_Core_Exception($this->__('Selected product is unavailable.'));
  126. } else {
  127. $this->setProduct($product);
  128. $productXmlObj = $this->productToXmlObject($product, 'product');
  129. $relatedProductsBlock = $this->getChild('related_products');
  130. if ($relatedProductsBlock) {
  131. $relatedXmlObj = $relatedProductsBlock->getRelatedProductsXmlObj();
  132. $productXmlObj->appendChild($relatedXmlObj);
  133. }
  134. }
  135. $productOptions = $this->getChild('xmlconnect.catalog.product.options')
  136. ->getProductOptionsXmlObject($product);
  137. if ($productOptions instanceof Mage_XmlConnect_Model_Simplexml_Element) {
  138. $productXmlObj->appendChild($productOptions);
  139. }
  140. return $productXmlObj->asNiceXml();
  141. }
  142. }