diff --git a/Action/ConvertEmailToComment.php b/Action/ConvertEmailToComment.php index 3bccf21..01db561 100755 --- a/Action/ConvertEmailToComment.php +++ b/Action/ConvertEmailToComment.php @@ -11,6 +11,7 @@ use Kanboard\Model\ProjectUserRoleModel; use Kanboard\Model\TaskModel; use Kanboard\Model\UserModel; +use Kanboard\Plugin\Mailmagik\Helper\MailHelper; use League\HTMLToMarkdown\HtmlConverter; use PhpImap; @@ -41,6 +42,7 @@ public function getCompatibleEvents() { return array( TaskModel::EVENT_DAILY_CRONJOB, + MailHelper::EVENT_FETCHMAIL, ); } /** diff --git a/Action/ConvertEmailToTask.php b/Action/ConvertEmailToTask.php index a47742a..19520c9 100755 --- a/Action/ConvertEmailToTask.php +++ b/Action/ConvertEmailToTask.php @@ -9,6 +9,7 @@ use Kanboard\Model\ProjectModel; use Kanboard\Model\TaskModel; use Kanboard\Model\UserModel; +use Kanboard\Plugin\Mailmagik\Helper\MailHelper; use League\HTMLToMarkdown\HtmlConverter; use PhpImap; @@ -39,6 +40,7 @@ public function getCompatibleEvents() { return array( TaskModel::EVENT_DAILY_CRONJOB, + MailHelper::EVENT_FETCHMAIL, ); } /** diff --git a/Console/Command.php b/Console/Command.php new file mode 100644 index 0000000..11e39d0 --- /dev/null +++ b/Console/Command.php @@ -0,0 +1,28 @@ +setName('mailmagik:fetchmail') // Name for the commandline + ->setDescription('Trigger Mailmagik mail fetching by automatic actions') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $command = 'projects:fetchmail'; + + $job = $this->getApplication()->find($command); + $job->run(new ArrayInput(array('command' => $command)), new NullOutput()); + } +} diff --git a/Console/FetchMail.php b/Console/FetchMail.php new file mode 100644 index 0000000..f761a9e --- /dev/null +++ b/Console/FetchMail.php @@ -0,0 +1,43 @@ +setName('projects:fetchmail') + ->setDescription('Trigger scheduler event for all tasks'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + foreach ($this->getProjectIds() as $project_id) { + $this->dispatcher->dispatch( + self::EVENT, + new GenericEvent(array('project_id' => $project_id)) + ); + } + } + + private function getProjectIds() + { + $listeners = $this->dispatcher->getListeners(self::EVENT); + $project_ids = array(); + + foreach ($listeners as $listener) { + $project_ids[] = $listener[0]->getProjectId(); + } + + return array_unique($project_ids); + } +} diff --git a/Helper/MailHelper.php b/Helper/MailHelper.php index a7c021d..f61f732 100644 --- a/Helper/MailHelper.php +++ b/Helper/MailHelper.php @@ -9,6 +9,8 @@ class MailHelper extends Base { + public const EVENT_FETCHMAIL = 'mailmagik.fetchmail'; + /** * Login to the IMAP server * diff --git a/Plugin.php b/Plugin.php index 644846c..9832c9b 100644 --- a/Plugin.php +++ b/Plugin.php @@ -3,8 +3,11 @@ namespace Kanboard\Plugin\Mailmagik; use Kanboard\Core\Plugin\Base; -use Kanboard\Plugin\Mailmagik\Action\ConvertEmailToTask; use Kanboard\Plugin\Mailmagik\Action\ConvertEmailToComment; +use Kanboard\Plugin\Mailmagik\Action\ConvertEmailToTask; +use Kanboard\Plugin\Mailmagik\Console\Command; +use Kanboard\Plugin\Mailmagik\Console\FetchMail; +use Kanboard\Plugin\Mailmagik\Helper\MailHelper; class Plugin extends Base { @@ -28,16 +31,25 @@ public function initialize() $this->template->hook->attach('template:task:sidebar:information', 'mailmagik:task/emails'); } - //CONFIG HOOK + // Config hook $this->template->hook->attach('template:config:email', 'mailmagik:config/config'); //css $this->hook->on('template:layout:css', array('template' => 'plugins/Mailmagik/Assets/css/mailmagik.css')); $this->hook->on('template:layout:js', array('template' => 'plugins/Mailmagik/Assets/js/mailmagik.js')); - //ACTIONS + // Actions $this->actionManager->register(new ConvertEmailToTask($this->container)); $this->actionManager->register(new ConvertEmailToComment($this->container)); + + // Commandline: ./cli mailmagik:fetchmail + $this->cli->add(new Command($this->container)); + $this->cli->add(new FetchMail($this->container)); + } + + public function onStartup() + { + $this->eventManager->register(MailHelper::EVENT_FETCHMAIL, t('Trigger Mailmagik mail fetching')); } public function getPluginName()