[ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => [ 'login', 'error', ], 'allow' => true, ], [ 'allow' => true, 'roles' => [ '@' ], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => [ 'POST' ], ], ], ]; } /** * Lists all OptionGroup models. * * @return mixed */ public function actionIndex() { $searchModel = $this->createSearchModel(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render( 'index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ] ); } /** * Displays a single ProductOptionGroupCompl model. * * @param integer $id * * @return mixed */ public function actionView($id) { return $this->render( 'view', [ 'model' => $this->findModel($id), ] ); } /** * Creates a new OptionGroup model. * If creation is successful, the browser will be redirected to the 'view' page. * * @return mixed */ public function actionCreate() { $model = $this->createModel(); $model->generateLangs(); if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { $categories = Category::find() ->where([ 'id' => \Yii::$app->request->post('categoryIds') ]) ->all(); $model->linkMany('categories', $categories); return $this->redirect( [ 'view', 'id' => $model->id, ] ); } return $this->render( 'create', [ 'model' => $model, 'modelLangs' => $model->modelLangs, ] ); } /** * Updates an existing OptionGroup model. * If update is successful, the browser will be redirected to the 'view' page. * * @param integer $id * * @return mixed */ public function actionUpdate($id) { // var_dump(\Yii::$app->request->post('Categories'));die(); $model = $this->findModel($id); $model->generateLangs(); $model->categoryIds = ArrayHelper::map( $model->categories, 'id', 'lang.title' ); if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { $categories = \Yii::$app->request->post('Categories') ?? []; $this->saveCategories($model, $categories); return $this->redirect( [ 'view', 'id' => $model->id, ] ); } return $this->render( 'update', [ 'model' => $model, 'modelLangs' => $model->modelLangs, ] ); } /** * Deletes an existing OptionGroup model. * If deletion is successful, the browser will be redirected to the 'index' page. * * @param integer $id * * @return mixed */ public function actionDelete($id) { $this->findModel($id) ->delete(); return $this->redirect([ 'index' ]); } /** * Finds the OptionGroup model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param integer $id * * @return OptionGroup the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id): OptionGroup { if (( $model = $this->findOne($id) ) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } /** * @param \artbox\catalog\models\OptionGroup $model * @param array $categories */ protected function saveCategories(OptionGroup $model, array $categories) { $batch = []; foreach ($categories as $id => $category) { $batch[] = [ $model->id, $id, $category[ 'sort' ] ?? null, $category[ 'status' ] ?? false, $category[ 'in_menu' ] ?? false, $category[ 'is_filter' ] ?? false, ]; } // var_dump($batch);die(); $transaction = \Yii::$app->db->beginTransaction(); try { $model->unlinkAll('categories', true); $model->insertCategories($batch); $transaction->commit(); } catch (\Exception $exception) { $transaction->rollBack(); die('error'); } } /** * Create exact model * * @return OptionGroup */ protected abstract function createModel(): OptionGroup; /** * Create exact search model * * @return OptionGroupSearch */ protected abstract function createSearchModel(): OptionGroupSearch; /** * Find exact model * * @param $id * * @return OptionGroup|null */ protected abstract function findOne($id); /** * Return new row for group-to-category table * * @return string */ public function actionGetRow() { $id = \Yii::$app->request->post('id'); $title = \Yii::$app->request->post('title'); if (empty($id) || empty($title)) { return ''; } $row = Html::beginTag('tr'); $row .= Html::tag('td', $title); $row .= Html::tag( 'td', Html::input( 'number', 'Categories[' . $id . '][sort]', null, [ 'class' => 'form-control', ] ) ); $row .= Html::tag( 'td', Html::checkbox( 'Categories[' . $id . '][status]', null, [ 'class' => 'flat', ] ) ); $row .= Html::tag( 'td', Html::checkbox( 'Categories[' . $id . '][in_menu]', null, [ 'class' => 'flat', ] ) ); $row .= Html::tag( 'td', Html::checkbox( 'Categories[' . $id . '][is_filter]', null, [ 'class' => 'flat', ] ) ); $row .= Html::tag( 'td', Html::a( '', '#', [ 'class' => 'delete-row', ] ) ); $row .= Html::endTag('tr'); return $row; } }