/** * @param null $id * @return string|\yii\web\Response * @throws NotFoundHttpException * @throws ServerErrorHttpException * @throws \Exception * @throws \yii\base\InvalidRouteException */ public function actionEdit($id = null) { /* * @todo Продумать механизм сохранения изображений для нового продукта. * Сейчас для нового продукта скрывается форма добавления изображений. */ if (null === ($object = Object::getForClass(Product::className()))) { throw new ServerErrorHttpException(); } /** @var null|Product|HasProperties|\devgroup\TagDependencyHelper\ActiveRecordHelper $model */ $model = null; $parent = null; if (null === $id) { $model = new Product(); $model->loadDefaultValues(); $parent_id = Yii::$app->request->get('owner_id', 0); if (0 !== intval($parent_id) && null !== Product::findById($parent_id)) { $model->parent_id = $parent_id; } $model->measure_id = $this->module->defaultMeasureId; } else { $model = Product::findById($id, null); if (null !== $model && $model->parent_id > 0) { $parent = Product::findById($model->parent_id, null); } } if (null === $model) { throw new NotFoundHttpException(); } $model->loadRelatedProductsArray(); $event = new BackendEntityEditEvent($model); $this->trigger(self::EVENT_BACKEND_PRODUCT_EDIT, $event); $post = \Yii::$app->request->post(); if ($event->isValid && $model->load($post)) { $saveStateEvent = new BackendEntityEditEvent($model); $this->trigger(self::EVENT_BACKEND_PRODUCT_EDIT_SAVE, $saveStateEvent); if ($model->validate()) { if (isset($post['GeneratePropertyValue'])) { $generateValues = $post['GeneratePropertyValue']; } else { $generateValues = []; } if (isset($post['PropertyGroup'])) { $model->option_generate = Json::encode(['group' => $post['PropertyGroup']['id'], 'values' => $generateValues]); } else { $model->option_generate = ''; } $save_result = $model->save(); $model->saveProperties($post); $model->saveRelatedProducts(); if (null !== ($view_object = ViewObject::getByModel($model, true))) { if ($view_object->load($post, 'ViewObject')) { if ($view_object->view_id <= 0) { $view_object->delete(); } else { $view_object->save(); } } } if ($save_result) { $modelAfterSaveEvent = new BackendEntityEditEvent($model); $this->trigger(self::EVENT_BACKEND_PRODUCT_AFTER_SAVE, $modelAfterSaveEvent); $categories = isset($post['Product']['categories']) ? $post['Product']['categories'] : []; $model->saveCategoriesBindings($categories); $this->runAction('save-info', ['model_id' => $model->id]); $model->invalidateTags(); $action = Yii::$app->request->post('action', 'save'); if (Yii::$app->request->post(HasProperties::FIELD_ADD_PROPERTY_GROUP) || Yii::$app->request->post(HasProperties::FIELD_REMOVE_PROPERTY_GROUP)) { $action = 'save'; } $returnUrl = Yii::$app->request->get('returnUrl', ['index']); switch ($action) { case 'next': return $this->redirect(['edit', 'returnUrl' => $returnUrl, 'parent_id' => Yii::$app->request->get('parent_id', null)]); case 'back': return $this->redirect($returnUrl); default: return $this->redirect(Url::toRoute(['edit', 'id' => $model->id, 'returnUrl' => $returnUrl, 'parent_id' => $model->main_category_id])); } } else { Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data')); } } else { Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data')); } } $items = ArrayHelper::map(Category::find()->all(), 'id', 'name'); return $this->render('product-form', ['object' => $object, 'model' => $model, 'items' => $items, 'selected' => $model->getCategoryIds(), 'parent' => $parent]); }