1<?php
2
3namespace Kanboard\Api\Procedure;
4
5use Kanboard\Api\Authorization\ProjectAuthorization;
6use Kanboard\Api\Authorization\TaskAuthorization;
7use Kanboard\Filter\TaskProjectFilter;
8use Kanboard\Model\TaskModel;
9
10/**
11 * Task API controller
12 *
13 * @package  Kanboard\Api\Procedure
14 * @author   Frederic Guillot
15 */
16class TaskProcedure extends BaseProcedure
17{
18    public function searchTasks($project_id, $query)
19    {
20        ProjectAuthorization::getInstance($this->container)->check($this->getClassName(), 'searchTasks', $project_id);
21        return $this->taskLexer->build($query)->withFilter(new TaskProjectFilter($project_id))->toArray();
22    }
23
24    public function getTask($task_id)
25    {
26        TaskAuthorization::getInstance($this->container)->check($this->getClassName(), 'getTask', $task_id);
27        $task = $this->taskFinderModel->getById($task_id);
28        return $this->taskApiFormatter->withTask($task)->format();
29    }
30
31    public function getTaskByReference($project_id, $reference)
32    {
33        ProjectAuthorization::getInstance($this->container)->check($this->getClassName(), 'getTaskByReference', $project_id);
34        $task = $this->taskFinderModel->getByReference($project_id, $reference);
35        return $this->taskApiFormatter->withTask($task)->format();
36    }
37
38    public function getAllTasks($project_id, $status_id = TaskModel::STATUS_OPEN)
39    {
40        ProjectAuthorization::getInstance($this->container)->check($this->getClassName(), 'getAllTasks', $project_id);
41        $tasks = $this->taskFinderModel->getAll($project_id, $status_id);
42        return $this->tasksApiFormatter->withTasks($tasks)->format();
43    }
44
45    public function getOverdueTasks()
46    {
47        return $this->taskFinderModel->getOverdueTasks();
48    }
49
50    public function getOverdueTasksByProject($project_id)
51    {
52        ProjectAuthorization::getInstance($this->container)->check($this->getClassName(), 'getOverdueTasksByProject', $project_id);
53        return $this->taskFinderModel->getOverdueTasksByProject($project_id);
54    }
55
56    public function openTask($task_id)
57    {
58        TaskAuthorization::getInstance($this->container)->check($this->getClassName(), 'openTask', $task_id);
59        return $this->taskStatusModel->open($task_id);
60    }
61
62    public function closeTask($task_id)
63    {
64        TaskAuthorization::getInstance($this->container)->check($this->getClassName(), 'closeTask', $task_id);
65        return $this->taskStatusModel->close($task_id);
66    }
67
68    public function removeTask($task_id)
69    {
70        TaskAuthorization::getInstance($this->container)->check($this->getClassName(), 'removeTask', $task_id);
71        return $this->taskModel->remove($task_id);
72    }
73
74    public function moveTaskPosition($project_id, $task_id, $column_id, $position, $swimlane_id)
75    {
76        ProjectAuthorization::getInstance($this->container)->check($this->getClassName(), 'moveTaskPosition', $project_id);
77        return $this->taskPositionModel->movePosition($project_id, $task_id, $column_id, $position, $swimlane_id);
78    }
79
80    public function moveTaskToProject($task_id, $project_id, $swimlane_id = null, $column_id = null, $category_id = null, $owner_id = null)
81    {
82        ProjectAuthorization::getInstance($this->container)->check($this->getClassName(), 'moveTaskToProject', $project_id);
83        return $this->taskProjectMoveModel->moveToProject($task_id, $project_id, $swimlane_id, $column_id, $category_id, $owner_id);
84    }
85
86    public function duplicateTaskToProject($task_id, $project_id, $swimlane_id = null, $column_id = null, $category_id = null, $owner_id = null)
87    {
88        ProjectAuthorization::getInstance($this->container)->check($this->getClassName(), 'duplicateTaskToProject', $project_id);
89        return $this->taskProjectDuplicationModel->duplicateToProject($task_id, $project_id, $swimlane_id, $column_id, $category_id, $owner_id);
90    }
91
92    public function createTask($title, $project_id, $color_id = '', $column_id = 0, $owner_id = 0, $creator_id = 0,
93                               $date_due = '', $description = '', $category_id = 0, $score = 0, $swimlane_id = null, $priority = 0,
94                               $recurrence_status = 0, $recurrence_trigger = 0, $recurrence_factor = 0, $recurrence_timeframe = 0,
95                               $recurrence_basedate = 0, $reference = '', array $tags = array(), $date_started = '',
96                               $time_spent = null, $time_estimated = null)
97    {
98        ProjectAuthorization::getInstance($this->container)->check($this->getClassName(), 'createTask', $project_id);
99
100        if ($owner_id !== 0 && ! $this->projectPermissionModel->isAssignable($project_id, $owner_id)) {
101            return false;
102        }
103
104        if ($this->userSession->isLogged()) {
105            $creator_id = $this->userSession->getId();
106        }
107
108        $values = array(
109            'title' => $title,
110            'project_id' => $project_id,
111            'color_id' => $color_id,
112            'column_id' => $column_id,
113            'owner_id' => $owner_id,
114            'creator_id' => $creator_id,
115            'date_due' => $date_due,
116            'description' => $description,
117            'category_id' => $category_id,
118            'score' => $score,
119            'swimlane_id' => $swimlane_id,
120            'recurrence_status' => $recurrence_status,
121            'recurrence_trigger' => $recurrence_trigger,
122            'recurrence_factor' => $recurrence_factor,
123            'recurrence_timeframe' => $recurrence_timeframe,
124            'recurrence_basedate' => $recurrence_basedate,
125            'reference' => $reference,
126            'priority' => $priority,
127            'tags' => $tags,
128            'date_started' => $date_started,
129            'time_spent' => $time_spent,
130            'time_estimated' => $time_estimated,
131        );
132
133        list($valid, ) = $this->taskValidator->validateCreation($values);
134
135        return $valid ? $this->taskCreationModel->create($values) : false;
136    }
137
138    public function updateTask($id, $title = null, $color_id = null, $owner_id = null,
139                               $date_due = null, $description = null, $category_id = null, $score = null, $priority = null,
140                               $recurrence_status = null, $recurrence_trigger = null, $recurrence_factor = null,
141                               $recurrence_timeframe = null, $recurrence_basedate = null, $reference = null, $tags = null, $date_started = null,
142                               $time_spent = null, $time_estimated = null)
143    {
144        TaskAuthorization::getInstance($this->container)->check($this->getClassName(), 'updateTask', $id);
145        $project_id = $this->taskFinderModel->getProjectId($id);
146
147        if ($project_id === 0) {
148            return false;
149        }
150
151        if ($owner_id !== null && $owner_id != 0 && ! $this->projectPermissionModel->isAssignable($project_id, $owner_id)) {
152            return false;
153        }
154
155        $values = $this->filterValues(array(
156            'id' => $id,
157            'title' => $title,
158            'color_id' => $color_id,
159            'owner_id' => $owner_id,
160            'date_due' => $date_due,
161            'description' => $description,
162            'category_id' => $category_id,
163            'score' => $score,
164            'recurrence_status' => $recurrence_status,
165            'recurrence_trigger' => $recurrence_trigger,
166            'recurrence_factor' => $recurrence_factor,
167            'recurrence_timeframe' => $recurrence_timeframe,
168            'recurrence_basedate' => $recurrence_basedate,
169            'reference' => $reference,
170            'priority' => $priority,
171            'tags' => $tags,
172            'date_started' => $date_started,
173            'time_spent' => $time_spent,
174            'time_estimated' => $time_estimated,
175        ));
176
177        list($valid) = $this->taskValidator->validateApiModification($values);
178        return $valid && $this->taskModificationModel->update($values);
179    }
180}
181