summaryrefslogtreecommitdiff
path: root/app/Formatter/TaskSuggestMenuFormatter.php
blob: 518f99e6db411654137ce4dcd7dbbfab81f41a6d (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
<?php

namespace Kanboard\Formatter;

use Kanboard\Core\Filter\FormatterInterface;
use Kanboard\Model\ProjectModel;
use Kanboard\Model\TaskModel;

/**
 * Class TaskSuggestMenuFormatter
 *
 * @package Kanboard\Formatter
 * @author  Frederic Guillot
 */
class TaskSuggestMenuFormatter extends BaseFormatter implements FormatterInterface
{
    protected $limit = 25;

    /**
     * Limit number of results
     *
     * @param  $limit
     * @return $this
     */
    public function withLimit($limit)
    {
        $this->limit = $limit;
        return $this;
    }

    /**
     * Apply formatter
     *
     * @access public
     * @return mixed
     */
    public function format()
    {
        $result = array();
        $tasks = $this->query
            ->columns(
                TaskModel::TABLE.'.id',
                TaskModel::TABLE.'.title',
                ProjectModel::TABLE.'.name AS project_name'
            )
            ->asc(TaskModel::TABLE.'.id')
            ->limit($this->limit)
            ->findAll();

        foreach ($tasks as $task) {
            $html = '#'.$task['id'].' ';
            $html .= $this->helper->text->e($task['title']).' ';
            $html .= '<small>'.$this->helper->text->e($task['project_name']).'</small>';

            $result[] = array(
                'value' => (string) $task['id'],
                'html' => $html,
            );
        }

        return $result;
    }
}