summaryrefslogtreecommitdiff
path: root/app/Controller/ICalendarController.php
blob: 38475a32fc66be204c4c21b2548ef2bc3ca99031 (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\Core\Controller\AccessForbiddenException;
use Kanboard\Core\Filter\QueryBuilder;
use Kanboard\Filter\TaskAssigneeFilter;
use Kanboard\Filter\TaskDueDateRangeFilter;
use Kanboard\Filter\TaskProjectFilter;
use Kanboard\Filter\TaskStatusFilter;
use Kanboard\Model\TaskModel;
use Eluceo\iCal\Component\Calendar as iCalendar;

/**
 * iCalendar Controller
 *
 * @package  Kanboard\Controller
 * @author   Frederic Guillot
 */
class ICalendarController extends BaseController
{
    public function user()
    {
        $token = $this->request->getStringParam('token');
        $user = $this->userModel->getByToken($token);

        if (empty($user)) {
            throw AccessForbiddenException::getInstance()->withoutLayout();
        }

        $startRange = strtotime('-2 months');
        $endRange = strtotime('+6 months');

        $startColumn = $this->configModel->get('calendar_project_tasks', 'date_started');

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

        $queryDueDateOnly = QueryBuilder::create()
            ->withQuery($this->taskFinderModel->getICalQuery())
            ->withFilter(new TaskStatusFilter(TaskModel::STATUS_OPEN))
            ->withFilter(new TaskDueDateRangeFilter(array($startRange, $endRange)))
            ->withFilter(new TaskAssigneeFilter($user['id']))
            ->getQuery();

        $queryStartAndDueDate = QueryBuilder::create()
            ->withQuery($this->taskFinderModel->getICalQuery())
            ->withFilter(new TaskStatusFilter(TaskModel::STATUS_OPEN))
            ->withFilter(new TaskAssigneeFilter($user['id']))
            ->getQuery()
            ->addCondition($this->getConditionForTasksWithStartAndDueDate($startRange, $endRange, $startColumn, 'date_due'));

        $this->response->ical($this->taskICalFormatter
            ->setCalendar($calendar)
            ->addTasksWithDueDateOnly($queryDueDateOnly)
            ->addTasksWithStartAndDueDate($queryStartAndDueDate, $startColumn, 'date_due')
            ->format());
    }

    public function project()
    {
        $token = $this->request->getStringParam('token');
        $project = $this->projectModel->getByToken($token);

        if (empty($project)) {
            throw AccessForbiddenException::getInstance()->withoutLayout();
        }

        $startRange = strtotime('-2 months');
        $endRange = strtotime('+6 months');

        $startColumn = $this->configModel->get('calendar_project_tasks', 'date_started');

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

        $queryDueDateOnly = QueryBuilder::create()
            ->withQuery($this->taskFinderModel->getICalQuery())
            ->withFilter(new TaskStatusFilter(TaskModel::STATUS_OPEN))
            ->withFilter(new TaskProjectFilter($project['id']))
            ->withFilter(new TaskDueDateRangeFilter(array($startRange, $endRange)))
            ->getQuery();

        $queryStartAndDueDate = QueryBuilder::create()
            ->withQuery($this->taskFinderModel->getICalQuery())
            ->withFilter(new TaskStatusFilter(TaskModel::STATUS_OPEN))
            ->withFilter(new TaskProjectFilter($project['id']))
            ->getQuery()
            ->addCondition($this->getConditionForTasksWithStartAndDueDate($startRange, $endRange, $startColumn, 'date_due'));

        $this->response->ical($this->taskICalFormatter
            ->setCalendar($calendar)
            ->addTasksWithDueDateOnly($queryDueDateOnly)
            ->addTasksWithStartAndDueDate($queryStartAndDueDate, $startColumn, 'date_due')
            ->format());
    }

    protected function getConditionForTasksWithStartAndDueDate($start_time, $end_time, $start_column, $end_column)
    {
        $start_column = $this->db->escapeIdentifier($start_column);
        $end_column = $this->db->escapeIdentifier($end_column);

        $conditions = array(
            "($start_column >= '$start_time' AND $start_column <= '$end_time')",
            "($start_column <= '$start_time' AND $end_column >= '$start_time')",
            "($start_column <= '$start_time' AND ($end_column = '0' OR $end_column IS NULL))",
        );

        return $start_column.' IS NOT NULL AND '.$start_column.' > 0 AND ('.implode(' OR ', $conditions).')';
    }
}