PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/classes/XLite/Module/CDev/ProductOptions/Model/Product.php

https://github.com/inhale/core
PHP | 261 lines | 120 code | 28 blank | 113 comment | 17 complexity | b62a9dfba2223a9df58599cf84a97892 MD5 | raw file
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * @category LiteCommerce
  17. * @package XLite
  18. * @subpackage Model
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @version GIT: $Id: 576eff983e566544074b656fc9948314d1145c76 $
  23. * @link http://www.litecommerce.com/
  24. * @see ____file_see____
  25. * @since 3.0.0
  26. */
  27. namespace XLite\Module\CDev\ProductOptions\Model;
  28. /**
  29. * Product
  30. *
  31. * @package XLite
  32. * @see ____class_see____
  33. * @since 3.0.0
  34. */
  35. class Product extends \XLite\Model\Product implements \XLite\Base\IDecorator
  36. {
  37. /**
  38. * Option groups (relation)
  39. *
  40. * @var \Doctrine\Common\Collections\ArrayCollection
  41. * @access protected
  42. * @see ____var_see____
  43. * @since 3.0.0
  44. *
  45. * @OneToMany (targetEntity="XLite\Module\CDev\ProductOptions\Model\OptionGroup", mappedBy="product", cascade={"all"})
  46. */
  47. protected $optionGroups;
  48. /**
  49. * Product options list (cache)
  50. *
  51. * @var array
  52. * @access protected
  53. * @see ____var_see____
  54. * @since 3.0.0
  55. */
  56. protected $productOptions = null;
  57. /**
  58. * Check - has product options list or not
  59. *
  60. * @return boolean
  61. * @access public
  62. * @see ____func_see____
  63. * @since 3.0.0
  64. */
  65. public function hasOptions()
  66. {
  67. return 0 < count($this->optionGroups);
  68. }
  69. /**
  70. * Get product options list
  71. *
  72. * @return array(\XLite\Module\CDev\ProductOptions\Model\OptionGroup)
  73. * @access public
  74. * @see ____func_see____
  75. * @since 3.0.0
  76. */
  77. public function getActiveOptions()
  78. {
  79. if (!isset($this->productOptions)) {
  80. $this->productOptions = \XLite\Core\Database::getRepo('XLite\Module\CDev\ProductOptions\Model\OptionGroup')
  81. ->findActiveByProductId($this->getProductId());
  82. }
  83. return $this->productOptions;
  84. }
  85. /**
  86. * Check - display price modifier or not
  87. *
  88. * @return boolean
  89. * @access public
  90. * @see ____func_see____
  91. * @since 3.0.0
  92. */
  93. public function isDisplayPriceModifier()
  94. {
  95. return true;
  96. }
  97. /**
  98. * Prepare options
  99. *
  100. * @param array $options Request-based selected options
  101. *
  102. * @return array|void
  103. * @access public
  104. * @see ____func_see____
  105. * @since 3.0.0
  106. */
  107. public function prepareOptions(array $options)
  108. {
  109. $prepared = array();
  110. foreach ($options as $groupId => $data) {
  111. $optionGroup = \XLite\Core\Database::getRepo('\XLite\Module\CDev\ProductOptions\Model\OptionGroup')
  112. ->findOneByGroupIdAndProductId($groupId, $this->getProductId());
  113. if (!isset($optionGroup)) {
  114. $prepared = null;
  115. break;
  116. }
  117. if ($optionGroup->getType() == $optionGroup::GROUP_TYPE) {
  118. $option = \XLite\Core\Database::getRepo('\XLite\Module\CDev\ProductOptions\Model\Option')
  119. ->find(intval($data));
  120. if (
  121. !$option
  122. || $option->getGroup()->getGroupId() != $optionGroup->getGroupId()
  123. ) {
  124. $prepared = null;
  125. break;
  126. }
  127. $prepared[$optionGroup->getGroupId()] = array(
  128. 'option' => $option,
  129. 'value' => intval($data),
  130. );
  131. } else {
  132. $prepared[$optionGroup->getGroupId()] = array(
  133. 'option' => null,
  134. 'value' => $data,
  135. );
  136. }
  137. }
  138. // Update list from default list
  139. if (is_array($prepared)) {
  140. foreach ($this->getDefaultProductOptions() as $groupId => $data) {
  141. if (!isset($prepared[$groupId])) {
  142. $prepared[$groupId] = $data;
  143. }
  144. }
  145. }
  146. return $prepared;
  147. }
  148. /**
  149. * Get default product options
  150. *
  151. * @return array
  152. * @access public
  153. * @see ____func_see____
  154. * @since 3.0.0
  155. */
  156. public function getDefaultProductOptions()
  157. {
  158. $list = $this->getActiveOptions();
  159. $ids = array();
  160. foreach ($list as $optionGroup) {
  161. if ($optionGroup::GROUP_TYPE == $optionGroup->getType()) {
  162. $ids[$optionGroup->getGroupId()] = array(
  163. 'start' => 0,
  164. 'limit' => count($optionGroup->getActiveOptions()) - 1,
  165. );
  166. }
  167. }
  168. $cnt = 0;
  169. do {
  170. $cntCurrent = $cnt;
  171. foreach ($ids as $i => $id) {
  172. if ($id['limit'] > $cntCurrent) {
  173. $ids[$i]['start'] = $cntCurrent;
  174. $cntCurrent = 0;
  175. } else {
  176. $ids[$i]['start'] = $id['limit'];
  177. $cntCurrent -= $id['limit'];
  178. }
  179. }
  180. $options = array();
  181. foreach ($list as $optionGroup) {
  182. $option = $optionGroup::GROUP_TYPE == $optionGroup->getType()
  183. ? $optionGroup->getDefaultOption($ids[$optionGroup->getGroupId()]['start'])
  184. : null;
  185. $value = $optionGroup->getDefaultPlainValue(
  186. isset($ids[$optionGroup->getGroupId()]) ? $ids[$optionGroup->getGroupId()]['start'] : 0
  187. );
  188. $options[$optionGroup->getGroupId()] = array(
  189. 'option' => $option,
  190. 'value' => $value,
  191. );
  192. }
  193. $cnt++;
  194. } while (!$this->checkOptionsException($options));
  195. return $options;
  196. }
  197. /**
  198. * Check options exception
  199. *
  200. * @param array $options Prepared array
  201. *
  202. * @return boolean
  203. * @access public
  204. * @see ____func_see____
  205. * @since 3.0.0
  206. */
  207. public function checkOptionsException(array $options)
  208. {
  209. $ids = array();
  210. foreach ($options as $groupId => $data) {
  211. if (isset($data['option'])) {
  212. $ids[] = $data['option']->getOptionId();
  213. }
  214. }
  215. return \XLite\Core\Database::getRepo('\XLite\Module\CDev\ProductOptions\Model\OptionException')
  216. ->checkOptions($ids);
  217. }
  218. /**
  219. * Constructor
  220. *
  221. * @param array $data Entity properties
  222. *
  223. * @return void
  224. * @access public
  225. * @see ____func_see____
  226. * @since 3.0.0
  227. */
  228. public function __construct(array $data = array())
  229. {
  230. $this->optionGroups = new \Doctrine\Common\Collections\ArrayCollection();
  231. parent::__construct($data);
  232. }
  233. }