From e8fa25f9ca246fea085da46ea705a74b6de30d10 Mon Sep 17 00:00:00 2001
From: Frédéric Guillot <fred@kanboard.net>
Date: Sat, 29 Nov 2014 12:28:35 -0500
Subject: Replace Core\Cli by Symfony\Console

---
 app/Console/Base.php                | 53 ++++++++++++++++++++++++++++
 app/Console/OverdueNotification.php | 69 +++++++++++++++++++++++++++++++++++++
 app/Console/TaskExport.php          | 35 +++++++++++++++++++
 3 files changed, 157 insertions(+)
 create mode 100644 app/Console/Base.php
 create mode 100644 app/Console/OverdueNotification.php
 create mode 100644 app/Console/TaskExport.php

(limited to 'app/Console')

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);
+        }
+    }
+}
-- 
cgit v1.2.3