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

namespace Kanboard\Controller;

use Kanboard\Model\TaskFilter;
use Kanboard\Model\Task as TaskModel;
use Eluceo\iCal\Component\Calendar as iCalendar;

/**
 * iCalendar controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class Ical extends Base
{
    /**
     * Get user iCalendar
     *
     * @access public
     */
    public function user()
    {
        $token = $this->request->getStringParam('token');
        $user = $this->user->getByToken($token);

        // Token verification
        if (empty($user)) {
            $this->forbidden(true);
        }

        // Common filter
        $filter = $this->taskFilterICalendarFormatter
            ->create()
            ->filterByStatus(TaskModel::STATUS_OPEN)
            ->filterByOwner($user['id']);

        // Calendar properties
        $calendar = new iCalendar('Kanboard');
        $calendar->setName($user['name'] ?: $user['username']);
        $calendar->setDescription($user['name'] ?: $user['username']);
        $calendar->setPublishedTTL('PT1H');

        $this->renderCalendar($filter, $calendar);
    }

    /**
     * Get project iCalendar
     *
     * @access public
     */
    public function project()
    {
        $token = $this->request->getStringParam('token');
        $project = $this->project->getByToken($token);

        // Token verification
        if (empty($project)) {
            $this->forbidden(true);
        }

        // Common filter
        $filter = $this->taskFilterICalendarFormatter
            ->create()
            ->filterByStatus(TaskModel::STATUS_OPEN)
            ->filterByProject($project['id']);

        // Calendar properties
        $calendar = new iCalendar('Kanboard');
        $calendar->setName($project['name']);
        $calendar->setDescription($project['name']);
        $calendar->setPublishedTTL('PT1H');

        $this->renderCalendar($filter, $calendar);
    }

    /**
     * Common method to render iCal events
     *
     * @access private
     */
    private function renderCalendar(TaskFilter $filter, iCalendar $calendar)
    {
        $start = $this->request->getStringParam('start', strtotime('-2 month'));
        $end = $this->request->getStringParam('end', strtotime('+6 months'));

        // Tasks
        if ($this->config->get('calendar_project_tasks', 'date_started') === 'date_creation') {
            $filter
                ->copy()
                ->filterByCreationDateRange($start, $end)
                ->setColumns('date_creation', 'date_completed')
                ->setCalendar($calendar)
                ->addDateTimeEvents();
        } else {
            $filter
                ->copy()
                ->filterByStartDateRange($start, $end)
                ->setColumns('date_started', 'date_completed')
                ->setCalendar($calendar)
                ->addDateTimeEvents($calendar);
        }

        // Tasks with due date
        $filter
            ->copy()
            ->filterByDueDateRange($start, $end)
            ->setColumns('date_due')
            ->setCalendar($calendar)
            ->addFullDayEvents($calendar);

        $this->response->contentType('text/calendar; charset=utf-8');
        echo $filter->setCalendar($calendar)->format();
    }
}