PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/dnejedly/eaparts
PHP | 327 lines | 207 code | 13 blank | 107 comment | 22 complexity | e6ff66982bac68e0b7b56c0c82256249 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 options api
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Option_Api extends Mage_Catalog_Model_Api_Resource
  34. {
  35. /**
  36. * Add custom option to product
  37. *
  38. * @param string $productId
  39. * @param array $data
  40. * @param int|string|null $store
  41. * @return bool $isAdded
  42. */
  43. public function add($productId, $data, $store = null)
  44. {
  45. $product = $this->_getProduct($productId, $store, null);
  46. if (!(is_array($data['additional_fields']) and count($data['additional_fields']))) {
  47. $this->_fault('invalid_data');
  48. }
  49. if (!$this->_isTypeAllowed($data['type'])) {
  50. $this->_fault('invalid_type');
  51. }
  52. $this->_prepareAdditionalFields(
  53. $data,
  54. $product->getOptionInstance()->getGroupByType($data['type'])
  55. );
  56. $this->_saveProductCustomOption($product, $data);
  57. return true;
  58. }
  59. /**
  60. * Update product custom option data
  61. *
  62. * @param string $optionId
  63. * @param array $data
  64. * @param int|string|null $store
  65. * @return bool
  66. */
  67. public function update($optionId, $data, $store = null)
  68. {
  69. /** @var $option Mage_Catalog_Model_Product_Option */
  70. $option = Mage::getModel('catalog/product_option')->load($optionId);
  71. if (!$option->getId()) {
  72. $this->_fault('option_not_exists');
  73. }
  74. $product = $this->_getProduct($option->getProductId(), $store, null);
  75. $option = $product->getOptionById($optionId);
  76. if (isset($data['type']) and !$this->_isTypeAllowed($data['type'])) {
  77. $this->_fault('invalid_type');
  78. }
  79. if (isset($data['additional_fields'])) {
  80. $this->_prepareAdditionalFields(
  81. $data,
  82. $option->getGroupByType()
  83. );
  84. }
  85. foreach ($option->getValues() as $valueId => $value) {
  86. if(isset($data['values'][$valueId])) {
  87. $data['values'][$valueId] = array_merge($value->getData(), $data['values'][$valueId]);
  88. }
  89. }
  90. $data = array_merge($option->getData(), $data);
  91. $this->_saveProductCustomOption($product, $data);
  92. return true;
  93. }
  94. /**
  95. * Prepare custom option data for saving by model. Used for custom option add and update
  96. *
  97. * @param array $data
  98. * @param string $groupType
  99. * @return void
  100. */
  101. protected function _prepareAdditionalFields(&$data, $groupType)
  102. {
  103. if (is_array($data['additional_fields'])) {
  104. if ($groupType != Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
  105. // reset can be used as there should be the only
  106. // element in 'additional_fields' for options of all types except those from Select group
  107. $field = reset($data['additional_fields']);
  108. if (!(is_array($field) and count($field))) {
  109. $this->_fault('invalid_data');
  110. } else {
  111. foreach ($field as $key => $value) {
  112. $data[$key] = $value;
  113. }
  114. }
  115. } else {
  116. // convert Select rows array to appropriate format for saving in the model
  117. foreach ($data['additional_fields'] as $row) {
  118. if (!(is_array($row) and count($row))) {
  119. $this->_fault('invalid_data');
  120. } else {
  121. foreach ($row as $key => $value) {
  122. $row[$key] = Mage::helper('catalog')->stripTags($value);
  123. }
  124. if (!empty($row['value_id'])) {
  125. // map 'value_id' to 'option_type_id'
  126. $row['option_type_id'] = $row['value_id'];
  127. unset($row['value_id']);
  128. $data['values'][$row['option_type_id']] = $row;
  129. } else {
  130. $data['values'][] = $row;
  131. }
  132. }
  133. }
  134. }
  135. }
  136. unset($data['additional_fields']);
  137. }
  138. /**
  139. * Save product custom option data. Used for custom option add and update.
  140. *
  141. * @param Mage_Catalog_Model_Product $product
  142. * @param array $data
  143. * @return void
  144. */
  145. protected function _saveProductCustomOption($product, $data)
  146. {
  147. foreach ($data as $key => $value) {
  148. if (is_string($value)) {
  149. $data[$key] = Mage::helper('catalog')->stripTags($value);
  150. }
  151. }
  152. // setProductOptions expects data to be an array of options arrays
  153. $data = array($data);
  154. if (!$product->getOptionsReadonly()) {
  155. $product->setProductOptions($data);
  156. }
  157. $product->setCanSaveCustomOptions(!$product->getOptionsReadonly());
  158. try {
  159. // an empty request can be set as event parameter
  160. // because it is not used for options changing in observers
  161. Mage::dispatchEvent(
  162. 'catalog_product_prepare_save',
  163. array('product' => $product, 'request' => new Mage_Core_Controller_Request_Http())
  164. );
  165. $product->save();
  166. } catch (Exception $e) {
  167. $this->_fault('save_option_error', $e->getMessage());
  168. }
  169. }
  170. /**
  171. * Read list of possible custom option types from module config
  172. *
  173. * @return array
  174. */
  175. public function types()
  176. {
  177. $path = Mage_Adminhtml_Model_System_Config_Source_Product_Options_Type::PRODUCT_OPTIONS_GROUPS_PATH;
  178. $types = array();
  179. foreach (Mage::getConfig()->getNode($path)->children() as $group) {
  180. $groupTypes = Mage::getConfig()->getNode($path . '/' . $group->getName() . '/types')->children();
  181. /** @var $type Mage_Core_Model_Config_Element */
  182. foreach($groupTypes as $type){
  183. $labelPath = $path . '/' . $group->getName() . '/types/' . $type->getName() . '/label';
  184. $types[] = array(
  185. 'label' => (string) Mage::getConfig()->getNode($labelPath),
  186. 'value' => $type->getName()
  187. );
  188. }
  189. }
  190. return $types;
  191. }
  192. /**
  193. * Get full information about custom option in product
  194. *
  195. * @param int|string $optionId
  196. * @param int|string|null $store
  197. * @return array
  198. */
  199. public function info($optionId, $store = null)
  200. {
  201. /** @var $option Mage_Catalog_Model_Product_Option */
  202. $option = Mage::getModel('catalog/product_option')->load($optionId);
  203. if (!$option->getId()) {
  204. $this->_fault('option_not_exists');
  205. }
  206. /** @var $product Mage_Catalog_Model_Product */
  207. $product = $this->_getProduct($option->getProductId(), $store, null);
  208. $option = $product->getOptionById($optionId);
  209. $result = array(
  210. 'title' => $option->getTitle(),
  211. 'type' => $option->getType(),
  212. 'is_require' => $option->getIsRequire(),
  213. 'sort_order' => $option->getSortOrder(),
  214. // additional_fields should be two-dimensional array for all option types
  215. 'additional_fields' => array(
  216. array(
  217. 'price' => $option->getPrice(),
  218. 'price_type' => $option->getPriceType(),
  219. 'sku' => $option->getSku()
  220. )
  221. )
  222. );
  223. // Set additional fields to each type group
  224. switch ($option->getGroupByType()) {
  225. case Mage_Catalog_Model_Product_Option::OPTION_GROUP_TEXT:
  226. $result['additional_fields'][0]['max_characters'] = $option->getMaxCharacters();
  227. break;
  228. case Mage_Catalog_Model_Product_Option::OPTION_GROUP_FILE:
  229. $result['additional_fields'][0]['file_extension'] = $option->getFileExtension();
  230. $result['additional_fields'][0]['image_size_x'] = $option->getImageSizeX();
  231. $result['additional_fields'][0]['image_size_y'] = $option->getImageSizeY();
  232. break;
  233. case Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT:
  234. $result['additional_fields'] = array();
  235. foreach ($option->getValuesCollection() as $value) {
  236. $result['additional_fields'][] = array(
  237. 'value_id' => $value->getId(),
  238. 'title' => $value->getTitle(),
  239. 'price' => $value->getPrice(),
  240. 'price_type' => $value->getPriceType(),
  241. 'sku' => $value->getSku(),
  242. 'sort_order' => $value->getSortOrder()
  243. );
  244. }
  245. break;
  246. default:
  247. break;
  248. }
  249. return $result;
  250. }
  251. /**
  252. * Retrieve list of product custom options
  253. *
  254. * @param string $productId
  255. * @param int|string|null $store
  256. * @return array
  257. */
  258. public function items($productId, $store = null)
  259. {
  260. $result = array();
  261. $product = $this->_getProduct($productId, $store, null);
  262. /** @var $option Mage_Catalog_Model_Product_Option */
  263. foreach ($product->getProductOptionsCollection() as $option) {
  264. $result[] = array(
  265. 'option_id' => $option->getId(),
  266. 'title' => $option->getTitle(),
  267. 'type' => $option->getType(),
  268. 'is_require' => $option->getIsRequire(),
  269. 'sort_order' => $option->getSortOrder()
  270. );
  271. }
  272. return $result;
  273. }
  274. /**
  275. * Remove product custom option
  276. *
  277. * @param string $optionId
  278. * @return boolean
  279. */
  280. public function remove($optionId)
  281. {
  282. /** @var $option Mage_Catalog_Model_Product_Option */
  283. $option = Mage::getModel('catalog/product_option')->load($optionId);
  284. if (!$option->getId()) {
  285. $this->_fault('option_not_exists');
  286. }
  287. try {
  288. $option->getValueInstance()->deleteValue($optionId);
  289. $option->deletePrices($optionId);
  290. $option->deleteTitles($optionId);
  291. $option->delete();
  292. } catch (Exception $e){
  293. $this->fault('delete_option_error');
  294. }
  295. return true;
  296. }
  297. /**
  298. * Check is type in allowed set
  299. *
  300. * @param string $type
  301. * @return bool
  302. */
  303. protected function _isTypeAllowed($type)
  304. {
  305. $allowedTypes = array();
  306. foreach($this->types() as $optionType){
  307. $allowedTypes[] = $optionType['value'];
  308. }
  309. if (!in_array($type, $allowedTypes)) {
  310. return false;
  311. }
  312. return true;
  313. }
  314. }