From 19120f31e17259a673fad49d538b22ada050180d Mon Sep 17 00:00:00 2001 From: Yarik Date: Thu, 19 Nov 2015 11:27:09 +0200 Subject: [PATCH] fix broken --- frontend/controllers/OptionController.php | 171 +++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------ frontend/models/Option.php | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 144 deletions(-) diff --git a/frontend/controllers/OptionController.php b/frontend/controllers/OptionController.php index 973405e..ee6fcd7 100644 --- a/frontend/controllers/OptionController.php +++ b/frontend/controllers/OptionController.php @@ -61,95 +61,14 @@ 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]); + $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]); } else { - return $this->render('create', [ - 'model' => $model, - 'modeldb' => $modeldb, - 'model_id' => $model_id, - 'fields' => $fields - ]); + return $this->redirect(['index']); } } - + /** * Updates an existing Option model. * If update is successful, the browser will be redirected to the 'view' page. @@ -158,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 - ]); } /** @@ -226,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/models/Option.php b/frontend/models/Option.php index f615d15..ed4383f 100644 --- a/frontend/models/Option.php +++ b/frontend/models/Option.php @@ -53,4 +53,145 @@ class Option extends \yii\db\ActiveRecord public function getLangs() { return (new Language())->find()->where(['>', 'language_id', 0])->asArray()->all(); } + public static function change($id, $post, $modeldb, $model_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; + } + } + $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 array('id' => $id, 'models' => $models, 'modelslang' => $modelslang, 'success' => true); + } else { + return array( + 'models' => $models, + 'modellang' => $modellang, + 'modeldb' => $modeldb, + 'model_id' => $model_id, + 'success' => false + ); + } + } + return array( + 'models' => $models, + 'modellang' => $modellang, + 'modeldb' => $modeldb, + 'model_id' => $model_id, + 'success' => false + ); + } + public static function create($post, $modeldb, $model_id, $fields) { + $model = new Option(); + $modellang = new OptionLang(); + 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]->addError('value', 'Value must be set'); + 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 array('models' => $models, 'modelslang' => $modelslang, 'success' => true); + } else { + return array( + 'models' => $models, + 'modellang' => $modelslang, + 'modeldb' => $modeldb, + 'model_id' => $model_id, + 'fields' => $fields, + 'success' => false + ); + } + } else { + return array( + 'model' => $model, + 'modeldb' => $modeldb, + 'model_id' => $model_id, + 'fields' => $fields, + 'success' => false + ); + } + } } -- libgit2 0.21.4