PageRenderTime 34ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Helper/Product/Configuration.php

https://github.com/ticean/magento-mirror
PHP | 254 lines | 147 code | 20 blank | 87 comment | 30 complexity | a25fd1cebdce5ac1a02468dd7ff61ec2 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_Catalog
  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. * Helper for fetching properties by product configurational item
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Helper_Product_Configuration extends Mage_Core_Helper_Abstract
  34. implements Mage_Catalog_Helper_Product_Configuration_Interface
  35. {
  36. /**
  37. * Retrieves product configuration options
  38. *
  39. * @param Mage_Catalog_Model_Product_Configuration_Item_Interface $item
  40. * @return array
  41. */
  42. public function getCustomOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
  43. {
  44. $product = $item->getProduct();
  45. $options = array();
  46. $optionIds = $item->getOptionByCode('option_ids');
  47. if ($optionIds) {
  48. $options = array();
  49. foreach (explode(',', $optionIds->getValue()) as $optionId) {
  50. $option = $product->getOptionById($optionId);
  51. if ($option) {
  52. $itemOption = $item->getOptionByCode('option_' . $option->getId());
  53. $group = $option->groupFactory($option->getType())
  54. ->setOption($option)
  55. ->setConfigurationItem($item)
  56. ->setConfigurationItemOption($itemOption);
  57. if ('file' == $option->getType()) {
  58. $downloadParams = $item->getFileDownloadParams();
  59. if ($downloadParams) {
  60. $url = $downloadParams->getUrl();
  61. if ($url) {
  62. $group->setCustomOptionDownloadUrl($url);
  63. }
  64. $urlParams = $downloadParams->getUrlParams();
  65. if ($urlParams) {
  66. $group->setCustomOptionUrlParams($urlParams);
  67. }
  68. }
  69. }
  70. $options[] = array(
  71. 'label' => $option->getTitle(),
  72. 'value' => $group->getFormattedOptionValue($itemOption->getValue()),
  73. 'print_value' => $group->getPrintableOptionValue($itemOption->getValue()),
  74. 'option_id' => $option->getId(),
  75. 'option_type' => $option->getType(),
  76. 'custom_view' => $group->isCustomizedView()
  77. );
  78. }
  79. }
  80. }
  81. $addOptions = $item->getOptionByCode('additional_options');
  82. if ($addOptions) {
  83. $options = array_merge($options, unserialize($addOptions->getValue()));
  84. }
  85. return $options;
  86. }
  87. /**
  88. * Retrieves configuration options for configurable product
  89. *
  90. * @param Mage_Catalog_Model_Product_Configuration_Item_Interface $item
  91. * @return array
  92. */
  93. public function getConfigurableOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
  94. {
  95. $product = $item->getProduct();
  96. $typeId = $product->getTypeId();
  97. if ($typeId != Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE) {
  98. Mage::throwException($this->__('Wrong product type to extract configurable options.'));
  99. }
  100. $attributes = $product->getTypeInstance(true)
  101. ->getSelectedAttributesInfo($product);
  102. return array_merge($attributes, $this->getCustomOptions($item));
  103. }
  104. /**
  105. * Retrieves configuration options for grouped product
  106. *
  107. * @param Mage_Catalog_Model_Product_Configuration_Item_Interface $item
  108. * @return array
  109. */
  110. public function getGroupedOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
  111. {
  112. $product = $item->getProduct();
  113. $typeId = $product->getTypeId();
  114. if ($typeId != Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE) {
  115. Mage::throwException($this->__('Wrong product type to extract configurable options.'));
  116. }
  117. $options = array();
  118. /**
  119. * @var Mage_Catalog_Model_Product_Type_Grouped
  120. */
  121. $typeInstance = $product->getTypeInstance(true);
  122. $associatedProducts = $typeInstance->getAssociatedProducts($product);
  123. if ($associatedProducts) {
  124. foreach ($associatedProducts as $associatedProduct) {
  125. $qty = $item->getOptionByCode('associated_product_' . $associatedProduct->getId());
  126. $option = array(
  127. 'label' => $associatedProduct->getName(),
  128. 'value' => ($qty && $qty->getValue()) ? $qty->getValue() : 0
  129. );
  130. $options[] = $option;
  131. }
  132. }
  133. return array_merge($options, $this->getCustomOptions($item));
  134. }
  135. /**
  136. * Retrieves product options list
  137. *
  138. * @param Mage_Catalog_Model_Product_Configuration_Item_Interface $item
  139. * @return array
  140. */
  141. public function getOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
  142. {
  143. $typeId = $item->getProduct()->getTypeId();
  144. switch ($typeId) {
  145. case Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE:
  146. return $this->getConfigurableOptions($item);
  147. break;
  148. case Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE:
  149. return $this->getGroupedOptions($item);
  150. break;
  151. }
  152. return $this->getCustomOptions($item);
  153. }
  154. /**
  155. * Accept option value and return its formatted view
  156. *
  157. * @param mixed $optionValue
  158. * Method works well with these $optionValue format:
  159. * 1. String
  160. * 2. Indexed array e.g. array(val1, val2, ...)
  161. * 3. Associative array, containing additional option info, including option value, e.g.
  162. * array
  163. * (
  164. * [label] => ...,
  165. * [value] => ...,
  166. * [print_value] => ...,
  167. * [option_id] => ...,
  168. * [option_type] => ...,
  169. * [custom_view] =>...,
  170. * )
  171. * @param array $params
  172. * All keys are options. Following supported:
  173. * - 'maxLength': truncate option value if needed, default: do not truncate
  174. * - 'cutReplacer': replacer for cut off value part when option value exceeds maxLength
  175. *
  176. * @return array
  177. */
  178. public function getFormattedOptionValue($optionValue, $params = null)
  179. {
  180. // Init params
  181. if (!$params) {
  182. $params = array();
  183. }
  184. $maxLength = isset($params['max_length']) ? $params['max_length'] : null;
  185. $cutReplacer = isset($params['cut_replacer']) ? $params['cut_replacer'] : '...';
  186. // Proceed with option
  187. $optionInfo = array();
  188. // Define input data format
  189. if (is_array($optionValue)) {
  190. if (isset($optionValue['option_id'])) {
  191. $optionInfo = $optionValue;
  192. if (isset($optionInfo['value'])) {
  193. $optionValue = $optionInfo['value'];
  194. }
  195. } else if (isset($optionValue['value'])) {
  196. $optionValue = $optionValue['value'];
  197. }
  198. }
  199. // Render customized option view
  200. if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
  201. $_default = array('value' => $optionValue);
  202. if (isset($optionInfo['option_type'])) {
  203. try {
  204. $group = Mage::getModel('catalog/product_option')->groupFactory($optionInfo['option_type']);
  205. return array('value' => $group->getCustomizedView($optionInfo));
  206. } catch (Exception $e) {
  207. return $_default;
  208. }
  209. }
  210. return $_default;
  211. }
  212. // Truncate standard view
  213. $result = array();
  214. if (is_array($optionValue)) {
  215. $_truncatedValue = implode("\n", $optionValue);
  216. $_truncatedValue = nl2br($_truncatedValue);
  217. return array('value' => $_truncatedValue);
  218. } else {
  219. if ($maxLength) {
  220. $_truncatedValue = Mage::helper('core/string')->truncate($optionValue, $maxLength, '');
  221. } else {
  222. $_truncatedValue = $optionValue;
  223. }
  224. $_truncatedValue = nl2br($_truncatedValue);
  225. }
  226. $result = array('value' => $_truncatedValue);
  227. if ($maxLength && (Mage::helper('core/string')->strlen($optionValue) > $maxLength)) {
  228. $result['value'] = $result['value'] . $cutReplacer;
  229. $optionValue = nl2br($optionValue);
  230. $result['full_view'] = $optionValue;
  231. }
  232. return $result;
  233. }
  234. }