summaryrefslogtreecommitdiff
path: root/app/Formatter/TaskICalFormatter.php
blob: 387074e51d397ebd5f4f44461274d40318edd239 (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
<?php

namespace Kanboard\Formatter;

use DateTime;
use Eluceo\iCal\Component\Calendar;
use Eluceo\iCal\Component\Event;
use Eluceo\iCal\Property\Event\Attendees;
use Eluceo\iCal\Property\Event\Organizer;
use Kanboard\Core\Filter\FormatterInterface;
use PicoDb\Table;

/**
 * iCal event formatter for tasks
 *
 * @package  formatter
 * @author   Frederic Guillot
 */
class TaskICalFormatter extends BaseFormatter implements FormatterInterface
{
    /**
     * Calendar object
     *
     * @access protected
     * @var \Eluceo\iCal\Component\Calendar
     */
    protected $vCalendar;

    /**
     * Get Ical events
     *
     * @access public
     * @return string
     */
    public function format()
    {
        return $this->vCalendar->render();
    }

    /**
     * Set calendar object
     *
     * @access public
     * @param \Eluceo\iCal\Component\Calendar $vCalendar
     * @return $this
     */
    public function setCalendar(Calendar $vCalendar)
    {
        $this->vCalendar = $vCalendar;
        return $this;
    }

    /**
     * Transform results to iCal events
     *
     * @access public
     * @param  Table  $query
     * @param  string $startColumn
     * @param  string $endColumn
     * @return $this
     */
    public function addTasksWithStartAndDueDate(Table $query, $startColumn, $endColumn)
    {
        foreach ($query->findAll() as $task) {
            $start = new DateTime;
            $start->setTimestamp($task[$startColumn]);

            $end = new DateTime;
            $end->setTimestamp($task[$endColumn] ?: time());

            $vEvent = $this->getTaskIcalEvent($task, 'task-#'.$task['id'].'-'.$startColumn.'-'.$endColumn);
            $vEvent->setDtStart($start);
            $vEvent->setDtEnd($end);

            $this->vCalendar->addComponent($vEvent);
        }

        return $this;
    }

    /**
     * Transform results to all day iCal events
     *
     * @access public
     * @param  Table $query
     * @return $this
     */
    public function addTasksWithDueDateOnly(Table $query)
    {
        foreach ($query->findAll() as $task) {
            $date = new DateTime;
            $date->setTimestamp($task['date_due']);

            $vEvent = $this->getTaskIcalEvent($task, 'task-#'.$task['id'].'-date_due');
            $vEvent->setDtStart($date);
            $vEvent->setDtEnd($date);

            if ($date->format('Hi') === '0000') {
                $vEvent->setNoTime(true);
            }

            $this->vCalendar->addComponent($vEvent);
        }

        return $this;
    }

    /**
     * Get common events for task iCal events
     *
     * @access protected
     * @param  array   $task
     * @param  string  $uid
     * @return Event
     */
    protected function getTaskIcalEvent(array &$task, $uid)
    {
        $dateCreation = new DateTime;
        $dateCreation->setTimestamp($task['date_creation']);

        $dateModif = new DateTime;
        $dateModif->setTimestamp($task['date_modification']);

        $vEvent = new Event($uid);
        $vEvent->setCreated($dateCreation);
        $vEvent->setModified($dateModif);
        $vEvent->setUseTimezone(true);
        $vEvent->setSummary(t('#%d', $task['id']).' '.$task['title']);
        $vEvent->setDescription($task['description']);
        $vEvent->setDescriptionHTML($this->helper->text->markdown($task['description']));
        $vEvent->setUrl($this->helper->url->base().$this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])));

        if (! empty($task['owner_id'])) {
            $attendees = new Attendees;
            $attendees->add(
                'MAILTO:'.($task['assignee_email'] ?: $task['assignee_username'].'@kanboard.local'),
                array('CN' => $task['assignee_name'] ?: $task['assignee_username'])
            );
            $vEvent->setAttendees($attendees);
        }

        if (! empty($task['creator_id'])) {
            $vEvent->setOrganizer(new Organizer(
                'MAILTO:' . $task['creator_email'] ?: $task['creator_username'].'@kanboard.local',
                array('CN' => $task['creator_name'] ?: $task['creator_username'])
            ));
        }

        return $vEvent;
    }
}