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

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

https://bitbucket.org/sevenly/magento-ce
PHP | 491 lines | 256 code | 46 blank | 189 comment | 28 complexity | b998f1ca9ab026604c596b5f9b455161 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 model
  28. *
  29. * @method Mage_Catalog_Model_Resource_Product_Option _getResource()
  30. * @method Mage_Catalog_Model_Resource_Product_Option getResource()
  31. * @method int getProductId()
  32. * @method Mage_Catalog_Model_Product_Option setProductId(int $value)
  33. * @method string getType()
  34. * @method Mage_Catalog_Model_Product_Option setType(string $value)
  35. * @method int getIsRequire()
  36. * @method Mage_Catalog_Model_Product_Option setIsRequire(int $value)
  37. * @method string getSku()
  38. * @method Mage_Catalog_Model_Product_Option setSku(string $value)
  39. * @method int getMaxCharacters()
  40. * @method Mage_Catalog_Model_Product_Option setMaxCharacters(int $value)
  41. * @method string getFileExtension()
  42. * @method Mage_Catalog_Model_Product_Option setFileExtension(string $value)
  43. * @method int getImageSizeX()
  44. * @method Mage_Catalog_Model_Product_Option setImageSizeX(int $value)
  45. * @method int getImageSizeY()
  46. * @method Mage_Catalog_Model_Product_Option setImageSizeY(int $value)
  47. * @method int getSortOrder()
  48. * @method Mage_Catalog_Model_Product_Option setSortOrder(int $value)
  49. *
  50. * @category Mage
  51. * @package Mage_Catalog
  52. * @author Magento Core Team <core@magentocommerce.com>
  53. */
  54. class Mage_Catalog_Model_Product_Option extends Mage_Core_Model_Abstract
  55. {
  56. const OPTION_GROUP_TEXT = 'text';
  57. const OPTION_GROUP_FILE = 'file';
  58. const OPTION_GROUP_SELECT = 'select';
  59. const OPTION_GROUP_DATE = 'date';
  60. const OPTION_TYPE_FIELD = 'field';
  61. const OPTION_TYPE_AREA = 'area';
  62. const OPTION_TYPE_FILE = 'file';
  63. const OPTION_TYPE_DROP_DOWN = 'drop_down';
  64. const OPTION_TYPE_RADIO = 'radio';
  65. const OPTION_TYPE_CHECKBOX = 'checkbox';
  66. const OPTION_TYPE_MULTIPLE = 'multiple';
  67. const OPTION_TYPE_DATE = 'date';
  68. const OPTION_TYPE_DATE_TIME = 'date_time';
  69. const OPTION_TYPE_TIME = 'time';
  70. protected $_product;
  71. protected $_options = array();
  72. protected $_valueInstance;
  73. protected $_values = array();
  74. protected function _construct()
  75. {
  76. $this->_init('catalog/product_option');
  77. }
  78. /**
  79. * Add value of option to values array
  80. *
  81. * @param Mage_Catalog_Model_Product_Option_Value $value
  82. * @return Mage_Catalog_Model_Product_Option
  83. */
  84. public function addValue(Mage_Catalog_Model_Product_Option_Value $value)
  85. {
  86. $this->_values[$value->getId()] = $value;
  87. return $this;
  88. }
  89. /**
  90. * Get value by given id
  91. *
  92. * @param int $valueId
  93. * @return Mage_Catalog_Model_Product_Option_Value
  94. */
  95. public function getValueById($valueId)
  96. {
  97. if (isset($this->_values[$valueId])) {
  98. return $this->_values[$valueId];
  99. }
  100. return null;
  101. }
  102. public function getValues()
  103. {
  104. return $this->_values;
  105. }
  106. /**
  107. * Retrieve value instance
  108. *
  109. * @return Mage_Catalog_Model_Product_Option_Value
  110. */
  111. public function getValueInstance()
  112. {
  113. if (!$this->_valueInstance) {
  114. $this->_valueInstance = Mage::getSingleton('catalog/product_option_value');
  115. }
  116. return $this->_valueInstance;
  117. }
  118. /**
  119. * Add option for save it
  120. *
  121. * @param array $option
  122. * @return Mage_Catalog_Model_Product_Option
  123. */
  124. public function addOption($option)
  125. {
  126. $this->_options[] = $option;
  127. return $this;
  128. }
  129. /**
  130. * Get all options
  131. *
  132. * @return array
  133. */
  134. public function getOptions()
  135. {
  136. return $this->_options;
  137. }
  138. /**
  139. * Set options for array
  140. *
  141. * @param array $options
  142. * @return Mage_Catalog_Model_Product_Option
  143. */
  144. public function setOptions($options)
  145. {
  146. $this->_options = $options;
  147. return $this;
  148. }
  149. /**
  150. * Set options to empty array
  151. *
  152. * @return Mage_Catalog_Model_Product_Option
  153. */
  154. public function unsetOptions()
  155. {
  156. $this->_options = array();
  157. return $this;
  158. }
  159. /**
  160. * Retrieve product instance
  161. *
  162. * @return Mage_Catalog_Model_Product
  163. */
  164. public function getProduct()
  165. {
  166. return $this->_product;
  167. }
  168. /**
  169. * Set product instance
  170. *
  171. * @param Mage_Catalog_Model_Product $product
  172. * @return Mage_Catalog_Model_Product_Option
  173. */
  174. public function setProduct(Mage_Catalog_Model_Product $product = null)
  175. {
  176. $this->_product = $product;
  177. return $this;
  178. }
  179. /**
  180. * Get group name of option by given option type
  181. *
  182. * @param string $type
  183. * @return string
  184. */
  185. public function getGroupByType($type = null)
  186. {
  187. if (is_null($type)) {
  188. $type = $this->getType();
  189. }
  190. $optionGroupsToTypes = array(
  191. self::OPTION_TYPE_FIELD => self::OPTION_GROUP_TEXT,
  192. self::OPTION_TYPE_AREA => self::OPTION_GROUP_TEXT,
  193. self::OPTION_TYPE_FILE => self::OPTION_GROUP_FILE,
  194. self::OPTION_TYPE_DROP_DOWN => self::OPTION_GROUP_SELECT,
  195. self::OPTION_TYPE_RADIO => self::OPTION_GROUP_SELECT,
  196. self::OPTION_TYPE_CHECKBOX => self::OPTION_GROUP_SELECT,
  197. self::OPTION_TYPE_MULTIPLE => self::OPTION_GROUP_SELECT,
  198. self::OPTION_TYPE_DATE => self::OPTION_GROUP_DATE,
  199. self::OPTION_TYPE_DATE_TIME => self::OPTION_GROUP_DATE,
  200. self::OPTION_TYPE_TIME => self::OPTION_GROUP_DATE,
  201. );
  202. return isset($optionGroupsToTypes[$type])?$optionGroupsToTypes[$type]:'';
  203. }
  204. /**
  205. * Group model factory
  206. *
  207. * @param string $type Option type
  208. * @return Mage_Catalog_Model_Product_Option_Group_Abstract
  209. */
  210. public function groupFactory($type)
  211. {
  212. $group = $this->getGroupByType($type);
  213. if (!empty($group)) {
  214. return Mage::getModel('catalog/product_option_type_' . $group);
  215. }
  216. Mage::throwException(Mage::helper('catalog')->__('Wrong option type to get group instance.'));
  217. }
  218. /**
  219. * Save options.
  220. *
  221. * @return Mage_Catalog_Model_Product_Option
  222. */
  223. public function saveOptions()
  224. {
  225. foreach ($this->getOptions() as $option) {
  226. $this->setData($option)
  227. ->setData('product_id', $this->getProduct()->getId())
  228. ->setData('store_id', $this->getProduct()->getStoreId());
  229. if ($this->getData('option_id') == '0') {
  230. $this->unsetData('option_id');
  231. } else {
  232. $this->setId($this->getData('option_id'));
  233. }
  234. $isEdit = (bool)$this->getId()? true:false;
  235. if ($this->getData('is_delete') == '1') {
  236. if ($isEdit) {
  237. $this->getValueInstance()->deleteValue($this->getId());
  238. $this->deletePrices($this->getId());
  239. $this->deleteTitles($this->getId());
  240. $this->delete();
  241. }
  242. } else {
  243. if ($this->getData('previous_type') != '') {
  244. $previousType = $this->getData('previous_type');
  245. /**
  246. * if previous option has different group from one is came now
  247. * need to remove all data of previous group
  248. */
  249. if ($this->getGroupByType($previousType) != $this->getGroupByType($this->getData('type'))) {
  250. switch ($this->getGroupByType($previousType)) {
  251. case self::OPTION_GROUP_SELECT:
  252. $this->unsetData('values');
  253. if ($isEdit) {
  254. $this->getValueInstance()->deleteValue($this->getId());
  255. }
  256. break;
  257. case self::OPTION_GROUP_FILE:
  258. $this->setData('file_extension', '');
  259. $this->setData('image_size_x', '0');
  260. $this->setData('image_size_y', '0');
  261. break;
  262. case self::OPTION_GROUP_TEXT:
  263. $this->setData('max_characters', '0');
  264. break;
  265. case self::OPTION_GROUP_DATE:
  266. break;
  267. }
  268. if ($this->getGroupByType($this->getData('type')) == self::OPTION_GROUP_SELECT) {
  269. $this->setData('sku', '');
  270. $this->unsetData('price');
  271. $this->unsetData('price_type');
  272. if ($isEdit) {
  273. $this->deletePrices($this->getId());
  274. }
  275. }
  276. }
  277. }
  278. $this->save(); }
  279. }//eof foreach()
  280. return $this;
  281. }
  282. protected function _afterSave()
  283. {
  284. $this->getValueInstance()->unsetValues();
  285. if (is_array($this->getData('values'))) {
  286. foreach ($this->getData('values') as $value) {
  287. $this->getValueInstance()->addValue($value);
  288. }
  289. $this->getValueInstance()->setOption($this)
  290. ->saveValues();
  291. } elseif ($this->getGroupByType($this->getType()) == self::OPTION_GROUP_SELECT) {
  292. Mage::throwException(Mage::helper('catalog')->__('Select type options required values rows.'));
  293. }
  294. return parent::_afterSave();
  295. }
  296. /**
  297. * Return price. If $flag is true and price is percent
  298. * return converted percent to price
  299. *
  300. * @param bool $flag
  301. * @return decimal
  302. */
  303. public function getPrice($flag=false)
  304. {
  305. if ($flag && $this->getPriceType() == 'percent') {
  306. $basePrice = $this->getProduct()->getFinalPrice();
  307. $price = $basePrice*($this->_getData('price')/100);
  308. return $price;
  309. }
  310. return $this->_getData('price');
  311. }
  312. /**
  313. * Delete prices of option
  314. *
  315. * @param int $option_id
  316. * @return Mage_Catalog_Model_Product_Option
  317. */
  318. public function deletePrices($option_id)
  319. {
  320. $this->getResource()->deletePrices($option_id);
  321. return $this;
  322. }
  323. /**
  324. * Delete titles of option
  325. *
  326. * @param int $option_id
  327. * @return Mage_Catalog_Model_Product_Option
  328. */
  329. public function deleteTitles($option_id)
  330. {
  331. $this->getResource()->deleteTitles($option_id);
  332. return $this;
  333. }
  334. /**
  335. * get Product Option Collection
  336. *
  337. * @param Mage_Catalog_Model_Product $product
  338. * @return Mage_Catalog_Model_Resource_Product_Option_Collection
  339. */
  340. public function getProductOptionCollection(Mage_Catalog_Model_Product $product)
  341. {
  342. $collection = $this->getCollection()
  343. ->addFieldToFilter('product_id', $product->getId())
  344. ->addTitleToResult($product->getStoreId())
  345. ->addPriceToResult($product->getStoreId())
  346. ->setOrder('sort_order', 'asc')
  347. ->setOrder('title', 'asc');
  348. if ($this->getAddRequiredFilter()) {
  349. $collection->addRequiredFilter($this->getAddRequiredFilterValue());
  350. }
  351. $collection->addValuesToResult($product->getStoreId());
  352. return $collection;
  353. }
  354. /**
  355. * Get collection of values for current option
  356. *
  357. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Option_Value_Collection
  358. */
  359. public function getValuesCollection()
  360. {
  361. $collection = $this->getValueInstance()
  362. ->getValuesCollection($this);
  363. return $collection;
  364. }
  365. /**
  366. * Get collection of values by given option ids
  367. *
  368. * @param array $optionIds
  369. * @param int $store_id
  370. * @return unknown
  371. */
  372. public function getOptionValuesByOptionId($optionIds, $store_id)
  373. {
  374. $collection = Mage::getModel('catalog/product_option_value')
  375. ->getValuesByOption($optionIds, $this->getId(), $store_id);
  376. return $collection;
  377. }
  378. /**
  379. * Prepare array of options for duplicate
  380. *
  381. * @return array
  382. */
  383. public function prepareOptionForDuplicate()
  384. {
  385. $this->setProductId(null);
  386. $this->setOptionId(null);
  387. $newOption = $this->__toArray();
  388. if ($_values = $this->getValues()) {
  389. $newValuesArray = array();
  390. foreach ($_values as $_value) {
  391. $newValuesArray[] = $_value->prepareValueForDuplicate();
  392. }
  393. $newOption['values'] = $newValuesArray;
  394. }
  395. return $newOption;
  396. }
  397. /**
  398. * Duplicate options for product
  399. *
  400. * @param int $oldProductId
  401. * @param int $newProductId
  402. * @return Mage_Catalog_Model_Product_Option
  403. */
  404. public function duplicate($oldProductId, $newProductId)
  405. {
  406. $this->getResource()->duplicate($this, $oldProductId, $newProductId);
  407. return $this;
  408. }
  409. /**
  410. * Retrieve option searchable data
  411. *
  412. * @param int $productId
  413. * @param int $storeId
  414. * @return array
  415. */
  416. public function getSearchableData($productId, $storeId)
  417. {
  418. return $this->_getResource()->getSearchableData($productId, $storeId);
  419. }
  420. /**
  421. * Clearing object's data
  422. *
  423. * @return Mage_Catalog_Model_Product_Option
  424. */
  425. protected function _clearData()
  426. {
  427. $this->_data = array();
  428. $this->_values = array();
  429. return $this;
  430. }
  431. /**
  432. * Clearing cyclic references
  433. *
  434. * @return Mage_Catalog_Model_Product_Option
  435. */
  436. protected function _clearReferences()
  437. {
  438. if (!empty($this->_values)) {
  439. foreach ($this->_values as $value) {
  440. $value->unsetOption();
  441. }
  442. }
  443. return $this;
  444. }
  445. }