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

/app/code/core/Mage/Catalog/Model/Product/Option/Type/Select.php

https://bitbucket.org/dnejedly/eaparts
PHP | 316 lines | 201 code | 17 blank | 98 comment | 41 complexity | 169e1249d406c7477f86f523d6103173 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. * Catalog product option select type
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Option_Type_Select extends Mage_Catalog_Model_Product_Option_Type_Default
  34. {
  35. /**
  36. * Validate user input for option
  37. *
  38. * @throws Mage_Core_Exception
  39. * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...)
  40. * @return Mage_Catalog_Model_Product_Option_Type_Default
  41. */
  42. public function validateUserValue($values)
  43. {
  44. parent::validateUserValue($values);
  45. $option = $this->getOption();
  46. $value = $this->getUserValue();
  47. if (empty($value) && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) {
  48. $this->setIsValid(false);
  49. Mage::throwException(Mage::helper('catalog')->__('Please specify the product required option(s).'));
  50. }
  51. if (!$this->_isSingleSelection()) {
  52. $valuesCollection = $option->getOptionValuesByOptionId($value, $this->getProduct()->getStoreId())
  53. ->load();
  54. if ($valuesCollection->count() != count($value)) {
  55. $this->setIsValid(false);
  56. Mage::throwException(Mage::helper('catalog')->__('Please specify the product required option(s).'));
  57. }
  58. }
  59. return $this;
  60. }
  61. /**
  62. * Prepare option value for cart
  63. *
  64. * @throws Mage_Core_Exception
  65. * @return mixed Prepared option value
  66. */
  67. public function prepareForCart()
  68. {
  69. if ($this->getIsValid() && $this->getUserValue()) {
  70. return is_array($this->getUserValue()) ? implode(',', $this->getUserValue()) : $this->getUserValue();
  71. } else {
  72. return null;
  73. }
  74. }
  75. /**
  76. * Return formatted option value for quote option
  77. *
  78. * @param string $optionValue Prepared for cart option value
  79. * @return string
  80. */
  81. public function getFormattedOptionValue($optionValue)
  82. {
  83. if ($this->_formattedOptionValue === null) {
  84. $this->_formattedOptionValue = Mage::helper('core')->htmlEscape(
  85. $this->getEditableOptionValue($optionValue)
  86. );
  87. }
  88. return $this->_formattedOptionValue;
  89. }
  90. /**
  91. * Return printable option value
  92. *
  93. * @param string $optionValue Prepared for cart option value
  94. * @return string
  95. */
  96. public function getPrintableOptionValue($optionValue)
  97. {
  98. return $this->getFormattedOptionValue($optionValue);
  99. }
  100. /**
  101. * Return wrong product configuration message
  102. *
  103. * @return string
  104. */
  105. protected function _getWrongConfigurationMessage()
  106. {
  107. return Mage::helper('catalog')->__('Some of the products below do not have all the required options. Please edit them and configure all the required options.');
  108. }
  109. /**
  110. * Return formatted option value ready to edit, ready to parse
  111. *
  112. * @param string $optionValue Prepared for cart option value
  113. * @return string
  114. */
  115. public function getEditableOptionValue($optionValue)
  116. {
  117. $option = $this->getOption();
  118. $result = '';
  119. if (!$this->_isSingleSelection()) {
  120. foreach (explode(',', $optionValue) as $_value) {
  121. if ($_result = $option->getValueById($_value)) {
  122. $result .= $_result->getTitle() . ', ';
  123. } else {
  124. if ($this->getListener()) {
  125. $this->getListener()
  126. ->setHasError(true)
  127. ->setMessage(
  128. $this->_getWrongConfigurationMessage()
  129. );
  130. $result = '';
  131. break;
  132. }
  133. }
  134. }
  135. $result = Mage::helper('core/string')->substr($result, 0, -2);
  136. } elseif ($this->_isSingleSelection()) {
  137. if ($_result = $option->getValueById($optionValue)) {
  138. $result = $_result->getTitle();
  139. } else {
  140. if ($this->getListener()) {
  141. $this->getListener()
  142. ->setHasError(true)
  143. ->setMessage(
  144. $this->_getWrongConfigurationMessage()
  145. );
  146. }
  147. $result = '';
  148. }
  149. } else {
  150. $result = $optionValue;
  151. }
  152. return $result;
  153. }
  154. /**
  155. * Parse user input value and return cart prepared value, i.e. "one, two" => "1,2"
  156. *
  157. * @param string $optionValue
  158. * @param array $productOptionValues Values for product option
  159. * @return string|null
  160. */
  161. public function parseOptionValue($optionValue, $productOptionValues)
  162. {
  163. $_values = array();
  164. if (!$this->_isSingleSelection()) {
  165. foreach (explode(',', $optionValue) as $_value) {
  166. $_value = trim($_value);
  167. if (array_key_exists($_value, $productOptionValues)) {
  168. $_values[] = $productOptionValues[$_value];
  169. }
  170. }
  171. } elseif ($this->_isSingleSelection() && array_key_exists($optionValue, $productOptionValues)) {
  172. $_values[] = $productOptionValues[$optionValue];
  173. }
  174. if (count($_values)) {
  175. return implode(',', $_values);
  176. } else {
  177. return null;
  178. }
  179. }
  180. /**
  181. * Prepare option value for info buy request
  182. *
  183. * @param string $optionValue
  184. * @return mixed
  185. */
  186. public function prepareOptionValueForRequest($optionValue)
  187. {
  188. if (!$this->_isSingleSelection()) {
  189. return explode(',', $optionValue);
  190. }
  191. return $optionValue;
  192. }
  193. /**
  194. * Return Price for selected option
  195. *
  196. * @param string $optionValue Prepared for cart option value
  197. * @return float
  198. */
  199. public function getOptionPrice($optionValue, $basePrice)
  200. {
  201. $option = $this->getOption();
  202. $result = 0;
  203. if (!$this->_isSingleSelection()) {
  204. foreach(explode(',', $optionValue) as $value) {
  205. if ($_result = $option->getValueById($value)) {
  206. $result += $this->_getChargableOptionPrice(
  207. $_result->getPrice(),
  208. $_result->getPriceType() == 'percent',
  209. $basePrice
  210. );
  211. } else {
  212. if ($this->getListener()) {
  213. $this->getListener()
  214. ->setHasError(true)
  215. ->setMessage(
  216. $this->_getWrongConfigurationMessage()
  217. );
  218. break;
  219. }
  220. }
  221. }
  222. } elseif ($this->_isSingleSelection()) {
  223. if ($_result = $option->getValueById($optionValue)) {
  224. $result = $this->_getChargableOptionPrice(
  225. $_result->getPrice(),
  226. $_result->getPriceType() == 'percent',
  227. $basePrice
  228. );
  229. } else {
  230. if ($this->getListener()) {
  231. $this->getListener()
  232. ->setHasError(true)
  233. ->setMessage(
  234. $this->_getWrongConfigurationMessage()
  235. );
  236. }
  237. }
  238. }
  239. return $result;
  240. }
  241. /**
  242. * Return SKU for selected option
  243. *
  244. * @param string $optionValue Prepared for cart option value
  245. * @param string $skuDelimiter Delimiter for Sku parts
  246. * @return string
  247. */
  248. public function getOptionSku($optionValue, $skuDelimiter)
  249. {
  250. $option = $this->getOption();
  251. if (!$this->_isSingleSelection()) {
  252. $skus = array();
  253. foreach(explode(',', $optionValue) as $value) {
  254. if ($optionSku = $option->getValueById($value)) {
  255. $skus[] = $optionSku->getSku();
  256. } else {
  257. if ($this->getListener()) {
  258. $this->getListener()
  259. ->setHasError(true)
  260. ->setMessage(
  261. $this->_getWrongConfigurationMessage()
  262. );
  263. break;
  264. }
  265. }
  266. }
  267. $result = implode($skuDelimiter, $skus);
  268. } elseif ($this->_isSingleSelection()) {
  269. if ($result = $option->getValueById($optionValue)) {
  270. return $result->getSku();
  271. } else {
  272. if ($this->getListener()) {
  273. $this->getListener()
  274. ->setHasError(true)
  275. ->setMessage(
  276. $this->_getWrongConfigurationMessage()
  277. );
  278. }
  279. return '';
  280. }
  281. } else {
  282. $result = parent::getOptionSku($optionValue, $skuDelimiter);
  283. }
  284. return $result;
  285. }
  286. /**
  287. * Check if option has single or multiple values selection
  288. *
  289. * @return boolean
  290. */
  291. protected function _isSingleSelection()
  292. {
  293. $_single = array(
  294. Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN,
  295. Mage_Catalog_Model_Product_Option::OPTION_TYPE_RADIO
  296. );
  297. return in_array($this->getOption()->getType(), $_single);
  298. }
  299. }