Commit 6d49b5d2004101004f75928cc12312981d34b144

Authored by Yarik
1 parent 839c0f76

Удалил ненужный код, добавил удаление всех дочерних элементов.

frontend/controllers/OptionController.php
@@ -61,95 +61,6 @@ class OptionController extends Controller @@ -61,95 +61,6 @@ class OptionController extends Controller
61 */ 61 */
62 public function actionCreate() 62 public function actionCreate()
63 { 63 {
64 - $model = new Option();  
65 - $modellang = new OptionLang();  
66 - $modeldb = 'user';  
67 - $model_id = '10';  
68 - $fields = [['name' => 'phone', 'template' => 'input'], ['name' => 'adres', 'template' => 'input']];  
69 - $post = \Yii::$app->request->post();  
70 - if(!empty($post['Option'])) {  
71 - $ok = 1;  
72 - $parentid = null;  
73 - $models = array();  
74 - foreach($post['Option'] as $index => $option) {  
75 - if(in_array($index, array('model', 'model_id')) && $index !== 0) { continue; }  
76 - $first = 1;  
77 - foreach($option['value'] as $key => $value) {  
78 - $models[$index][$key] = new Option();  
79 - $models[$index][$key]->model = $post['Option']['model'];  
80 - $models[$index][$key]->model_id = $post['Option']['model_id'];  
81 - $models[$index][$key]->template = $option['template'];  
82 - $models[$index][$key]->name = $key;  
83 - if(!$first) {  
84 - $models[$index][$key]->parent_id = $parentid;  
85 - }  
86 - $modelslang[$index][$key][0] = new OptionLang();  
87 - if(!empty($value) && $models[$index][$key]->save()) {  
88 - if($first) {  
89 - $parentid = $models[$index][$key]->option_id;  
90 - }  
91 - $modelslang[$index][$key][0]->id = $models[$index][$key]->option_id;  
92 - $modelslang[$index][$key][0]->lang_id = 0;  
93 - $modelslang[$index][$key][0]->value = $value;  
94 - if($modelslang[$index][$key][0]->save()) {  
95 - if(!empty($option['lang'][$key])) {  
96 - foreach($option['lang'][$key] as $code => $lang) {  
97 - if(!empty($lang)) {  
98 - $modelslang[$index][$key][$code] = new OptionLang();  
99 - $modelslang[$index][$key][$code]->id = $models[$index][$key]->option_id;  
100 - $modelslang[$index][$key][$code]->lang_id = $code;  
101 - $modelslang[$index][$key][$code]->value = $lang;  
102 - if(!$modelslang[$index][$key][$code]->save()) {  
103 - $ok = 0;  
104 - }  
105 - }  
106 - }  
107 - }  
108 - }  
109 - } else {  
110 - $models[$index][$key]->validate();  
111 - $modelslang[$index][$key][0]->validate();  
112 - $modelslang[$index][$key][0];  
113 - if(!empty($option['lang'][$key])) {  
114 - foreach($option['lang'][$key] as $code => $lang) {  
115 - if(!empty($lang)) {  
116 - $modelslang[$index][$key][$code] = new OptionLang();  
117 - $modelslang[$index][$key][$code]->id = $models[$index][$key]->option_id;  
118 - $modelslang[$index][$key][$code]->lang_id = $code;  
119 - $modelslang[$index][$key][$code]->value = $lang;  
120 - }  
121 - }  
122 - }  
123 - $ok = 0;  
124 - }  
125 - $first = 0;  
126 - }  
127 - }  
128 - if($ok) {  
129 - return $this->redirect(['view', 'id' => $parentid]);  
130 - } else {  
131 - return $this->render('create', [  
132 - 'model' => $model,  
133 - 'models' => $models,  
134 - 'modellang' => $modelslang,  
135 - 'modeldb' => $modeldb,  
136 - 'model_id' => $model_id,  
137 - 'fields' => $fields  
138 - ]);  
139 - }  
140 - }  
141 - if ($model->load(Yii::$app->request->post()) && $model->save()) {  
142 - return $this->redirect(['view', 'id' => $model->option_id]);  
143 - } else {  
144 - return $this->render('create', [  
145 - 'model' => $model,  
146 - 'modeldb' => $modeldb,  
147 - 'model_id' => $model_id,  
148 - 'fields' => $fields  
149 - ]);  
150 - }  
151 - }  
152 - public function actionTest() {  
153 $form[0] = Option::create(\Yii::$app->request->post(), 'User', 10, [['name' => 'phone', 'template' => 'text'], ['name' => 'adres', 'template' => 'text']]); 64 $form[0] = Option::create(\Yii::$app->request->post(), 'User', 10, [['name' => 'phone', 'template' => 'text'], ['name' => 'adres', 'template' => 'text']]);
154 if($form[0]['success'] == false) { 65 if($form[0]['success'] == false) {
155 return $this->render('create', ['forms' => $form]); 66 return $this->render('create', ['forms' => $form]);
@@ -157,14 +68,7 @@ class OptionController extends Controller @@ -157,14 +68,7 @@ class OptionController extends Controller
157 return $this->redirect(['index']); 68 return $this->redirect(['index']);
158 } 69 }
159 } 70 }
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 - } 71 +
168 /** 72 /**
169 * Updates an existing Option model. 73 * Updates an existing Option model.
170 * If update is successful, the browser will be redirected to the 'view' page. 74 * If update is successful, the browser will be redirected to the 'view' page.
@@ -173,64 +77,12 @@ class OptionController extends Controller @@ -173,64 +77,12 @@ class OptionController extends Controller
173 */ 77 */
174 public function actionUpdate($id) 78 public function actionUpdate($id)
175 { 79 {
176 - $models[$id] = Option::findOne($id);  
177 - $modellang[$id] = array();  
178 - $langs = OptionLang::findAll(['id' => $id]);  
179 - foreach($langs as $lang) {  
180 - $modellang[$id][$lang->lang_id] = $lang;  
181 - }  
182 - $children = (new Option())->find()->where(['parent_id' => $id])->all();  
183 - foreach($children as $child) {  
184 - $models[$child->option_id] = $child;  
185 - $modellang[$child->option_id] = array();  
186 - $langs = OptionLang::findAll(['id' =>$child->option_id]);  
187 - foreach($langs as $lang) {  
188 - $modellang[$child->option_id][$lang->lang_id] = $lang;  
189 - }  
190 - }  
191 - $modeldb = 'user';  
192 - $model_id = '10';  
193 - $fields = [['name' => 'phone', 'template' => 'input'], ['name' => 'adres', 'template' => 'input']];  
194 - $post = \Yii::$app->request->post();  
195 - $ok = 1;  
196 - if(!empty($post)) {  
197 - foreach($post['Option'] as $key => $option) {  
198 - if(in_array($key, array('model', 'model_id'))) { continue; }  
199 - $modellang[$key][0]->value = $option['value'][$models[$key]->name];  
200 - if(!$modellang[$key][0]->save()) {  
201 - $ok = 0;  
202 - }  
203 - foreach($option['lang'] as $lang_id => $lang) {  
204 - if(empty($modellang[$key][$lang_id])) {  
205 - $modellang[$key][$lang_id] = new OptionLang();  
206 - $modellang[$key][$lang_id]->id = $models[$key]->option_id;  
207 - $modellang[$key][$lang_id]->lang_id = $lang_id;  
208 - $modellang[$key][$lang_id]->value = $lang;  
209 - } else {  
210 - $modellang[$key][$lang_id]->value = $lang;  
211 - }  
212 - if(!$modellang[$key][$lang_id]->save()) {  
213 - $ok = 0;  
214 - }  
215 - }  
216 - }  
217 - if($ok) {  
218 - return $this->redirect(['view', 'id' => $id]);  
219 - } else {  
220 - return $this->render('update', [  
221 - 'models' => $models,  
222 - 'modellang' => $modellang,  
223 - 'modeldb' => $modeldb,  
224 - 'model_id' => $model_id  
225 - ]);  
226 - } 80 + $form[0] = Option::change($id, \Yii::$app->request->post(), 'User', 10);
  81 + if($form[0]['success'] == false) {
  82 + return $this->render('update', ['forms' => $form]);
  83 + } else {
  84 + return $this->redirect(['view', 'id' => $id]);
227 } 85 }
228 - return $this->render('update', [  
229 - 'models' => $models,  
230 - 'modellang' => $modellang,  
231 - 'modeldb' => $modeldb,  
232 - 'model_id' => $model_id  
233 - ]);  
234 } 86 }
235 87
236 /** 88 /**
@@ -241,7 +93,23 @@ class OptionController extends Controller @@ -241,7 +93,23 @@ class OptionController extends Controller
241 */ 93 */
242 public function actionDelete($id) 94 public function actionDelete($id)
243 { 95 {
244 - $this->findModel($id)->delete(); 96 + $model = $this->findModel($id);
  97 + $children = $model->hasMany(Option::className(), ['parent_id' => 'option_id'])->all();
  98 + $langs = array();
  99 + if(!empty($children)) {
  100 + foreach($children as $child) {
  101 + $langs = OptionLang::findAll(['id' => $child->option_id]);
  102 + foreach($langs as $lang) {
  103 + $lang->delete();
  104 + }
  105 + $child->delete();
  106 + }
  107 + }
  108 + $langs = OptionLang::findAll(['id' => $id]);
  109 + foreach($langs as $lang) {
  110 + $lang->delete();
  111 + }
  112 + $model->delete();
245 113
246 return $this->redirect(['index']); 114 return $this->redirect(['index']);
247 } 115 }
frontend/views/layouts/main.php
@@ -35,10 +35,7 @@ AppAsset::register($this); @@ -35,10 +35,7 @@ AppAsset::register($this);
35 ] 35 ]
36 ]); 36 ]);
37 37
38 - echo Nav::widget([  
39 - 'options' => ['class' => 'navbar-nav navbar-right'],  
40 - 'items' => (new MenuTree())->build('TOP')  
41 - ]); 38 +
42 /* 39 /*
43 $menuItems = [ 40 $menuItems = [
44 ['label' => 'Home', 'url' => ['/site/index']], 41 ['label' => 'Home', 'url' => ['/site/index']],