Commit 19120f31e17259a673fad49d538b22ada050180d

Authored by Yarik
1 parent 82a330b9

fix broken

frontend/controllers/OptionController.php
@@ -61,95 +61,14 @@ class OptionController extends Controller @@ -61,95 +61,14 @@ 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]); 64 + $form[0] = Option::create(\Yii::$app->request->post(), 'User', 10, [['name' => 'phone', 'template' => 'text'], ['name' => 'adres', 'template' => 'text']]);
  65 + if($form[0]['success'] == false) {
  66 + return $this->render('create', ['forms' => $form]);
143 } else { 67 } else {
144 - return $this->render('create', [  
145 - 'model' => $model,  
146 - 'modeldb' => $modeldb,  
147 - 'model_id' => $model_id,  
148 - 'fields' => $fields  
149 - ]); 68 + return $this->redirect(['index']);
150 } 69 }
151 } 70 }
152 - 71 +
153 /** 72 /**
154 * Updates an existing Option model. 73 * Updates an existing Option model.
155 * 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.
@@ -158,64 +77,12 @@ class OptionController extends Controller @@ -158,64 +77,12 @@ class OptionController extends Controller
158 */ 77 */
159 public function actionUpdate($id) 78 public function actionUpdate($id)
160 { 79 {
161 - $models[$id] = Option::findOne($id);  
162 - $modellang[$id] = array();  
163 - $langs = OptionLang::findAll(['id' => $id]);  
164 - foreach($langs as $lang) {  
165 - $modellang[$id][$lang->lang_id] = $lang;  
166 - }  
167 - $children = (new Option())->find()->where(['parent_id' => $id])->all();  
168 - foreach($children as $child) {  
169 - $models[$child->option_id] = $child;  
170 - $modellang[$child->option_id] = array();  
171 - $langs = OptionLang::findAll(['id' =>$child->option_id]);  
172 - foreach($langs as $lang) {  
173 - $modellang[$child->option_id][$lang->lang_id] = $lang;  
174 - }  
175 - }  
176 - $modeldb = 'user';  
177 - $model_id = '10';  
178 - $fields = [['name' => 'phone', 'template' => 'input'], ['name' => 'adres', 'template' => 'input']];  
179 - $post = \Yii::$app->request->post();  
180 - $ok = 1;  
181 - if(!empty($post)) {  
182 - foreach($post['Option'] as $key => $option) {  
183 - if(in_array($key, array('model', 'model_id'))) { continue; }  
184 - $modellang[$key][0]->value = $option['value'][$models[$key]->name];  
185 - if(!$modellang[$key][0]->save()) {  
186 - $ok = 0;  
187 - }  
188 - foreach($option['lang'] as $lang_id => $lang) {  
189 - if(empty($modellang[$key][$lang_id])) {  
190 - $modellang[$key][$lang_id] = new OptionLang();  
191 - $modellang[$key][$lang_id]->id = $models[$key]->option_id;  
192 - $modellang[$key][$lang_id]->lang_id = $lang_id;  
193 - $modellang[$key][$lang_id]->value = $lang;  
194 - } else {  
195 - $modellang[$key][$lang_id]->value = $lang;  
196 - }  
197 - if(!$modellang[$key][$lang_id]->save()) {  
198 - $ok = 0;  
199 - }  
200 - }  
201 - }  
202 - if($ok) {  
203 - return $this->redirect(['view', 'id' => $id]);  
204 - } else {  
205 - return $this->render('update', [  
206 - 'models' => $models,  
207 - 'modellang' => $modellang,  
208 - 'modeldb' => $modeldb,  
209 - 'model_id' => $model_id  
210 - ]);  
211 - } 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]);
212 } 85 }
213 - return $this->render('update', [  
214 - 'models' => $models,  
215 - 'modellang' => $modellang,  
216 - 'modeldb' => $modeldb,  
217 - 'model_id' => $model_id  
218 - ]);  
219 } 86 }
220 87
221 /** 88 /**
@@ -226,7 +93,23 @@ class OptionController extends Controller @@ -226,7 +93,23 @@ class OptionController extends Controller
226 */ 93 */
227 public function actionDelete($id) 94 public function actionDelete($id)
228 { 95 {
229 - $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();
230 113
231 return $this->redirect(['index']); 114 return $this->redirect(['index']);
232 } 115 }
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 }