Blame view

frontend/controllers/CabinetController.php 13.3 KB
3bc9af21   Yarik   Layout
1
2
3
  <?php
      namespace frontend\controllers;
      
8195fc24   Yarik   Models
4
      use common\models\User;
0d91ef5d   Alexey Boroda   -Delete action added
5
      use frontend\models\CreativeRole;
b73541b6   Yarik   Property
6
      use frontend\models\IntellectualProperty;
3533e4e9   Yarik   Forms
7
      use frontend\models\Report;
8195fc24   Yarik   Models
8
9
      use frontend\models\UserData;
      use frontend\models\UserPassport;
b6741a94   Yarik   Login
10
      use yii\filters\AccessControl;
30e3d244   Yarik   Forms
11
      use yii\filters\VerbFilter;
3bc9af21   Yarik   Layout
12
      use yii\web\Controller;
b73541b6   Yarik   Property
13
      use yii\web\NotFoundHttpException;
43cb22d0   Yarik   Property
14
      
3bc9af21   Yarik   Layout
15
16
17
18
19
      /**
       * Cabinet controller
       */
      class CabinetController extends Controller
      {
7f0970a7   Yarik   Layout
20
21
22
          
          public $layout = 'cabinet';
          
3bc9af21   Yarik   Layout
23
24
25
26
27
28
          /**
           * @inheritdoc
           */
          public function behaviors()
          {
              return [
3ebbbaa9   Alexey Boroda   -Reports
29
                  'verbs'  => [
43cb22d0   Yarik   Property
30
                      'class'   => VerbFilter::className(),
30e3d244   Yarik   Forms
31
                      'actions' => [
43cb22d0   Yarik   Property
32
33
                          'personal' => [ 'post' ],
                          'passport' => [ 'post' ],
30e3d244   Yarik   Forms
34
35
                      ],
                  ],
b6741a94   Yarik   Login
36
37
38
39
40
                  'access' => [
                      'class' => AccessControl::className(),
                      'rules' => [
                          [
                              'allow' => true,
3ebbbaa9   Alexey Boroda   -Reports
41
                              'roles' => [ '@' ],
b6741a94   Yarik   Login
42
43
44
                          ],
                      ],
                  ],
3bc9af21   Yarik   Layout
45
46
47
48
49
50
51
52
53
54
              ];
          }
          
          /**
           * Displays index page.
           *
           * @return mixed
           */
          public function actionIndex()
          {
8195fc24   Yarik   Models
55
56
57
58
              /**
               * @var User $user
               */
              $user = \Yii::$app->user->identity;
43cb22d0   Yarik   Property
59
              if (!$userData = $user->userData) {
8195fc24   Yarik   Models
60
61
                  $userData = new UserData();
              }
43cb22d0   Yarik   Property
62
              if (!$userPassport = $user->userPassport) {
8195fc24   Yarik   Models
63
64
                  $userPassport = new UserPassport();
              }
fb0f9630   Alexey Boroda   -Greed ready
65
              
3fd7d43c   Yarik   Creative roles
66
67
68
69
70
71
72
              $tableQuery = IntellectualProperty::find();
              if (!$user->isAdmin()) {
                  $tableQuery->joinWith('creativeRoles')
                             ->where([ 'intellectual_property.user_id' => $user->id ])
                             ->orWhere([ 'creative_role.user_id' => $user->id ]);
              }
              $table = $tableQuery->all();
fb0f9630   Alexey Boroda   -Greed ready
73
              
43cb22d0   Yarik   Property
74
75
76
77
78
79
80
81
              return $this->render(
                  'index',
                  [
                      'userData'     => $userData,
                      'userPassport' => $userPassport,
                      'table'        => $table,
                  ]
              );
3bc9af21   Yarik   Layout
82
83
          }
          
3ebbbaa9   Alexey Boroda   -Reports
84
          public function actionSales($id = NULL)
f1b535e4   Yarik   Datepicker
85
          {
43cb22d0   Yarik   Property
86
87
              $newRecord = false;
              if ($id) {
b73541b6   Yarik   Property
88
89
90
                  $property = $this->findProperty($id);
              } else {
                  $property = new IntellectualProperty();
43cb22d0   Yarik   Property
91
                  $newRecord = true;
b73541b6   Yarik   Property
92
              }
43cb22d0   Yarik   Property
93
              if ($property->load(\Yii::$app->request->post()) && $property->save()) {
aebb725c   Alexey Boroda   -List ready
94
95
96
97
98
99
100
                  if ($newRecord) {
                      return $this->redirect(
                          [
                              'cabinet/sales',
                              'id' => $property->id,
                          ]
                      );
43cb22d0   Yarik   Property
101
102
103
104
105
106
107
108
                  } else {
                      $response = \Yii::$app->response;
                      $response->format = $response::FORMAT_JSON;
                      return [
                          'success' => true,
                          'message' => 'Данные успешно сохранены',
                      ];
                  }
b73541b6   Yarik   Property
109
              }
0d91ef5d   Alexey Boroda   -Delete action added
110
              
aebb725c   Alexey Boroda   -List ready
111
112
113
114
115
116
117
              $table = CreativeRole::find()
                                   ->where(
                                       [
                                           'intellectual_property_id' => $id,
                                       ]
                                   )
                                   ->all();
0d91ef5d   Alexey Boroda   -Delete action added
118
              
43cb22d0   Yarik   Property
119
120
121
122
              return $this->render(
                  'sales',
                  [
                      'property' => $property,
aebb725c   Alexey Boroda   -List ready
123
                      'table'    => $table,
43cb22d0   Yarik   Property
124
125
                  ]
              );
f1b535e4   Yarik   Datepicker
126
127
          }
          
caf85dfb   Yarik   Html
128
129
          public function actionList()
          {
669a8f62   Alexey Boroda   Merge remote-trac...
130
131
132
133
134
135
              $table = IntellectualProperty::find()
  //                                         ->where(
  //                                             [
  //                                                 'user_id' => \Yii::$app->user->identity->id,
  //                                             ]
  //                                         )
aebb725c   Alexey Boroda   -List ready
136
137
138
139
140
141
142
                                           ->all();
              return $this->render(
                  'list',
                  [
                      'table' => $table,
                  ]
              );
caf85dfb   Yarik   Html
143
144
145
146
          }
          
          public function actionArrivals()
          {
3533e4e9   Yarik   Forms
147
148
149
150
151
152
153
154
155
              $reports = Report::find()
                               ->with('intellectualProperty')
                               ->all();
              return $this->render(
                  'arrivals',
                  [
                      'reports' => $reports,
                  ]
              );
caf85dfb   Yarik   Html
156
          }
aebb725c   Alexey Boroda   -List ready
157
          
caf85dfb   Yarik   Html
158
159
          public function actionNotifications()
          {
3ebbbaa9   Alexey Boroda   -Reports
160
161
162
163
164
165
166
167
              $table = Report::find()
                             ->all();
              return $this->render(
                  'notifications',
                  [
                      'table' => $table,
                  ]
              );
caf85dfb   Yarik   Html
168
          }
aebb725c   Alexey Boroda   -List ready
169
          
caf85dfb   Yarik   Html
170
171
          public function actionUsers()
          {
3ebbbaa9   Alexey Boroda   -Reports
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
              /**
               * @var User $user
               */
              $user = \Yii::$app->user->identity;
              if ($user->isAdmin()) {
                  $reports = Report::find()
                                   ->with('intellectualProperty')
                                   ->all();
              } else {
                  $reports = Report::find()
                                   ->joinWith('intellectualProperty.creativeRoles')
                                   ->where([ 'creative_role.user_id' => $user->id ])
                                   ->all();
              }
              
3533e4e9   Yarik   Forms
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
              return $this->render(
                  'users',
                  [
                      'reports' => $reports,
                  ]
              );
          }
          
          public function actionUsersAdd()
          {
              $request = \Yii::$app->request;
              $response = \Yii::$app->response;
              $response->format = $response::FORMAT_JSON;
              /**
               * @var User $user
               */
              $report = new Report();
              if ($report->load($request->post()) && $report->save()) {
                  return [
                      'success' => true,
                      'message' => 'Данные успешно сохранены',
                  ];
              } else {
                  return [
                      'error'   => true,
                      'message' => 'Ошибка сохранения данных',
                  ];
              }
caf85dfb   Yarik   Html
215
216
          }
          
30e3d244   Yarik   Forms
217
218
219
220
221
222
223
224
225
          public function actionPersonal()
          {
              $request = \Yii::$app->request;
              $response = \Yii::$app->response;
              $response->format = $response::FORMAT_JSON;
              /**
               * @var User $user
               */
              $user = \Yii::$app->user->identity;
43cb22d0   Yarik   Property
226
              if (!$userData = $user->userData) {
30e3d244   Yarik   Forms
227
228
229
                  $userData = new UserData();
                  $userData->user_id = $user->id;
              }
43cb22d0   Yarik   Property
230
              if ($userData->load($request->post()) && $userData->save()) {
30e3d244   Yarik   Forms
231
232
233
234
235
236
                  return [
                      'success' => true,
                      'message' => 'Данные успешно сохранены',
                  ];
              } else {
                  return [
43cb22d0   Yarik   Property
237
                      'error'   => true,
30e3d244   Yarik   Forms
238
239
240
241
                      'message' => 'Ошибка сохранения данных',
                  ];
              }
          }
43cb22d0   Yarik   Property
242
          
30e3d244   Yarik   Forms
243
244
245
246
247
248
249
250
251
          public function actionPassport()
          {
              $request = \Yii::$app->request;
              $response = \Yii::$app->response;
              $response->format = $response::FORMAT_JSON;
              /**
               * @var User $user
               */
              $user = \Yii::$app->user->identity;
43cb22d0   Yarik   Property
252
              if (!$userPassport = $user->userPassport) {
30e3d244   Yarik   Forms
253
254
255
                  $userPassport = new UserPassport();
                  $userPassport->user_id = $user->id;
              }
43cb22d0   Yarik   Property
256
              if ($userPassport->load($request->post()) && $userPassport->save()) {
30e3d244   Yarik   Forms
257
258
259
260
261
262
                  return [
                      'success' => true,
                      'message' => 'Данные успешно сохранены',
                  ];
              } else {
                  return [
43cb22d0   Yarik   Property
263
                      'error'   => true,
30e3d244   Yarik   Forms
264
265
266
267
                      'message' => 'Ошибка сохранения данных',
                  ];
              }
          }
43cb22d0   Yarik   Property
268
          
dcfb3d5c   Alexey Boroda   -Form ajax ready
269
270
271
272
273
274
275
276
          public function actionAddIntProp()
          {
              $request = \Yii::$app->request;
              $response = \Yii::$app->response;
              $response->format = $response::FORMAT_JSON;
              
              $intProperty = new IntellectualProperty();
              
fb0f9630   Alexey Boroda   -Greed ready
277
278
              $intProperty->user_id = \Yii::$app->user->identity->id;
              
43cb22d0   Yarik   Property
279
              if ($intProperty->load($request->post()) && $intProperty->save()) {
dcfb3d5c   Alexey Boroda   -Form ajax ready
280
281
282
283
284
285
                  return [
                      'success' => true,
                      'message' => 'Данные успешно сохранены',
                  ];
              } else {
                  return [
43cb22d0   Yarik   Property
286
                      'error'   => true,
dcfb3d5c   Alexey Boroda   -Form ajax ready
287
288
289
290
                      'message' => 'Ошибка сохранения данных',
                  ];
              }
          }
3533e4e9   Yarik   Forms
291
          
aebb725c   Alexey Boroda   -List ready
292
          public function actionDeleteIntProperty()
0d91ef5d   Alexey Boroda   -Delete action added
293
294
295
296
          {
              $request = \Yii::$app->request;
              $response = \Yii::$app->response;
              $response->format = $response::FORMAT_JSON;
3533e4e9   Yarik   Forms
297
              
3ebbbaa9   Alexey Boroda   -Reports
298
              if (!empty($request->post('id'))) {
aebb725c   Alexey Boroda   -List ready
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
                  $role = IntellectualProperty::findOne($request->post('id'));
                  if ($role->delete()) {
                      return [
                          'success' => true,
                          'message' => 'Данные успешно удалены',
                      ];
                  } else {
                      return [
                          'error'   => true,
                          'message' => 'Ошибка удаления данных',
                      ];
                  }
              } else {
                  return [
                      'error'   => true,
                      'message' => 'Элемент не найден',
                  ];
              }
          }
0d91ef5d   Alexey Boroda   -Delete action added
318
          
aebb725c   Alexey Boroda   -List ready
319
320
321
322
323
324
325
326
          public function actionAddRole()
          {
              $request = \Yii::$app->request;
              $response = \Yii::$app->response;
              $response->format = $response::FORMAT_JSON;
              
              $role = new CreativeRole();
              
0d91ef5d   Alexey Boroda   -Delete action added
327
328
329
330
331
332
333
334
335
336
337
338
              if ($role->load($request->post()) && $role->save()) {
                  return [
                      'success' => true,
                      'message' => 'Данные успешно сохранены',
                  ];
              } else {
                  return [
                      'error'   => true,
                      'message' => 'Ошибка сохранения данных',
                  ];
              }
          }
3ebbbaa9   Alexey Boroda   -Reports
339
          
9e523ea9   Alexey Boroda   -Update ready
340
341
342
343
344
          public function actionUpdateRole()
          {
              $request = \Yii::$app->request;
              $response = \Yii::$app->response;
              $response->format = $response::FORMAT_JSON;
3ebbbaa9   Alexey Boroda   -Reports
345
              
9e523ea9   Alexey Boroda   -Update ready
346
              $role = CreativeRole::findOne($request->get('id'));
3ebbbaa9   Alexey Boroda   -Reports
347
              
9e523ea9   Alexey Boroda   -Update ready
348
349
350
351
352
353
354
355
356
357
358
359
              if ($role->load($request->post()) && $role->save()) {
                  return [
                      'success' => true,
                      'message' => 'Данные успешно сохранены',
                  ];
              } else {
                  return [
                      'error'   => true,
                      'message' => 'Ошибка сохранения данных',
                  ];
              }
          }
3ebbbaa9   Alexey Boroda   -Reports
360
          
9e523ea9   Alexey Boroda   -Update ready
361
362
363
364
365
          public function actionGetRoleForm()
          {
              $request = \Yii::$app->request;
              $response = \Yii::$app->response;
              $response->format = $response::FORMAT_JSON;
3ebbbaa9   Alexey Boroda   -Reports
366
              
9e523ea9   Alexey Boroda   -Update ready
367
368
369
              $model = CreativeRole::findOne($request->post('id'));
              
              return [
3ebbbaa9   Alexey Boroda   -Reports
370
                  'answer' => $this->renderPartial('_update_form', [ 'model' => $model ]),
9e523ea9   Alexey Boroda   -Update ready
371
372
              ];
          }
0d91ef5d   Alexey Boroda   -Delete action added
373
374
375
376
377
378
          
          public function actionDeleteRole()
          {
              $request = \Yii::$app->request;
              $response = \Yii::$app->response;
              $response->format = $response::FORMAT_JSON;
0d91ef5d   Alexey Boroda   -Delete action added
379
              
3ebbbaa9   Alexey Boroda   -Reports
380
              if (!empty($request->post('id'))) {
aebb725c   Alexey Boroda   -List ready
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
                  $role = CreativeRole::findOne($request->post('id'));
                  if ($role->delete()) {
                      return [
                          'success' => true,
                          'message' => 'Данные успешно удалены',
                      ];
                  } else {
                      return [
                          'error'   => true,
                          'message' => 'Ошибка удаления данных',
                      ];
                  }
              } else {
                  return [
                      'error'   => true,
                      'message' => 'Роль не найдена',
                  ];
              }
0d91ef5d   Alexey Boroda   -Delete action added
399
          }
b73541b6   Yarik   Property
400
401
402
403
          
          public function findProperty($id)
          {
              $model = IntellectualProperty::findOne($id);
3ebbbaa9   Alexey Boroda   -Reports
404
              if (empty($model)) {
b73541b6   Yarik   Property
405
406
407
408
                  throw new NotFoundHttpException();
              }
              return $model;
          }
3bc9af21   Yarik   Layout
409
      }