PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/guiohm/magento-french
PHP | 313 lines | 180 code | 39 blank | 94 comment | 27 complexity | 988d311f31693ea5aa93254b82803360 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) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.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. ->setStoreId($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 array $productData
  120. * @return int
  121. */
  122. public function create($type, $set, $sku, $productData)
  123. {
  124. if (!$type || !$set || !$sku) {
  125. $this->_fault('data_invalid');
  126. }
  127. $product = Mage::getModel('catalog/product');
  128. /* @var $product Mage_Catalog_Model_Product */
  129. $product->setStoreId($this->_getStoreId())
  130. ->setAttributeSetId($set)
  131. ->setTypeId($type)
  132. ->setSku($sku);
  133. if (isset($productData['website_ids']) && is_array($productData['website_ids'])) {
  134. $product->setWebsiteIds($productData['website_ids']);
  135. }
  136. foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
  137. if ($this->_isAllowedAttribute($attribute)
  138. && isset($productData[$attribute->getAttributeCode()])) {
  139. $product->setData(
  140. $attribute->getAttributeCode(),
  141. $productData[$attribute->getAttributeCode()]
  142. );
  143. }
  144. }
  145. $this->_prepareDataForSave($product, $productData);
  146. if (is_array($errors = $product->validate())) {
  147. $this->_fault('data_invalid', implode("\n", $errors));
  148. }
  149. try {
  150. $product->save();
  151. } catch (Mage_Core_Exception $e) {
  152. $this->_fault('data_invalid', $e->getMessage());
  153. }
  154. return $product->getId();
  155. }
  156. /**
  157. * Update product data
  158. *
  159. * @param int|string $productId
  160. * @param array $productData
  161. * @param string|int $store
  162. * @return boolean
  163. */
  164. public function update($productId, $productData, $store = null, $identifierType = null)
  165. {
  166. $product = $this->_getProduct($productId, $store, $identifierType);
  167. if (!$product->getId()) {
  168. $this->_fault('not_exists');
  169. }
  170. if (isset($productData['website_ids']) && is_array($productData['website_ids'])) {
  171. $product->setWebsiteIds($productData['website_ids']);
  172. }
  173. foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
  174. if ($this->_isAllowedAttribute($attribute)
  175. && isset($productData[$attribute->getAttributeCode()])) {
  176. $product->setData(
  177. $attribute->getAttributeCode(),
  178. $productData[$attribute->getAttributeCode()]
  179. );
  180. }
  181. }
  182. $this->_prepareDataForSave($product, $productData);
  183. try {
  184. if (is_array($errors = $product->validate())) {
  185. $this->_fault('data_invalid', implode("\n", $errors));
  186. }
  187. } catch (Mage_Core_Exception $e) {
  188. $this->_fault('data_invalid', $e->getMessage());
  189. }
  190. try {
  191. $product->save();
  192. } catch (Mage_Core_Exception $e) {
  193. $this->_fault('data_invalid', $e->getMessage());
  194. }
  195. return true;
  196. }
  197. /**
  198. * Set additional data before product saved
  199. *
  200. * @param Mage_Catalog_Model_Product $product
  201. * @param array $productData
  202. * @return object
  203. */
  204. protected function _prepareDataForSave ($product, $productData)
  205. {
  206. if (isset($productData['categories']) && is_array($productData['categories'])) {
  207. $product->setCategoryIds($productData['categories']);
  208. }
  209. if (isset($productData['websites']) && is_array($productData['websites'])) {
  210. foreach ($productData['websites'] as &$website) {
  211. if (is_string($website)) {
  212. try {
  213. $website = Mage::app()->getWebsite($website)->getId();
  214. } catch (Exception $e) { }
  215. }
  216. }
  217. $product->setWebsiteIds($productData['websites']);
  218. }
  219. if (Mage::app()->isSingleStoreMode()) {
  220. $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
  221. }
  222. if (isset($productData['stock_data']) && is_array($productData['stock_data'])) {
  223. $product->setStockData($productData['stock_data']);
  224. }
  225. }
  226. /**
  227. * Update product special price
  228. *
  229. * @param int|string $productId
  230. * @param float $specialPrice
  231. * @param string $fromDate
  232. * @param string $toDate
  233. * @param string|int $store
  234. * @return boolean
  235. */
  236. public function setSpecialPrice($productId, $specialPrice = null, $fromDate = null, $toDate = null, $store = null)
  237. {
  238. return $this->update($productId, array(
  239. 'special_price' => $specialPrice,
  240. 'special_from_date' => $fromDate,
  241. 'special_to_date' => $toDate
  242. ), $store);
  243. }
  244. /**
  245. * Retrieve product special price
  246. *
  247. * @param int|string $productId
  248. * @param string|int $store
  249. * @return array
  250. */
  251. public function getSpecialPrice($productId, $store = null)
  252. {
  253. return $this->info($productId, $store, array('special_price', 'special_from_date', 'special_to_date'));
  254. }
  255. /**
  256. * Delete product
  257. *
  258. * @param int|string $productId
  259. * @return boolean
  260. */
  261. public function delete($productId, $identifierType = null)
  262. {
  263. $product = $this->_getProduct($productId, null, $identifierType);
  264. if (!$product->getId()) {
  265. $this->_fault('not_exists');
  266. }
  267. try {
  268. $product->delete();
  269. } catch (Mage_Core_Exception $e) {
  270. $this->_fault('not_deleted', $e->getMessage());
  271. }
  272. return true;
  273. }
  274. } // Class Mage_Catalog_Model_Product_Api End