Blame view

frontend/models/Option.php 7.73 KB
0012685b   Yarik   Многоязычные опции
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  <?php
  
  namespace frontend\models;
  
  use Yii;
  
  /**
   * This is the model class for table "option".
   *
   * @property integer $option_id
   * @property string $model
   * @property integer $model_id
   * @property string $name
   * @property string $template
   * @property integer $parent_id
   */
  class Option extends \yii\db\ActiveRecord
  {
      /**
       * @inheritdoc
       */
      public static function tableName()
      {
          return 'option';
      }
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['model', 'model_id', 'name', 'template'], 'required'],
              [['model_id', 'parent_id'], 'integer'],
              [['model', 'name', 'template'], 'string', 'max' => 200]
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              'option_id' => Yii::t('app', 'Option ID'),
              'model' => Yii::t('app', 'Model'),
              'model_id' => Yii::t('app', 'Model ID'),
              'name' => Yii::t('app', 'Name'),
              'template' => Yii::t('app', 'Template'),
              'parent_id' => Yii::t('app', 'Parent ID'),
          ];
      }
      public function getLangs() {
          return (new Language())->find()->where(['>', 'language_id', 0])->asArray()->all();
      }
19120f31   Yarik   fix broken
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
      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
              );
          }
      }
0012685b   Yarik   Многоязычные опции
197
  }