summaryrefslogtreecommitdiff
path: root/plugins/Calendar/Formatter/TaskCalendarFormatter.php
blob: 9286eaf8354b0bd9220218c2cbcf24c99de70891 (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
<?php

namespace Kanboard\Plugin\Calendar\Formatter;

use DateTime;
use Kanboard\Core\Filter\FormatterInterface;
use Kanboard\Formatter\BaseFormatter;

/**
 * Calendar event formatter for task filter
 *
 * @package  Kanboard\Plugin\Calendar\Formatter
 * @author   Frederic Guillot
 */
class TaskCalendarFormatter extends BaseFormatter implements FormatterInterface
{
    /**
     * Column used for event start date
     *
     * @access protected
     * @var string
     */
    protected $startColumn = '';

    /**
     * Column used for event end date
     *
     * @access protected
     * @var string
     */
    protected $endColumn = '';

    /**
     * Transform results to calendar events
     *
     * @access public
     * @param  string  $start_column    Column name for the start date
     * @param  string  $end_column      Column name for the end date
     * @return $this
     */
    public function setColumns($start_column, $end_column = '')
    {
        $this->startColumn = $start_column;
        $this->endColumn = $end_column ?: $start_column;
        return $this;
    }

    /**
     * Transform tasks to calendar events
     *
     * @access public
     * @return array
     */
    public function format()
    {
        $events = array();

        foreach ($this->query->findAll() as $task) {
            $startDate = new DateTime();
            $startDate->setTimestamp($task[$this->startColumn]);

            $endDate = new DateTime();
            if (! empty($task[$this->endColumn])) {
                $endDate->setTimestamp($task[$this->endColumn]);
            }

            $allDay = $startDate == $endDate && $endDate->format('Hi') == '0000';
            $format = $allDay ? 'Y-m-d' : 'Y-m-d\TH:i:s';

            $events[] = array(
                'timezoneParam' => $this->timezoneModel->getCurrentTimezone(),
                'id' => $task['id'],
                'title' => t('#%d', $task['id']).' '.$task['title'],
                'backgroundColor' => $this->colorModel->getBackgroundColor($task['color_id']),
                'borderColor' => $this->colorModel->getBorderColor($task['color_id']),
                'textColor' => 'black',
                'url' => $this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])),
                'start' => $startDate->format($format),
                'end' => $endDate->format($format),
                'editable' => $allDay,
                'allday' => $allDay,
            );
        }

        return $events;
    }
}