Blame view

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