Blame view

backend/controllers/ServiceController.php 8.88 KB
6d62827f   Anastasia   services
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  <?php
      /**
       * Created by PhpStorm.
       * User: stes
       * Date: 22.05.18
       * Time: 14:44
       */
      
      namespace backend\controllers;
      
      use artbox\core\admin\actions\Create;
      use artbox\core\admin\actions\Delete;
      use artbox\core\admin\actions\Index;
      use artbox\core\admin\actions\Update;
      use artbox\core\admin\actions\View;
      use artbox\core\admin\interfaces\ControllerInterface;
      use artbox\core\admin\widgets\Form;
      use common\models\Service;
      use yii\filters\AccessControl;
      use yii\filters\VerbFilter;
dac1c903   Anastasia   add button in admin
21
      use yii\helpers\Url;
6d62827f   Anastasia   services
22
      use yii\web\Controller;
dac1c903   Anastasia   add button in admin
23
      use yii\web\JsExpression;
6d62827f   Anastasia   services
24
      use yii\web\NotFoundHttpException;
dac1c903   Anastasia   add button in admin
25
      
6d62827f   Anastasia   services
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
      class ServiceController extends Controller implements ControllerInterface
      {
          public function behaviors()
          {
              return [
                  'verbs'  => [
                      'class'   => VerbFilter::className(),
                      'actions' => [
                          'delete' => [ 'POST' ],
                      ],
                  ],
                  'access' => [
                      'class' => AccessControl::className(),
                      'rules' => [
                          [
                              'allow' => true,
                              'roles' => [ '@' ],
                          ],
                      ],
                  ],
              ];
          }
dac1c903   Anastasia   add button in admin
48
          
6d62827f   Anastasia   services
49
50
51
52
53
54
55
56
57
          public function actions()
          {
              return [
                  'index'  => [
                      'class'            => Index::className(),
                      'columns'          => [
                          'title'      => [
                              'type' => Index::ACTION_COL,
                          ],
dac1c903   Anastasia   add button in admin
58
                          'parent'     => [
6d62827f   Anastasia   services
59
60
61
62
63
64
65
66
67
68
69
                              'type'         => Index::RELATION_COL,
                              'columnConfig' => [
                                  'relationField' => 'title',
                              ],
                          ],
                          'updated_at' => [
                              'type' => Index::DATETIME_COL,
                          ],
                          'sort'       => [
                              'type' => Index::POSITION_COL,
                          ],
dac1c903   Anastasia   add button in admin
70
                          'status'     => [
6d62827f   Anastasia   services
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
                              'type' => Index::STATUS_COL,
                          ],
                      ],
                      'model'            => Service::className(),
                      'hasLanguage'      => true,
                      'enableMassDelete' => true,
                      'modelPrimaryKey'  => 'id',
                  ],
                  'create' => array_merge([ 'class' => Create::className() ], self::fieldsConfig()),
                  'update' => array_merge([ 'class' => Update::className() ], self::fieldsConfig()),
                  'view'   => [
                      'class'          => View::className(),
                      'model'          => Service::className(),
                      'hasAlias'       => true,
                      'languageFields' => [
                          [
                              'name' => 'title',
                              'type' => Form::STRING,
                          ],
                          [
                              'name' => 'body',
                              'type' => Form::WYSIWYG,
                          ],
                      ],
                      'fields'         => [
                          [
                              'name'              => 'parent_id',
                              'type'              => Form::RELATION,
                              'relationAttribute' => 'title',
                              'relationName'      => 'parent',
                              'multiple'          => false,
                          ],
                      ],
                  ],
                  'delete' => [
                      'class' => Delete::className(),
                  ],
              ];
          }
dac1c903   Anastasia   add button in admin
110
          
6d62827f   Anastasia   services
111
112
113
          public function findModel($id)
          {
              $model = Service::find()
dac1c903   Anastasia   add button in admin
114
115
116
                              ->with('languages')
                              ->where([ 'id' => $id ])
                              ->one();
6d62827f   Anastasia   services
117
118
119
120
121
122
              if ($model !== null) {
                  return $model;
              } else {
                  throw new NotFoundHttpException('The requested page does not exist.');
              }
          }
dac1c903   Anastasia   add button in admin
123
          
6d62827f   Anastasia   services
124
125
126
127
          public function newModel()
          {
              return new Service();
          }
dac1c903   Anastasia   add button in admin
128
          
6d62827f   Anastasia   services
129
130
131
          public function deleteModel($id)
          {
              $page = Service::find()
dac1c903   Anastasia   add button in admin
132
133
134
135
136
137
138
                             ->with('languages.alias')
                             ->where(
                                 [
                                     'id' => $id,
                                 ]
                             )
                             ->one();
6d62827f   Anastasia   services
139
140
141
142
143
144
145
146
147
148
149
              $langs = call_user_func(
                  [
                      $page,
                      'getVariationModels',
                  ]
              );
              foreach ($langs as $lang) {
                  if ($lang->alias !== null) {
                      $lang->alias->delete();
                  }
              }
dac1c903   Anastasia   add button in admin
150
              
6d62827f   Anastasia   services
151
152
              return $page->delete();
          }
dac1c903   Anastasia   add button in admin
153
          
6d62827f   Anastasia   services
154
155
156
          protected static function fieldsConfig()
          {
              return [
dac1c903   Anastasia   add button in admin
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
                  'tinyMceConfig'  => [
                      'language'      => 'ru',
                      'clientOptions' => [
                          'file_browser_callback' => new JsExpression(
                              "function(field_name, url, type, win) {
  window.open('" . Url::to(
                                  [
                                      'imagemanager/manager',
                                      'view-mode'   => 'iframe',
                                      'select-type' => 'tinymce',
                                  ]
                              ) . "&tag_name='+field_name,'','width=800,height=540 ,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no');
  }"
                          ),
                          'plugins'               => [
                              "advlist autolink lists link charmap print preview anchor",
                              "searchreplace visualblocks code fullscreen",
                              "insertdatetime media table contextmenu paste image",
                          ],
                          'toolbar'               => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code | prices | button",
                          'setup' => new JsExpression('function (editor) {
                                  editor.addButton("prices", {
                                                            text: "Цена",
                                                            icon: false,
                                                            onclick: function () {
                                                              editor.insertContent("[[prices]]");
                                                            }
                                                          });
                                                           editor.addButton("button", {
                                                            text: "Записаться на прием",
                                                            icon: false,
                                                            onclick: function () {
                                                              editor.insertContent("[[button]]");
                                                            }
                                                          });
                          }')
                          ],
                  ],
6d62827f   Anastasia   services
195
196
197
198
                  'model'          => Service::className(),
                  'hasAlias'       => true,
                  'languageFields' => [
                      [
dac1c903   Anastasia   add button in admin
199
200
201
                          'name'     => 'title',
                          'type'     => Form::STRING,
                          'decorate' => true,
6d62827f   Anastasia   services
202
203
204
205
206
207
208
                      ],
                      [
                          'name' => 'body',
                          'type' => Form::WYSIWYG,
                      ],
                  ],
                  'fields'         => [
7f403223   Anastasia   - add image to se...
209
210
                      [
                          'name' => 'image_id',
dac1c903   Anastasia   add button in admin
211
                          'type' => Form::IMAGE,
7f403223   Anastasia   - add image to se...
212
                      ],
6d62827f   Anastasia   services
213
214
215
216
217
                      [
                          'name'              => 'parent_id',
                          'type'              => Form::RELATION,
                          'relationAttribute' => 'title',
                          'relationName'      => 'parent',
bd63d31c   Anastasia   - delete multiply...
218
                          'multiple'          => false,
6d62827f   Anastasia   services
219
220
221
222
223
224
225
226
227
228
229
230
231
                      ],
                      [
                          'name' => 'status',
                          'type' => Form::BOOL,
                      ],
                      [
                          'name' => 'sort',
                          'type' => Form::NUMBER,
                      ],
                  ],
              ];
          }
      }