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

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

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