diff options
Diffstat (limited to 'app/Console')
-rw-r--r-- | app/Console/Base.php | 25 | ||||
-rw-r--r-- | app/Console/Cronjob.php | 32 | ||||
-rw-r--r-- | app/Console/TaskOverdueNotification.php | 68 | ||||
-rw-r--r-- | app/Console/TaskTrigger.php | 51 | ||||
-rw-r--r-- | app/Console/TransitionExport.php | 2 |
5 files changed, 164 insertions, 14 deletions
diff --git a/app/Console/Base.php b/app/Console/Base.php index 4c5caf73..25d48e44 100644 --- a/app/Console/Base.php +++ b/app/Console/Base.php @@ -11,18 +11,19 @@ use Symfony\Component\Console\Command\Command; * @package console * @author Frederic Guillot * - * @property \Kanboard\Model\Notification $notification - * @property \Kanboard\Model\Project $project - * @property \Kanboard\Model\ProjectPermission $projectPermission - * @property \Kanboard\Model\ProjectAnalytic $projectAnalytic - * @property \Kanboard\Model\ProjectDailyColumnStats $projectDailyColumnStats - * @property \Kanboard\Model\ProjectDailyStats $projectDailyStats - * @property \Kanboard\Model\SubtaskExport $subtaskExport - * @property \Kanboard\Model\OverdueNotification $overdueNotification - * @property \Kanboard\Model\Task $task - * @property \Kanboard\Model\TaskExport $taskExport - * @property \Kanboard\Model\TaskFinder $taskFinder - * @property \Kanboard\Model\Transition $transition + * @property \Kanboard\Export\SubtaskExport $subtaskExport + * @property \Kanboard\Export\TaskExport $taskExport + * @property \Kanboard\Export\TransitionExport $transitionExport + * @property \Kanboard\Model\Notification $notification + * @property \Kanboard\Model\Project $project + * @property \Kanboard\Model\ProjectPermission $projectPermission + * @property \Kanboard\Model\ProjectDailyColumnStats $projectDailyColumnStats + * @property \Kanboard\Model\ProjectDailyStats $projectDailyStats + * @property \Kanboard\Model\Task $task + * @property \Kanboard\Model\TaskFinder $taskFinder + * @property \Kanboard\Model\UserNotification $userNotification + * @property \Kanboard\Model\UserNotificationFilter $userNotificationFilter + * @property \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher */ abstract class Base extends Command { diff --git a/app/Console/Cronjob.php b/app/Console/Cronjob.php new file mode 100644 index 00000000..3a5c5596 --- /dev/null +++ b/app/Console/Cronjob.php @@ -0,0 +1,32 @@ +<?php + +namespace Kanboard\Console; + +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Output\NullOutput; + +class Cronjob extends Base +{ + private $commands = array( + 'projects:daily-stats', + 'notification:overdue-tasks', + 'trigger:tasks', + ); + + protected function configure() + { + $this + ->setName('cronjob') + ->setDescription('Execute daily cronjob'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + foreach ($this->commands as $command) { + $job = $this->getApplication()->find($command); + $job->run(new ArrayInput(array('command' => $command)), new NullOutput()); + } + } +} diff --git a/app/Console/TaskOverdueNotification.php b/app/Console/TaskOverdueNotification.php index ffb9fab5..43be4df8 100644 --- a/app/Console/TaskOverdueNotification.php +++ b/app/Console/TaskOverdueNotification.php @@ -2,6 +2,7 @@ namespace Kanboard\Console; +use Kanboard\Model\Task; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -19,7 +20,7 @@ class TaskOverdueNotification extends Base protected function execute(InputInterface $input, OutputInterface $output) { - $tasks = $this->overdueNotification->sendOverdueTaskNotifications(); + $tasks = $this->sendOverdueTaskNotifications(); if ($input->getOption('show')) { $this->showTable($output, $tasks); @@ -47,4 +48,69 @@ class TaskOverdueNotification extends Base ->setRows($rows) ->render(); } + + /** + * Send overdue tasks + * + * @access public + */ + public function sendOverdueTaskNotifications() + { + $tasks = $this->taskFinder->getOverdueTasks(); + + foreach ($this->groupByColumn($tasks, 'project_id') as $project_id => $project_tasks) { + $users = $this->userNotification->getUsersWithNotificationEnabled($project_id); + + foreach ($users as $user) { + $this->sendUserOverdueTaskNotifications($user, $project_tasks); + } + } + + return $tasks; + } + + /** + * Send overdue tasks for a given user + * + * @access public + * @param array $user + * @param array $tasks + */ + public function sendUserOverdueTaskNotifications(array $user, array $tasks) + { + $user_tasks = array(); + + foreach ($tasks as $task) { + if ($this->userNotificationFilter->shouldReceiveNotification($user, array('task' => $task))) { + $user_tasks[] = $task; + } + } + + if (! empty($user_tasks)) { + $this->userNotification->sendUserNotification( + $user, + Task::EVENT_OVERDUE, + array('tasks' => $user_tasks, 'project_name' => $tasks[0]['project_name']) + ); + } + } + + /** + * Group a collection of records by a column + * + * @access public + * @param array $collection + * @param string $column + * @return array + */ + public function groupByColumn(array $collection, $column) + { + $result = array(); + + foreach ($collection as $item) { + $result[$item[$column]][] = $item; + } + + return $result; + } } diff --git a/app/Console/TaskTrigger.php b/app/Console/TaskTrigger.php new file mode 100644 index 00000000..8d707211 --- /dev/null +++ b/app/Console/TaskTrigger.php @@ -0,0 +1,51 @@ +<?php + +namespace Kanboard\Console; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Kanboard\Model\Task; +use Kanboard\Event\TaskListEvent; + +class TaskTrigger extends Base +{ + protected function configure() + { + $this + ->setName('trigger:tasks') + ->setDescription('Trigger scheduler event for all tasks'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + foreach ($this->getProjectIds() as $project_id) { + $tasks = $this->taskFinder->getAll($project_id); + $nb_tasks = count($tasks); + + if ($nb_tasks > 0) { + $output->writeln('Trigger task event: project_id='.$project_id.', nb_tasks='.$nb_tasks); + $this->sendEvent($tasks, $project_id); + } + } + } + + private function getProjectIds() + { + $listeners = $this->dispatcher->getListeners(Task::EVENT_DAILY_CRONJOB); + $project_ids = array(); + + foreach ($listeners as $listener) { + $project_ids[] = $listener[0]->getProjectId(); + } + + return array_unique($project_ids); + } + + private function sendEvent(array &$tasks, $project_id) + { + $event = new TaskListEvent(array('project_id' => $project_id)); + $event->setTasks($tasks); + + $this->dispatcher->dispatch(Task::EVENT_DAILY_CRONJOB, $event); + } +} diff --git a/app/Console/TransitionExport.php b/app/Console/TransitionExport.php index 9ae41417..d9f805a4 100644 --- a/app/Console/TransitionExport.php +++ b/app/Console/TransitionExport.php @@ -21,7 +21,7 @@ class TransitionExport extends Base protected function execute(InputInterface $input, OutputInterface $output) { - $data = $this->transition->export( + $data = $this->transitionExport->export( $input->getArgument('project_id'), $input->getArgument('start_date'), $input->getArgument('end_date') |