Alfred/bot/Commands/TasksList.php

56 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2020-03-26 00:11:37 +01:00
<?php
namespace Shikiryu\Bot\Commands;
use JsonRPC\Client;
use Shikiryu\Bot\Bot;
class TasksList implements Icommands
{
public static function getMessage(Bot $bot, array $data)
{
$config = $bot->getConfig();
if (!array_key_exists('kanboard', $config)) {
$bot->replyPolitely('Je n\'ai pas la configuration nécessaire');
}
$config = $config['kanboard'];
$client = new Client($config['url']);
$client->authentication('jsonrpc', $config['token']);
$project = $data[1];
if (!is_numeric($project)) {
$asked_project = strtolower($project);
$projects = $client->getAllProjects();
foreach ($projects as $project) {
if (strtolower($project['name']) === $asked_project) {
$project = (int) $project['id'];
break;
}
}
}
$task_list = $client->getAllTasks([
'project_id' => $project,
]);
if ($task_list === false) {
$bot->replyPolitely('Je n\'ai pas réussi à récupérer la liste');
} else {
if (empty($task_list)) {
$bot->replyPolitely('Il n\'y a pas de tâches dans ce projet');
}
$task_list = array_map(function($task) { return sprintf('%u: %s', $task['id'], $task['title']); }, $task_list);
$bot->replyPolitely(sprintf('Voici la liste des tâches du projet %u : %s', $project, implode('\n', $task_list)));
}
}
/**
* @return string
*/
public static function getDescription()
{
return 'Liste les tâches d\'un projet Kanboard';
}
}