/**
  * Save data action for ShopAttribute.
  * If user has not permissions to do this action, a 403 HTTP exception will be thrown.
  *
  * @param integer $languageId
  * @param integer $attrId
  * @return mixed
  * @throws ForbiddenHttpException if user has not permissions
  */
 public function actionSave($languageId = null, $attrId = null)
 {
     if (empty($languageId)) {
         $languageId = Language::getCurrent()->id;
     }
     $selectedLanguage = Language::findOne($languageId);
     $languages = Language::find()->all();
     $attributeType = ArrayHelper::toArray(ShopAttributeType::find()->all(), ['bl\\cms\\shop\\common\\entities\\ShopAttributeType' => ['id', 'title' => function ($attributeType) {
         return \Yii::t('shop', $attributeType->title);
     }]]);
     if (empty($attrId)) {
         $model = new ShopAttribute();
         $modelTranslation = new ShopAttributeTranslation();
         $searchAttributeValueModel = null;
         $dataProviderAttributeValue = null;
     } else {
         $searchAttributeValueModel = new SearchAttributeValue();
         $dataProviderAttributeValue = $searchAttributeValueModel->search(Yii::$app->request->queryParams);
         $model = ShopAttribute::findOne($attrId);
         $modelTranslation = ShopAttributeTranslation::find()->where(['attr_id' => $attrId, 'language_id' => $languageId])->one();
         if (empty($modelTranslation)) {
             $modelTranslation = new ShopAttributeTranslation();
         }
     }
     if (Yii::$app->request->isPost) {
         $model->load(Yii::$app->request->post());
         $modelTranslation->load(Yii::$app->request->post());
         if ($model->validate()) {
             $model->save();
             $modelTranslation->attr_id = $model->id;
             $modelTranslation->language_id = $languageId;
         }
         if ($modelTranslation->validate()) {
             $modelTranslation->save();
             Yii::$app->getSession()->setFlash('success', 'Data were successfully modified.');
             return $this->redirect(['save', 'attrId' => $model->id, 'languageId' => $languageId]);
         }
     }
     return $this->render('save', ['attribute' => $model, 'attributeTranslation' => $modelTranslation, 'attributeType' => $attributeType, 'languages' => $languages, 'selectedLanguage' => $selectedLanguage, 'searchModel' => $searchAttributeValueModel, 'dataProvider' => $dataProviderAttributeValue, 'valueModel' => new ShopAttributeValue(), 'valueModelTranslation' => new ShopAttributeValueTranslation(), 'attributeTextureModel' => new AttributeTextureForm()]);
 }
Example #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getType()
 {
     return $this->hasOne(ShopAttributeType::className(), ['id' => 'type_id']);
 }