-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathGroupController.php
More file actions
140 lines (111 loc) · 3.91 KB
/
Copy pathGroupController.php
File metadata and controls
140 lines (111 loc) · 3.91 KB
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
<?php
declare(strict_types=1);
namespace PHPCensor\Controller;
use DateTime;
use PHPCensor\Form;
use PHPCensor\Helper\Lang;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use PHPCensor\Model\ProjectGroup;
use PHPCensor\Model\User;
use PHPCensor\Store\ProjectGroupStore;
use PHPCensor\WebController;
use PHPCensor\Form\Element\Csrf;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dan Cryer <dan@block8.co.uk>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class GroupController extends WebController
{
public string $layoutName = 'layout';
protected ProjectGroupStore $groupStore;
public function init(): void
{
parent::init();
$this->groupStore = $this->storeRegistry->get('ProjectGroup');
}
/**
* List project groups.
*/
public function index(): void
{
$this->requireAdmin();
$groups = [];
$groupList = $this->groupStore->getWhere([], 100, 0, ['title' => 'ASC']);
foreach ($groupList['items'] as $group) {
$thisGroup = [
'title' => $group->getTitle(),
'id' => $group->getId(),
];
$projectsActive = $this->storeRegistry->get('Project')->getByGroupId($group->getId(), false);
$projectsArchived = $this->storeRegistry->get('Project')->getByGroupId($group->getId(), true);
$thisGroup['projects'] = \array_merge($projectsActive['items'], $projectsArchived['items']);
$groups[] = $thisGroup;
}
$this->layout->title = Lang::get('group_projects');
$this->view->groups = $groups;
$this->view->user = $this->getUser();
}
/**
* Add or edit a project group.
*
* @return Response
*
* @throws \PHPCensor\Common\Exception\InvalidArgumentException
* @throws \PHPCensor\Common\Exception\RuntimeException
* @throws \PHPCensor\Exception\HttpException
*/
public function edit(?int $groupId = null)
{
$this->requireAdmin();
if (!\is_null($groupId)) {
$group = $this->groupStore->getById($groupId);
} else {
$group = new ProjectGroup($this->storeRegistry);
}
if ($this->request->getMethod() === 'POST') {
$group->setTitle($this->getParam('title'));
if (\is_null($groupId)) {
/** @var User $user */
$user = $this->getUser();
$group->setCreateDate(new DateTime());
$group->setUserId($user->getId());
}
$this->groupStore->save($group);
$response = new RedirectResponse(APP_URL . 'group');
return $response;
}
$form = new Form();
$form->setMethod('POST');
$form->setAction(APP_URL . 'group/edit' . (!\is_null($groupId) ? '/' . $groupId : ''));
$form->addField(new Csrf($this->session, 'group_form'));
$title = new Form\Element\Text('title');
$title->setContainerClass('form-group');
$title->setClass('form-control');
$title->setLabel(Lang::get('group_title'));
$title->setValue($group->getTitle());
$submit = new Form\Element\Submit();
$submit->setClass('btn btn-success');
$submit->setValue(Lang::get('group_save'));
$form->addField($title);
$form->addField($submit);
$this->view->form = $form;
}
/**
* Delete a project group.
*
* @throws \PHPCensor\Common\Exception\Exception
* @throws \PHPCensor\Common\Exception\InvalidArgumentException
* @throws \PHPCensor\Exception\HttpException
*/
public function delete(int $groupId): Response
{
$this->requireAdmin();
$group = $this->groupStore->getById($groupId);
$this->groupStore->delete($group);
return new RedirectResponse(APP_URL . 'group');
}
}