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

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

https://github.com/rgranadino/magento-mirror
PHP | 335 lines | 190 code | 41 blank | 104 comment | 30 complexity | 6ec56d074334aecaf04ff87758c44a97 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) 2011 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 api
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Api extends Mage_Catalog_Model_Api_Resource
  34. {
  35. protected $_filtersMap = array(
  36. 'product_id' => 'entity_id',
  37. 'set' => 'attribute_set_id',
  38. 'type' => 'type_id'
  39. );
  40. public function __construct()
  41. {
  42. $this->_storeIdSessionField = 'product_store_id';
  43. $this->_ignoredAttributeTypes[] = 'gallery';
  44. $this->_ignoredAttributeTypes[] = 'media_image';
  45. }
  46. /**
  47. * Retrieve list of products with basic info (id, sku, type, set, name)
  48. *
  49. * @param array $filters
  50. * @param string|int $store
  51. * @return array
  52. */
  53. public function items($filters = null, $store = null)
  54. {
  55. $collection = Mage::getModel('catalog/product')->getCollection()
  56. ->addStoreFilter($this->_getStoreId($store))
  57. ->addAttributeToSelect('name');
  58. if (is_array($filters)) {
  59. try {
  60. foreach ($filters as $field => $value) {
  61. if (isset($this->_filtersMap[$field])) {
  62. $field = $this->_filtersMap[$field];
  63. }
  64. $collection->addFieldToFilter($field, $value);
  65. }
  66. } catch (Mage_Core_Exception $e) {
  67. $this->_fault('filters_invalid', $e->getMessage());
  68. }
  69. }
  70. $result = array();
  71. foreach ($collection as $product) {
  72. // $result[] = $product->getData();
  73. $result[] = array( // Basic product data
  74. 'product_id' => $product->getId(),
  75. 'sku' => $product->getSku(),
  76. 'name' => $product->getName(),
  77. 'set' => $product->getAttributeSetId(),
  78. 'type' => $product->getTypeId(),
  79. 'category_ids' => $product->getCategoryIds()
  80. );
  81. }
  82. return $result;
  83. }
  84. /**
  85. * Retrieve product info
  86. *
  87. * @param int|string $productId
  88. * @param string|int $store
  89. * @param array $attributes
  90. * @return array
  91. */
  92. public function info($productId, $store = null, $attributes = null, $identifierType = null)
  93. {
  94. $product = $this->_getProduct($productId, $store, $identifierType);
  95. if (!$product->getId()) {
  96. $this->_fault('not_exists');
  97. }
  98. $result = array( // Basic product data
  99. 'product_id' => $product->getId(),
  100. 'sku' => $product->getSku(),
  101. 'set' => $product->getAttributeSetId(),
  102. 'type' => $product->getTypeId(),
  103. 'categories' => $product->getCategoryIds(),
  104. 'websites' => $product->getWebsiteIds()
  105. );
  106. foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
  107. if ($this->_isAllowedAttribute($attribute, $attributes)) {
  108. $result[$attribute->getAttributeCode()] = $product->getData(
  109. $attribute->getAttributeCode());
  110. }
  111. }
  112. return $result;
  113. }
  114. /**
  115. * Create new product.
  116. *
  117. * @param string $type
  118. * @param int $set
  119. * @param string $sku
  120. * @param array $productData
  121. * @param string $store
  122. * @return int
  123. */
  124. public function create($type, $set, $sku, $productData, $store = null)
  125. {
  126. if (!$type || !$set || !$sku) {
  127. $this->_fault('data_invalid');
  128. }
  129. /** @var $product Mage_Catalog_Model_Product */
  130. $product = Mage::getModel('catalog/product');
  131. $product->setStoreId($this->_getStoreId($store))
  132. ->setAttributeSetId($set)
  133. ->setTypeId($type)
  134. ->setSku($sku);
  135. if (isset($productData['website_ids']) && is_array($productData['website_ids'])) {
  136. $product->setWebsiteIds($productData['website_ids']);
  137. }
  138. foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
  139. if ($this->_isAllowedAttribute($attribute)
  140. && isset($productData[$attribute->getAttributeCode()])) {
  141. $product->setData(
  142. $attribute->getAttributeCode(),
  143. $productData[$attribute->getAttributeCode()]
  144. );
  145. }
  146. }
  147. $this->_prepareDataForSave($product, $productData);
  148. try {
  149. /**
  150. * @todo implement full validation process with errors returning which are ignoring now
  151. * @todo see Mage_Catalog_Model_Product::validate()
  152. */
  153. if (is_array($errors = $product->validate())) {
  154. $strErrors = array();
  155. foreach($errors as $code=>$error) {
  156. $strErrors[] = ($error === true)? Mage::helper('catalog')->__('Attribute "%s" is invalid.', $code) : $error;
  157. }
  158. $this->_fault('data_invalid', implode("\n", $strErrors));
  159. }
  160. $product->save();
  161. } catch (Mage_Core_Exception $e) {
  162. $this->_fault('data_invalid', $e->getMessage());
  163. }
  164. return $product->getId();
  165. }
  166. /**
  167. * Update product data
  168. *
  169. * @param int|string $productId
  170. * @param array $productData
  171. * @param string|int $store
  172. * @return boolean
  173. */
  174. public function update($productId, $productData, $store = null, $identifierType = null)
  175. {
  176. $product = $this->_getProduct($productId, $store, $identifierType);
  177. if (!$product->getId()) {
  178. $this->_fault('not_exists');
  179. }
  180. if (isset($productData['website_ids']) && is_array($productData['website_ids'])) {
  181. $product->setWebsiteIds($productData['website_ids']);
  182. }
  183. foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
  184. if ($this->_isAllowedAttribute($attribute)
  185. && isset($productData[$attribute->getAttributeCode()])) {
  186. $product->setData(
  187. $attribute->getAttributeCode(),
  188. $productData[$attribute->getAttributeCode()]
  189. );
  190. }
  191. }
  192. $this->_prepareDataForSave($product, $productData);
  193. try {
  194. /**
  195. * @todo implement full validation process with errors returning which are ignoring now
  196. * @todo see Mage_Catalog_Model_Product::validate()
  197. */
  198. if (is_array($errors = $product->validate())) {
  199. $strErrors = array();
  200. foreach($errors as $code=>$error) {
  201. $strErrors[] = ($error === true)? Mage::helper('catalog')->__('Value for "%s" is invalid.', $code) : Mage::helper('catalog')->__('Value for "%s" is invalid: %s', $code, $error);
  202. }
  203. $this->_fault('data_invalid', implode("\n", $strErrors));
  204. }
  205. $product->save();
  206. } catch (Mage_Core_Exception $e) {
  207. $this->_fault('data_invalid', $e->getMessage());
  208. }
  209. return true;
  210. }
  211. /**
  212. * Set additional data before product saved
  213. *
  214. * @param Mage_Catalog_Model_Product $product
  215. * @param array $productData
  216. * @return object
  217. */
  218. protected function _prepareDataForSave ($product, $productData)
  219. {
  220. if (isset($productData['categories']) && is_array($productData['categories'])) {
  221. $product->setCategoryIds($productData['categories']);
  222. }
  223. if (isset($productData['websites']) && is_array($productData['websites'])) {
  224. foreach ($productData['websites'] as &$website) {
  225. if (is_string($website)) {
  226. try {
  227. $website = Mage::app()->getWebsite($website)->getId();
  228. } catch (Exception $e) { }
  229. }
  230. }
  231. $product->setWebsiteIds($productData['websites']);
  232. }
  233. if (Mage::app()->isSingleStoreMode()) {
  234. $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
  235. }
  236. if (isset($productData['stock_data']) && is_array($productData['stock_data'])) {
  237. $product->setStockData($productData['stock_data']);
  238. } else {
  239. $product->setStockData(array('use_config_manage_stock' => 0));
  240. }
  241. if (isset($productData['tier_price']) && is_array($productData['tier_price'])) {
  242. $tierPrices = Mage::getModel('catalog/product_attribute_tierprice_api')->prepareTierPrices($product, $productData['tier_price']);
  243. $product->setData(Mage_Catalog_Model_Product_Attribute_Tierprice_Api::ATTRIBUTE_CODE, $tierPrices);
  244. }
  245. }
  246. /**
  247. * Update product special price
  248. *
  249. * @param int|string $productId
  250. * @param float $specialPrice
  251. * @param string $fromDate
  252. * @param string $toDate
  253. * @param string|int $store
  254. * @return boolean
  255. */
  256. public function setSpecialPrice($productId, $specialPrice = null, $fromDate = null, $toDate = null, $store = null)
  257. {
  258. return $this->update($productId, array(
  259. 'special_price' => $specialPrice,
  260. 'special_from_date' => $fromDate,
  261. 'special_to_date' => $toDate
  262. ), $store);
  263. }
  264. /**
  265. * Retrieve product special price
  266. *
  267. * @param int|string $productId
  268. * @param string|int $store
  269. * @return array
  270. */
  271. public function getSpecialPrice($productId, $store = null)
  272. {
  273. return $this->info($productId, $store, array('special_price', 'special_from_date', 'special_to_date'));
  274. }
  275. /**
  276. * Delete product
  277. *
  278. * @param int|string $productId
  279. * @return boolean
  280. */
  281. public function delete($productId, $identifierType = null)
  282. {
  283. $product = $this->_getProduct($productId, null, $identifierType);
  284. if (!$product->getId()) {
  285. $this->_fault('not_exists');
  286. }
  287. try {
  288. $product->delete();
  289. } catch (Mage_Core_Exception $e) {
  290. $this->_fault('not_deleted', $e->getMessage());
  291. }
  292. return true;
  293. }
  294. } // Class Mage_Catalog_Model_Product_Api End