| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 | <?phpnamespace backend\controllers;use common\enums\StatusEnum;use common\models\Category;use Yii;use yii\data\ActiveDataProvider;use yii\helpers\Url;use yii\filters\VerbFilter;use yii\web\Controller;use yii\web\NotFoundHttpException;use yii\web\Response;/** * CaetgoryController implements the CRUD actions for Category model. */class CategoryController extends Controller{    public function behaviors()    {        return [            'verbs' => [                'class' => VerbFilter::className(),                'actions' => [                    'delete' => ['post'],                ],            ],        ];    }    public function actions()    {        return [            'ajax-update-field' => [                'class' => 'common\\actions\\AjaxUpdateFieldAction',                'allowFields' => ['status'],                'findModel' => [$this, 'findModel']            ],            'switcher' => [                'class' => 'backend\widgets\grid\SwitcherAction'            ],            'position' => [                'class' => 'backend\\actions\\Position',                'returnUrl' => Url::current()            ]        ];    }    /**     * Lists all Category models.     *     * @return mixed     */    public function actionIndex()    {        $dataProvider = new ActiveDataProvider(['query' => Category::find()->active()->orderBy('sort asc'),  'pagination' => false]);        return $this->render('index', [            'dataProvider' => $dataProvider,            'pagination' => false        ]);    }    /**     * Displays a single Category model.     *     * @param int $id     *     * @return mixed     */    public function actionView($id)    {        return $this->render('view', [            'model' => $this->findModel($id),        ]);    }    /**     * Creates a new Category model.     * If creation is successful, the browser will be redirected to the 'view' page.     * @param integer $id     * @return mixed     */    public function actionCreate($id = 0)    {        $model = new Category();        $model->loadDefaultValues();        $model->pid = $id;        if ($model->load(Yii::$app->request->post()) && $model->save()) {            return $this->redirect(['index']);        } else {            //二级分类id列表            $childCateList = Category::find()->where(['type' => 0])->andWhere(['!=', 'pid', 0])->select('id')->column();            return $this->render('create', [                'model' => $model,                'childCateList' => $childCateList,            ]);        }    }    /**     * Updates an existing Category model.     * If update is successful, the browser will be redirected to the 'view' page.     *     * @param int $id     *     * @return mixed     */    public function actionUpdate($id)    {        $model = $this->findModel($id);        if ($model->load(Yii::$app->request->post()) && $model->save()) {            return $this->redirect(['index']);        } else {            //二级分类id列表            $childCateList = Category::find()->where(['type' => 0])->andWhere(['!=', 'pid', 0])->select('id')->column();            return $this->render('update', [                'model' => $model,                'childCateList' => $childCateList,            ]);        }    }    /**     * Deletes an existing Category model.     * If deletion is successful, the browser will be redirected to the 'index' page.     *     * @param int $id     *     * @return mixed     */    public function actionDelete($id)    {        $this->findModel($id)->softDelete();        Yii::$app->cache->flush();        Yii::$app->session->setFlash('success', '操作成功');        return $this->redirect(['index']);    }    /**     * Finds the Category model based on its primary key value.     * If the model is not found, a 404 HTTP exception will be thrown.     *     * @param int $id     *     * @return Category the loaded model     *     * @throws NotFoundHttpException if the model cannot be found     */    public function findModel($id)    {        if (($model = Category::findOne($id)) !== null) {            return $model;        } else {            throw new NotFoundHttpException('The requested page does not exist.');        }    }    /**     * 产业链列表     * @param $id     * @return array     * @author jiang     */    public function actionChain($id = null)    {        $where = [];        if (!empty($id)) {            //二级分类            $ids = Category::find()->where(['pid' => $id, 'type' => 0])->select('id')->column();            $ids[] = $id;            $where = ['in', 'pid', $ids];        }        $list = Category::find()->where(['type' => 1])->active()->andWhere($where)->orderBy('sort asc')->select('title')->column();        Yii::$app->response->format = Response::FORMAT_JSON;        return ['data' => $list];    }}
 |