ArticleCategoryController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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['language'] = isset($request['language']) ? (int)$request['language'] : '';
  87. $request['order_list'] = isset($request['order_list']) ? (int)$request['order_list'] : '';
  88. $model->setAttributes($request);
  89. if ($model->save()) {
  90. if ($request['parent_id']) {
  91. $model->path = $request['parent_id'].','.$model->id;
  92. } else {
  93. $model->path = (string)$model->id;
  94. }
  95. $model->save();
  96. return $this->outJson(1, $model->id);
  97. } else {
  98. return $this->outJson(0, [], '新增分类失败');
  99. }
  100. }
  101. /**
  102. * 检测sign是否唯一
  103. */
  104. public function actionCheckSign()
  105. {
  106. $response = [];
  107. $request = \Yii::$app->getRequest()->get();
  108. $data['oldValue'] = isset($request['oldValue']) ? $request['oldValue'] : '';
  109. $data['sign'] = isset($request['sign']) ? $request['sign'] : '';
  110. $isExist = $this->isUniqueBySign($data['oldValue'], $data['sign']);
  111. if ($isExist) {
  112. $response['isSuccess'] = true;
  113. } else {
  114. $response['isSuccess'] = false;
  115. }
  116. return $this->outJson(1, $response);
  117. }
  118. private function isUniqueBySign($oldValue, $newSign)
  119. {
  120. if (strtolower($oldValue) == strtolower($newSign)) {
  121. return true;
  122. } else {
  123. if ($this->isExistBySign($newSign)) {
  124. return false;
  125. } else {
  126. return true;
  127. }
  128. }
  129. }
  130. private function isExistBySign($sign)
  131. {
  132. $sign = strtolower($sign);
  133. $exist = ArticleCategory::find()->where(['lower(sign)' => $sign])->one();
  134. if ($exist != null) {
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * 获取文章分类详情
  141. */
  142. public function actionArticleCategoryDetail()
  143. {
  144. $request = \Yii::$app->getRequest()->get();
  145. $request['id'] = isset($request['id']) ? (int)$request['id'] : '';
  146. $category = ArticleCategory::find();
  147. $rs = $category->where(['id' => $request['id']])->asArray()->one();
  148. if ($rs) {
  149. if ($rs['parent_id']) {
  150. $parent = $category->where(['id' => $rs['parent_id']])->asArray()->one();
  151. $rs['parent'] = $parent;
  152. }
  153. }
  154. return $this->outJson(1, $rs);
  155. }
  156. /**
  157. * 删除文章分类
  158. */
  159. public function actionArticleCategoryDelete()
  160. {
  161. $id = \Yii::$app->getRequest()->get('id');
  162. $model = ArticleCategory::findOne(['id' => $id]);
  163. $children = intval(ArticleCategory::find()->where(['id' => $id])->orWhere(['parent_id' => $id])->count());
  164. if ($children >= 2) {
  165. return $this->outJson(0, [], "文章分类\"".$model->name."\"存在下级分类,删除失败!");
  166. }
  167. $article = Article::find();
  168. $articleQuery = $article->where(['article_category_id' => $model->id]);
  169. $count = intval($articleQuery->count());
  170. if ($count > 0) {
  171. return $this->outJson(0, [], "文章分类\"".$model->name."\"下存在文章,删除失败!");
  172. }
  173. $rs = ArticleCategory::deleteAll(['id' => $id]);
  174. return $this->outJson(1, $rs);
  175. }
  176. }