Commit 839c0f764772b7da9ab39beb257bd8c7fa193236
1 parent
82a330b9
Переделал в МВЦ опции.
Showing
6 changed files
with
167 additions
and
24 deletions
Show diff stats
frontend/controllers/OptionController.php
@@ -149,7 +149,22 @@ class OptionController extends Controller | @@ -149,7 +149,22 @@ class OptionController extends Controller | ||
149 | ]); | 149 | ]); |
150 | } | 150 | } |
151 | } | 151 | } |
152 | - | 152 | + public function actionTest() { |
153 | + $form[0] = Option::create(\Yii::$app->request->post(), 'User', 10, [['name' => 'phone', 'template' => 'text'], ['name' => 'adres', 'template' => 'text']]); | ||
154 | + if($form[0]['success'] == false) { | ||
155 | + return $this->render('create', ['forms' => $form]); | ||
156 | + } else { | ||
157 | + return $this->redirect(['index']); | ||
158 | + } | ||
159 | + } | ||
160 | + public function actionTest2($id) { | ||
161 | + $form[0] = Option::change($id, \Yii::$app->request->post(), 'User', 10); | ||
162 | + if($form[0]['success'] == false) { | ||
163 | + return $this->render('update', ['forms' => $form]); | ||
164 | + } else { | ||
165 | + return $this->redirect(['view', 'id' => $id]); | ||
166 | + } | ||
167 | + } | ||
153 | /** | 168 | /** |
154 | * Updates an existing Option model. | 169 | * Updates an existing Option model. |
155 | * If update is successful, the browser will be redirected to the 'view' page. | 170 | * If update is successful, the browser will be redirected to the 'view' page. |
frontend/models/Option.php
@@ -53,4 +53,145 @@ class Option extends \yii\db\ActiveRecord | @@ -53,4 +53,145 @@ class Option extends \yii\db\ActiveRecord | ||
53 | public function getLangs() { | 53 | public function getLangs() { |
54 | return (new Language())->find()->where(['>', 'language_id', 0])->asArray()->all(); | 54 | return (new Language())->find()->where(['>', 'language_id', 0])->asArray()->all(); |
55 | } | 55 | } |
56 | + public static function change($id, $post, $modeldb, $model_id) { | ||
57 | + $models[$id] = Option::findOne($id); | ||
58 | + $modellang[$id] = array(); | ||
59 | + $langs = OptionLang::findAll(['id' => $id]); | ||
60 | + foreach($langs as $lang) { | ||
61 | + $modellang[$id][$lang->lang_id] = $lang; | ||
62 | + } | ||
63 | + $children = (new Option())->find()->where(['parent_id' => $id])->all(); | ||
64 | + foreach($children as $child) { | ||
65 | + $models[$child->option_id] = $child; | ||
66 | + $modellang[$child->option_id] = array(); | ||
67 | + $langs = OptionLang::findAll(['id' =>$child->option_id]); | ||
68 | + foreach($langs as $lang) { | ||
69 | + $modellang[$child->option_id][$lang->lang_id] = $lang; | ||
70 | + } | ||
71 | + } | ||
72 | + $ok = 1; | ||
73 | + if(!empty($post)) { | ||
74 | + foreach($post['Option'] as $key => $option) { | ||
75 | + if(in_array($key, array('model', 'model_id'))) { continue; } | ||
76 | + $modellang[$key][0]->value = $option['value'][$models[$key]->name]; | ||
77 | + if(!$modellang[$key][0]->save()) { | ||
78 | + $ok = 0; | ||
79 | + } | ||
80 | + foreach($option['lang'] as $lang_id => $lang) { | ||
81 | + if(empty($modellang[$key][$lang_id])) { | ||
82 | + $modellang[$key][$lang_id] = new OptionLang(); | ||
83 | + $modellang[$key][$lang_id]->id = $models[$key]->option_id; | ||
84 | + $modellang[$key][$lang_id]->lang_id = $lang_id; | ||
85 | + $modellang[$key][$lang_id]->value = $lang; | ||
86 | + } else { | ||
87 | + $modellang[$key][$lang_id]->value = $lang; | ||
88 | + } | ||
89 | + if(!$modellang[$key][$lang_id]->save()) { | ||
90 | + $ok = 0; | ||
91 | + } | ||
92 | + } | ||
93 | + } | ||
94 | + if($ok) { | ||
95 | + return array('id' => $id, 'models' => $models, 'modelslang' => $modelslang, 'success' => true); | ||
96 | + } else { | ||
97 | + return array( | ||
98 | + 'models' => $models, | ||
99 | + 'modellang' => $modellang, | ||
100 | + 'modeldb' => $modeldb, | ||
101 | + 'model_id' => $model_id, | ||
102 | + 'success' => false | ||
103 | + ); | ||
104 | + } | ||
105 | + } | ||
106 | + return array( | ||
107 | + 'models' => $models, | ||
108 | + 'modellang' => $modellang, | ||
109 | + 'modeldb' => $modeldb, | ||
110 | + 'model_id' => $model_id, | ||
111 | + 'success' => false | ||
112 | + ); | ||
113 | + } | ||
114 | + public static function create($post, $modeldb, $model_id, $fields) { | ||
115 | + $model = new Option(); | ||
116 | + $modellang = new OptionLang(); | ||
117 | + if(!empty($post['Option'])) { | ||
118 | + $ok = 1; | ||
119 | + $parentid = null; | ||
120 | + $models = array(); | ||
121 | + foreach($post['Option'] as $index => $option) { | ||
122 | + if(in_array($index, array('model', 'model_id')) && $index !== 0) { continue; } | ||
123 | + $first = 1; | ||
124 | + foreach($option['value'] as $key => $value) { | ||
125 | + $models[$index][$key] = new Option(); | ||
126 | + $models[$index][$key]->model = $post['Option']['model']; | ||
127 | + $models[$index][$key]->model_id = $post['Option']['model_id']; | ||
128 | + $models[$index][$key]->template = $option['template']; | ||
129 | + $models[$index][$key]->name = $key; | ||
130 | + if(!$first) { | ||
131 | + $models[$index][$key]->parent_id = $parentid; | ||
132 | + } | ||
133 | + $modelslang[$index][$key][0] = new OptionLang(); | ||
134 | + if(!empty($value) && $models[$index][$key]->save()) { | ||
135 | + if($first) { | ||
136 | + $parentid = $models[$index][$key]->option_id; | ||
137 | + } | ||
138 | + $modelslang[$index][$key][0]->id = $models[$index][$key]->option_id; | ||
139 | + $modelslang[$index][$key][0]->lang_id = 0; | ||
140 | + $modelslang[$index][$key][0]->value = $value; | ||
141 | + if($modelslang[$index][$key][0]->save()) { | ||
142 | + if(!empty($option['lang'][$key])) { | ||
143 | + foreach($option['lang'][$key] as $code => $lang) { | ||
144 | + if(!empty($lang)) { | ||
145 | + $modelslang[$index][$key][$code] = new OptionLang(); | ||
146 | + $modelslang[$index][$key][$code]->id = $models[$index][$key]->option_id; | ||
147 | + $modelslang[$index][$key][$code]->lang_id = $code; | ||
148 | + $modelslang[$index][$key][$code]->value = $lang; | ||
149 | + if(!$modelslang[$index][$key][$code]->save()) { | ||
150 | + $ok = 0; | ||
151 | + } | ||
152 | + } | ||
153 | + } | ||
154 | + } | ||
155 | + } | ||
156 | + } else { | ||
157 | + $models[$index][$key]->validate(); | ||
158 | + $modelslang[$index][$key][0]->validate(); | ||
159 | + $modelslang[$index][$key][0]->addError('value', 'Value must be set'); | ||
160 | + if(!empty($option['lang'][$key])) { | ||
161 | + foreach($option['lang'][$key] as $code => $lang) { | ||
162 | + if(!empty($lang)) { | ||
163 | + $modelslang[$index][$key][$code] = new OptionLang(); | ||
164 | + $modelslang[$index][$key][$code]->id = $models[$index][$key]->option_id; | ||
165 | + $modelslang[$index][$key][$code]->lang_id = $code; | ||
166 | + $modelslang[$index][$key][$code]->value = $lang; | ||
167 | + } | ||
168 | + } | ||
169 | + } | ||
170 | + $ok = 0; | ||
171 | + } | ||
172 | + $first = 0; | ||
173 | + } | ||
174 | + } | ||
175 | + if($ok) { | ||
176 | + return array('models' => $models, 'modelslang' => $modelslang, 'success' => true); | ||
177 | + } else { | ||
178 | + return array( | ||
179 | + 'models' => $models, | ||
180 | + 'modellang' => $modelslang, | ||
181 | + 'modeldb' => $modeldb, | ||
182 | + 'model_id' => $model_id, | ||
183 | + 'fields' => $fields, | ||
184 | + 'success' => false | ||
185 | + ); | ||
186 | + } | ||
187 | + } else { | ||
188 | + return array( | ||
189 | + 'model' => $model, | ||
190 | + 'modeldb' => $modeldb, | ||
191 | + 'model_id' => $model_id, | ||
192 | + 'fields' => $fields, | ||
193 | + 'success' => false | ||
194 | + ); | ||
195 | + } | ||
196 | + } | ||
56 | } | 197 | } |
frontend/views/option/_form.php
1 | <?php | 1 | <?php |
2 | - | ||
3 | use yii\helpers\Html; | 2 | use yii\helpers\Html; |
4 | use yii\widgets\ActiveForm; | 3 | use yii\widgets\ActiveForm; |
5 | - | 4 | +use frontend\models\OptionWidget; |
6 | /* @var $this yii\web\View */ | 5 | /* @var $this yii\web\View */ |
7 | /* @var $model frontend\models\Option */ | 6 | /* @var $model frontend\models\Option */ |
8 | /* @var $form yii\widgets\ActiveForm */ | 7 | /* @var $form yii\widgets\ActiveForm */ |
@@ -37,6 +36,7 @@ use yii\widgets\ActiveForm; | @@ -37,6 +36,7 @@ use yii\widgets\ActiveForm; | ||
37 | $first = 1; | 36 | $first = 1; |
38 | foreach($models as $index => $row) { | 37 | foreach($models as $index => $row) { |
39 | foreach($row as $key => $value) { | 38 | foreach($row as $key => $value) { |
39 | + var_dump($modellang[$index][$key]); | ||
40 | if(!$modellang[$index][$key][0]->hasErrors('value')) { | 40 | if(!$modellang[$index][$key][0]->hasErrors('value')) { |
41 | continue; | 41 | continue; |
42 | } | 42 | } |
frontend/views/option/_form_edit.php
@@ -7,7 +7,6 @@ use yii\widgets\ActiveForm; | @@ -7,7 +7,6 @@ use yii\widgets\ActiveForm; | ||
7 | /* @var $model frontend\models\Option */ | 7 | /* @var $model frontend\models\Option */ |
8 | /* @var $form yii\widgets\ActiveForm */ | 8 | /* @var $form yii\widgets\ActiveForm */ |
9 | ?> | 9 | ?> |
10 | - | ||
11 | <div class="option-form"> | 10 | <div class="option-form"> |
12 | <?php $form = ActiveForm::begin(); ?> | 11 | <?php $form = ActiveForm::begin(); ?> |
13 | <input type="hidden" name="Option[model]" value="<?=$modeldb?>"/> | 12 | <input type="hidden" name="Option[model]" value="<?=$modeldb?>"/> |
frontend/views/option/create.php
@@ -11,16 +11,9 @@ $this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Options'), 'url' => | @@ -11,16 +11,9 @@ $this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Options'), 'url' => | ||
11 | $this->params['breadcrumbs'][] = $this->title; | 11 | $this->params['breadcrumbs'][] = $this->title; |
12 | ?> | 12 | ?> |
13 | <div class="option-create"> | 13 | <div class="option-create"> |
14 | - | ||
15 | <h1><?= Html::encode($this->title) ?></h1> | 14 | <h1><?= Html::encode($this->title) ?></h1> |
16 | - | ||
17 | - <?= $this->render('_form', [ | ||
18 | - 'model' => $model, | ||
19 | - 'models' => $models, | ||
20 | - 'modellang' => $modellang, | ||
21 | - 'modeldb' => $modeldb, | ||
22 | - 'model_id' => $model_id, | ||
23 | - 'fields' => $fields | ||
24 | - ]) ?> | 15 | + <?php foreach($forms as $oneform) { |
16 | + echo $this->render('_form', $oneform); | ||
17 | + }?> | ||
25 | 18 | ||
26 | </div> | 19 | </div> |
frontend/views/option/update.php
@@ -7,20 +7,15 @@ use yii\helpers\Html; | @@ -7,20 +7,15 @@ use yii\helpers\Html; | ||
7 | 7 | ||
8 | $this->title = Yii::t('app', 'Update {modelClass}: ', [ | 8 | $this->title = Yii::t('app', 'Update {modelClass}: ', [ |
9 | 'modelClass' => 'Option', | 9 | 'modelClass' => 'Option', |
10 | -]) . ' ' . $models[\Yii::$app->request->get('id')]->name; | 10 | +]) . ' ' . $forms[0]['models'][\Yii::$app->request->get('id')]->name; |
11 | $this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Options'), 'url' => ['index']]; | 11 | $this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Options'), 'url' => ['index']]; |
12 | -$this->params['breadcrumbs'][] = ['label' => $models[\Yii::$app->request->get('id')]->name, 'url' => ['view', 'id' => $models[\Yii::$app->request->get('id')]->option_id]]; | 12 | +$this->params['breadcrumbs'][] = ['label' => $forms[0]['models'][\Yii::$app->request->get('id')]->name, 'url' => ['view', 'id' => $forms[0]['models'][\Yii::$app->request->get('id')]->option_id]]; |
13 | $this->params['breadcrumbs'][] = Yii::t('app', 'Update'); | 13 | $this->params['breadcrumbs'][] = Yii::t('app', 'Update'); |
14 | ?> | 14 | ?> |
15 | <div class="option-update"> | 15 | <div class="option-update"> |
16 | - | ||
17 | <h1><?= Html::encode($this->title) ?></h1> | 16 | <h1><?= Html::encode($this->title) ?></h1> |
18 | - | ||
19 | - <?= $this->render('_form_edit', [ | ||
20 | - 'models' => $models, | ||
21 | - 'modellang' => $modellang, | ||
22 | - 'modeldb' => $modeldb, | ||
23 | - 'model_id' => $model_id | ||
24 | - ]) ?> | 17 | + <?php foreach($forms as $oneform) { |
18 | + echo $this->render('_form_edit', $oneform); | ||
19 | + }?> | ||
25 | 20 | ||
26 | </div> | 21 | </div> |