[ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => [ 'login', 'error', ], 'allow' => true, ], [ 'allow' => true, 'roles' => [ '@' ], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => [ 'POST' ], ], ], ]; } /** * Lists all Variant models. * * @param $product_id * * @return mixed */ public function actionIndex($product_id) { $product = $this->findProduct($product_id); $searchModel = new VariantSearch( [ 'product_id' => $product_id, ] ); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render( 'index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'product' => $product, ] ); } /** * Displays a single Variant model. * * @param integer $id * * @return mixed */ public function actionView($id) { return $this->render( 'view', [ 'model' => $this->findModel($id), ] ); } /** * Creates a new Variant model. * If creation is successful, the browser will be redirected to the 'view' page. * * @param $product_id * * @return mixed */ public function actionCreate($product_id) { /** * @var \yii\db\ActiveQuery $findShop */ if (class_exists('\artbox\stock\models\Shop')){ $findShop = call_user_func('\artbox\stock\models\Shop::find'); $shops = $findShop->with('lang')->all(); }else{ $shops = null; } $product = $this->findProduct($product_id); $model = new Variant( [ 'product_id' => $product_id, ] ); $model->generateLangs(); $groups_compl = []; $groups_excl = []; if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { $counts = \Yii::$app->request->post('counts'); print_r($counts); $insert = []; foreach ($counts as $key => $item){ if (!empty($item) or $item === '0'){ $insert[] = [$model->id, $key, $item]; } } VariantToShop::deleteAll(['variant_id' => $model->id]); \Yii::$app->db->createCommand()->batchInsert('variant_to_shop', ['variant_id', 'shop_id', 'count'],$insert)->execute(); return $this->redirect( [ 'view', 'id' => $model->id, ] ); } return $this->render( 'create', [ 'model' => $model, 'modelLangs' => $model->modelLangs, 'product' => $product, 'groups_compl' => $groups_compl, 'groups_excl' => $groups_excl, 'shops' => $shops, ] ); } /** * Updates an existing Variant model. * If update is successful, the browser will be redirected to the 'view' page. * * @param integer $id * * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $model->generateLangs(); if (class_exists('\artbox\stock\models\Shop')){ $findShop = call_user_func('\artbox\stock\models\Shop::find'); $shops = $findShop->with('lang')->all(); }else{ $shops = null; } $groups_compl = []; $groups_excl = []; if (!empty( $model->product->categories )) { $groups_compl = VariantOptionGroupCompl::find() ->innerJoin( 'variant_option_group_compl_to_category', 'variant_option_group_compl_to_category.variant_option_group_compl_id = variant_option_group_compl.id' ) ->where( [ 'variant_option_group_compl_to_category.category_id' => ArrayHelper::getColumn( $model->product->categories, 'id' ), ] ) ->with('options.lang') ->all(); $groups_excl = VariantOptionGroupExcl::find() ->innerJoin( 'variant_option_group_excl_to_category', 'variant_option_group_excl_to_category.variant_option_group_excl_id = variant_option_group_excl.id' ) ->where( [ 'variant_option_group_excl_to_category.category_id' => ArrayHelper::getColumn( $model->product->categories, 'id' ), ] ) ->with('options.lang') ->all(); } if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { if (!empty( \Yii::$app->request->post('Variant')[ 'variantOptionCompls' ] )) { $options = VariantOptionCompl::findAll($model->variantOptionCompls); $model->linkMany('variantOptionCompls', $options); } else { $model->unlinkAll('variantOptionCompls', true); } if (!empty( \Yii::$app->request->post('Variant')[ 'variantOptionExcls' ] )) { $options = VariantOptionExcl::findAll($model->variantOptionExcls); $model->linkMany('variantOptionExcls', $options); } else { $model->unlinkAll('variantOptionExcls', true); } if (class_exists('\artbox\stock\models\VariantToShop')) { $counts = \Yii::$app->request->post('counts'); $insert = []; foreach ($counts as $key => $item){ if (!empty($item) or $item === '0'){ $insert[] = [$model->id, $key, $item]; } } VariantToShop::deleteAll(['variant_id' => $model->id]); \Yii::$app->db->createCommand()->batchInsert('variant_to_shop', ['variant_id', 'shop_id', 'count'],$insert)->execute(); } return $this->redirect( [ 'view', 'id' => $model->id, ] ); } return $this->render( 'update', [ 'model' => $model, 'modelLangs' => $model->modelLangs, 'groups_compl' => $groups_compl, 'groups_excl' => $groups_excl, 'shops' => $shops, ] ); } /** * Deletes an existing Variant model. * If deletion is successful, the browser will be redirected to the 'index' page. * * @param integer $id * * @return mixed */ public function actionDelete($id) { $model = $this->findModel($id); $product_id = $model->product_id; $model->delete(); return $this->redirect( [ 'index', 'product_id' => $product_id, ] ); } /** * Finds the Variant model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param integer $id * * @return Variant the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (( $model = Variant::findOne($id) ) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } /** * Finds Product by $id * * @param $id * * @return Product * @throws \yii\web\NotFoundHttpException */ protected function findProduct($id) { if (( $model = Product::findOne($id) ) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page not exist'); } } }