Blame view

protected/modules/admin/components/StaticDataEditor.php 7.32 KB
a1684257   Administrator   first commit
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
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
  <?php
  /** Usage example
   *   class PageController extends AdminController
   *   {
   *       public function actions()
   *       {
   *           return array(
   *               'index' => array(
   *                   'class' => 'StaticDataEditor',
   *
   *                   'submitLabel' => 'Сохранить',
   *                   'section' => 'root',
   *                   'key' => 'index',
   *                   'view' => 'static_editor',
   *                   'model' => array(
   *                       'schema' => array(
   *                           'item1' => array(
   *                               'label' => 'Item #1',
   *                               'default' => null,
   *                           ),
   *                       ),
   *                       'rules' => array(),
   *                   ),
   *                   'form' => array(
   *                       array(
   *                           'type' => 'simpleInput',
   *                           'name' => 'textField',
   *                           'attribute' => 'item1',
   *                           'options' => array(),
   *                       ),
   *                   ),
   *                   'i18nModel' => array(
   *                       'schema' => array(
   *                           'item1' => array(
   *                               'label' => 'Item #1',
   *                               'default' => null,
   *                           ),
   *                       ),
   *                       'rules' => array(),
   *                   ),
   *                   'i18nForm' => array(
   *                       array(
   *                           'type' => 'simpleInput',
   *                           'name' => 'textField',
   *                           'attribute' => 'item1',
   *                           'options' => array(),
   *                       ),
   *                   ),
   *               ),
   *           );
   *       }
   *   }
   */
  class StaticDataEditor extends CAction
  {
      public $section;
      public $key;
  
      public $model;
      public $form;
  
      public $i18nModel;
      public $i18nForm;
  
      public $redirectUrl = null;
      public $view = null;
      public $submitLabel = 'Save';
  
      public function init()
      {
  
      }
  
      public function run()
      {
          /**
           * @var StaticData $model
           */
          $model = StaticData::model()->findByPk(array(
                  'section' => $this->section,
                  'key' => $this->key)
          );
  
          /**
           * @var StaticDataI18n[] $i18nModels
           */
          $criteria = new CDbCriteria();
          $criteria->compare('`section`', $this->section);
          $criteria->compare('`key`', $this->key);
  
          $criteria->index = 'lang';
  
          $i18nModels = StaticDataI18n::model()->findAll($criteria);
  
          if ($model === null) {
              $model = new StaticData();
              $model->key = $this->key;
              $model->section = $this->section;
              $model->setDataAttributes(array());
          }
          foreach (Yii::app()->params['languages'] as $lang) {
              if (!isset($i18nModels[$lang])) {
                  $i18nModel = new StaticDataI18n();
                  $i18nModel->key = $this->key;
                  $i18nModel->section = $this->section;
                  $i18nModel->lang = $lang;
                  $i18nModel->setDataAttributes(array());
                  $i18nModels[$lang] = $i18nModel;
              }
          }
  
          $dataModel = new DataModel($this->model);
          $dataModel->setAttributes($model->getDataAttributes(), false);
          /** @var $i18nDataModels DataModelI18n[] */
          $i18nDataModels = array();
          foreach (Yii::app()->params['languages'] as $lang) {
              $i18nDataModels[$lang] = new DataModelI18n($this->i18nModel);
              $i18nDataModels[$lang]->setAttributes($i18nModels[$lang]->getDataAttributes(), false);
          }
          if (isset($_POST['DataModel']) || isset($_POST['DataModelI18n'])) {
              if (isset($_POST['DataModel']))
                  $dataModel->setAttributes($_POST['DataModel']);
  
              if (isset($_POST['DataModelI18n'])) {
                  foreach ($_POST['DataModelI18n'] as $lang => $post) {
                      if (isset($i18nDataModels[$lang]))
                          $i18nDataModels[$lang]->setAttributes($post);
                  }
              }
              $i18nValidation = true;
              foreach ($i18nDataModels as $i18nDataModel) {
                  $i18nValidation = $i18nValidation && $i18nDataModel->validate();
              }
  
              if ($dataModel->validate() && $i18nValidation) {
                  $model->setDataAttributes($dataModel->getAttributes());
                  $model->save(false);
                  foreach (Yii::app()->params['languages'] as $lang) {
                      $i18nModels[$lang]->setDataAttributes($i18nDataModels[$lang]->getAttributes());
                      $i18nModels[$lang]->save(false);
                  }
                  //redirect to index
                  if (isset($this->redirectUrl)) {
                      $this->getController()->redirect($this->redirectUrl);
                  }
              }
  
          }
          //render
  
          $content = $this->renderForm($dataModel, $i18nDataModels, true);
          if (isset($this->view)) {
              $this->getController()->render($this->view, array('content' => $content));
          } else {
              $this->getController()->renderText($content);
          }
      }
  
      /**
       * @param DataModel $dataModel
       * @param DataModelI18n[] $i18nDataModels
       * @param bool $processOutput
       * @return null|string
       */
      private function renderForm($dataModel, $i18nDataModels, $processOutput = false)
      {
f6e211e4   Administrator   finish work part 1
167
  
a1684257   Administrator   first commit
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
          $out = null;
          if ($processOutput) {
              ob_start();
          }
          $form = $this->getController()->beginWidget('BHorizontalForm', array());
  
          $formConfig = array();
          if (!empty($this->form))
              $formConfig[] = array(
                  'type' => 'widget',
                  'class' => 'FormWidget',
                  'config' => array(
                      'form' => $form,
                      'model' => $dataModel,
                      'config' => $this->form,
                  ),
              );
f6e211e4   Administrator   finish work part 1
185
186
  
  
a1684257   Administrator   first commit
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
          if (!empty($this->i18nForm)) {
              $tabs = array();
              foreach (Yii::app()->params['languages'] as $lang) {
                  $tabs[] = array(
                      'label' => Yii::app()->params['languageNames'][$lang],
                      'className' => 'FormWidget',
                      'properties' => array(
                          'form' => $form,
                          'suffix' => $lang,
                          'model' => $i18nDataModels[$lang],
                          'config' => $this->i18nForm,
                      ),
                  );
              }
              //CVarDumper::dump($tabs,10,true);
              $formConfig[] = array(
                  'type' => 'widget',
                  'class' => 'WidgetTabs',
                  'config' => array(
                      'tabs' => $tabs,
                  ),
              );
          }
f6e211e4   Administrator   finish work part 1
210
211
  
  
a1684257   Administrator   first commit
212
213
214
215
216
217
218
219
220
221
222
223
          $this->getController()->widget('FormWidget', array(
              'form' => $form,
              'model' => $dataModel,
              'config' => $formConfig,
          ));
  
          echo '<div class="form-actions"><button type="submit" class="btn btn-large btn-primary">',
          $this->submitLabel,
          '</button></div>';
          if ($processOutput) {
              $out = ob_get_clean();
          }
f6e211e4   Administrator   finish work part 1
224
  
a1684257   Administrator   first commit
225
          return $out;
f6e211e4   Administrator   finish work part 1
226
  
a1684257   Administrator   first commit
227
228
      }
  }