summaryrefslogtreecommitdiff
path: root/controllers/board.php
blob: 13714b3c33840efac6fd6d3d54f6d83221e91d16 (plain)
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
<?php

namespace Controller;

class Board extends Base
{
    // Change a task assignee directly from the board
    public function assign()
    {
        $task = $this->task->getById($this->request->getIntegerParam('task_id'));
        $project = $this->project->getById($task['project_id']);
        $projects = $this->project->getListByStatus(\Model\Project::ACTIVE);

        if ($this->acl->isRegularUser()) {
            $projects = $this->project->filterListByAccess($projects, $this->acl->getUserId());
        }

        if (! $project) $this->notfound();
        $this->checkProjectPermissions($project['id']);

        $this->response->html($this->template->layout('board_assign', array(
            'errors' => array(),
            'values' => $task,
            'users_list' => $this->project->getUsersList($project['id']),
            'projects' => $projects,
            'current_project_id' => $project['id'],
            'current_project_name' => $project['name'],
            'menu' => 'boards',
            'title' => t('Change assignee').' - '.$task['title'],
        )));
    }

    // Validate an assignee change
    public function assignTask()
    {
        $values = $this->request->getValues();
        $this->checkProjectPermissions($values['project_id']);

        list($valid,) = $this->task->validateAssigneeModification($values);

        if ($valid && $this->task->update($values)) {
            $this->session->flash(t('Task updated successfully.'));
        }
        else {
            $this->session->flashError(t('Unable to update your task.'));
        }

        $this->response->redirect('?controller=board&action=show&project_id='.$values['project_id']);
    }

    // Display the public version of a board
    // Access checked by a simple token, no user login, read only, auto-refresh
    public function readonly()
    {
        $token = $this->request->getStringParam('token');
        $project = $this->project->getByToken($token);

        // Token verification
        if (! $project) {
            $this->response->text('Not Authorized', 401);
        }

        // Display the board with a specific layout
        $this->response->html($this->template->layout('board_public', array(
            'project' => $project,
            'columns' => $this->board->get($project['id']),
            'title' => $project['name'],
            'no_layout' => true,
            'auto_refresh' => true,
        )));
    }

    // Display the default user project or the first project
    public function index()
    {
        $projects = $this->project->getListByStatus(\Model\Project::ACTIVE);

        if ($this->acl->isRegularUser()) {
            $projects = $this->project->filterListByAccess($projects, $this->acl->getUserId());
        }

        if (empty($projects)) {

            if ($this->acl->isAdminUser()) {
                $this->redirectNoProject();
            }
            else {
                $this->response->redirect('?controller=project&action=forbidden');
            }
        }
        else if (! empty($_SESSION['user']['default_project_id']) && isset($projects[$_SESSION['user']['default_project_id']])) {
            $project_id = $_SESSION['user']['default_project_id'];
            $project_name = $projects[$_SESSION['user']['default_project_id']];
        }
        else {
            list($project_id, $project_name) = each($projects);
        }

        $this->checkProjectPermissions($project_id);

        $this->response->html($this->template->layout('board_index', array(
            'projects' => $projects,
            'current_project_id' => $project_id,
            'current_project_name' => $project_name,
            'columns' => $this->board->get($project_id),
            'menu' => 'boards',
            'title' => $project_name
        )));
    }

    // Show a board for a given project
    public function show()
    {
        $projects = $this->project->getListByStatus(\Model\Project::ACTIVE);

        if ($this->acl->isRegularUser()) {
            $projects = $this->project->filterListByAccess($projects, $this->acl->getUserId());
        }

        $project_id = $this->request->getIntegerParam('project_id');

        $this->checkProjectPermissions($project_id);
        if (! isset($projects[$project_id])) $this->notfound();

        $project_name = $projects[$project_id];

        $this->response->html($this->template->layout('board_index', array(
            'projects' => $projects,
            'current_project_id' => $project_id,
            'current_project_name' => $project_name,
            'columns' => $this->board->get($project_id),
            'menu' => 'boards',
            'title' => $project_name
        )));
    }

