View source
<?php
namespace Drupal\checklistapi\Commands;
use Consolidation\OutputFormatters\FormatterManager;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Render\Element;
use Drupal\user\Entity\User;
use Drush\Commands\DrushCommands;
use Drush\Commands\help\ListCommands;
use Psr\Log\LoggerAwareInterface;
class ChecklistapiCommands extends DrushCommands implements LoggerAwareInterface {
protected $logger;
public function __construct(LoggerChannelFactoryInterface $logger_channel) {
$this->logger = $logger_channel
->get('drush');
}
public function listCommand() {
$definitions = checklistapi_get_checklist_info();
if (empty($definitions)) {
return $this->logger
->alert(dt('No checklists available.'));
}
$rows = [];
$rows[] = [
dt('Checklist'),
dt('Progress'),
dt('Last updated'),
dt('Last updated by'),
];
foreach ($definitions as $id => $definition) {
$checklist = checklistapi_checklist_load($id);
$row = [];
$row[] = dt('!title (@id)', [
'!title' => strip_tags($checklist->title),
'@id' => $id,
]);
$row[] = dt('@completed of @total (@percent%)', [
'@completed' => $checklist
->getNumberCompleted(),
'@total' => $checklist
->getNumberOfItems(),
'@percent' => round($checklist
->getPercentComplete()),
]);
$row[] = $checklist
->getLastUpdatedDate();
$row[] = $checklist
->getLastUpdatedUser();
$rows[] = $row;
}
$formatter_manager = new FormatterManager();
$opts = [
FormatterOptions::INCLUDE_FIELD_LABELS => FALSE,
FormatterOptions::TABLE_STYLE => 'compact',
FormatterOptions::TERMINAL_WIDTH => ListCommands::getTerminalWidth(),
];
$formatter_options = new FormatterOptions([], $opts);
$formatter_manager
->write($this
->output(), 'table', new RowsOfFields($rows), $formatter_options);
}
public function infoCommand($checklist_id) {
$checklist = checklistapi_checklist_load($checklist_id);
if (!$checklist) {
return $this->logger
->error(dt('No such checklist "@id".', [
'@id' => $checklist_id,
]));
}
$output = [];
if (!empty($checklist->help)) {
$output[] = strip_tags($checklist->help);
}
if ($checklist
->hasSavedProgress()) {
$output[] = '';
$output[] = dt('Last updated @date by @user', [
'@date' => $checklist
->getLastUpdatedDate(),
'@user' => $checklist
->getLastUpdatedUser(),
]);
$output[] = dt('@completed of @total (@percent%) complete', [
'@completed' => $checklist
->getNumberCompleted(),
'@total' => $checklist
->getNumberOfItems(),
'@percent' => round($checklist
->getPercentComplete()),
]);
}
$groups = $checklist->items;
foreach (Element::children($groups) as $group_key) {
$group =& $groups[$group_key];
$output[] = '';
$output[] = strip_tags($group['#title']) . ':';
foreach (Element::children($group) as $item_key) {
$item =& $group[$item_key];
$saved_item = !empty($checklist->savedProgress['#items'][$item_key]) ? $checklist->savedProgress['#items'][$item_key] : 0;
$title = strip_tags($item['#title']);
if ($saved_item) {
$user = User::load($saved_item['#uid']);
$title .= ' - ' . dt('Completed @time by @user', [
'@time' => \Drupal::service('date.formatter')
->format($saved_item['#completed'], 'short'),
'@user' => $user
->getAccountName(),
]);
}
$output[] = dt(' [@x] !title', [
'@x' => $saved_item ? 'x' : ' ',
'!title' => $title,
]);
}
}
$output[] = '';
return implode(PHP_EOL, $output);
}
}