languages = $languages; parent::__construct($id, $module, $config); } /** * @inheritdoc */ public function behaviors() { return [ 'verbs' => [ 'class' => VerbFilter::class, 'actions' => [ 'index' => [ 'POST', 'GET', ], 'delete' => [ 'POST' ], ], ], ]; } /** * Lists all Group models. * * @param string $sort * * @return mixed */ public function actionIndex($sort = null) { $sortParam = SORT_ASC; if ($sort) { if ($sort === 'asc') { $sortParam = SORT_ASC; } elseif ($sort === 'desc') { $sortParam = SORT_DESC; } } $dataProvider = new ArrayDataProvider( [ 'allModels' => Group::find() ->orderBy( [ 'sort' => $sortParam, ] ) ->with('language') ->asArray() ->all(), 'sort' => [ 'attributes' => [ 'sort', ], ], 'pagination' => [ 'pageSize' => 20, ], ] ); if (\Yii::$app->request->isPost) { \Yii::$app->response->format = Response::FORMAT_JSON; return $dataProvider->getModels(); } return $this->render( 'index', [ 'provider' => $dataProvider, ] ); } public function actionLanguages() { \Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::toArray($this->languages->getActive()); } /** * @param $id * * @return string * @throws \yii\web\NotFoundHttpException */ public function actionView($id) { return $this->render( 'view', [ 'model' => $this->findModel($id), ] ); } /** * Creates a new Group model. * If creation is successful, the browser will be redirected to the 'view' page. * * @return mixed */ public function actionCreate() { $model = new Group(); $post = Yii::$app->request->post(); if ($model->load($post) && Model::loadMultiple($model->getVariationModels(), $post) && $model->save()) { return $this->redirect([ 'index' ]); } return $this->render( 'create', [ 'model' => $model, ] ); } /** * @param $id * * @return string|\yii\web\Response * @throws \yii\web\NotFoundHttpException */ public function actionUpdate($id) { $model = $this->findModel($id); $post = Yii::$app->request->post(); if ($model->load($post) && Model::loadMultiple($model->getVariationModels(), $post) && $model->save()) { return $this->redirect([ 'index' ]); } return $this->render( 'update', [ 'model' => $model, ] ); } public function actionOptions($id) { \Yii::$app->response->format = Response::FORMAT_JSON; $manipulator = new OptionManipulator(); return $manipulator->findAll($id); } public function actionOption($id) { \Yii::$app->response->format = Response::FORMAT_JSON; $manipulator = new OptionManipulator(); return $manipulator->findOne($id); } public function actionSort($id, $sort) { \Yii::$app->response->format = Response::FORMAT_JSON; $model = $this->findModel($id); $model->sort = $sort; if ($model->save()) { return 'Model saved'; } else { throw new BadRequestHttpException(); } } public function actionSortOption($id, $sort, $groupId) { \Yii::$app->response->format = Response::FORMAT_JSON; $option = Option::findOne($id); if ($option) { $option->sort = $sort; if ($option->save()) { return ( new OptionManipulator() )->findAll($groupId); } else { throw new ServerErrorHttpException(); } } else { throw new NotFoundHttpException("Can't find option by id"); } } /** * @param $id * * @return \yii\web\Response * @throws \Exception * @throws \yii\db\StaleObjectException * @throws \yii\web\NotFoundHttpException * @throws \Throwable */ public function actionDelete($id) { $this->findModel($id) ->delete(); return $this->redirect([ 'index' ]); } public function actionCreateOption() { $model = new OptionManipulator(); $model->load(Json::decode(\Yii::$app->request->post('data')), ''); return $model->create(); } public function actionUpdateOption() { $model = new OptionManipulator(); $model->load(Json::decode(\Yii::$app->request->post('data')), ''); return $model->update(); } public function actionDeleteOption($id) { $model = new OptionManipulator(); return $model->delete($id); } public function actionFindOptions($q = null) { $language_id = Language::getCurrent()->id; \Yii::$app->response->format = Response::FORMAT_JSON; if (!is_null($q)) { $query = new Query(); $query->select('option.id, (g.title || \' : \' || o.title) AS text') ->from('option') ->leftJoin('option_lang o', 'o.option_id = option.id') ->leftJoin('group', 'option.group_id = "group".id') ->leftJoin('group_lang g', 'g.group_id = "group".id') ->where( [ 'g.language_id' => $language_id, ] ) ->andWhere( [ 'o.language_id' => $language_id, ] ) ->andWhere( [ 'ilike', 'o.title', $q, ] ); $data = $query->all(); $out[ 'results' ] = $data; } return $out; } /** * Finds the Group model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param integer $id * * @return Group the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (( $model = Group::findOne($id) ) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }