|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_forloeb\Controller; |
| 4 | + |
| 5 | +use Drupal\Component\Utility\UrlHelper; |
| 6 | +use Drupal\Core\Controller\ControllerBase; |
| 7 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 8 | +use Drupal\Core\Url; |
| 9 | +use Drupal\maestro\Engine\MaestroEngine; |
| 10 | +use Drupal\maestro\Utility\TaskHandler; |
| 11 | +use Drupal\os2forms_forloeb\ForloebTaskConsole; |
| 12 | +use Drupal\Core\StringTranslation\PluralTranslatableMarkup; |
| 13 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 14 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class ForloebTaskConsoleController. |
| 18 | + */ |
| 19 | +class ForloebTaskConsoleController extends ControllerBase { |
| 20 | + |
| 21 | + /** |
| 22 | + * Update manager service. |
| 23 | + * |
| 24 | + * @var \Drupal\os2forms_forloeb\ForloebTaskConsole |
| 25 | + */ |
| 26 | + protected $forloebTaskConsole; |
| 27 | + |
| 28 | + /** |
| 29 | + * Drupal\Core\Entity\EntityTypeManagerInterface definition. |
| 30 | + * |
| 31 | + * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
| 32 | + */ |
| 33 | + protected $entityTypeManager; |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructs update status data. |
| 37 | + * |
| 38 | + * @param \Drupal\os2forms_forloeb\ForloebTaskConsole $forloebTaskConsole |
| 39 | + * Forloeb task console Service. |
| 40 | + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
| 41 | + * The Entity type manager. |
| 42 | + */ |
| 43 | + public function __construct(ForloebTaskConsole $forloeb_task_console, EntityTypeManagerInterface $entity_type_manager) { |
| 44 | + $this->forloebTaskConsole = $forloeb_task_console; |
| 45 | + $this->entityTypeManager = $entity_type_manager; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritdoc} |
| 50 | + */ |
| 51 | + public static function create(ContainerInterface $container) { |
| 52 | + return new static( |
| 53 | + $container->get('os2forms_forloeb.task_console'), |
| 54 | + $container->get('entity_type.manager') |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Redirects to the task execution URL. |
| 60 | + * |
| 61 | + * In case it's not possible to define task, redirects to task console. |
| 62 | + * |
| 63 | + * @return RedirectResponse |
| 64 | + * Redirect object. |
| 65 | + */ |
| 66 | + public function execute() { |
| 67 | + $redirect_to = Url::fromRoute('maestro_taskconsole.taskconsole'); |
| 68 | + |
| 69 | + // Check webform submission token. |
| 70 | + $token = \Drupal::request()->query->get('token', ''); |
| 71 | + if ($token) { |
| 72 | + $queueRecord = $this->forloebTaskConsole->getQueueIdByWebformSubmissionToken($token); |
| 73 | + } |
| 74 | + else { |
| 75 | + // For empty token there is user last task from taskconsole queue. |
| 76 | + $queueIDs = MaestroEngine::getAssignedTaskQueueIds(\Drupal::currentUser()->id()); |
| 77 | + $queueRecord = count($queueIDs) ? $this->entityTypeManager->getStorage('maestro_queue')->load(end($queueIDs)) : NULL; |
| 78 | + |
| 79 | + // In case there are more than 1 task warning message will be shown. |
| 80 | + if (count($queueIDs) > 1) { |
| 81 | + $this->messenger()->addWarning($this->t('You have @amount @tasks available for you. See list of the all tasks on <a href=":tasksonsole">taskconsole</a>', [ |
| 82 | + ':tasksonsole' => Url::fromRoute('maestro_taskconsole.taskconsole')->toString(), |
| 83 | + '@amount' => count($queueIDs), |
| 84 | + '@tasks' => new PluralTranslatableMarkup(count($queueIDs), 'task','tasks'), |
| 85 | + ])); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (empty($queueRecord)) { |
| 90 | + $this->messenger()->addWarning($this->t('No tasks found to execute.')); |
| 91 | + return new RedirectResponse($redirect_to->toString()); |
| 92 | + } |
| 93 | + |
| 94 | + // Processing QueueRecord to get execution URL to redirect to. |
| 95 | + $handler = $queueRecord->handler->getString(); |
| 96 | + $query_options = [ |
| 97 | + 'queueid' => $queueRecord->id(), |
| 98 | + 'modal' => 'notmodal' |
| 99 | + ]; |
| 100 | + |
| 101 | + // As inspiration MaestroTaskConsoleController::getTasks() method was used. |
| 102 | + if ($handler && !empty($handler) && $queueRecord->is_interactive->getString() == '1') { |
| 103 | + global $base_url; |
| 104 | + $handler = str_replace($base_url, '', $handler); |
| 105 | + $handler_type = TaskHandler::getType($handler); |
| 106 | + |
| 107 | + $handler_url_parts = UrlHelper::parse($handler); |
| 108 | + $query_options += $handler_url_parts['query']; |
| 109 | + |
| 110 | + } |
| 111 | + elseif ($queueRecord->is_interactive->getString() == '1' && empty($handler)) { |
| 112 | + // Handler is empty. If this is an interactive task and has no handler, we're still OK. This is an interactive function that uses a default handler then. |
| 113 | + $handler_type = 'function'; |
| 114 | + } |
| 115 | + else { |
| 116 | + $this->messenger()->addWarning($this->t('Undefined handler')); |
| 117 | + } |
| 118 | + |
| 119 | + switch ($handler_type) { |
| 120 | + case 'external': |
| 121 | + $redirect_to = Url::fromUri($handler, ['query' => $query_options]); |
| 122 | + break; |
| 123 | + |
| 124 | + case 'internal': |
| 125 | + $redirect_to = Url::fromUserInput($handler, ['query' => $query_options]); |
| 126 | + break; |
| 127 | + |
| 128 | + case 'function': |
| 129 | + $redirect_to = Url::fromRoute('maestro.execute', $query_options); |
| 130 | + break; |
| 131 | + } |
| 132 | + |
| 133 | + return new RedirectResponse($redirect_to->toString()); |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments