-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.php
168 lines (152 loc) · 4.81 KB
/
controller.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
<?php
namespace Plugin\Kanban;
use Helper\View;
use Model\Issue;
use Model\Issue\Backlog;
use Model\Issue\Detail as IssueDetail;
use Model\Issue\Status;
use Model\Issue\Type;
use Model\Sprint;
use Model\User;
use Model\User\Group;
class Controller extends \Controller
{
public function __construct()
{
$this->_userId = $this->_requireLogin();
}
/**
* View a Kanban board
*
* Accepts parameters: group, sprint
*
* @param \Base $f3
* @param array $params
*/
public function index(\Base $f3, array $params)
{
$group = new User;
$group->load($params['group']);
if (!$group->id) {
$f3->error(404);
return;
}
$sprint = new Sprint;
if (empty($params['sprint']) || !intval($params['sprint'])) {
$localDate = date('Y-m-d', View::instance()->utc2local());
$sprint->load(['? BETWEEN start_date AND end_date', $localDate]);
} else {
$sprint = $sprint->load($params['sprint']);
}
if (!$sprint->id) {
$f3->error(404);
return;
}
$f3->set('title', 'Kanban Board');
$f3->set('group', $group->id);
$f3->set('sprint', $sprint->id);
$this->_render("kanban/view/index.html");
}
/**
* Get swimlanes for the Kanban board
* @param \Base $f3
*/
public function boardLanes(\Base $f3)
{
$statusModel = new Status;
$statuses = $statusModel->find('taskboard > 0', ['order' => 'taskboard_sort ASC']);
$return = [];
foreach ($statuses as $status) {
$return[] = $status->cast();
}
$this->_printJson($return);
}
/**
* Get items on the Kanban board
* @param \Base $f3
*/
public function boardData(\Base $f3)
{
$groupId = $f3->get('GET.group');
if (!$groupId) {
$f3->error(400, 'A group ID is required to load board data.');
}
$sprintId = $f3->get('GET.sprint');
if (!intval($sprintId)) {
$f3->error(400, 'A sprint ID is required to load board data.');
}
// Add owner/sprint filters
$user = new User;
$user->load($groupId);
if ($user->role == 'group') {
$groupModel = new Group;
$groupUsers = $groupModel->find(['group_id = ?', $user->id]);
$filterUsers = [intval($groupId)];
foreach ($groupUsers as $u) {
$filterUsers[] = $u['user_id'];
}
} else {
$filterUsers = [intval($groupId)];
}
$ownerStr = implode(',', $filterUsers);
$filter = "owner_id IN ($ownerStr)";
$filter .= ' AND sprint_id = ' . intval($sprintId);
// Add status filter
$statusModel = new Status;
$statuses = $statusModel->find('taskboard > 0', ['order' => 'taskboard_sort ASC']);
$statusIds = [];
foreach ($statuses as $status) {
$statusIds[] = $status->id;
}
$statusStr = implode(',', $statusIds);
$filter .= " AND status IN ($statusStr)";
// Add type filter
$type = new Type;
$types = $type->find(['role = ?', 'project']);
$typeIds = [];
foreach ($types as $type) {
$typeIds[] = $type->id;
}
$typeStr = implode(',', $typeIds);
$filter .= " AND type_id IN ($typeStr)";
// Find issues
$issueModel = new IssueDetail;
$backlogModel = new Backlog;
$backlog = $backlogModel->load(['sprint_id' => intval($sprintId)]);
if ($backlog->issues) {
$backlogIds = "'" . implode("','", explode(',', trim($backlog->issues, '[]'))) . "'" ?: "'0'";
}
$issues = $issueModel->find($filter, $backlog->issues ? ['order' => "FIELD(id, $backlogIds), id"] : null);
$return = [];
foreach ($issues as $issue) {
$return[] = [
'id' => $issue->id,
'name' => $issue->name,
'status' => $issue->status,
'owner_id' => $issue->owner_id,
// TODO: get owner metadata from client-side list using computed props on kanban-issue
'owner_name' => $issue->owner_name,
'owner_task_color' => $issue->owner_task_color,
];
}
$this->_printJson($return);
}
/**
* Handle moving issues on the board
*
* @param \Base $f3
* @param array $params
*/
public function move(\Base $f3, array $params)
{
$issue = new Issue;
$issue->load($params['id']);
if (!$issue->id) {
$f3->error(404);
return;
}
$issue->status = $f3->get('POST.status');
$issue->save();
return $this->_printJson($issue->cast());
}
}