From cafbd1f5a7e60d931e4dbd700b12e5f50319340d Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Wed, 8 Nov 2017 14:50:02 -0800 Subject: Improve iCalendar feed to include tasks with start/end date and due date with a time --- app/Controller/ICalendarController.php | 97 ++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 40 deletions(-) (limited to 'app/Controller/ICalendarController.php') diff --git a/app/Controller/ICalendarController.php b/app/Controller/ICalendarController.php index 4fe8b78a..20e2a9d7 100644 --- a/app/Controller/ICalendarController.php +++ b/app/Controller/ICalendarController.php @@ -5,6 +5,7 @@ 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; @@ -18,81 +19,97 @@ use Eluceo\iCal\Component\Calendar as iCalendar; */ class ICalendarController extends BaseController { - /** - * Get user iCalendar - * - * @access public - */ public function user() { $token = $this->request->getStringParam('token'); $user = $this->userModel->getByToken($token); - // Token verification if (empty($user)) { throw AccessForbiddenException::getInstance()->withoutLayout(); } - // Common filter - $queryBuilder = new QueryBuilder(); - $queryBuilder - ->withQuery($this->taskFinderModel->getICalQuery()) - ->withFilter(new TaskStatusFilter(TaskModel::STATUS_OPEN)) - ->withFilter(new TaskAssigneeFilter($user['id'])); + $startRange = $this->request->getStringParam('start', strtotime('-2 months')); + $endRange = $this->request->getStringParam('end', strtotime('+6 months')); + + $startColumn = $this->configModel->get('calendar_project_tasks', 'date_started'); - // Calendar properties $calendar = new iCalendar('Kanboard'); $calendar->setName($user['name'] ?: $user['username']); $calendar->setDescription($user['name'] ?: $user['username']); $calendar->setPublishedTTL('PT1H'); - $this->renderCalendar($queryBuilder, $calendar); + $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()); } - /** - * Get project iCalendar - * - * @access public - */ public function project() { $token = $this->request->getStringParam('token'); $project = $this->projectModel->getByToken($token); - // Token verification if (empty($project)) { throw AccessForbiddenException::getInstance()->withoutLayout(); } - // Common filter - $queryBuilder = new QueryBuilder(); - $queryBuilder - ->withQuery($this->taskFinderModel->getICalQuery()) - ->withFilter(new TaskStatusFilter(TaskModel::STATUS_OPEN)) - ->withFilter(new TaskProjectFilter($project['id'])); + $startRange = $this->request->getStringParam('start', strtotime('-2 months')); + $endRange = $this->request->getStringParam('end', strtotime('+6 months')); + + $startColumn = $this->configModel->get('calendar_project_tasks', 'date_started'); - // Calendar properties $calendar = new iCalendar('Kanboard'); $calendar->setName($project['name']); $calendar->setDescription($project['name']); $calendar->setPublishedTTL('PT1H'); - $this->renderCalendar($queryBuilder, $calendar); + $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()); } - /** - * Common method to render iCal events - * - * @access private - * @param QueryBuilder $queryBuilder - * @param iCalendar $calendar - */ - private function renderCalendar(QueryBuilder $queryBuilder, iCalendar $calendar) + protected function getConditionForTasksWithStartAndDueDate($start_time, $end_time, $start_column, $end_column) { - $start = $this->request->getStringParam('start', strtotime('-2 month')); - $end = $this->request->getStringParam('end', strtotime('+6 months')); + $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))", + ); - $this->helper->ical->addTaskDateDueEvents($queryBuilder, $calendar, $start, $end); - $this->response->ical($this->taskICalFormatter->setCalendar($calendar)->format()); + return $start_column.' IS NOT NULL AND '.$start_column.' > 0 AND ('.implode(' OR ', $conditions).')'; } } -- cgit v1.2.3