PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/leochaves/magento-pt_br
PHP | 498 lines | 283 code | 73 blank | 142 comment | 27 complexity | 4c4692cbcfda6234884c8f824dfee78a MD5 | raw file
Possible License(s): GPL-2.0
  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) 2008 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 category api
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Category_Api extends Mage_Catalog_Model_Api_Resource
  34. {
  35. public function __construct()
  36. {
  37. $this->_storeIdSessionField = 'category_store_id';
  38. }
  39. /**
  40. * Retrive level of categories for category/store view/website
  41. *
  42. * @param string|int $website
  43. * @param string|int $store
  44. * @return array
  45. */
  46. public function level($website = null, $store = null, $categoryId = null)
  47. {
  48. $ids = array();
  49. $storeId = Mage_Catalog_Model_Category::DEFAULT_STORE_ID;
  50. // load root categories of website
  51. if (null !== $website) {
  52. try {
  53. $website = Mage::app()->getWebsite($website);
  54. foreach ($website->getStores() as $store) {
  55. /* @var $store Mage_Core_Model_Store */
  56. $ids[] = $store->getRootCategoryId();
  57. }
  58. } catch (Mage_Core_Exception $e) {
  59. $this->_fault('website_not_exists', $e->getMessage());
  60. }
  61. }
  62. elseif (null !== $store) {
  63. // load children of root category of store
  64. if (null === $categoryId) {
  65. try {
  66. $store = Mage::app()->getStore($store);
  67. $storeId = $store->getId();
  68. $ids = $store->getRootCategoryId();
  69. } catch (Mage_Core_Model_Store_Exception $e) {
  70. $this->_fault('store_not_exists');
  71. }
  72. }
  73. // load children of specified category id
  74. else {
  75. $storeId = $this->_getStoreId($store);
  76. $ids = (int)$categoryId;
  77. }
  78. }
  79. // load all root categories
  80. else {
  81. $ids = Mage_Catalog_Model_Category::TREE_ROOT_ID;
  82. }
  83. $collection = Mage::getModel('catalog/category')->getCollection()
  84. ->setStoreId($storeId)
  85. ->addAttributeToSelect('name')
  86. ->addAttributeToSelect('is_active');
  87. if (is_array($ids)) {
  88. $collection->addFieldToFilter('entity_id', array('in' => $ids));
  89. } else {
  90. $collection->addFieldToFilter('parent_id', $ids);
  91. }
  92. // Only basic category data
  93. $result = array();
  94. foreach ($collection as $category) {
  95. /* @var $category Mage_Catalog_Model_Category */
  96. $result[] = array(
  97. 'category_id' => $category->getId(),
  98. 'parent_id' => $category->getParentId(),
  99. 'name' => $category->getName(),
  100. 'is_active' => $category->getIsActive(),
  101. 'position' => $category->getPosition(),
  102. 'level' => $category->getLevel()
  103. );
  104. }
  105. return $result;
  106. }
  107. /**
  108. * Retrieve category tree
  109. *
  110. * @param int $parent
  111. * @param string|int $store
  112. * @return array
  113. */
  114. public function tree($parentId = null, $store = null)
  115. {
  116. $tree = Mage::getResourceSingleton('catalog/category_tree')
  117. ->load();
  118. if (is_null($parentId) && !is_null($store)) {
  119. $parentId = Mage::app()->getStore($this->_getStoreId($store))->getRootCategoryId();
  120. } elseif (is_null($parentId)) {
  121. $parentId = 1;
  122. }
  123. $tree = Mage::getResourceSingleton('catalog/category_tree')
  124. ->load();
  125. /* @var $tree Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree */
  126. $root = $tree->getNodeById($parentId);
  127. if($root && $root->getId() == 1) {
  128. $root->setName(Mage::helper('catalog')->__('Root'));
  129. }
  130. $collection = Mage::getModel('catalog/category')->getCollection()
  131. ->setStoreId($this->_getStoreId($store))
  132. ->addAttributeToSelect('name')
  133. ->addAttributeToSelect('is_active');
  134. $tree->addCollectionData($collection, true);
  135. return $this->_nodeToArray($root);
  136. }
  137. /**
  138. * Convert node to array
  139. *
  140. * @param Varien_Data_Tree_Node $node
  141. * @return array
  142. */
  143. protected function _nodeToArray(Varien_Data_Tree_Node $node)
  144. {
  145. // Only basic category data
  146. $result = array();
  147. $result['category_id'] = $node->getId();
  148. $result['parent_id'] = $node->getParentId();
  149. $result['name'] = $node->getName();
  150. $result['is_active'] = $node->getIsActive();
  151. $result['position'] = $node->getPosition();
  152. $result['level'] = $node->getLevel();
  153. $result['children'] = array();
  154. foreach ($node->getChildren() as $child) {
  155. $result['children'][] = $this->_nodeToArray($child);
  156. }
  157. return $result;
  158. }
  159. /**
  160. * Initilize and return category model
  161. *
  162. * @param int $categoryId
  163. * @param string|int $store
  164. * @return Mage_Catalog_Model_Category
  165. */
  166. protected function _initCategory($categoryId, $store = null)
  167. {
  168. $category = Mage::getModel('catalog/category')
  169. ->setStoreId($this->_getStoreId($store))
  170. ->load($categoryId);
  171. if (!$category->getId()) {
  172. $this->_fault('not_exists');
  173. }
  174. return $category;
  175. }
  176. /**
  177. * Retrieve category data
  178. *
  179. * @param int $categoryId
  180. * @param string|int $store
  181. * @param array $attributes
  182. * @return array
  183. */
  184. public function info($categoryId, $store = null, $attributes = null)
  185. {
  186. $category = $this->_initCategory($categoryId, $store);
  187. // Basic category data
  188. $result = array();
  189. $result['category_id'] = $category->getId();
  190. $result['is_active'] = $category->getIsActive();
  191. $result['position'] = $category->getPosition();
  192. $result['level'] = $category->getLevel();
  193. foreach ($category->getAttributes() as $attribute) {
  194. if ($this->_isAllowedAttribute($attribute, $attributes)) {
  195. $result[$attribute->getAttributeCode()] = $category->getData($attribute->getAttributeCode());
  196. }
  197. }
  198. $result['parent_id'] = $category->getParentId();
  199. $result['children'] = $category->getChildren();
  200. $result['all_children'] = $category->getAllChildren();
  201. return $result;
  202. }
  203. /**
  204. * Create new category
  205. *
  206. * @param int $parentId
  207. * @param array $categoryData
  208. * @return int
  209. */
  210. public function create($parentId, $categoryData, $store = null)
  211. {
  212. $parent_category = $this->_initCategory($parentId);
  213. $category = Mage::getModel('catalog/category')
  214. ->setStoreId($this->_getStoreId($store));
  215. $category->addData(array('path'=>implode('/',$parent_category->getPathIds())));
  216. $category ->setAttributeSetId($category->getDefaultAttributeSetId());
  217. /* @var $category Mage_Catalog_Model_Category */
  218. foreach ($category->getAttributes() as $attribute) {
  219. if ($this->_isAllowedAttribute($attribute)
  220. && isset($categoryData[$attribute->getAttributeCode()])) {
  221. $category->setData(
  222. $attribute->getAttributeCode(),
  223. $categoryData[$attribute->getAttributeCode()]
  224. );
  225. }
  226. }
  227. $category->setParentId($parent_category->getId());
  228. try {
  229. $category->save();
  230. } catch (Mage_Core_Exception $e) {
  231. $this->_fault('data_invalid', $e->getMessage());
  232. }
  233. return $category->getId();
  234. }
  235. /**
  236. * Update category data
  237. *
  238. * @param int $categoryId
  239. * @param array $categoryData
  240. * @param string|int $store
  241. * @return boolean
  242. */
  243. public function update($categoryId, $categoryData, $store = null)
  244. {
  245. $category = $this->_initCategory($categoryId, $store);
  246. foreach ($category->getAttributes() as $attribute) {
  247. if ($this->_isAllowedAttribute($attribute)
  248. && isset($categoryData[$attribute->getAttributeCode()])) {
  249. $category->setData(
  250. $attribute->getAttributeCode(),
  251. $categoryData[$attribute->getAttributeCode()]
  252. );
  253. }
  254. }
  255. try {
  256. $category->save();
  257. } catch (Mage_Core_Exception $e) {
  258. $this->_fault('data_invalid', $e->getMessage());
  259. }
  260. return true;
  261. }
  262. /**
  263. * Move category in tree
  264. *
  265. * @param int $categoryId
  266. * @param int $parentId
  267. * @param int $afterId
  268. * @return boolean
  269. */
  270. public function move($categoryId, $parentId, $afterId = null)
  271. {
  272. $category = $this->_initCategory($categoryId);
  273. $parent_category = $this->_initCategory($parentId);
  274. $tree = Mage::getResourceModel('catalog/category_tree')
  275. ->load();
  276. $node = $tree->getNodeById($category->getId());
  277. $newParentNode = $tree->getNodeById($parent_category->getId());
  278. if (!$node || !$node->getId()) {
  279. $this->_fault('not_exists');
  280. }
  281. // if $afterId is null - move category to the down
  282. if ($afterId === null && $parent_category->hasChildren()) {
  283. $parentChildren = $parent_category->getChildren();
  284. $afterId = array_pop(explode(',', $parentChildren));
  285. }
  286. $prevNode = $tree->getNodeById($afterId);
  287. if (!$prevNode || !$prevNode->getId()) {
  288. $prevNode = null;
  289. }
  290. try {
  291. $tree->move($node, $newParentNode, $prevNode);
  292. } catch (Mage_Core_Exception $e) {
  293. $this->_fault('not_moved', $e->getMessage());
  294. }
  295. return true;
  296. }
  297. /**
  298. * Delete category
  299. *
  300. * @param int $categoryId
  301. * @return boolean
  302. */
  303. public function delete($categoryId)
  304. {
  305. $category = $this->_initCategory($categoryId);
  306. try {
  307. $category->delete();
  308. } catch (Mage_Core_Exception $e) {
  309. $this->_fault('not_deleted', $e->getMessage());
  310. }
  311. return true;
  312. }
  313. /**
  314. * Get prduct Id from sku or from product id
  315. *
  316. * @param int|string $productId
  317. * @return int
  318. */
  319. protected function _getProductId($productId)
  320. {
  321. $product = Mage::getModel('catalog/product');
  322. $idBySku = $product->getIdBySku($productId);
  323. if ($idBySku) {
  324. $productId = $idBySku;
  325. }
  326. $product->load($productId);
  327. if (!$product->getId()) {
  328. $this->_fault('not_exists','Product not exists.');
  329. }
  330. return $productId;
  331. }
  332. /**
  333. * Retrieve list of assigned products to category
  334. *
  335. * @param int $categoryId
  336. * @return array
  337. */
  338. public function assignedProducts($categoryId)
  339. {
  340. $category = $this->_initCategory($categoryId);
  341. $collection = $category->getProductCollection()->setOrder('position', 'asc');
  342. $result = array();
  343. foreach ($collection as $product) {
  344. $result[] = array(
  345. 'product_id' => $product->getId(),
  346. 'type' => $product->getTypeId(),
  347. 'set' => $product->getAttributeSetId(),
  348. 'sku' => $product->getSku(),
  349. 'position' => $product->getPosition()
  350. );
  351. }
  352. return $result;
  353. }
  354. /**
  355. * Assign product to category
  356. *
  357. * @param int $categoryId
  358. * @param int $productId
  359. * @param int $position
  360. * @return boolean
  361. */
  362. public function assignProduct($categoryId, $productId, $position = null)
  363. {
  364. $category = $this->_initCategory($categoryId);
  365. $positions = $category->getProductsPosition();
  366. $productId = $this->_getProductId($productId);
  367. $positions[$productId] = $position;
  368. $category->setPostedProducts($positions);
  369. try {
  370. $category->save();
  371. } catch (Mage_Core_Exception $e) {
  372. $this->_fault('data_invalid', $e->getMessage());
  373. }
  374. return true;
  375. }
  376. /**
  377. * Update product assignment
  378. *
  379. * @param int $categoryId
  380. * @param int $productId
  381. * @param int $position
  382. * @return boolean
  383. */
  384. public function updateProduct($categoryId, $productId, $position = null)
  385. {
  386. $category = $this->_initCategory($categoryId);
  387. $positions = $category->getProductsPosition();
  388. $productId = $this->_getProductId($productId);
  389. if (!isset($positions[$productId])) {
  390. $this->_fault('product_not_assigned');
  391. }
  392. $positions[$productId] = $position;
  393. $category->setPostedProducts($positions);
  394. try {
  395. $category->save();
  396. } catch (Mage_Core_Exception $e) {
  397. $this->_fault('data_invalid', $e->getMessage());
  398. }
  399. return true;
  400. }
  401. /**
  402. * Remove product assignment from category
  403. *
  404. * @param int $categoryId
  405. * @param int $productId
  406. * @return boolean
  407. */
  408. public function removeProduct($categoryId, $productId)
  409. {
  410. $category = $this->_initCategory($categoryId);
  411. $positions = $category->getProductsPosition();
  412. $productId = $this->_getProductId($productId);
  413. if (!isset($positions[$productId])) {
  414. $this->_fault('product_not_assigned');
  415. }
  416. unset($positions[$productId]);
  417. $category->setPostedProducts($positions);
  418. try {
  419. $category->save();
  420. } catch (Mage_Core_Exception $e) {
  421. $this->_fault('data_invalid', $e->getMessage());
  422. }
  423. return true;
  424. }
  425. } // Class Mage_Catalog_Model_Category_Api End