public function actionEdit($parent_id, $id = null) { $object = Object::getForClass(Page::className()); /** @var null|Page|HasProperties $model */ $model = new Page(); $model->published = 1; if ($id !== null) { $model = Page::findOne($id); if ($model === null) { throw new NotFoundHttpException(); } } $model->parent_id = $parent_id; $event = new BackendEntityEditEvent($model); $this->trigger(self::BACKEND_PAGE_EDIT, $event); $post = \Yii::$app->request->post(); if ($event->isValid && $model->load($post)) { $saveStateEvent = new BackendEntityEditEvent($model); $this->trigger(self::BACKEND_PAGE_EDIT_SAVE, $saveStateEvent); if ($saveStateEvent->isValid && $model->validate()) { $save_result = $model->save(); $model->saveProperties($post); 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::BACKEND_PAGE_AFTER_SAVE, $modelAfterSaveEvent); $this->runAction('save-info', ['model_id' => $model->id]); Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved')); $returnUrl = Yii::$app->request->get('returnUrl', ['/page/backend/index']); switch (Yii::$app->request->post('action', 'save')) { case 'next': return $this->redirect(['/page/backend/edit', 'returnUrl' => $returnUrl, 'parent_id' => Yii::$app->request->get('parent_id', null)]); case 'back': return $this->redirect($returnUrl); default: return $this->redirect(Url::toRoute(['/page/backend/edit', 'id' => $model->id, 'returnUrl' => $returnUrl, 'parent_id' => $model->parent_id])); } } else { \Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data')); } } } return $this->render('page-form', ['model' => $model, 'object' => $object]); }
/** * @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]); }
public function actionEdit($parent_id = null, $id = null) { if (null === $parent_id) { throw new NotFoundHttpException(); } if (null === ($object = Object::getForClass(Category::className()))) { throw new ServerErrorHttpException(); } /** @var null|Category|HasProperties $model */ $model = null; if (null !== $id) { $model = Category::findById($id, null, null); } else { $parent = Category::findById($parent_id, null, null); if ($parent_id === '0' || !is_null($parent)) { $model = new Category(); $model->loadDefaultValues(); $model->parent_id = $parent_id; if ($parent_id !== '0') { $model->category_group_id = $parent->category_group_id; } } else { throw new ServerErrorHttpException(); } } if (null === $model) { throw new ServerErrorHttpException(); } $event = new BackendEntityEditEvent($model); $this->trigger(self::BACKEND_CATEGORY_EDIT, $event); $post = \Yii::$app->request->post(); if ($event->isValid && $model->load($post) && $model->validate()) { $saveStateEvent = new BackendEntityEditEvent($model); $this->trigger(self::BACKEND_CATEGORY_EDIT_SAVE, $saveStateEvent); $save_result = $model->save(); $model->saveProperties($post); 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::BACKEND_CATEGORY_AFTER_SAVE, $modelAfterSaveEvent); $this->runAction('save-info', ['model_id' => $model->id]); Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); $returnUrl = Yii::$app->request->get('returnUrl', ['index']); switch (Yii::$app->request->post('action', 'save')) { 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->parent_id])); } } else { throw new ServerErrorHttpException(); } } return $this->render('category-form', ['model' => $model, 'object' => $object]); }