PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/rgranadino/magento-mirror
PHP | 219 lines | 128 code | 15 blank | 76 comment | 25 complexity | 1b881b64e34a5d3b5931e0d297b0eb42 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) 2011 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 Options 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_Options extends Mage_XmlConnect_Block_Catalog
  34. {
  35. const OPTION_TYPE_SELECT = 'select';
  36. const OPTION_TYPE_CHECKBOX = 'checkbox';
  37. const OPTION_TYPE_TEXT = 'text';
  38. /**
  39. * Store supported product options xml renderers based on product types
  40. *
  41. * @var array
  42. */
  43. protected $_renderers = array();
  44. /**
  45. * Add new product options renderer
  46. *
  47. * @param string $type
  48. * @param string $renderer
  49. * @return Mage_XmlConnect_Block_Product_Options
  50. */
  51. public function addRenderer($type, $renderer)
  52. {
  53. if (!isset($this->_renderers[$type])) {
  54. $this->_renderers[$type] = $renderer;
  55. }
  56. return $this;
  57. }
  58. /**
  59. * Create produc custom options Mage_XmlConnect_Model_Simplexml_Element object
  60. *
  61. * @param Mage_Catalog_Model_Product $product
  62. * @return Mage_XmlConnect_Model_Simplexml_Element
  63. */
  64. public function getProductCustomOptionsXmlObject(Mage_Catalog_Model_Product $product)
  65. {
  66. $xmlModel = Mage::getModel('xmlconnect/simplexml_element', '<product></product>');
  67. $optionsNode = $xmlModel->addChild('options');
  68. if (!$product->getId()) {
  69. return $xmlModel;
  70. }
  71. $xmlModel->addAttribute('id', $product->getId());
  72. if (!$product->isSaleable() || !sizeof($product->getOptions())) {
  73. return $xmlModel;
  74. }
  75. foreach ($product->getOptions() as $option) {
  76. $optionNode = $optionsNode->addChild('option');
  77. $type = $this->_getOptionTypeForXmlByRealType($option->getType());
  78. $code = 'options[' . $option->getId() . ']';
  79. if ($type == self::OPTION_TYPE_CHECKBOX) {
  80. $code .= '[]';
  81. }
  82. $optionNode->addAttribute('code', $code);
  83. $optionNode->addAttribute('type', $type);
  84. $optionNode->addAttribute('label', $xmlModel->xmlentities(strip_tags($option->getTitle())));
  85. if ($option->getIsRequire()) {
  86. $optionNode->addAttribute('is_required', 1);
  87. }
  88. /**
  89. * Process option price
  90. */
  91. $price = $option->getPrice();
  92. if ($price) {
  93. $optionNode->addAttribute('price', Mage::helper('xmlconnect')->formatPriceForXml($price));
  94. $formatedPrice = Mage::app()->getStore($product->getStoreId())->formatPrice($price, false);
  95. $optionNode->addAttribute('formated_price', $formatedPrice);
  96. }
  97. if ($type == self::OPTION_TYPE_CHECKBOX ||
  98. $type == self::OPTION_TYPE_SELECT) {
  99. foreach ($option->getValues() as $value) {
  100. $valueNode = $optionNode->addChild('value');
  101. $valueNode->addAttribute('code', $value->getId());
  102. $valueNode->addAttribute('label', $xmlModel->xmlentities(strip_tags($value->getTitle())));
  103. $price = Mage::helper('xmlconnect')->formatPriceForXml($value->getPrice());
  104. if ((float)$price != 0.00) {
  105. $valueNode->addAttribute('price', $price);
  106. $formatedPrice = $this->_formatPriceString($price, $product);
  107. $valueNode->addAttribute('formated_price', $formatedPrice);
  108. }
  109. }
  110. }
  111. }
  112. return $xmlModel;
  113. }
  114. /**
  115. * Format price with currency code and taxes
  116. *
  117. * @param string|int|float $price
  118. * @param Mage_Catalog_Model_Product $product
  119. * @return string
  120. */
  121. protected function _formatPriceString($price, $product)
  122. {
  123. $priceTax = Mage::helper('tax')->getPrice($product, $price);
  124. $priceIncTax = Mage::helper('tax')->getPrice($product, $price, true);
  125. if (Mage::helper('tax')->displayBothPrices() && $priceTax != $priceIncTax) {
  126. $formatted = Mage::helper('core')->currency($priceTax, true, false)
  127. . ' (+'
  128. . Mage::helper('core')->currency($priceIncTax, true, false)
  129. . ' '
  130. . Mage::helper('tax')->__('Incl. Tax')
  131. . ')';
  132. } else {
  133. $formatted = $this->helper('core')->currency($priceTax, true, false);
  134. }
  135. return $formatted;
  136. }
  137. /**
  138. * Retrieve option type name by specified option real type name
  139. *
  140. * @param string $realType
  141. * @return string
  142. */
  143. protected function _getOptionTypeForXmlByRealType($realType)
  144. {
  145. switch ($realType) {
  146. case Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN:
  147. case Mage_Catalog_Model_Product_Option::OPTION_TYPE_RADIO:
  148. $type = self::OPTION_TYPE_SELECT;
  149. break;
  150. case Mage_Catalog_Model_Product_Option::OPTION_TYPE_MULTIPLE:
  151. case Mage_Catalog_Model_Product_Option::OPTION_TYPE_CHECKBOX:
  152. $type = self::OPTION_TYPE_CHECKBOX;
  153. break;
  154. case Mage_Catalog_Model_Product_Option::OPTION_TYPE_FIELD:
  155. case Mage_Catalog_Model_Product_Option::OPTION_TYPE_AREA:
  156. default:
  157. $type = self::OPTION_TYPE_TEXT;
  158. break;
  159. }
  160. return $type;
  161. }
  162. /**
  163. * Create product custom options Mage_XmlConnect_Model_Simplexml_Element object
  164. *
  165. * @param Mage_Catalog_Model_Product $product
  166. * @return Mage_XmlConnect_Model_Simplexml_Element | false
  167. */
  168. public function getProductOptionsXmlObject(Mage_Catalog_Model_Product $product)
  169. {
  170. if ($product->getId()) {
  171. $type = $product->getTypeId();
  172. if (isset($this->_renderers[$type])) {
  173. $renderer = $this->getLayout()->createBlock($this->_renderers[$type]);
  174. if ($renderer) {
  175. return $renderer->getProductOptionsXml($product, true);
  176. }
  177. }
  178. }
  179. return false;
  180. }
  181. /**
  182. * Generate product options xml
  183. *
  184. * @return string
  185. */
  186. protected function _toHtml()
  187. {
  188. $productId = $this->getRequest()->getParam('id', null);
  189. $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId());
  190. if ($productId) {
  191. $product->load($productId);
  192. }
  193. if ($product->getId()) {
  194. $type = $product->getTypeId();
  195. if (isset($this->_renderers[$type])) {
  196. $renderer = $this->getLayout()->createBlock($this->_renderers[$type]);
  197. if ($renderer) {
  198. return $renderer->getProductOptionsXml($product);
  199. }
  200. }
  201. }
  202. return '<?xml version="1.0" encoding="UTF-8"?><options/>';
  203. }
  204. }