summaryrefslogtreecommitdiff
path: root/app/Controller/TaskAjaxController.php
blob: 6d0b3fc207213f1fc8d3e61fc5f0188745234fd0 (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
<?php

namespace Kanboard\Controller;

use Kanboard\Filter\TaskIdExclusionFilter;
use Kanboard\Filter\TaskIdFilter;
use Kanboard\Filter\TaskProjectsFilter;
use Kanboard\Filter\TaskStartsWithIdFilter;
use Kanboard\Filter\TaskStatusFilter;
use Kanboard\Filter\TaskTitleFilter;
use Kanboard\Model\TaskModel;

/**
 * Task Ajax Controller
 *
 * @package  Kanboard\Controller
 * @author   Frederic Guillot
 */
class TaskAjaxController extends BaseController
{
    /**
     * Task auto-completion (Ajax)
     *
     */
    public function autocomplete()
    {
        $search = $this->request->getStringParam('term');
        $project_ids = $this->projectPermissionModel->getActiveProjectIds($this->userSession->getId());
        $exclude_task_id = $this->request->getIntegerParam('exclude_task_id');

        if (empty($project_ids)) {
            $this->response->json(array());
        } else {

            $filter = $this->taskQuery->withFilter(new TaskProjectsFilter($project_ids));

            if (! empty($exclude_task_id)) {
                $filter->withFilter(new TaskIdExclusionFilter(array($exclude_task_id)));
            }

            if (ctype_digit($search)) {
                $filter->withFilter(new TaskIdFilter($search));
            } else {
                $filter->withFilter(new TaskTitleFilter($search));
            }

            $this->response->json($filter->format($this->taskAutoCompleteFormatter));
        }
    }

    /**
     * Task ID suggest menu
     */
    public function suggest()
    {
        $taskId = $this->request->getIntegerParam('search');
        $projectIds = $this->projectPermissionModel->getActiveProjectIds($this->userSession->getId());

        if (empty($projectIds)) {
            $this->response->json(array());
        } else {
            $filter = $this->taskQuery
                ->withFilter(new TaskProjectsFilter($projectIds))
                ->withFilter(new TaskStatusFilter(TaskModel::STATUS_OPEN))
                ->withFilter(new TaskStartsWithIdFilter($taskId));

            $this->response->json($filter->format($this->taskSuggestMenuFormatter));
        }
    }
}