zeloras

Untitled

Dec 7th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5. use Yii;
  6. use app\models\db\Category as CategoryModel;
  7. use app\helpers\Basehelp;
  8. use yii\helpers\ArrayHelper;
  9.  
  10. /**
  11. * Category model
  12. */
  13. class Category extends CategoryModel
  14. {
  15. /**
  16. * Create new category item.
  17. * @return bool
  18. */
  19. public function createCategory() {
  20. if ($this->validate()) {
  21. $this->save();
  22. return true;
  23. }
  24. return false;
  25. }
  26.  
  27. /**
  28. * Update current category
  29. * @return boolean
  30. */
  31. public function updateCategory() {
  32. if ($this->validate()) {
  33. $posted = Yii::$app->request->post(ucfirst($this->tableName()));
  34. $this->name = $posted['name'];
  35. $this->link = $posted['link'];
  36. $this->parent = $posted['parent'];
  37. $this->update();
  38. return true;
  39. }
  40. return false;
  41. }
  42.  
  43. /**
  44. * remove Current category
  45. * @return bool
  46. */
  47. public function deleteCategory($id) {
  48. $status = false;
  49. $this->id = $id;
  50.  
  51. if ($this->id > 0) {
  52. $categories = $this->getChildsTree();
  53. $status = self::deleteAll(['in', 'id', $categories]);
  54. }
  55.  
  56. return $status;
  57. }
  58.  
  59. /**
  60. * get childs list
  61. * @return array ids list
  62. */
  63. public function getChildsTree() {
  64. $childs[] = $this->id;
  65. while ($child = $this->getChilds()) {
  66. $haveChild = [];
  67. foreach ($child as $item) {
  68. $this->id = $item['id'];
  69. $childs[] = $item['id'];
  70. }
  71.  
  72. if (sizeof($child) > 1) {
  73. foreach ($child as $item) {
  74. $haveChild = $this->getChilds($item['id']);
  75. if (sizeof($haveChild) > 0) {
  76. $this->id = $item['id'];
  77. }
  78. }
  79. }
  80. }
  81.  
  82. return $childs;
  83. }
  84.  
  85.  
  86. /**
  87. * Get all items
  88. * @return array
  89. */
  90. private function getCategoryQuery() {
  91. return parent::find()->orderBy(['parent' => SORT_ASC])->asArray()->all();
  92. }
  93.  
  94. /**
  95. * Get current menu
  96. * @return array
  97. */
  98. public function getCategory() {
  99. $list = $this->getCategoryQuery();
  100. $list = Basehelp::formatTree($list, 0);
  101.  
  102. return $list;
  103. }
  104.  
  105.  
  106. /**
  107. * Get all menu links with format tree view
  108. * @return array
  109. */
  110. public function getCategorySelect() {
  111. $menu_list = $this->getCategoryQuery();
  112. $list = ArrayHelper::map($menu_list, 'id', 'name');
  113. $list[0] = 'Without parent';
  114. ksort($list);
  115.  
  116. return $list;
  117. }
  118.  
  119. /**
  120. * Get menu name
  121. * @return string
  122. */
  123. public function getCategoryName() {
  124. return $this->name;
  125. }
  126.  
  127. /**
  128. * Get menu link
  129. * @return string
  130. */
  131. public function getCategoryLink() {
  132. return $this->link;
  133. }
  134.  
  135. /**
  136. * Get menu parent
  137. * @return integer
  138. */
  139. public function getCategoryParent() {
  140. return (int)$this->parent;
  141. }
  142.  
  143. /**
  144. * Get child category
  145. * @return object
  146. */
  147. public function getChilds($parent = 0) {
  148. if ($parent < 1) {
  149. $parent = $this->id;
  150. }
  151. return self::find()->where(['=', 'parent', $parent])->orderBy(['parent' => SORT_DESC])->all();
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment