Blame view

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