From 2021dccc5a444f60c5ba1673d94b39999912cd26 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 20 Sep 2015 15:53:28 -0400 Subject: Move subtask forecast to a plugin Plugin repo: https://github.com/kanboard/plugin-subtask-forecast --- app/Controller/Calendar.php | 15 ++-- app/Controller/Config.php | 2 +- app/Core/Base.php | 1 - app/Core/Plugin/Hook.php | 70 +++++++++++++++++++ app/Helper/Hook.php | 10 +-- app/Model/SubtaskForecast.php | 124 ---------------------------------- app/Schema/Mysql.php | 1 - app/Schema/Postgres.php | 1 - app/Schema/Sqlite.php | 1 - app/ServiceProvider/ClassProvider.php | 4 +- app/Template/app/sidebar.php | 2 +- app/Template/config/calendar.php | 1 - app/Template/config/sidebar.php | 2 +- app/Template/export/sidebar.php | 2 +- app/Template/layout.php | 6 +- app/Template/project/dropdown.php | 2 +- app/Template/project/sidebar.php | 2 +- app/Template/project_user/sidebar.php | 2 +- app/Template/task/sidebar.php | 4 +- app/Template/user/sidebar.php | 4 +- 20 files changed, 101 insertions(+), 155 deletions(-) create mode 100644 app/Core/Plugin/Hook.php delete mode 100644 app/Model/SubtaskForecast.php (limited to 'app') diff --git a/app/Controller/Calendar.php b/app/Controller/Calendar.php index 8a24d705..5ac92622 100644 --- a/app/Controller/Calendar.php +++ b/app/Controller/Calendar.php @@ -52,6 +52,12 @@ class Calendar extends Base // Tasks with due date $events = array_merge($events, $filter->copy()->filterByDueDateRange($start, $end)->toAllDayCalendarEvents()); + $events = $this->hook->merge('controller:calendar:project:events', $events, array( + 'project_id' => $project_id, + 'start' => $start, + 'end' => $end, + )); + $this->response->json($events); } @@ -83,10 +89,11 @@ class Calendar extends Base $events = array_merge($events, $this->subtaskTimeTracking->getUserCalendarEvents($user_id, $start, $end)); } - // Subtask estimates - if ($this->config->get('calendar_user_subtasks_forecast') == 1) { - $events = array_merge($events, $this->subtaskForecast->getCalendarEvents($user_id, $end)); - } + $events = $this->hook->merge('controller:calendar:user:events', $events, array( + 'user_id' => $user_id, + 'start' => $start, + 'end' => $end, + )); $this->response->json($events); } diff --git a/app/Controller/Config.php b/app/Controller/Config.php index 6f14cc31..790bdcd3 100644 --- a/app/Controller/Config.php +++ b/app/Controller/Config.php @@ -48,7 +48,7 @@ class Config extends Base $values += array('integration_slack_webhook' => 0, 'integration_hipchat' => 0, 'integration_gravatar' => 0, 'integration_jabber' => 0); break; case 'calendar': - $values += array('calendar_user_subtasks_forecast' => 0, 'calendar_user_subtasks_time_tracking' => 0); + $values += array('calendar_user_subtasks_time_tracking' => 0); break; } diff --git a/app/Core/Base.php b/app/Core/Base.php index 5ed8f40a..2dec4b29 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -55,7 +55,6 @@ use Pimple\Container; * @property \Model\ProjectPermission $projectPermission * @property \Model\Subtask $subtask * @property \Model\SubtaskExport $subtaskExport - * @property \Model\SubtaskForecast $subtaskForecast * @property \Model\SubtaskTimeTracking $subtaskTimeTracking * @property \Model\Swimlane $swimlane * @property \Model\Task $task diff --git a/app/Core/Plugin/Hook.php b/app/Core/Plugin/Hook.php new file mode 100644 index 00000000..4fb55569 --- /dev/null +++ b/app/Core/Plugin/Hook.php @@ -0,0 +1,70 @@ +hooks[$hook])) { + $this->hooks[$hook] = array(); + } + + $this->hooks[$hook][] = $value; + } + + /** + * Get all bindings for a hook + * + * @access public + * @param string $hook + * @return array + */ + public function getListeners($hook) + { + return isset($this->hooks[$hook]) ? $this->hooks[$hook] : array(); + } + + /** + * Merge listener results with input array + * + * @access public + * @param string $hook + * @param array $values + * @param array $params + * @return array + */ + public function merge($hook, array &$values, array $params = array()) + { + foreach ($this->getListeners($hook) as $listener) { + $result = call_user_func_array($listener, $params); + + if (is_array($result) && ! empty($result)) { + $values = array_merge($values, $result); + } + } + + return $values; + } +} diff --git a/app/Helper/Hook.php b/app/Helper/Hook.php index 77756757..d7fe3d34 100644 --- a/app/Helper/Hook.php +++ b/app/Helper/Hook.php @@ -10,8 +10,6 @@ namespace Helper; */ class Hook extends \Core\Base { - private $hooks = array(); - /** * Render all attached hooks * @@ -24,10 +22,8 @@ class Hook extends \Core\Base { $buffer = ''; - foreach ($this->hooks as $name => $template) { - if ($hook === $name) { - $buffer .= $this->template->render($template, $variables); - } + foreach ($this->hook->getListeners($hook) as $template) { + $buffer .= $this->template->render($template, $variables); } return $buffer; @@ -43,7 +39,7 @@ class Hook extends \Core\Base */ public function attach($hook, $template) { - $this->hooks[$hook] = $template; + $this->hook->on($hook, $template); return $this; } } diff --git a/app/Model/SubtaskForecast.php b/app/Model/SubtaskForecast.php deleted file mode 100644 index 263aa27a..00000000 --- a/app/Model/SubtaskForecast.php +++ /dev/null @@ -1,124 +0,0 @@ -db - ->table(Subtask::TABLE) - ->columns(Subtask::TABLE.'.id', Task::TABLE.'.project_id', Subtask::TABLE.'.task_id', Subtask::TABLE.'.title', Subtask::TABLE.'.time_estimated') - ->join(Task::TABLE, 'id', 'task_id') - ->asc(Task::TABLE.'.position') - ->asc(Subtask::TABLE.'.position') - ->gt(Subtask::TABLE.'.time_estimated', 0) - ->eq(Subtask::TABLE.'.status', Subtask::STATUS_TODO) - ->eq(Subtask::TABLE.'.user_id', $user_id) - ->findAll(); - } - - /** - * Get the start date for the forecast - * - * @access public - * @param integer $user_id - * @return array - */ - public function getStartDate($user_id) - { - $subtask = $this->db->table(Subtask::TABLE) - ->columns(Subtask::TABLE.'.time_estimated', SubtaskTimeTracking::TABLE.'.start') - ->eq(SubtaskTimeTracking::TABLE.'.user_id', $user_id) - ->eq(SubtaskTimeTracking::TABLE.'.end', 0) - ->status('status', Subtask::STATUS_INPROGRESS) - ->join(SubtaskTimeTracking::TABLE, 'subtask_id', 'id') - ->findOne(); - - if ($subtask && $subtask['time_estimated'] && $subtask['start']) { - return date('Y-m-d H:i', $subtask['start'] + $subtask['time_estimated'] * 3600); - } - - return date('Y-m-d H:i'); - } - - /** - * Get all calendar events according to the user timetable and the subtasks estimates - * - * @access public - * @param integer $user_id - * @param string $end End date of the calendar - * @return array - */ - public function getCalendarEvents($user_id, $end) - { - $events = array(); - $start_date = new DateTime($this->getStartDate($user_id)); - $timetable = $this->timetable->calculate($user_id, $start_date, new DateTime($end)); - $subtasks = $this->getSubtasks($user_id); - $total = count($subtasks); - $offset = 0; - - foreach ($timetable as $slot) { - - $interval = $this->dateParser->getHours($slot[0], $slot[1]); - $start = $slot[0]->getTimestamp(); - - if ($slot[0] < $start_date) { - - if (! $this->dateParser->withinDateRange($start_date, $slot[0], $slot[1])) { - continue; - } - - $interval = $this->dateParser->getHours(new DateTime, $slot[1]); - $start = time(); - } - - while ($offset < $total) { - - $event = array( - 'id' => $subtasks[$offset]['id'].'-'.$subtasks[$offset]['task_id'].'-'.$offset, - 'subtask_id' => $subtasks[$offset]['id'], - 'title' => t('#%d', $subtasks[$offset]['task_id']).' '.$subtasks[$offset]['title'], - 'url' => $this->helper->url->to('task', 'show', array('task_id' => $subtasks[$offset]['task_id'], 'project_id' => $subtasks[$offset]['project_id'])), - 'editable' => false, - 'start' => date('Y-m-d\TH:i:s', $start), - ); - - if ($subtasks[$offset]['time_estimated'] <= $interval) { - - $start += $subtasks[$offset]['time_estimated'] * 3600; - $interval -= $subtasks[$offset]['time_estimated']; - $offset++; - - $event['end'] = date('Y-m-d\TH:i:s', $start); - $events[] = $event; - } - else { - $subtasks[$offset]['time_estimated'] -= $interval; - $event['end'] = $slot[1]->format('Y-m-d\TH:i:s'); - $events[] = $event; - break; - } - } - } - - return $events; - } -} diff --git a/app/Schema/Mysql.php b/app/Schema/Mysql.php index e5dff0d5..23a7a90a 100644 --- a/app/Schema/Mysql.php +++ b/app/Schema/Mysql.php @@ -166,7 +166,6 @@ function version_69($pdo) $result = $rq->fetch(PDO::FETCH_ASSOC); $rq = $pdo->prepare('INSERT INTO settings VALUES (?, ?)'); - $rq->execute(array('calendar_user_subtasks_forecast', isset($result['subtask_forecast']) && $result['subtask_forecast'] == 1 ? 1 : 0)); $rq->execute(array('calendar_user_subtasks_time_tracking', 0)); $rq->execute(array('calendar_user_tasks', 'date_started')); $rq->execute(array('calendar_project_tasks', 'date_started')); diff --git a/app/Schema/Postgres.php b/app/Schema/Postgres.php index e7422e8c..cd4c295e 100644 --- a/app/Schema/Postgres.php +++ b/app/Schema/Postgres.php @@ -161,7 +161,6 @@ function version_50($pdo) $result = $rq->fetch(PDO::FETCH_ASSOC); $rq = $pdo->prepare('INSERT INTO settings VALUES (?, ?)'); - $rq->execute(array('calendar_user_subtasks_forecast', isset($result['subtask_forecast']) && $result['subtask_forecast'] == 1 ? 1 : 0)); $rq->execute(array('calendar_user_subtasks_time_tracking', 0)); $rq->execute(array('calendar_user_tasks', 'date_started')); $rq->execute(array('calendar_project_tasks', 'date_started')); diff --git a/app/Schema/Sqlite.php b/app/Schema/Sqlite.php index b76e902a..175a583c 100644 --- a/app/Schema/Sqlite.php +++ b/app/Schema/Sqlite.php @@ -138,7 +138,6 @@ function version_68($pdo) $result = $rq->fetch(PDO::FETCH_ASSOC); $rq = $pdo->prepare('INSERT INTO settings VALUES (?, ?)'); - $rq->execute(array('calendar_user_subtasks_forecast', isset($result['subtask_forecast']) && $result['subtask_forecast'] == 1 ? 1 : 0)); $rq->execute(array('calendar_user_subtasks_time_tracking', 0)); $rq->execute(array('calendar_user_tasks', 'date_started')); $rq->execute(array('calendar_project_tasks', 'date_started')); diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index 899574e9..ee5ddbe4 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -41,7 +41,6 @@ class ClassProvider implements ServiceProviderInterface 'ProjectPermission', 'Subtask', 'SubtaskExport', - 'SubtaskForecast', 'SubtaskTimeTracking', 'Swimlane', 'Task', @@ -80,6 +79,9 @@ class ClassProvider implements ServiceProviderInterface 'Core\Cache' => array( 'MemoryCache', ), + 'Core\Plugin' => array( + 'Hook', + ), 'Integration' => array( 'BitbucketWebhook', 'GithubWebhook', diff --git a/app/Template/app/sidebar.php b/app/Template/app/sidebar.php index f4a455f8..c1de0dbe 100644 --- a/app/Template/app/sidebar.php +++ b/app/Template/app/sidebar.php @@ -19,7 +19,7 @@
  • app->getRouterAction() === 'activity' ? 'class="active"' : '' ?>> url->link(t('My activity stream'), 'app', 'activity', array('user_id' => $user['id'])) ?>
  • - hook->render('dashboard:sidebar') ?> + hook->render('template:dashboard:sidebar') ?> diff --git a/app/Template/config/calendar.php b/app/Template/config/calendar.php index 1cc985c8..25f4b43d 100644 --- a/app/Template/config/calendar.php +++ b/app/Template/config/calendar.php @@ -23,7 +23,6 @@

    form->checkbox('calendar_user_subtasks_time_tracking', t('Show subtasks based on the time tracking'), 1, $values['calendar_user_subtasks_time_tracking'] == 1) ?> - form->checkbox('calendar_user_subtasks_forecast', t('Show subtask estimates (forecast of future work)'), 1, $values['calendar_user_subtasks_forecast'] == 1) ?>
    diff --git a/app/Template/config/sidebar.php b/app/Template/config/sidebar.php index 083da283..ed4f01e7 100644 --- a/app/Template/config/sidebar.php +++ b/app/Template/config/sidebar.php @@ -34,7 +34,7 @@
  • url->link(t('Documentation'), 'doc', 'show') ?>
  • - hook->render('config:sidebar') ?> + hook->render('template:config:sidebar') ?> diff --git a/app/Template/export/sidebar.php b/app/Template/export/sidebar.php index 7e39a5af..44448520 100644 --- a/app/Template/export/sidebar.php +++ b/app/Template/export/sidebar.php @@ -13,7 +13,7 @@
  • app->getRouterAction() === 'summary' ? 'class="active"' : '' ?>> url->link(t('Daily project summary'), 'export', 'summary', array('project_id' => $project['id'])) ?>
  • - hook->render('export:sidebar') ?> + hook->render('template:export:sidebar') ?> diff --git a/app/Template/layout.php b/app/Template/layout.php index 934fb62c..49ac2a08 100644 --- a/app/Template/layout.php +++ b/app/Template/layout.php @@ -29,7 +29,7 @@ <?= isset($title) ? $this->e($title) : 'Kanboard' ?> - hook->render('layout:head') ?> + hook->render('template:layout:head') ?> - hook->render('layout:top') ?> + hook->render('template:layout:top') ?> render('header', array( 'title' => $title, 'description' => isset($description) ? $description : '', @@ -50,7 +50,7 @@ app->flashMessage() ?> - hook->render('layout:bottom') ?> + hook->render('template:layout:bottom') ?> diff --git a/app/Template/project/dropdown.php b/app/Template/project/dropdown.php index ee1ca3f7..96b6a43a 100644 --- a/app/Template/project/dropdown.php +++ b/app/Template/project/dropdown.php @@ -9,7 +9,7 @@ -hook->render('project:dropdown', array('project' => $project)) ?> +hook->render('template:project:dropdown', array('project' => $project)) ?> user->isProjectManagementAllowed($project['id'])): ?>
  • diff --git a/app/Template/project/sidebar.php b/app/Template/project/sidebar.php index 84bbb6b1..482a95d2 100644 --- a/app/Template/project/sidebar.php +++ b/app/Template/project/sidebar.php @@ -49,7 +49,7 @@ - hook->render('project:sidebar') ?> + hook->render('template:project:sidebar') ?> diff --git a/app/Template/project_user/sidebar.php b/app/Template/project_user/sidebar.php index 98219a87..b81ba14a 100644 --- a/app/Template/project_user/sidebar.php +++ b/app/Template/project_user/sidebar.php @@ -25,6 +25,6 @@ url->link(t('Closed tasks'), 'projectuser', 'closed', $filter) ?>
  • - hook->render('project-user:sidebar') ?> + hook->render('template:project-user:sidebar') ?>
    \ No newline at end of file diff --git a/app/Template/task/sidebar.php b/app/Template/task/sidebar.php index cf0e9f76..9ee1e7df 100644 --- a/app/Template/task/sidebar.php +++ b/app/Template/task/sidebar.php @@ -19,7 +19,7 @@ - hook->render('task:sidebar:information') ?> + hook->render('template:task:sidebar:information') ?>

    diff --git a/app/Template/user/sidebar.php b/app/Template/user/sidebar.php index 80fe8684..6a4e9c47 100644 --- a/app/Template/user/sidebar.php +++ b/app/Template/user/sidebar.php @@ -21,7 +21,7 @@ - hook->render('user:sidebar:information') ?> + hook->render('template:user:sidebar:information') ?>

    @@ -67,7 +67,7 @@ - hook->render('user:sidebar:actions', array('user' => $user)) ?> + hook->render('template:user:sidebar:actions', array('user' => $user)) ?> user->isAdmin() && ! $this->user->isCurrentUser($user['id'])): ?>
  • app->getRouterController() === 'user' && $this->app->getRouterAction() === 'remove' ? 'class="active"' : '' ?>> -- cgit v1.2.3