ArticleCategoryController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/13/013
  6. * Time: 14:30
  7. */
  8. namespace frontend\controllers;
  9. use frontend\models\Article;
  10. use frontend\models\ArticleCategory;
  11. class ArticleCategoryController extends BaseController
  12. {
  13. // public $exceptIds = [2,4,6,7,8,9,10,11,13,14,15,16,20,22,23];
  14. /**
  15. * 获取所有文章分类
  16. */
  17. public function actionGetArticleCategoryTreeList()
  18. {
  19. $allCategory = ArticleCategory::find()->asArray()->all();
  20. $result = $this->recursiveArticleCategoryTreeList($allCategory, null);
  21. return $this->outJson(1, $result);
  22. }
  23. /**
  24. * 递归父类排序分类树(获取层级)
  25. * @param $allArticleCategoryList
  26. * @param $parent_id
  27. * @param int $temp
  28. * @return array
  29. */
  30. private function recursiveArticleCategoryTreeList($allArticleCategoryList, $parent_id, &$temp = [])
  31. {
  32. foreach ($allArticleCategoryList as $articleCategory) {
  33. if ($articleCategory['parent_id'] == $parent_id) {
  34. $temp[] = $articleCategory;
  35. $this->recursiveArticleCategoryTreeList($allArticleCategoryList, $articleCategory['id'], $temp);
  36. }
  37. }
  38. return $temp;
  39. }
  40. /**
  41. * 获取文章分类列表
  42. */
  43. public function actionGetArticleCategoryList()
  44. {
  45. $categoryModel = ArticleCategory::find();
  46. $query = clone $categoryModel;
  47. $allCategory = $query->asArray()->all();
  48. $allCategory = $this->recursiveArticleCategoryTreeList($allCategory, null);
  49. if ($allCategory) {
  50. $article = Article::find();
  51. foreach ($allCategory as $key => $category) {
  52. $articleQuery = $article->where(['article_category_id' => $category['id']]);
  53. $count = $articleQuery->count();
  54. $allCategory[$key]['article_count'] = (int)$count;
  55. $parent = $categoryModel->where(['id' => $category['parent_id']])->asArray()->one();
  56. $allCategory[$key]['parent'] = !empty($parent) ? $parent : [];
  57. }
  58. return $this->outJson(1, $allCategory);
  59. } else {
  60. return $this->outJson(0, [], '不存在分类');
  61. }
  62. }
  63. /**
  64. * 新增文章分类
  65. */
  66. public function actionArticleCategorySave()
  67. {
  68. $request = \Yii::$app->getRequest()->post();
  69. if (!empty($request['id'])) {
  70. $model = ArticleCategory::findOne(['id' => $request['id']]);
  71. $request['modify_date'] = date('Y-m-d H:i:s', time());
  72. $request['create_date'] = $model->create_date;
  73. $request['parent_id'] = $model->parent_id;
  74. if (!$this->isUniqueBySign($model->sign, $request['sign'])) {
  75. return $this->outJson(0, [], '标识已存在!');
  76. }
  77. } else {
  78. $model = new ArticleCategory();
  79. $request['create_date'] = date('Y-m-d H:i:s', time());
  80. $request['modify_date'] = date('Y-m-d H:i:s', time());
  81. if ($this->isExistBySign($request['sign'])) {
  82. return $this->outJson(0, [], '标识已存在!');
  83. }
  84. }
  85. $request['grade'] = isset($request['grade']) ? (int)$request['grade'] : '';
  86. $request['order_list'] = isset($request['order_list']) ? (int)$request['order_list'] : '';
  87. $model->setAttributes($request);
  88. if ($model->save()) {
  89. if ($request['parent_id']) {
  90. $model->path = $request['parent_id'].','.$model->id;
  91. } else {
  92. $model->path = (string)$model->id;
  93. }
  94. $model->save();
  95. return $this->outJson(1, $model->id);
  96. } else {
  97. return $this->outJson(0, [], '新增分类失败');
  98. }
  99. }
  100. /**
  101. * 检测sign是否唯一
  102. */
  103. public function actionCheckSign()
  104. {
  105. $response = [];
  106. $request = \Yii::$app->getRequest()->get();
  107. $data['oldValue'] = isset($request['oldValue']) ? $request['oldValue'] : '';
  108. $data['sign'] = isset($request['sign']) ? $request['sign'] : '';
  109. $isExist = $this->isUniqueBySign($data['oldValue'], $data['sign']);
  110. if ($isExist) {
  111. $response['isSuccess'] = true;
  112. } else {
  113. $response['isSuccess'] = false;
  114. }
  115. return $this->outJson(1, $response);
  116. }
  117. private function isUniqueBySign($oldValue, $newSign)
  118. {
  119. if (strtolower($oldValue) == strtolower($newSign)) {
  120. return true;
  121. } else {
  122. if ($this->isExistBySign($newSign)) {
  123. return false;
  124. } else {
  125. return true;
  126. }
  127. }
  128. }
  129. private function isExistBySign($sign)
  130. {
  131. $sign = strtolower($sign);
  132. $exist = ArticleCategory::find()->where(['lower(sign)' => $sign])->one();
  133. if ($exist != null) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. /**
  139. * 获取文章分类详情
  140. */
  141. public function actionArticleCategoryDetail()
  142. {
  143. $request = \Yii::$app->getRequest()->get();
  144. $request['id'] = isset($request['id']) ? (int)$request['id'] : '';
  145. $category = ArticleCategory::find();
  146. $rs = $category->where(['id' => $request['id']])->asArray()->one();
  147. if ($rs) {
  148. if ($rs['parent_id']) {
  149. $parent = $category->where(['id' => $rs['parent_id']])->asArray()->one();
  150. $rs['parent'] = $parent;
  151. }
  152. }
  153. return $this->outJson(1, $rs);
  154. }
  155. /**
  156. * 删除文章分类
  157. */
  158. public function actionArticleCategoryDelete()
  159. {
  160. $id = \Yii::$app->getRequest()->get('id');
  161. $model = ArticleCategory::findOne(['id' => $id]);
  162. $children = intval(ArticleCategory::find()->where(['id' => $id])->orWhere(['parent_id' => $id])->count());
  163. if ($children >= 2) {
  164. return $this->outJson(0, [], "文章分类\"".$model->name."\"存在下级分类,删除失败!");
  165. }
  166. $article = Article::find();
  167. $articleQuery = $article->where(['article_category_id' => $model->id]);
  168. $count = intval($articleQuery->count());
  169. if ($count > 0) {
  170. return $this->outJson(0, [], "文章分类\"".$model->name."\"下存在文章,删除失败!");
  171. }
  172. $rs = ArticleCategory::deleteAll(['id' => $id]);
  173. return $this->outJson(1, $rs);
  174. }
  175. }