View source
<?php
namespace Drupal\maestro_taskconsole\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\maestro\Engine\MaestroEngine;
use Drupal\maestro\Utility\TaskHandler;
use Drupal\maestro\Utility\MaestroStatus;
use Drupal\Core\Url;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\Serialization\Json;
use Drupal\maestro\Controller\MaestroOrchestrator;
use Drupal\views\Views;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\CssCommand;
class MaestroTaskConsoleController extends ControllerBase {
public function getTasks($highlightQueueID = 0) {
global $base_url;
$config = \Drupal::config('maestro.settings');
if ($config
->get('maestro_orchestrator_task_console')) {
$orchestrator = new MaestroOrchestrator();
$orchestrator
->orchestrate($config
->get('maestro_orchestrator_token'));
}
$engine = new MaestroEngine();
$build = [];
$build['task_console_table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Task'),
$this
->t('Flow'),
$this
->t('Assigned'),
$this
->t('Actions'),
$this
->t('Details'),
],
'#empty' => t('You have no tasks.'),
'#attributes' => [
'class' => [
'taskconsole-tasks',
],
],
];
$queueIDs = MaestroEngine::getAssignedTaskQueueIds(\Drupal::currentUser()
->id());
foreach ($queueIDs as $queueID) {
$highlight = '';
$url_from_route = FALSE;
if ($highlightQueueID == $queueID) {
$highlight = 'maestro-highlight-task';
}
\Drupal::entityTypeManager()
->getStorage('maestro_queue')
->resetCache([
$queueID,
]);
$queueRecord = \Drupal::entityTypeManager()
->getStorage('maestro_queue')
->load($queueID);
$processID = MaestroEngine::getProcessIdFromQueueId($queueID);
$processRecord = MaestroEngine::getProcessEntryById($processID);
$build['task_console_table'][$queueID]['#attributes'] = [
'class' => $highlight,
];
$build['task_console_table'][$queueID]['task'] = [
'#plain_text' => $queueRecord->task_label
->getString(),
];
$build['task_console_table'][$queueID]['flow'] = [
'#plain_text' => $processRecord->process_name
->getString(),
];
$build['task_console_table'][$queueID]['assigned'] = [
'#plain_text' => \Drupal::service('date.formatter')
->format($queueRecord->created
->getString(), 'custom', 'Y-m-d H:i:s'),
];
$templateMachineName = $engine
->getTemplateIdFromProcessId($queueRecord->process_id
->getString());
$taskTemplate = $engine
->getTemplateTaskByID($templateMachineName, $queueRecord->task_id
->getString());
$template = MaestroEngine::getTemplate($templateMachineName);
$link = 'Execute';
$use_modal = FALSE;
$query_options = [
'queueid' => $queueID,
];
if (array_key_exists('data', $taskTemplate) && array_key_exists('modal', $taskTemplate['data']) && $taskTemplate['data']['modal'] == 'modal') {
$use_modal = TRUE;
}
$handler = $queueRecord->handler
->getString();
if ($handler && !empty($handler) && $queueRecord->is_interactive
->getString() == '1') {
$handler = str_replace($base_url, '', $handler);
$handler_type = TaskHandler::getType($handler);
$handler_url_parts = UrlHelper::parse($handler);
$query_options += $handler_url_parts['query'];
\Drupal::moduleHandler()
->invokeAll('maestro_task_console_interactive_link_alter', [
&$link,
$taskTemplate,
$queueRecord,
$templateMachineName,
]);
}
elseif ($queueRecord->is_interactive
->getString() == '1' && empty($handler)) {
$handler_type = 'function';
}
else {
continue;
}
$links = [];
switch ($handler_type) {
case 'external':
$build['task_console_table'][$queueID]['execute']['maestro_link'] = [
'#type' => 'link',
'#title' => $this
->t($link),
'#url' => Url::fromUri($handler, [
'query' => $query_options,
]),
];
break;
case 'internal':
$build['task_console_table'][$queueID]['execute'] = [
'data' => [
'#type' => 'operations',
'#links' => [
'maestro_link' => [
'title' => $this
->t($link),
'url' => Url::fromUserInput($handler, [
'query' => $query_options,
]),
],
],
],
];
\Drupal::moduleHandler()
->invokeAll('maestro_task_console_interactive_link_alter', [
&$link,
$taskTemplate,
$queueRecord,
$templateMachineName,
]);
break;
case 'function':
\Drupal::moduleHandler()
->invokeAll('maestro_task_console_interactive_link_alter', [
&$link,
$taskTemplate,
$queueRecord,
$templateMachineName,
]);
if ($use_modal) {
$query_options += [
'modal' => 'modal',
];
$links[$link] = [
'title' => $this
->t($link),
'url' => Url::fromRoute('maestro.execute', $query_options),
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
}
else {
$query_options += [
'modal' => 'notmodal',
];
$links[$link] = [
'title' => $this
->t($link),
'url' => Url::fromRoute('maestro.execute', $query_options),
];
}
$build['task_console_table'][$queueID]['execute'] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
];
break;
default:
$build['task_console_table'][$queueID]['execute'] = [
'#plain_text' => $this
->t('Invalid Link'),
];
}
\Drupal::moduleHandler()
->invokeAll('maestro_task_console_alter_execution_link', [
&$build['task_console_table'][$queueID]['execute'],
$taskTemplate,
$queueRecord,
$templateMachineName,
]);
$build['task_console_table'][$queueID]['expand'] = [
'#wrapper_attributes' => [
'class' => [
'maestro-expand-wrapper',
],
],
'#plain_text' => '',
];
$var_workflow_stage_count = intval(MaestroEngine::getProcessVariable('workflow_timeline_stage_count', $processID));
if (isset($template->show_details) && $template->show_details || isset($template->default_workflow_timeline_stage_count) && intval($template->default_workflow_timeline_stage_count) > 0 && $var_workflow_stage_count > 0) {
$build['task_console_table'][$queueID]['expand'] = [
'#wrapper_attributes' => [
'class' => [
'maestro-expand-wrapper',
'maestro-status-toggle-' . $queueID,
],
],
'#attributes' => [
'class' => [
'maestro-timeline-status',
'maestro-status-toggle',
],
'title' => $this
->t('Open Details'),
],
'#type' => 'link',
'#id' => 'maestro-id-ajax-' . $queueID,
'#url' => Url::fromRoute('maestro_taskconsole.status_ajax_open', [
'processID' => $processID,
'queueID' => $queueID,
]),
'#title' => $this
->t('Open Details'),
'#ajax' => [
'progress' => [
'type' => 'throbber',
'message' => NULL,
],
],
];
$build['task_console_table'][$queueID . '_ajax']['#attributes']['id'] = $queueID . '_ajax';
$build['task_console_table'][$queueID . '_ajax']['#attributes']['class'] = [
'maestro-ajax-row',
];
$build['task_console_table'][$queueID . '_ajax']['task'] = [
'#wrapper_attributes' => [
'colspan' => count($build['task_console_table'][$queueID]),
],
'#prefix' => '<div id="maestro-ajax-' . $queueID . '">',
'#suffix' => '</div>',
];
}
}
$build['#attached']['library'][] = 'maestro_taskconsole/maestro_taskconsole_css';
$build['#attached']['library'][] = 'maestro/maestro-engine-css';
$build['#attached']['drupalSettings'] = [
'baseURL' => base_path(),
];
return $build;
}
public function getStatus($processID, $queueID) {
$build = [];
$replace = [];
$status_bar = '';
$canExecute = MaestroEngine::canUserExecuteTask($queueID, \Drupal::currentUser()
->id());
if ($canExecute) {
$queueRecord = \Drupal::entityTypeManager()
->getStorage('maestro_queue')
->load($queueID);
$templateMachineName = MaestroEngine::getTemplateIdFromProcessId($processID);
$taskTemplate = MaestroEngine::getTemplateTaskByID($templateMachineName, $queueRecord->task_id
->getString());
$template = MaestroEngine::getTemplate($templateMachineName);
$var_workflow_stage_count = intval(MaestroEngine::getProcessVariable('workflow_timeline_stage_count', $processID));
$build = MaestroStatus::getMaestroStatusBar($processID, $queueID, FALSE);
$taskDetails = '';
$customInformation = '';
if (isset($template->show_details) && $template->show_details) {
\Drupal::moduleHandler()
->invokeAll('maestro_task_console_custominformation_alter', [
&$customInformation,
$taskTemplate,
$queueRecord,
$templateMachineName,
]);
if (isset($template->views_attached)) {
foreach ($template->views_attached as $machine_name => $arr) {
$view = Views::getView($machine_name);
if ($view) {
$display = explode(';', $arr['view_display']);
$display_to_use = isset($display[0]) ? $display[0] : 'default';
$render_build = $view
->buildRenderable($display_to_use, [
$processID,
$queueID,
], FALSE);
if ($render_build) {
$thisViewOutput = \Drupal::service('renderer')
->render($render_build);
if ($thisViewOutput) {
$task_information_render_array = [
'#theme' => 'taskconsole_views',
'#task_information' => $thisViewOutput,
'#title' => $view->storage
->label(),
];
$taskDetails .= \Drupal::service('renderer')
->render($task_information_render_array);
}
}
}
}
}
\Drupal::moduleHandler()
->invokeAll('maestro_task_console_taskdetails_alter', [
&$taskDetails,
$taskTemplate,
$queueRecord,
$templateMachineName,
]);
$build['custom_information_bar'] = [
'#children' => '<div class="custom-information">' . $customInformation . '</div>',
];
$build['views_bar'] = [
'#children' => '<div class="maestro-task-details">' . $taskDetails . '</div>',
];
$replace['expand'] = [
'#attributes' => [
'class' => [
'maestro-timeline-status',
'maestro-status-toggle-up',
],
'title' => $this
->t('Close Details'),
],
'#type' => 'link',
'#id' => 'maestro-id-ajax-' . $queueID,
'#url' => Url::fromRoute('maestro_taskconsole.status_ajax_close', [
'processID' => $processID,
'queueID' => $queueID,
]),
'#title' => $this
->t('Close Details'),
'#ajax' => [
'progress' => [
'type' => 'throbber',
'message' => NULL,
],
],
];
}
}
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand('#maestro-ajax-' . $queueID, $build));
$response
->addCommand(new HtmlCommand('.maestro-status-toggle-' . $queueID . '', $replace['expand']));
$response
->addCommand(new CssCommand('#' . $queueID . '_ajax', [
'display' => 'table-row',
]));
return $response;
}
public function closeStatus($processID, $queueID) {
$build = [];
$build['expand'] = [
'#attributes' => [
'class' => [
'maestro-timeline-status',
'maestro-status-toggle',
],
'title' => $this
->t('Open Details'),
],
'#type' => 'link',
'#id' => 'maestro-id-ajax-' . $queueID,
'#url' => Url::fromRoute('maestro_taskconsole.status_ajax_open', [
'processID' => $processID,
'queueID' => $queueID,
]),
'#title' => $this
->t('Open Details'),
'#ajax' => [
'progress' => [
'type' => 'throbber',
'message' => NULL,
],
],
];
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand('#maestro-ajax-' . $queueID, ''));
$response
->addCommand(new HtmlCommand('.maestro-status-toggle-' . $queueID, $build['expand']));
$response
->addCommand(new CssCommand('#' . $queueID . '_ajax', [
'display' => 'none',
]));
return $response;
}
}