    // Display a form to edit a board
    public function edit()
    {
        $project_id = $this->request->getIntegerParam('project_id');
        $project = $this->project->getById($project_id);

        if (! $project) $this->notfound();

        $columns = $this->board->getColumns($project_id);
        $values = array();

        foreach ($columns as $column) {
            $values['title['.$column['id'].']'] = $column['title'];
            $values['task_limit['.$column['id'].']'] = $column['task_limit'] ?: null;
        }

        $this->response->html($this->template->layout('board_edit', array(
            'errors' => array(),
            'values' => $values + array('project_id' => $project_id),
            'columns' => $columns,
            'project' => $project,
            'menu' => 'projects',
            'title' => t('Edit board')
        )));
    }

    // Validate and update a board
    public function update()
    {
        $project_id = $this->request->getIntegerParam('project_id');
        $project = $this->project->getById($project_id);

        if (! $project) $this->notfound();

        $columns = $this->board->getColumns($project_id);
        $data = $this->request->getValues();
        $values = $columns_list = array();

        foreach ($columns as $column) {
            $columns_list[$column['id']] = $column['title'];
            $values['title['.$column['id'].']'] = isset($data['title'][$column['id']]) ? $data['title'][$column['id']] : '';
            $values['task_limit['.$column['id'].']'] = isset($data['task_limit'][$column['id']]) ? $data['task_limit'][$column['id']] : 0;
        }

        list($valid, $errors) = $this->board->validateModification($columns_list, $values);

        if ($valid) {

            if ($this->board->update($data)) {
                $this->session->flash(t('Board updated successfully.'));
                $this->response->redirect('?controller=board&action=edit&project_id='.$project['id']);
            }
            else {
                $this->session->flashError(t('Unable to update this board.'));
            }
        }

        $this->response->html($this->template->layout('board_edit', array(
            'errors' => $errors,
            'values' => $values + array('project_id' => $project_id),
            'columns' => $columns,
            'project' => $project,
            'menu' => 'projects',
            'title' => t('Edit board')
        )));
    }

    // Validate and add a new column
    public function add()
    {
        $project_id = $this->request->getIntegerParam('project_id');
        $project = $this->project->getById($project_id);

        if (! $project) $this->notfound();

        $columns = $this->board->getColumnsList($project_id);
        $data = $this->request->getValues();
        $values = array();

        foreach ($columns as $column_id => $column_title) {
            $values['title['.$column_id.']'] = $column_title;
        }

        list($valid, $errors) = $this->board->validateCreation($data);

        if ($valid) {

            if ($this->board->add($data)) {
                $this->session->flash(t('Board updated successfully.'));
                $this->response->redirect('?controller=board&action=edit&project_id='.$project['id']);
            }
            else {
                $this->session->flashError(t('Unable to update this board.'));
            }
        }

        $this->response->html($this->template->layout('board_edit', array(
            'errors' => $errors,
            'values' => $values + $data,
            'columns' => $columns,
            'project' => $project,
            'menu' => 'projects',
            'title' => t('Edit board')
        )));
    }

    // Confirmation dialog before removing a column
    public function confirm()
    {
        $this->response->html($this->template->layout('board_remove', array(
            'column' => $this->board->getColumn($this->request->getIntegerParam('column_id')),
            'menu' => 'projects',
            'title' => t('Remove a column from a board')
        )));
    }

    // Remove a column
    public function remove()
    {
        $column = $this->board->getColumn($this->request->getIntegerParam('column_id'));

        if ($column && $this->board->removeColumn($column['id'])) {
            $this->session->flash(t('Column removed successfully.'));
        } else {
            $this->session->flashError(t('Unable to remove this column.'));
        }

        $this->response->redirect('?controller=board&action=edit&project_id='.$column['project_id']);
    }

    // Save the board (Ajax request made by the drag and drop)
    public function save()
    {
        $project_id = $this->request->getIntegerParam('project_id');

        if ($project_id > 0 && ! $this->project->isUserAllowed($project_id, $this->acl->getUserId())) {
            $this->response->json(array('result' => false), 401);
        }

        $this->response->json(array(
            'result' => $this->board->saveTasksPosition($this->request->getValues())
        ));
    }
}