From 11858be4e8d5aba983700c6cba1c4d0a33ea8e9d Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 9 Apr 2016 22:42:17 -0400 Subject: Filter refactoring --- app/Formatter/BaseFormatter.php | 37 ++++++ app/Formatter/BaseTaskCalendarFormatter.php | 45 +++++++ app/Formatter/BoardFormatter.php | 56 +++++++++ app/Formatter/FormatterInterface.php | 14 --- app/Formatter/GroupAutoCompleteFormatter.php | 28 +++-- app/Formatter/ProjectGanttFormatter.php | 39 +----- .../SubtaskTimeTrackingCalendarFormatter.php | 38 ++++++ app/Formatter/TaskAutoCompleteFormatter.php | 33 +++++ app/Formatter/TaskCalendarFormatter.php | 74 ++++++++++++ app/Formatter/TaskFilterAutoCompleteFormatter.php | 33 ----- app/Formatter/TaskFilterCalendarEvent.php | 76 ------------ app/Formatter/TaskFilterCalendarFormatter.php | 52 -------- app/Formatter/TaskFilterGanttFormatter.php | 78 ------------ app/Formatter/TaskFilterICalendarFormatter.php | 133 -------------------- app/Formatter/TaskGanttFormatter.php | 78 ++++++++++++ app/Formatter/TaskICalFormatter.php | 134 +++++++++++++++++++++ app/Formatter/UserAutoCompleteFormatter.php | 38 ++++++ app/Formatter/UserFilterAutoCompleteFormatter.php | 38 ------ 18 files changed, 558 insertions(+), 466 deletions(-) create mode 100644 app/Formatter/BaseFormatter.php create mode 100644 app/Formatter/BaseTaskCalendarFormatter.php create mode 100644 app/Formatter/BoardFormatter.php delete mode 100644 app/Formatter/FormatterInterface.php create mode 100644 app/Formatter/SubtaskTimeTrackingCalendarFormatter.php create mode 100644 app/Formatter/TaskAutoCompleteFormatter.php create mode 100644 app/Formatter/TaskCalendarFormatter.php delete mode 100644 app/Formatter/TaskFilterAutoCompleteFormatter.php delete mode 100644 app/Formatter/TaskFilterCalendarEvent.php delete mode 100644 app/Formatter/TaskFilterCalendarFormatter.php delete mode 100644 app/Formatter/TaskFilterGanttFormatter.php delete mode 100644 app/Formatter/TaskFilterICalendarFormatter.php create mode 100644 app/Formatter/TaskGanttFormatter.php create mode 100644 app/Formatter/TaskICalFormatter.php create mode 100644 app/Formatter/UserAutoCompleteFormatter.php delete mode 100644 app/Formatter/UserFilterAutoCompleteFormatter.php (limited to 'app/Formatter') diff --git a/app/Formatter/BaseFormatter.php b/app/Formatter/BaseFormatter.php new file mode 100644 index 00000000..a9f0ad15 --- /dev/null +++ b/app/Formatter/BaseFormatter.php @@ -0,0 +1,37 @@ +query = $query; + return $this; + } +} diff --git a/app/Formatter/BaseTaskCalendarFormatter.php b/app/Formatter/BaseTaskCalendarFormatter.php new file mode 100644 index 00000000..8fab3e9a --- /dev/null +++ b/app/Formatter/BaseTaskCalendarFormatter.php @@ -0,0 +1,45 @@ +startColumn = $start_column; + $this->endColumn = $end_column ?: $start_column; + return $this; + } +} diff --git a/app/Formatter/BoardFormatter.php b/app/Formatter/BoardFormatter.php new file mode 100644 index 00000000..6a96b3e6 --- /dev/null +++ b/app/Formatter/BoardFormatter.php @@ -0,0 +1,56 @@ +projectId = $projectId; + return $this; + } + + /** + * Apply formatter + * + * @access public + * @return array + */ + public function format() + { + $tasks = $this->query + ->eq(Task::TABLE.'.project_id', $this->projectId) + ->asc(Task::TABLE.'.position') + ->findAll(); + + return $this->board->getBoard($this->projectId, function ($project_id, $column_id, $swimlane_id) use ($tasks) { + return array_filter($tasks, function (array $task) use ($column_id, $swimlane_id) { + return $task['column_id'] == $column_id && $task['swimlane_id'] == $swimlane_id; + }); + }); + } +} diff --git a/app/Formatter/FormatterInterface.php b/app/Formatter/FormatterInterface.php deleted file mode 100644 index 0bb61292..00000000 --- a/app/Formatter/FormatterInterface.php +++ /dev/null @@ -1,14 +0,0 @@ -groups = $groups; + } + + /** + * Set query + * + * @access public + * @param Table $query + * @return FormatterInterface + */ + public function withQuery(Table $query) + { return $this; } /** - * Format groups for the ajax autocompletion + * Format groups for the ajax auto-completion * * @access public * @return array diff --git a/app/Formatter/ProjectGanttFormatter.php b/app/Formatter/ProjectGanttFormatter.php index 4f73e217..aee1f27f 100644 --- a/app/Formatter/ProjectGanttFormatter.php +++ b/app/Formatter/ProjectGanttFormatter.php @@ -2,7 +2,7 @@ namespace Kanboard\Formatter; -use Kanboard\Model\Project; +use Kanboard\Core\Filter\FormatterInterface; /** * Gantt chart formatter for projects @@ -10,40 +10,8 @@ use Kanboard\Model\Project; * @package formatter * @author Frederic Guillot */ -class ProjectGanttFormatter extends Project implements FormatterInterface +class ProjectGanttFormatter extends BaseFormatter implements FormatterInterface { - /** - * List of projects - * - * @access private - * @var array - */ - private $projects = array(); - - /** - * Filter projects to generate the Gantt chart - * - * @access public - * @param int[] $project_ids - * @return ProjectGanttFormatter - */ - public function filter(array $project_ids) - { - if (empty($project_ids)) { - $this->projects = array(); - } else { - $this->projects = $this->db - ->table(self::TABLE) - ->asc('start_date') - ->in('id', $project_ids) - ->eq('is_active', self::ACTIVE) - ->eq('is_private', 0) - ->findAll(); - } - - return $this; - } - /** * Format projects to be displayed in the Gantt chart * @@ -52,10 +20,11 @@ class ProjectGanttFormatter extends Project implements FormatterInterface */ public function format() { + $projects = $this->query->findAll(); $colors = $this->color->getDefaultColors(); $bars = array(); - foreach ($this->projects as $project) { + foreach ($projects as $project) { $start = empty($project['start_date']) ? time() : strtotime($project['start_date']); $end = empty($project['end_date']) ? $start : strtotime($project['end_date']); $color = next($colors) ?: reset($colors); diff --git a/app/Formatter/SubtaskTimeTrackingCalendarFormatter.php b/app/Formatter/SubtaskTimeTrackingCalendarFormatter.php new file mode 100644 index 00000000..c5d4e2be --- /dev/null +++ b/app/Formatter/SubtaskTimeTrackingCalendarFormatter.php @@ -0,0 +1,38 @@ +query->findAll() as $row) { + $user = isset($row['username']) ? ' ('.($row['user_fullname'] ?: $row['username']).')' : ''; + + $events[] = array( + 'id' => $row['id'], + 'subtask_id' => $row['subtask_id'], + 'title' => t('#%d', $row['task_id']).' '.$row['subtask_title'].$user, + 'start' => date('Y-m-d\TH:i:s', $row['start']), + 'end' => date('Y-m-d\TH:i:s', $row['end'] ?: time()), + 'backgroundColor' => $this->color->getBackgroundColor($row['color_id']), + 'borderColor' => $this->color->getBorderColor($row['color_id']), + 'textColor' => 'black', + 'url' => $this->helper->url->to('task', 'show', array('task_id' => $row['task_id'], 'project_id' => $row['project_id'])), + 'editable' => false, + ); + } + + return $events; + } +} diff --git a/app/Formatter/TaskAutoCompleteFormatter.php b/app/Formatter/TaskAutoCompleteFormatter.php new file mode 100644 index 00000000..480ee797 --- /dev/null +++ b/app/Formatter/TaskAutoCompleteFormatter.php @@ -0,0 +1,33 @@ +query->columns(Task::TABLE.'.id', Task::TABLE.'.title')->findAll(); + + foreach ($tasks as &$task) { + $task['value'] = $task['title']; + $task['label'] = '#'.$task['id'].' - '.$task['title']; + } + + return $tasks; + } +} diff --git a/app/Formatter/TaskCalendarFormatter.php b/app/Formatter/TaskCalendarFormatter.php new file mode 100644 index 00000000..60b9a062 --- /dev/null +++ b/app/Formatter/TaskCalendarFormatter.php @@ -0,0 +1,74 @@ +fullDay = true; + return $this; + } + + /** + * Transform tasks to calendar events + * + * @access public + * @return array + */ + public function format() + { + $events = array(); + + foreach ($this->query->findAll() as $task) { + $events[] = array( + 'timezoneParam' => $this->config->getCurrentTimezone(), + 'id' => $task['id'], + 'title' => t('#%d', $task['id']).' '.$task['title'], + 'backgroundColor' => $this->color->getBackgroundColor($task['color_id']), + 'borderColor' => $this->color->getBorderColor($task['color_id']), + 'textColor' => 'black', + 'url' => $this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])), + 'start' => date($this->getDateTimeFormat(), $task[$this->startColumn]), + 'end' => date($this->getDateTimeFormat(), $task[$this->endColumn] ?: time()), + 'editable' => $this->fullDay, + 'allday' => $this->fullDay, + ); + } + + return $events; + } + + /** + * Get DateTime format for event + * + * @access private + * @return string + */ + private function getDateTimeFormat() + { + return $this->fullDay ? 'Y-m-d' : 'Y-m-d\TH:i:s'; + } +} diff --git a/app/Formatter/TaskFilterAutoCompleteFormatter.php b/app/Formatter/TaskFilterAutoCompleteFormatter.php deleted file mode 100644 index c9af4654..00000000 --- a/app/Formatter/TaskFilterAutoCompleteFormatter.php +++ /dev/null @@ -1,33 +0,0 @@ -query->columns(Task::TABLE.'.id', Task::TABLE.'.title')->findAll(); - - foreach ($tasks as &$task) { - $task['value'] = $task['title']; - $task['label'] = '#'.$task['id'].' - '.$task['title']; - } - - return $tasks; - } -} diff --git a/app/Formatter/TaskFilterCalendarEvent.php b/app/Formatter/TaskFilterCalendarEvent.php deleted file mode 100644 index 12ea8687..00000000 --- a/app/Formatter/TaskFilterCalendarEvent.php +++ /dev/null @@ -1,76 +0,0 @@ -startColumn = $start_column; - $this->endColumn = $end_column ?: $start_column; - return $this; - } - - /** - * When called calendar events will be full day - * - * @access public - * @return TaskFilterCalendarEvent - */ - public function setFullDay() - { - $this->fullDay = true; - return $this; - } - - /** - * Return true if the events are full day - * - * @access public - * @return boolean - */ - public function isFullDay() - { - return $this->fullDay; - } -} diff --git a/app/Formatter/TaskFilterCalendarFormatter.php b/app/Formatter/TaskFilterCalendarFormatter.php deleted file mode 100644 index 1b5d6ca4..00000000 --- a/app/Formatter/TaskFilterCalendarFormatter.php +++ /dev/null @@ -1,52 +0,0 @@ -query->findAll() as $task) { - $events[] = array( - 'timezoneParam' => $this->config->getCurrentTimezone(), - 'id' => $task['id'], - 'title' => t('#%d', $task['id']).' '.$task['title'], - 'backgroundColor' => $this->color->getBackgroundColor($task['color_id']), - 'borderColor' => $this->color->getBorderColor($task['color_id']), - 'textColor' => 'black', - 'url' => $this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])), - 'start' => date($this->getDateTimeFormat(), $task[$this->startColumn]), - 'end' => date($this->getDateTimeFormat(), $task[$this->endColumn] ?: time()), - 'editable' => $this->isFullDay(), - 'allday' => $this->isFullDay(), - ); - } - - return $events; - } - - /** - * Get DateTime format for event - * - * @access private - * @return string - */ - private function getDateTimeFormat() - { - return $this->isFullDay() ? 'Y-m-d' : 'Y-m-d\TH:i:s'; - } -} diff --git a/app/Formatter/TaskFilterGanttFormatter.php b/app/Formatter/TaskFilterGanttFormatter.php deleted file mode 100644 index a4eef1ee..00000000 --- a/app/Formatter/TaskFilterGanttFormatter.php +++ /dev/null @@ -1,78 +0,0 @@ -query->findAll() as $task) { - $bars[] = $this->formatTask($task); - } - - return $bars; - } - - /** - * Format a single task - * - * @access private - * @param array $task - * @return array - */ - private function formatTask(array $task) - { - if (! isset($this->columns[$task['project_id']])) { - $this->columns[$task['project_id']] = $this->column->getList($task['project_id']); - } - - $start = $task['date_started'] ?: time(); - $end = $task['date_due'] ?: $start; - - return array( - 'type' => 'task', - 'id' => $task['id'], - 'title' => $task['title'], - 'start' => array( - (int) date('Y', $start), - (int) date('n', $start), - (int) date('j', $start), - ), - 'end' => array( - (int) date('Y', $end), - (int) date('n', $end), - (int) date('j', $end), - ), - 'column_title' => $task['column_name'], - 'assignee' => $task['assignee_name'] ?: $task['assignee_username'], - 'progress' => $this->task->getProgress($task, $this->columns[$task['project_id']]).'%', - 'link' => $this->helper->url->href('task', 'show', array('project_id' => $task['project_id'], 'task_id' => $task['id'])), - 'color' => $this->color->getColorProperties($task['color_id']), - 'not_defined' => empty($task['date_due']) || empty($task['date_started']), - ); - } -} diff --git a/app/Formatter/TaskFilterICalendarFormatter.php b/app/Formatter/TaskFilterICalendarFormatter.php deleted file mode 100644 index 25b3aea0..00000000 --- a/app/Formatter/TaskFilterICalendarFormatter.php +++ /dev/null @@ -1,133 +0,0 @@ -vCalendar->render(); - } - - /** - * Set calendar object - * - * @access public - * @param \Eluceo\iCal\Component\Calendar $vCalendar - * @return TaskFilterICalendarFormatter - */ - public function setCalendar(Calendar $vCalendar) - { - $this->vCalendar = $vCalendar; - return $this; - } - - /** - * Transform results to ical events - * - * @access public - * @return TaskFilterICalendarFormatter - */ - public function addDateTimeEvents() - { - foreach ($this->query->findAll() as $task) { - $start = new DateTime; - $start->setTimestamp($task[$this->startColumn]); - - $end = new DateTime; - $end->setTimestamp($task[$this->endColumn] ?: time()); - - $vEvent = $this->getTaskIcalEvent($task, 'task-#'.$task['id'].'-'.$this->startColumn.'-'.$this->endColumn); - $vEvent->setDtStart($start); - $vEvent->setDtEnd($end); - - $this->vCalendar->addComponent($vEvent); - } - - return $this; - } - - /** - * Transform results to all day ical events - * - * @access public - * @return TaskFilterICalendarFormatter - */ - public function addFullDayEvents() - { - foreach ($this->query->findAll() as $task) { - $date = new DateTime; - $date->setTimestamp($task[$this->startColumn]); - - $vEvent = $this->getTaskIcalEvent($task, 'task-#'.$task['id'].'-'.$this->startColumn); - $vEvent->setDtStart($date); - $vEvent->setDtEnd($date); - $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->setUrl($this->helper->url->base().$this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']))); - - if (! empty($task['owner_id'])) { - $vEvent->setOrganizer($task['assignee_name'] ?: $task['assignee_username'], $task['assignee_email']); - } - - if (! empty($task['creator_id'])) { - $attendees = new Attendees; - $attendees->add('MAILTO:'.($task['creator_email'] ?: $task['creator_username'].'@kanboard.local')); - $vEvent->setAttendees($attendees); - } - - return $vEvent; - } -} diff --git a/app/Formatter/TaskGanttFormatter.php b/app/Formatter/TaskGanttFormatter.php new file mode 100644 index 00000000..3209aa37 --- /dev/null +++ b/app/Formatter/TaskGanttFormatter.php @@ -0,0 +1,78 @@ +query->findAll() as $task) { + $bars[] = $this->formatTask($task); + } + + return $bars; + } + + /** + * Format a single task + * + * @access private + * @param array $task + * @return array + */ + private function formatTask(array $task) + { + if (! isset($this->columns[$task['project_id']])) { + $this->columns[$task['project_id']] = $this->column->getList($task['project_id']); + } + + $start = $task['date_started'] ?: time(); + $end = $task['date_due'] ?: $start; + + return array( + 'type' => 'task', + 'id' => $task['id'], + 'title' => $task['title'], + 'start' => array( + (int) date('Y', $start), + (int) date('n', $start), + (int) date('j', $start), + ), + 'end' => array( + (int) date('Y', $end), + (int) date('n', $end), + (int) date('j', $end), + ), + 'column_title' => $task['column_name'], + 'assignee' => $task['assignee_name'] ?: $task['assignee_username'], + 'progress' => $this->task->getProgress($task, $this->columns[$task['project_id']]).'%', + 'link' => $this->helper->url->href('task', 'show', array('project_id' => $task['project_id'], 'task_id' => $task['id'])), + 'color' => $this->color->getColorProperties($task['color_id']), + 'not_defined' => empty($task['date_due']) || empty($task['date_started']), + ); + } +} diff --git a/app/Formatter/TaskICalFormatter.php b/app/Formatter/TaskICalFormatter.php new file mode 100644 index 00000000..a149f725 --- /dev/null +++ b/app/Formatter/TaskICalFormatter.php @@ -0,0 +1,134 @@ +vCalendar->render(); + } + + /** + * Set calendar object + * + * @access public + * @param \Eluceo\iCal\Component\Calendar $vCalendar + * @return FormatterInterface + */ + public function setCalendar(Calendar $vCalendar) + { + $this->vCalendar = $vCalendar; + return $this; + } + + /** + * Transform results to iCal events + * + * @access public + * @return FormatterInterface + */ + public function addDateTimeEvents() + { + foreach ($this->query->findAll() as $task) { + $start = new DateTime; + $start->setTimestamp($task[$this->startColumn]); + + $end = new DateTime; + $end->setTimestamp($task[$this->endColumn] ?: time()); + + $vEvent = $this->getTaskIcalEvent($task, 'task-#'.$task['id'].'-'.$this->startColumn.'-'.$this->endColumn); + $vEvent->setDtStart($start); + $vEvent->setDtEnd($end); + + $this->vCalendar->addComponent($vEvent); + } + + return $this; + } + + /** + * Transform results to all day iCal events + * + * @access public + * @return FormatterInterface + */ + public function addFullDayEvents() + { + foreach ($this->query->findAll() as $task) { + $date = new DateTime; + $date->setTimestamp($task[$this->startColumn]); + + $vEvent = $this->getTaskIcalEvent($task, 'task-#'.$task['id'].'-'.$this->startColumn); + $vEvent->setDtStart($date); + $vEvent->setDtEnd($date); + $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->setUrl($this->helper->url->base().$this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']))); + + if (! empty($task['owner_id'])) { + $vEvent->setOrganizer($task['assignee_name'] ?: $task['assignee_username'], $task['assignee_email']); + } + + if (! empty($task['creator_id'])) { + $attendees = new Attendees; + $attendees->add('MAILTO:'.($task['creator_email'] ?: $task['creator_username'].'@kanboard.local')); + $vEvent->setAttendees($attendees); + } + + return $vEvent; + } +} diff --git a/app/Formatter/UserAutoCompleteFormatter.php b/app/Formatter/UserAutoCompleteFormatter.php new file mode 100644 index 00000000..c46a24d0 --- /dev/null +++ b/app/Formatter/UserAutoCompleteFormatter.php @@ -0,0 +1,38 @@ +query->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name')->findAll(); + + foreach ($users as &$user) { + $user['value'] = $user['username'].' (#'.$user['id'].')'; + + if (empty($user['name'])) { + $user['label'] = $user['username']; + } else { + $user['label'] = $user['name'].' ('.$user['username'].')'; + } + } + + return $users; + } +} diff --git a/app/Formatter/UserFilterAutoCompleteFormatter.php b/app/Formatter/UserFilterAutoCompleteFormatter.php deleted file mode 100644 index b98e0d69..00000000 --- a/app/Formatter/UserFilterAutoCompleteFormatter.php +++ /dev/null @@ -1,38 +0,0 @@ -query->columns(User::TABLE.'.id', User::TABLE.'.username', User::TABLE.'.name')->findAll(); - - foreach ($users as &$user) { - $user['value'] = $user['username'].' (#'.$user['id'].')'; - - if (empty($user['name'])) { - $user['label'] = $user['username']; - } else { - $user['label'] = $user['name'].' ('.$user['username'].')'; - } - } - - return $users; - } -} -- cgit v1.2.3