diff options
author | Frédéric Guillot <fred@kanboard.net> | 2014-11-29 12:28:35 -0500 |
---|---|---|
committer | Frédéric Guillot <fred@kanboard.net> | 2014-11-29 12:28:35 -0500 |
commit | e8fa25f9ca246fea085da46ea705a74b6de30d10 (patch) | |
tree | abf0fd767540e89ba5cfbbb7b6cabb64c4fcbb4e /app | |
parent | d9879161281785c8e11fd074cec98f09df94dd46 (diff) |
Replace Core\Cli by Symfony\Console
Diffstat (limited to 'app')
-rw-r--r-- | app/Console/Base.php | 53 | ||||
-rw-r--r-- | app/Console/OverdueNotification.php | 69 | ||||
-rw-r--r-- | app/Console/TaskExport.php | 35 | ||||
-rw-r--r-- | app/Core/Cli.php | 75 |
4 files changed, 157 insertions, 75 deletions
diff --git a/app/Console/Base.php b/app/Console/Base.php new file mode 100644 index 00000000..bf93aec4 --- /dev/null +++ b/app/Console/Base.php @@ -0,0 +1,53 @@ +<?php + +namespace Console; + +use Core\Tool; +use Pimple\Container; +use Symfony\Component\Console\Command\Command; + +/** + * Base command class + * + * @package console + * @author Frederic Guillot + * + * @property \Model\Notification $notification + * @property \Model\Task $task + * @property \Model\TaskExport $taskExport + * @property \Model\TaskFinder $taskFinder + */ +abstract class Base extends Command +{ + /** + * Container instance + * + * @access protected + * @var \Pimple\Container + */ + protected $container; + + /** + * Constructor + * + * @access public + * @param \Pimple\Container $container + */ + public function __construct(Container $container) + { + parent::__construct(); + $this->container = $container; + } + + /** + * Load automatically models + * + * @access public + * @param string $name Model name + * @return mixed + */ + public function __get($name) + { + return Tool::loadModel($this->container, $name); + } +} diff --git a/app/Console/OverdueNotification.php b/app/Console/OverdueNotification.php new file mode 100644 index 00000000..0987bf2a --- /dev/null +++ b/app/Console/OverdueNotification.php @@ -0,0 +1,69 @@ +<?php + +namespace Console; + +use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class OverdueNotification extends Base +{ + protected function configure() + { + $this + ->setName('notification:overdue-tasks') + ->setDescription('Send notifications for overdue tasks') + ->addOption('show', null, InputOption::VALUE_NONE, 'Show sent overdue tasks'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $projects = array(); + $tasks = $this->taskFinder->getOverdueTasks(); + + // Group tasks by project + foreach ($tasks as $task) { + $projects[$task['project_id']][] = $task; + } + + // Send notifications for each project + foreach ($projects as $project_id => $project_tasks) { + + $users = $this->notification->getUsersList($project_id); + + $this->notification->sendEmails( + 'task_due', + $users, + array('tasks' => $project_tasks, 'project' => $project_tasks[0]['project_name']) + ); + } + + if ($input->getOption('show')) { + $this->showTable($output, $tasks); + } + } + + public function showTable(OutputInterface $output, array $tasks) + { + $rows = array(); + + foreach ($tasks as $task) { + $rows[] = array( + $task['id'], + $task['title'], + date('Y-m-d', $task['date_due']), + $task['project_id'], + $task['project_name'], + $task['assignee_name'] ?: $task['assignee_username'], + ); + } + + $table = new Table($output); + $table + ->setHeaders(array('Id', 'Title', 'Due date', 'Project Id', 'Project name', 'Assignee')) + ->setRows($rows) + ->render(); + } +} diff --git a/app/Console/TaskExport.php b/app/Console/TaskExport.php new file mode 100644 index 00000000..b9f151fb --- /dev/null +++ b/app/Console/TaskExport.php @@ -0,0 +1,35 @@ +<?php + +namespace Console; + +use Core\Tool; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class TaskExport extends Base +{ + protected function configure() + { + $this + ->setName('export:tasks') + ->setDescription('Tasks export (CSV)') + ->addArgument('project_id', InputArgument::REQUIRED, 'Project id') + ->addArgument('start_date', InputArgument::REQUIRED, 'Start date (YYYY-MM-DD)') + ->addArgument('end_date', InputArgument::REQUIRED, 'End date (YYYY-MM-DD)'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $data = $this->taskExport->export( + $input->getArgument('project_id'), + $input->getArgument('start_date'), + $input->getArgument('end_date') + ); + + if (is_array($data)) { + Tool::csv($data); + } + } +} diff --git a/app/Core/Cli.php b/app/Core/Cli.php deleted file mode 100644 index 13533b9a..00000000 --- a/app/Core/Cli.php +++ /dev/null @@ -1,75 +0,0 @@ -<?php - -namespace Core; - -use Closure; - -/** - * CLI class - * - * @package core - * @author Frederic Guillot - */ -class Cli -{ - /** - * Default command name - * - * @access public - * @var string - */ - public $default_command = 'help'; - - /** - * List of registered commands - * - * @access private - * @var array - */ - private $commands = array(); - - /** - * - * - * @access public - * @param string $command Command name - * @param Closure $callback Command callback - */ - public function register($command, Closure $callback) - { - $this->commands[$command] = $callback; - } - - /** - * Execute a command - * - * @access public - * @param string $command Command name - */ - public function call($command) - { - if (isset($this->commands[$command])) { - $this->commands[$command](); - exit; - } - } - - /** - * Determine which command to execute - * - * @access public - */ - public function execute() - { - if (php_sapi_name() !== 'cli') { - die('This script work only from the command line.'); - } - - if ($GLOBALS['argc'] === 1) { - $this->call($this->default_command); - } - - $this->call($GLOBALS['argv'][1]); - $this->call($this->default_command); - } -} |