From 6d49b5d2004101004f75928cc12312981d34b144 Mon Sep 17 00:00:00 2001 From: Yarik Date: Thu, 19 Nov 2015 10:34:42 +0200 Subject: [PATCH] Удалил ненужный код, добавил удаление всех дочерних элементов. --- frontend/controllers/OptionController.php | 178 +++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------------------- frontend/views/layouts/main.php | 5 +---- 2 files changed, 24 insertions(+), 159 deletions(-) diff --git a/frontend/controllers/OptionController.php b/frontend/controllers/OptionController.php index 77260b6..ee6fcd7 100644 --- a/frontend/controllers/OptionController.php +++ b/frontend/controllers/OptionController.php @@ -61,95 +61,6 @@ class OptionController extends Controller */ public function actionCreate() { - $model = new Option(); - $modellang = new OptionLang(); - $modeldb = 'user'; - $model_id = '10'; - $fields = [['name' => 'phone', 'template' => 'input'], ['name' => 'adres', 'template' => 'input']]; - $post = \Yii::$app->request->post(); - if(!empty($post['Option'])) { - $ok = 1; - $parentid = null; - $models = array(); - foreach($post['Option'] as $index => $option) { - if(in_array($index, array('model', 'model_id')) && $index !== 0) { continue; } - $first = 1; - foreach($option['value'] as $key => $value) { - $models[$index][$key] = new Option(); - $models[$index][$key]->model = $post['Option']['model']; - $models[$index][$key]->model_id = $post['Option']['model_id']; - $models[$index][$key]->template = $option['template']; - $models[$index][$key]->name = $key; - if(!$first) { - $models[$index][$key]->parent_id = $parentid; - } - $modelslang[$index][$key][0] = new OptionLang(); - if(!empty($value) && $models[$index][$key]->save()) { - if($first) { - $parentid = $models[$index][$key]->option_id; - } - $modelslang[$index][$key][0]->id = $models[$index][$key]->option_id; - $modelslang[$index][$key][0]->lang_id = 0; - $modelslang[$index][$key][0]->value = $value; - if($modelslang[$index][$key][0]->save()) { - if(!empty($option['lang'][$key])) { - foreach($option['lang'][$key] as $code => $lang) { - if(!empty($lang)) { - $modelslang[$index][$key][$code] = new OptionLang(); - $modelslang[$index][$key][$code]->id = $models[$index][$key]->option_id; - $modelslang[$index][$key][$code]->lang_id = $code; - $modelslang[$index][$key][$code]->value = $lang; - if(!$modelslang[$index][$key][$code]->save()) { - $ok = 0; - } - } - } - } - } - } else { - $models[$index][$key]->validate(); - $modelslang[$index][$key][0]->validate(); - $modelslang[$index][$key][0]; - if(!empty($option['lang'][$key])) { - foreach($option['lang'][$key] as $code => $lang) { - if(!empty($lang)) { - $modelslang[$index][$key][$code] = new OptionLang(); - $modelslang[$index][$key][$code]->id = $models[$index][$key]->option_id; - $modelslang[$index][$key][$code]->lang_id = $code; - $modelslang[$index][$key][$code]->value = $lang; - } - } - } - $ok = 0; - } - $first = 0; - } - } - if($ok) { - return $this->redirect(['view', 'id' => $parentid]); - } else { - return $this->render('create', [ - 'model' => $model, - 'models' => $models, - 'modellang' => $modelslang, - 'modeldb' => $modeldb, - 'model_id' => $model_id, - 'fields' => $fields - ]); - } - } - if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect(['view', 'id' => $model->option_id]); - } else { - return $this->render('create', [ - 'model' => $model, - 'modeldb' => $modeldb, - 'model_id' => $model_id, - 'fields' => $fields - ]); - } - } - public function actionTest() { $form[0] = Option::create(\Yii::$app->request->post(), 'User', 10, [['name' => 'phone', 'template' => 'text'], ['name' => 'adres', 'template' => 'text']]); if($form[0]['success'] == false) { return $this->render('create', ['forms' => $form]); @@ -157,14 +68,7 @@ class OptionController extends Controller return $this->redirect(['index']); } } - public function actionTest2($id) { - $form[0] = Option::change($id, \Yii::$app->request->post(), 'User', 10); - if($form[0]['success'] == false) { - return $this->render('update', ['forms' => $form]); - } else { - return $this->redirect(['view', 'id' => $id]); - } - } + /** * Updates an existing Option model. * If update is successful, the browser will be redirected to the 'view' page. @@ -173,64 +77,12 @@ class OptionController extends Controller */ public function actionUpdate($id) { - $models[$id] = Option::findOne($id); - $modellang[$id] = array(); - $langs = OptionLang::findAll(['id' => $id]); - foreach($langs as $lang) { - $modellang[$id][$lang->lang_id] = $lang; - } - $children = (new Option())->find()->where(['parent_id' => $id])->all(); - foreach($children as $child) { - $models[$child->option_id] = $child; - $modellang[$child->option_id] = array(); - $langs = OptionLang::findAll(['id' =>$child->option_id]); - foreach($langs as $lang) { - $modellang[$child->option_id][$lang->lang_id] = $lang; - } - } - $modeldb = 'user'; - $model_id = '10'; - $fields = [['name' => 'phone', 'template' => 'input'], ['name' => 'adres', 'template' => 'input']]; - $post = \Yii::$app->request->post(); - $ok = 1; - if(!empty($post)) { - foreach($post['Option'] as $key => $option) { - if(in_array($key, array('model', 'model_id'))) { continue; } - $modellang[$key][0]->value = $option['value'][$models[$key]->name]; - if(!$modellang[$key][0]->save()) { - $ok = 0; - } - foreach($option['lang'] as $lang_id => $lang) { - if(empty($modellang[$key][$lang_id])) { - $modellang[$key][$lang_id] = new OptionLang(); - $modellang[$key][$lang_id]->id = $models[$key]->option_id; - $modellang[$key][$lang_id]->lang_id = $lang_id; - $modellang[$key][$lang_id]->value = $lang; - } else { - $modellang[$key][$lang_id]->value = $lang; - } - if(!$modellang[$key][$lang_id]->save()) { - $ok = 0; - } - } - } - if($ok) { - return $this->redirect(['view', 'id' => $id]); - } else { - return $this->render('update', [ - 'models' => $models, - 'modellang' => $modellang, - 'modeldb' => $modeldb, - 'model_id' => $model_id - ]); - } + $form[0] = Option::change($id, \Yii::$app->request->post(), 'User', 10); + if($form[0]['success'] == false) { + return $this->render('update', ['forms' => $form]); + } else { + return $this->redirect(['view', 'id' => $id]); } - return $this->render('update', [ - 'models' => $models, - 'modellang' => $modellang, - 'modeldb' => $modeldb, - 'model_id' => $model_id - ]); } /** @@ -241,7 +93,23 @@ class OptionController extends Controller */ public function actionDelete($id) { - $this->findModel($id)->delete(); + $model = $this->findModel($id); + $children = $model->hasMany(Option::className(), ['parent_id' => 'option_id'])->all(); + $langs = array(); + if(!empty($children)) { + foreach($children as $child) { + $langs = OptionLang::findAll(['id' => $child->option_id]); + foreach($langs as $lang) { + $lang->delete(); + } + $child->delete(); + } + } + $langs = OptionLang::findAll(['id' => $id]); + foreach($langs as $lang) { + $lang->delete(); + } + $model->delete(); return $this->redirect(['index']); } diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index c03efd6..ae55fd5 100644 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -35,10 +35,7 @@ AppAsset::register($this); ] ]); - echo Nav::widget([ - 'options' => ['class' => 'navbar-nav navbar-right'], - 'items' => (new MenuTree())->build('TOP') - ]); + /* $menuItems = [ ['label' => 'Home', 'url' => ['/site/index']], -- libgit2 0.21.4