Guest User

Untitled

a guest
Nov 13th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.33 KB | None | 0 0
  1. <?php
  2.  
  3. require_once('app/Mage.php');
  4. Mage::app();
  5.  
  6. $cat_model = Mage::getModel('ultimate_mars/category');
  7. $_categories = $cat_model->getTreeModel()->getCollection()->addFieldToFilter("entity_id", array('gt' => 1));
  8.  
  9. $category = array();
  10. foreach($_categories as $_category)
  11. {
  12. $category[] = array("name" => $_category->getCategoryname(), "id" => $_category->getEntityId());
  13. }
  14.  
  15. echo json_encode($category);
  16.  
  17. <?php
  18. /**
  19. * Ultimate_Mars extension
  20. *
  21. * NOTICE OF LICENSE
  22. *
  23. * This source file is subject to the MIT License
  24. * that is bundled with this package in the file LICENSE.txt.
  25. * It is also available through the world-wide-web at this URL:
  26. * http://opensource.org/licenses/mit-license.php
  27. *
  28. * @category Ultimate
  29. * @package Ultimate_Mars
  30. * @copyright Copyright (c) 2018
  31. * @license http://opensource.org/licenses/mit-license.php MIT License
  32. */
  33. /**
  34. * Category model
  35. *
  36. * @category Ultimate
  37. * @package Ultimate_Mars
  38. * @author Ultimate Module Creator
  39. */
  40. class Ultimate_Mars_Model_Category extends Mage_Core_Model_Abstract
  41. {
  42. /**
  43. * Entity code.
  44. * Can be used as part of method name for entity processing
  45. */
  46. const ENTITY = 'ultimate_mars_category';
  47. const CACHE_TAG = 'ultimate_mars_category';
  48.  
  49. /**
  50. * Prefix of model events names
  51. *
  52. * @var string
  53. */
  54. protected $_eventPrefix = 'ultimate_mars_category';
  55.  
  56. /**
  57. * Parameter name in event
  58. *
  59. * @var string
  60. */
  61. protected $_eventObject = 'category';
  62.  
  63. /**
  64. * constructor
  65. *
  66. * @access public
  67. * @return void
  68. * @author Ultimate Module Creator
  69. */
  70. public function _construct()
  71. {
  72. parent::_construct();
  73. $this->_init('ultimate_mars/category');
  74. }
  75.  
  76. /**
  77. * before save category
  78. *
  79. * @access protected
  80. * @return Ultimate_Mars_Model_Category
  81. * @author Ultimate Module Creator
  82. */
  83. protected function _beforeSave()
  84. {
  85. parent::_beforeSave();
  86. $now = Mage::getSingleton('core/date')->gmtDate();
  87. if ($this->isObjectNew()) {
  88. $this->setCreatedAt($now);
  89. }
  90. $this->setUpdatedAt($now);
  91. return $this;
  92. }
  93.  
  94. /**
  95. * save category relation
  96. *
  97. * @access public
  98. * @return Ultimate_Mars_Model_Category
  99. * @author Ultimate Module Creator
  100. */
  101. protected function _afterSave()
  102. {
  103. return parent::_afterSave();
  104. }
  105.  
  106. /**
  107. * Retrieve collection
  108. *
  109. * @access public
  110. * @return Ultimate_Mars_Model_Resource_Product_Collection
  111. * @author Ultimate Module Creator
  112. */
  113. public function getSelectedProductsCollection()
  114. {
  115. if (!$this->hasData('_product_collection')) {
  116. if (!$this->getId()) {
  117. return new Varien_Data_Collection();
  118. } else {
  119. $collection = Mage::getResourceModel('ultimate_mars/product_collection')
  120. ->addFieldToFilter('category_id', $this->getId());
  121. $this->setData('_product_collection', $collection);
  122. }
  123. }
  124. return $this->getData('_product_collection');
  125. }
  126.  
  127. /**
  128. * get the tree model
  129. *
  130. * @access public
  131. * @return Ultimate_Mars_Model_Resource_Category_Tree
  132. * @author Ultimate Module Creator
  133. */
  134. public function getTreeModel()
  135. {
  136. return Mage::getResourceModel('ultimate_mars/category_tree');
  137. }
  138.  
  139. /**
  140. * get tree model instance
  141. *
  142. * @access public
  143. * @return Ultimate_Mars_Model_Resource_Category_Tree
  144. * @author Ultimate Module Creator
  145. */
  146. public function getTreeModelInstance()
  147. {
  148. if (is_null($this->_treeModel)) {
  149. $this->_treeModel = Mage::getResourceSingleton('ultimate_mars/category_tree');
  150. }
  151. return $this->_treeModel;
  152. }
  153.  
  154. /**
  155. * Move category
  156. *
  157. * @access public
  158. * @param int $parentId new parent category id
  159. * @param int $afterCategoryId category id after which we have put current category
  160. * @return Ultimate_Mars_Model_Category
  161. * @author Ultimate Module Creator
  162. */
  163. public function move($parentId, $afterCategoryId)
  164. {
  165. $parent = Mage::getModel('ultimate_mars/category')->load($parentId);
  166. if (!$parent->getId()) {
  167. Mage::throwException(
  168. Mage::helper('ultimate_mars')->__(
  169. 'Category move operation is not possible: the new parent category was not found.'
  170. )
  171. );
  172. }
  173. if (!$this->getId()) {
  174. Mage::throwException(
  175. Mage::helper('ultimate_mars')->__(
  176. 'Category move operation is not possible: the current category was not found.'
  177. )
  178. );
  179. } elseif ($parent->getId() == $this->getId()) {
  180. Mage::throwException(
  181. Mage::helper('ultimate_mars')->__(
  182. 'Category move operation is not possible: parent category is equal to child category.'
  183. )
  184. );
  185. }
  186. $this->setMovedCategoryId($this->getId());
  187. $eventParams = array(
  188. $this->_eventObject => $this,
  189. 'parent' => $parent,
  190. 'category_id' => $this->getId(),
  191. 'prev_parent_id' => $this->getParentId(),
  192. 'parent_id' => $parentId
  193. );
  194. $moveComplete = false;
  195. $this->_getResource()->beginTransaction();
  196. try {
  197. $this->getResource()->changeParent($this, $parent, $afterCategoryId);
  198. $this->_getResource()->commit();
  199. $this->setAffectedCategoryIds(array($this->getId(), $this->getParentId(), $parentId));
  200. $moveComplete = true;
  201. } catch (Exception $e) {
  202. $this->_getResource()->rollBack();
  203. throw $e;
  204. }
  205. if ($moveComplete) {
  206. Mage::app()->cleanCache(array(self::CACHE_TAG));
  207. }
  208. return $this;
  209. }
  210.  
  211. /**
  212. * Get the parent category
  213. *
  214. * @access public
  215. * @return Ultimate_Mars_Model_Category
  216. * @author Ultimate Module Creator
  217. */
  218. public function getParentCategory()
  219. {
  220. if (!$this->hasData('parent_category')) {
  221. $this->setData(
  222. 'parent_category',
  223. Mage::getModel('ultimate_mars/category')->load($this->getParentId())
  224. );
  225. }
  226. return $this->_getData('parent_category');
  227. }
  228.  
  229. /**
  230. * Get the parent id
  231. *
  232. * @access public
  233. * @return int
  234. * @author Ultimate Module Creator
  235. */
  236. public function getParentId()
  237. {
  238. $parentIds = $this->getParentIds();
  239. return intval(array_pop($parentIds));
  240. }
  241.  
  242. /**
  243. * Get all parent categorys ids
  244. *
  245. * @access public
  246. * @return array
  247. * @author Ultimate Module Creator
  248. */
  249. public function getParentIds()
  250. {
  251. return array_diff($this->getPathIds(), array($this->getId()));
  252. }
  253.  
  254. /**
  255. * Get all categorys children
  256. *
  257. * @access public
  258. * @param bool $asArray
  259. * @return mixed (array|string)
  260. * @author Ultimate Module Creator
  261. */
  262. public function getAllChildren($asArray = false)
  263. {
  264. $children = $this->getResource()->getAllChildren($this);
  265. if ($asArray) {
  266. return $children;
  267. } else {
  268. return implode(',', $children);
  269. }
  270. }
  271.  
  272. /**
  273. * Get all categorys children
  274. *
  275. * @access public
  276. * @return string
  277. * @author Ultimate Module Creator
  278. */
  279. public function getChildCategorys()
  280. {
  281. return implode(',', $this->getResource()->getChildren($this, false));
  282. }
  283.  
  284. /**
  285. * check the id
  286. *
  287. * @access public
  288. * @param int $id
  289. * @return bool
  290. * @author Ultimate Module Creator
  291. */
  292. public function checkId($id)
  293. {
  294. return $this->_getResource()->checkId($id);
  295. }
  296.  
  297. /**
  298. * Get array categorys ids which are part of category path
  299. *
  300. * @access public
  301. * @return array
  302. * @author Ultimate Module Creator
  303. */
  304. public function getPathIds()
  305. {
  306. $ids = $this->getData('path_ids');
  307. if (is_null($ids)) {
  308. $ids = explode('/', $this->getPath());
  309. $this->setData('path_ids', $ids);
  310. }
  311. return $ids;
  312. }
  313.  
  314. /**
  315. * Retrieve level
  316. *
  317. * @access public
  318. * @return int
  319. * @author Ultimate Module Creator
  320. */
  321. public function getLevel()
  322. {
  323. if (!$this->hasLevel()) {
  324. return count(explode('/', $this->getPath())) - 1;
  325. }
  326. return $this->getData('level');
  327. }
  328.  
  329. /**
  330. * Verify category ids
  331. *
  332. * @access public
  333. * @param array $ids
  334. * @return bool
  335. * @author Ultimate Module Creator
  336. */
  337. public function verifyIds(array $ids)
  338. {
  339. return $this->getResource()->verifyIds($ids);
  340. }
  341.  
  342. /**
  343. * check if category has children
  344. *
  345. * @access public
  346. * @return bool
  347. * @author Ultimate Module Creator
  348. */
  349. public function hasChildren()
  350. {
  351. return $this->_getResource()->getChildrenAmount($this) > 0;
  352. }
  353.  
  354. /**
  355. * check if category can be deleted
  356. *
  357. * @access protected
  358. * @return Ultimate_Mars_Model_Category
  359. * @author Ultimate Module Creator
  360. */
  361. protected function _beforeDelete()
  362. {
  363. if ($this->getResource()->isForbiddenToDelete($this->getId())) {
  364. Mage::throwException(Mage::helper('ultimate_mars')->__("Can't delete root category."));
  365. }
  366. return parent::_beforeDelete();
  367. }
  368.  
  369. /**
  370. * get the categorys
  371. *
  372. * @access public
  373. * @param Ultimate_Mars_Model_Category $parent
  374. * @param int $recursionLevel
  375. * @param bool $sorted
  376. * @param bool $asCollection
  377. * @param bool $toLoad
  378. * @author Ultimate Module Creator
  379. */
  380. public function getCategorys($parent, $recursionLevel = 0, $sorted=false, $asCollection=false, $toLoad=true)
  381. {
  382. return $this->getResource()->getCategorys($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
  383. }
  384.  
  385. /**
  386. * Return parent categorys of current category
  387. *
  388. * @access public
  389. * @return array
  390. * @author Ultimate Module Creator
  391. */
  392. public function getParentCategorys()
  393. {
  394. return $this->getResource()->getParentCategorys($this);
  395. }
  396.  
  397. /**
  398. * Return children categorys of current category
  399. *
  400. * @access public
  401. * @return array
  402. * @author Ultimate Module Creator
  403. */
  404. public function getChildrenCategorys()
  405. {
  406. return $this->getResource()->getChildrenCategorys($this);
  407. }
  408.  
  409. /**
  410. * check if parents are enabled
  411. *
  412. * @access public
  413. * @return bool
  414. * @author Ultimate Module Creator
  415. */
  416. public function getStatusPath()
  417. {
  418. $parents = $this->getParentCategorys();
  419. $rootId = Mage::helper('ultimate_mars/category')->getRootCategoryId();
  420. foreach ($parents as $parent) {
  421. if ($parent->getId() == $rootId) {
  422. continue;
  423. }
  424. if (!$parent->getStatus()) {
  425. return false;
  426. }
  427. }
  428. return $this->getStatus();
  429. }
  430.  
  431. /**
  432. * get default values
  433. *
  434. * @access public
  435. * @return array
  436. * @author Ultimate Module Creator
  437. */
  438. public function getDefaultValues()
  439. {
  440. $values = array();
  441. $values['status'] = 1;
  442. return $values;
  443. }
  444.  
  445. }
Add Comment
Please, Sign In to add comment