summaryrefslogtreecommitdiff
path: root/controllers/task.php
blob: b30758b602edc2a0d1b1a3e1793267002e9c0e79 (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
<?php

namespace Controller;

class Task extends Base
{
    // Webhook to create a task (useful for external software)
    public function add()
    {
        $token = $this->request->getStringParam('token');

        if ($this->config->get('webhooks_token') !== $token) {
            $this->response->text('Not Authorized', 401);
        }

        $projectModel = new \Model\Project;
        $defaultProject = $projectModel->getFirst();

        $values = array(
            'title' => $this->request->getStringParam('title'),
            'description' => $this->request->getStringParam('description'),
            'color_id' => $this->request->getStringParam('color_id', 'blue'),
            'project_id' => $this->request->getIntegerParam('project_id', $defaultProject['id']),
            'owner_id' => $this->request->getIntegerParam('owner_id'),
            'column_id' => $this->request->getIntegerParam('column_id'),
        );

        if ($values['column_id'] == 0) {
            $boardModel = new \Model\Board;
            $values['column_id'] = $boardModel->getFirstColumn($values['project_id']);
        }

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

        if ($valid && $this->task->create($values)) {
            $this->response->text('OK');
        }

        $this->response->text('FAILED');
    }

    // Show a task
    public function show()
    {
        $task = $this->task->getById($this->request->getIntegerParam('task_id'), true);

        $this->response->html($this->template->layout('task_show', array(
            'task' => $task,
            'columns_list' => $this->board->getColumnsList($task['project_id']),
            'colors_list' => $this->task->getColors(),
            'menu' => 'tasks',
            'title' => $task['title']
        )));
    }

    // Display a form to create a new task
    public function create()
    {
        $project_id = $this->request->getIntegerParam('project_id');

        $this->response->html($this->template->layout('task_new', array(
            'errors' => array(),
            'values' => array(
                'project_id' => $project_id,
                'column_id' => $this->request->getIntegerParam('column_id'),
                'color_id' => $this->request->getStringParam('color_id'),
                'owner_id' => $this->request->getIntegerParam('owner_id'),
                'another_task' => $this->request->getIntegerParam('another_task'),
            ),
            'projects_list' => $this->project->getListByStatus(\Model\Project::ACTIVE),
            'columns_list' => $this->board->getColumnsList($project_id),
            'users_list' => $this->user->getList(),
            'colors_list' => $this->task->getColors(),
            'menu' => 'tasks',
            'title' => t('New task')
        )));
    }

    // Validate and save a new task
    public function save()
    {
        $values = $this->request->getValues();
        list($valid, $errors) = $this->task->validateCreation($values);

        if ($valid) {

            if ($this->task->create($values)) {
                $this->session->flash(t('Task created successfully.'));

                if (isset($values['another_task']) && $values['another_task'] == 1) {
                    unset($values['title']);
                    unset($values['description']);
                    $this->response->redirect('?controller=task&action=create&'.http_build_query($values));
                }
                else {
                    $this->response->redirect('?controller=board&action=show&project_id='.$values['project_id']);
                }
            }
            else {
                $this->session->flashError(t('Unable to create your task.'));
            }
        }

        $this->response->html($this->template->layout('task_new', array(
            'errors' => $errors,
            'values' => $values,
            'projects_list' => $this->project->getListByStatus(\Model\Project::ACTIVE),
            'columns_list' => $this->board->getColumnsList($values['project_id']),
            'users_list' => $this->user->getList(),
            'colors_list' => $this->task->getColors(),
            'menu' => 'tasks',
            'title' => t('New task')
        )));
    }

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

        $this->response->html($this->template->layout('task_edit', array(
            'errors' => array(),
            'values' => $task,
            'projects_list' => $this->project->getListByStatus(\Model\Project::ACTIVE),
            'columns_list' => $this->board->getColumnsList($task['project_id']),
            'users_list' => $this->user->getList(),
            'colors_list' => $this->task->getColors(),
            'menu' => 'tasks',
            'title' => t('Edit a task')
        )));
    }

    // Validate and update a task
    public function update()
    {
        $values = $this->request->getValues();
        list($valid, $errors) = $this->task->validateModification($values);

        if ($valid) {

            if ($this->task->update($values)) {
                $this->session->flash(t('Task updated successfully.'));
                $this->response->redirect('?controller=task&action=show&task_id='.$values['id']);
            }
            else {
                $this->session->flashError(t('Unable to update your task.'));
            }
        }

        $this->response->html($this->template->layout('task_edit', array(
            'errors' => $errors,
            'values' => $values,
            'projects_list' => $this->project->getListByStatus(\Model\Project::ACTIVE),
            'columns_list' => $this->board->getColumnsList($values['project_id']),
            'users_list' => $this->user->getList(),
            'colors_list' => $this->task->getColors(),
            'menu' => 'tasks',
            'title' => t('Edit a task')
        )));
    }

    // Hide a task
    public function close()
    {
        $task = $this->task->getById($this->request->getIntegerParam('task_id'));

        if ($task && $this->task->close($task['id'])) {
            $this->session->flash(t('Task closed successfully.'));
        } else {
            $this->session->flashError(t('Unable to close this task.'));
        }

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

    // Confirmation dialog before to close a task
    public function confirmClose()
    {
        $this->response->html($this->template->layout('task_close', array(
            'task' => $this->task->getById($this->request->getIntegerParam('task_id')),
            'menu' => 'tasks',
            'title' => t('Close a task')
        )));
    }

    // Open a task
    public function open()
    {
        $task = $this->task->getById($this->request->getIntegerParam('task_id'));

        if ($task && $this->task->close($task['id'])) {
            $this->session->flash(t('Task opened successfully.'));
        } else {
            $this->session->flashError(t('Unable to open this task.'));
        }

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

    // Confirmation dialog before to open a task
    public function confirmOpen()
    {
        $this->response->html($this->template->layout('task_open', array(
            'task' => $this->task->getById($this->request->getIntegerParam('task_id')),
            'menu' => 'tasks',
            'title' => t('Open a task')
        )));
    }
}