/
EventsController.php
538 lines (476 loc) · 17.7 KB
/
EventsController.php
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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<?php
namespace Myks92\Vmc\Event\Controller;
use DomainException;
use Myks92\Vmc\Event\Model\Entity\Events\Event;
use Myks92\Vmc\Event\Model\Entity\Events\Id;
use Myks92\Vmc\Event\Model\Entity\Events\Status;
use Myks92\Vmc\Event\Model\UseCase\Events\Create;
use Myks92\Vmc\Event\Model\UseCase\Events\Edit;
use Myks92\Vmc\Event\Model\UseCase\Events\Move;
use Myks92\Vmc\Event\Model\UseCase\Events\Poster;
use Myks92\Vmc\Event\Model\UseCase\Events\Remove;
use Myks92\Vmc\Event\Model\UseCase\Events\Status\Activate;
use Myks92\Vmc\Event\Model\UseCase\Events\Status\Cancel;
use Myks92\Vmc\Event\Model\UseCase\Events\Status\Reject;
use Myks92\Vmc\Event\Model\UseCase\Events\Place\Assign;
use Myks92\Vmc\Event\Model\UseCase\Events\Place\Revoke;
use Myks92\Vmc\Event\Model\UseCase\Events\View;
use Myks92\Vmc\Event\ReadModel\Events\Filter;
use Myks92\Vmc\Event\Security\Access\Events\EventChecker;
use Myks92\Vmc\Event\Service\Uploader\FileUploader;
use Myks92\Vmc\Event\Service\Uploader\QRUploadedFile;
use Myks92\Vmc\Event\Service\Uploader\QRUploader;
use RuntimeException;
use Throwable;
use Yii;
use yii\base\Exception;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\base\Module;
use yii\db\StaleObjectException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Html;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
/**
* Class EventController
* @author Maxim Vorozhtsov <myks1992@mail.ru>
*/
class EventsController extends Controller
{
/**
* @var EventChecker
*/
private $checker;
/**
* EventController constructor.
* @param $id
* @param Module $module
* @param EventChecker $checker
* @param array $config
*/
public function __construct($id, Module $module, EventChecker $checker, $config = [])
{
$this->checker = $checker;
parent::__construct($id, $module, $config);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'only' => ['create', 'update', 'move', 'reject', 'cancel', 'delete'],
'rules' => [
[
'allow' => true,
'actions' => ['create', 'update', 'move', 'reject', 'cancel', 'delete'],
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* @return string
* @throws DomainException
* @throws InvalidArgumentException
* @throws InvalidParamException
*/
public function actionIndex()
{
$searchModel = new Filter();
if (!$this->checker->allowManager()) {
$searchModel->status = [
Status::activate()->getValue(),
Status::rejected()->getValue(),
Status::cancelled()->getValue()
];
}
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if (Yii::$app->request->isAjax) {
return $this->renderPartial('_list', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'checker' => $this->checker
]);
}
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'checker' => $this->checker
]);
}
/**
* @return string|Response
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws RuntimeException
* @throws Throwable
*/
public function actionCreate()
{
if (!$this->checker->allowCreate()) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
$form = new Create\Form(Yii::$app->request);
$handler = Yii::createObject(Create\Handler::class);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$command = $form->getCommand();
$command->owner = Yii::$app->user->getIdentity()->getId();
$event = $handler->handle($command);
return $this->redirect(['view', 'id' => $event->getId()->getValue()]);
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('create', [
'createForm' => $form,
]);
}
/**
* @param $id
* @return string|Response
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws StaleObjectException
* @throws Throwable
*/
public function actionUpdate($id)
{
$event = $this->findModel($id);
$form = new Edit\Form(Yii::$app->request, $event);
$handler = Yii::createObject(Edit\Handler::class);
if (!$this->checker->allowEdit($event->getId())) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$command = $form->getCommand();
$command->id = $id;
$handler->handle($command);
return $this->redirect(['view', 'id' => $event->getId()->getValue()]);
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('update', [
'model' => $event,
'editForm' => $form,
]);
}
/**
* @param $id
* @return string
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws StaleObjectException
* @throws Throwable
*/
public function actionView($id)
{
$event = $this->findModel($id);
$handler = Yii::createObject(View\Handler::class);
$uploader = Yii::createObject(QRUploader::class);
$uploader->upload(new QRUploadedFile(Yii::$app->urlManager->createAbsoluteUrl(['/events/events/view', 'id' => $id]), $id));
$handler->handle(new View\Command($event->getId()->getValue()));
return $this->render('view', [
'model' => $event,
'checker' => $this->checker
]);
}
/**
* @param $id
* @return string|Response
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws StaleObjectException
* @throws Throwable
*/
public function actionMove($id)
{
$event = $this->findModel($id);
$form = new Move\Form($event);
$handler = Yii::createObject(Move\Handler::class);
if (!$this->checker->allowEdit($event->getId())) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$handler->handle(new Move\Command($event->getId()->getValue(), $form->from, $form->to));
Yii::$app->session->setFlash('success', 'Мероприятие перенесено!');
return $this->redirect(['view', 'id' => $event->getId()->getValue()]);
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('move', [
'model' => $event,
'moveForm' => $form,
]);
}
/**
* @param $id
* @return string|Response
* @throws ForbiddenHttpException
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws InvalidParamException
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws StaleObjectException
* @throws Throwable
* @throws Exception
*/
public function actionUploadPoster($id)
{
$event = $this->findModel($id);
$form = new Poster\Upload\Form();
$handler = Yii::createObject(Poster\Upload\Handler::class);
if (!$this->checker->allowEdit($event->getId())) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
$uploader = Yii::createObject(FileUploader::class);
$uploader->remove($event->getPoster());
$uploaded = $uploader->upload($form->poster);
$file = new Poster\Upload\File(
$uploaded->getPath(),
$uploaded->getName(),
$uploaded->getSize()
);
$handler->handle(new Poster\Upload\Command($event->getId()->getValue(), $file));
return $this->redirect(['view', 'id' => $event->getId()->getValue()]);
}
return $this->render('upload-poster', [
'model' => $event,
'posterForm' => $form,
]);
}
/**
* @param $id
* @return Response
* @throws ForbiddenHttpException
* @throws InvalidConfigException
* @throws InvalidParamException
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws StaleObjectException
* @throws Throwable
*/
public function actionRemovePoster($id)
{
$event = $this->findModel($id);
$handler = Yii::createObject(Poster\Remove\Handler::class);
if (!$this->checker->allowEdit($event->getId())) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
$uploader = Yii::createObject(FileUploader::class);
$uploader->remove($event->getPoster());
$handler->handle(new Poster\Remove\Command($event->getId()->getValue(), $event->getPoster()));
return $this->redirect(['view', 'id' => $event->getId()->getValue()]);
}
/**
* @param $id
* @return Response
* @throws InvalidConfigException
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws StaleObjectException
* @throws Throwable
*/
public function actionActivate($id)
{
if (!$this->checker->allowChangeStatus()) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
$event = $this->findModel($id);
$handler = Yii::createObject(Activate\Handler::class);
try {
$handler->handle(new Activate\Command($event->getId()->getValue()));
Yii::$app->session->setFlash('success', 'Мероприятие активировано!');
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['view', 'id' => $event->getId()->getValue()]);
}
/**
* @param $id
* @return Response
* @throws InvalidConfigException
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws StaleObjectException
* @throws Throwable
*/
public function actionReject($id)
{
if (!$this->checker->allowChangeStatus()) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
$event = $this->findModel($id);
$handler = Yii::createObject(Reject\Handler::class);
try {
$handler->handle(new Reject\Command($event->getId()->getValue()));
Yii::$app->session->setFlash('success', 'Мероприятие отклонено!');
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['view', 'id' => $event->getId()->getValue()]);
}
/**
* @param $id
* @return string|Response
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws StaleObjectException
* @throws Throwable
*/
public function actionCancel($id)
{
if (!$this->checker->allowChangeStatus()) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
$event = $this->findModel($id);
$form = new Cancel\Form();
$handler = Yii::createObject(Cancel\Handler::class);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$handler->handle(new Cancel\Command($event->getId()->getValue(), $form->reason));
Yii::$app->session->setFlash('success', 'Мероприятие отменено!');
return $this->redirect(['view', 'id' => $event->getId()->getValue()]);
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('cancel', [
'cancelForm' => $form,
'model' => $event,
]);
}
/**
* @param $event_id
* @return string|Response
* @throws ForbiddenHttpException
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws InvalidParamException
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws StaleObjectException
* @throws Throwable
*/
public function actionAssignPlace($event_id)
{
if (!$this->checker->allowEdit(new Id($event_id))) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
$event = $this->findModel($event_id);
$form = new Assign\Form();
$handler = Yii::createObject(Assign\Handler::class);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$handler->handle(new Assign\Command($event->getId()->getValue(), $form->name, $form->street, $form->city));
return $this->redirect(['view', 'id' => $event->getId()->getValue()]);
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('assign-place', [
'assignForm' => $form,
'model' => $event,
]);
}
/**
* @return Response
* @throws ForbiddenHttpException
* @throws InvalidConfigException
* @throws InvalidParamException
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws StaleObjectException
* @throws Throwable
*/
public function actionRevokePlace()
{
$event_id = Yii::$app->request->post('event_id');
$id = Yii::$app->request->post('id');
if (!$this->checker->allowEdit(new Id($event_id))) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
$event = $this->findModel($event_id);
$handler = Yii::createObject(Revoke\Handler::class);
try {
$handler->handle(new Revoke\Command($event->getId()->getValue(), $id));
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['view', 'id' => $event->getId()->getValue()]);
}
/**
* @param $id
* @return Response
* @throws NotFoundHttpException
* @throws RuntimeException
* @throws Throwable
* @throws InvalidConfigException
* @throws StaleObjectException
*/
public function actionDelete($id)
{
if (!$this->checker->allowRemove()) {
throw new ForbiddenHttpException('Вам не разрешено производить данное действие!');
}
$event = $this->findModel($id);
$handler = Yii::createObject(Remove\Handler::class);
try {
$handler->handle(new Remove\Command($event->getId()->getValue()));
Yii::$app->session->setFlash('success', 'Мероприятие удалёно!');
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->redirect(['index']);
}
/**
* @param $id
* @return Event
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = Event::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('event-event', 'Запрашиваемая страница не найдена.'));
}
}