CategoryController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace backend\controllers;
  3. use common\enums\StatusEnum;
  4. use common\models\Category;
  5. use Yii;
  6. use yii\data\ActiveDataProvider;
  7. use yii\helpers\Url;
  8. use yii\filters\VerbFilter;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\web\Response;
  12. /**
  13. * CaetgoryController implements the CRUD actions for Category model.
  14. */
  15. class CategoryController extends Controller
  16. {
  17. public function behaviors()
  18. {
  19. return [
  20. 'verbs' => [
  21. 'class' => VerbFilter::className(),
  22. 'actions' => [
  23. 'delete' => ['post'],
  24. ],
  25. ],
  26. ];
  27. }
  28. public function actions()
  29. {
  30. return [
  31. 'ajax-update-field' => [
  32. 'class' => 'common\\actions\\AjaxUpdateFieldAction',
  33. 'allowFields' => ['status'],
  34. 'findModel' => [$this, 'findModel']
  35. ],
  36. 'switcher' => [
  37. 'class' => 'backend\widgets\grid\SwitcherAction'
  38. ],
  39. 'position' => [
  40. 'class' => 'backend\\actions\\Position',
  41. 'returnUrl' => Url::current()
  42. ]
  43. ];
  44. }
  45. /**
  46. * Lists all Category models.
  47. *
  48. * @return mixed
  49. */
  50. public function actionIndex()
  51. {
  52. $dataProvider = new ActiveDataProvider(['query' => Category::find()->active()->orderBy('sort asc'), 'pagination' => false]);
  53. return $this->render('index', [
  54. 'dataProvider' => $dataProvider,
  55. 'pagination' => false
  56. ]);
  57. }
  58. /**
  59. * Displays a single Category model.
  60. *
  61. * @param int $id
  62. *
  63. * @return mixed
  64. */
  65. public function actionView($id)
  66. {
  67. return $this->render('view', [
  68. 'model' => $this->findModel($id),
  69. ]);
  70. }
  71. /**
  72. * Creates a new Category model.
  73. * If creation is successful, the browser will be redirected to the 'view' page.
  74. * @param integer $id
  75. * @return mixed
  76. */
  77. public function actionCreate($id = 0)
  78. {
  79. $model = new Category();
  80. $model->loadDefaultValues();
  81. $model->pid = $id;
  82. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  83. return $this->redirect(['index']);
  84. } else {
  85. //二级分类id列表
  86. $childCateList = Category::find()->where(['type' => 0])->andWhere(['!=', 'pid', 0])->select('id')->column();
  87. return $this->render('create', [
  88. 'model' => $model,
  89. 'childCateList' => $childCateList,
  90. ]);
  91. }
  92. }
  93. /**
  94. * Updates an existing Category model.
  95. * If update is successful, the browser will be redirected to the 'view' page.
  96. *
  97. * @param int $id
  98. *
  99. * @return mixed
  100. */
  101. public function actionUpdate($id)
  102. {
  103. $model = $this->findModel($id);
  104. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  105. return $this->redirect(['index']);
  106. } else {
  107. //二级分类id列表
  108. $childCateList = Category::find()->where(['type' => 0])->andWhere(['!=', 'pid', 0])->select('id')->column();
  109. return $this->render('update', [
  110. 'model' => $model,
  111. 'childCateList' => $childCateList,
  112. ]);
  113. }
  114. }
  115. /**
  116. * Deletes an existing Category model.
  117. * If deletion is successful, the browser will be redirected to the 'index' page.
  118. *
  119. * @param int $id
  120. *
  121. * @return mixed
  122. */
  123. public function actionDelete($id)
  124. {
  125. $this->findModel($id)->softDelete();
  126. Yii::$app->cache->flush();
  127. Yii::$app->session->setFlash('success', '操作成功');
  128. return $this->redirect(['index']);
  129. }
  130. /**
  131. * Finds the Category model based on its primary key value.
  132. * If the model is not found, a 404 HTTP exception will be thrown.
  133. *
  134. * @param int $id
  135. *
  136. * @return Category the loaded model
  137. *
  138. * @throws NotFoundHttpException if the model cannot be found
  139. */
  140. public function findModel($id)
  141. {
  142. if (($model = Category::findOne($id)) !== null) {
  143. return $model;
  144. } else {
  145. throw new NotFoundHttpException('The requested page does not exist.');
  146. }
  147. }
  148. /**
  149. * 产业链列表
  150. * @param $id
  151. * @return array
  152. * @author jiang
  153. */
  154. public function actionChain($id = null)
  155. {
  156. $where = [];
  157. if (!empty($id)) {
  158. //二级分类
  159. $ids = Category::find()->where(['pid' => $id, 'type' => 0])->select('id')->column();
  160. $ids[] = $id;
  161. $where = ['in', 'pid', $ids];
  162. }
  163. $list = Category::find()->where(['type' => 1])->active()->andWhere($where)->orderBy('sort asc')->select('title')->column();
  164. Yii::$app->response->format = Response::FORMAT_JSON;
  165. return ['data' => $list];
  166. }
  167. }