forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupController.php
More file actions
138 lines (111 loc) · 3.58 KB
/
Copy pathGroupController.php
File metadata and controls
138 lines (111 loc) · 3.58 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
<?php
namespace PHPCensor\Controller;
use DateTime;
use PHPCensor\Form;
use PHPCensor\Helper\Lang;
use PHPCensor\Http\Response\RedirectResponse;
use PHPCensor\Model\ProjectGroup;
use PHPCensor\Model\User;
use PHPCensor\Store\Factory;
use PHPCensor\Store\ProjectGroupStore;
use PHPCensor\WebController;
/**
* Project Controller - Allows users to create, edit and view projects.
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class GroupController extends WebController
{
/**
* @var string
*/
public $layoutName = 'layout';
/**
* @var ProjectGroupStore
*/
protected $groupStore;
public function init()
{
parent::init();
$this->groupStore = Factory::getStore('ProjectGroup');
}
/**
* List project groups.
*/
public function index()
{
$this->requireAdmin();
$groups = [];
$groupList = $this->groupStore->getWhere([], 100, 0, ['title' => 'ASC']);
foreach ($groupList['items'] as $group) {
$thisGroup = [
'title' => $group->getTitle(),
'id' => $group->getId(),
];
$projectsActive = Factory::getStore('Project')->getByGroupId($group->getId(), false);
$projectsArchived = Factory::getStore('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;
}
/**
* Add or edit a project group.
*
* @param null $groupId
*
* @return RedirectResponse
*/
public function edit($groupId = null)
{
$this->requireAdmin();
if (!is_null($groupId)) {
$group = $this->groupStore->getById($groupId);
} else {
$group = new ProjectGroup();
}
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();
$response->setHeader('Location', APP_URL.'group');
return $response;
}
$form = new Form();
$form->setMethod('POST');
$form->setAction(APP_URL . 'group/edit' . (!is_null($groupId) ? '/' . $groupId : ''));
$form->addField(new Form\Element\Csrf('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.
* @param $groupId
* @return RedirectResponse
*/
public function delete($groupId)
{
$this->requireAdmin();
$group = $this->groupStore->getById($groupId);
$this->groupStore->delete($group);
$response = new RedirectResponse();
$response->setHeader('Location', APP_URL.'group');
return $response;
}
}