class ScheduleController in Business Rules 2.x
Same name and namespace in other branches
- 8 src/Controller/ScheduleController.php \Drupal\business_rules\Controller\ScheduleController
Class ScheduleController.
Returns responses for Schedule routes.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\business_rules\Controller\ScheduleController implements ContainerInjectionInterface
Expanded class hierarchy of ScheduleController
File
- src/
Controller/ ScheduleController.php, line 26
Namespace
Drupal\business_rules\ControllerView source
class ScheduleController extends ControllerBase implements ContainerInjectionInterface {
/**
* A dateFormatter object.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
private $dateFormatter;
/**
* {@inheritdoc}
*/
public function __construct(DateFormatterInterface $dateFormatter) {
$this->dateFormatter = $dateFormatter;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$dateFormatter = $container
->get('date.formatter');
return new static($dateFormatter);
}
/**
* View schedule canonical.
*
* @return array
* The render array.
*/
public function view($business_rules_schedule) {
$schedule = Schedule::load($business_rules_schedule);
$output['#title'] = $schedule
->label();
$output['name'] = [
'#type' => 'item',
'#title' => $this
->t('Name'),
'#markup' => $schedule
->getName(),
];
$output['status'] = [
'#type' => 'item',
'#title' => $this
->t('Executed'),
'#markup' => $schedule
->isExecuted() ? $this
->t('Yes') : $this
->t('No'),
];
$output['created'] = [
'#type' => 'item',
'#title' => $this
->t('Created'),
'#markup' => $this->dateFormatter
->format($schedule
->getCreatedTime(), 'medium'),
];
$output['changed'] = [
'#type' => 'item',
'#title' => $this
->t('Changed'),
'#markup' => $this->dateFormatter
->format($schedule
->getChangedTime(), 'medium'),
];
$output['scheduled'] = [
'#type' => 'item',
'#title' => $this
->t('Scheduled'),
'#markup' => $this->dateFormatter
->format($schedule
->getScheduled(), 'medium'),
];
$output['triggered_by'] = [
'#type' => 'item',
'#title' => $this
->t('Triggered by'),
'#markup' => $schedule
->getTriggeredBy()
->toLink(NULL, 'edit-form')
->toString(),
];
return $output;
}
/**
* Manual execution of a scheduled item.
*
* @param string $business_rules_schedule
* The business_rules_schedule id.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* The redirect response.
*/
public function execute($business_rules_schedule) {
$task = Schedule::load($business_rules_schedule);
/** @var \Drupal\business_rules\Entity\Action $action */
$action = $task
->getTriggeredBy();
$items = $action
->getSettings('items');
$container = \Drupal::getContainer();
$util = new BusinessRulesUtil($container);
$reacts_on_definition = \Drupal::getContainer()
->get('plugin.manager.business_rules.reacts_on')
->getDefinition('cron_runs');
$task_event = $task
->getEvent();
$loop_control = time();
$dummy = new \stdClass();
$variables = new VariablesSet();
$dummy_event = new BusinessRulesEvent($dummy, [
'entity_type_id' => '',
'bundle' => NULL,
'entity' => NULL,
'entity_unchanged' => NULL,
'variables' => $variables,
'reacts_on' => $reacts_on_definition,
'loop_control' => $loop_control,
]);
$event = $task_event instanceof BusinessRulesEvent ? $task_event : $dummy_event;
/** @var \Drupal\Core\Entity\Entity $entity */
$entity = $task_event ? $task_event
->getSubject() instanceof Entity ? $task_event
->getSubject() : FALSE : FALSE;
if ($entity) {
$entity = \Drupal::entityTypeManager()
->getStorage($entity
->getEntityTypeId())
->load($entity
->id());
$task_event
->setArgument('entity', $entity);
$event = new BusinessRulesEvent($entity, $task_event
->getArguments());
}
try {
foreach ($items as $item) {
$action_item = Action::load($item['id']);
$action_item
->execute($event);
}
$entity = $event
->getSubject() instanceof Entity ? $event
->getSubject() : FALSE;
if ($entity && $task
->getUpdateEntity()) {
$entity_exists = \Drupal::entityTypeManager()
->getStorage($entity
->getEntityTypeId())
->load($entity
->id());
if ($entity_exists instanceof EntityInterface) {
$entity
->save();
}
}
$task
->setExecuted(1);
$task
->save();
$util->logger
->notice(t('Scheduled task id: @id, name: "@name", triggered by: "@by" has been executed at: @time', [
'@id' => $task
->id(),
'@name' => $task
->getName(),
'@by' => $task
->getTriggeredBy()
->id(),
'@time' => $container
->get('date.formatter')
->format(time(), 'medium'),
]));
} catch (\Exception $e) {
$util->logger
->error($e
->getMessage());
}
return new RedirectResponse('/admin/config/workflow/business_rules/schedule/collection');
}
/**
* Displays a Schedule revision.
*
* @param int $schedule_revision
* The Schedule revision ID.
*
* @return array
* An array suitable for drupal_render().
*/
public function revisionShow($schedule_revision) {
$schedule = $this
->entityTypeManager()
->getStorage('business_rules_schedule')
->loadRevision($schedule_revision);
$view_builder = $this
->entityTypeManager()
->getViewBuilder('business_rules_schedule');
// Return $view_builder->view($schedule);
return $this
->view($schedule_revision);
}
/**
* Page title callback for a Schedule revision.
*
* @param int $schedule_revision
* The Schedule revision ID.
*
* @return string
* The page title.
*/
public function revisionPageTitle($schedule_revision) {
$schedule = $this
->entityTypeManager()
->getStorage('schedule')
->loadRevision($schedule_revision);
return $this
->t('Revision of %title from %date', [
'%title' => $schedule
->label(),
'%date' => \Drupal::service('date.formatter')
->format($schedule
->getRevisionCreationTime()),
]);
}
/**
* Generates an overview table of older revisions of a Schedule .
*
* @param string $business_rules_schedule
* A Schedule object id.
*
* @return array
* An array as expected by drupal_render().
*/
public function revisionOverview($business_rules_schedule) {
$business_rules_schedule = Schedule::load($business_rules_schedule);
$account = $this
->currentUser();
$langcode = $business_rules_schedule
->language()
->getId();
$langname = $business_rules_schedule
->language()
->getName();
$languages = $business_rules_schedule
->getTranslationLanguages();
$has_translations = count($languages) > 1;
$schedule_storage = $this
->entityTypeManager()
->getStorage('business_rules_schedule');
$build['#title'] = $has_translations ? $this
->t('@langname revisions for %title', [
'@langname' => $langname,
'%title' => $business_rules_schedule
->label(),
]) : $this
->t('Revisions for %title', [
'%title' => $business_rules_schedule
->label(),
]);
$header = [
$this
->t('Revision'),
$this
->t('Operations'),
];
$revert_permission = $account
->hasPermission("revert all schedule revisions") || $account
->hasPermission('administer schedule entities');
$delete_permission = $account
->hasPermission("delete all schedule revisions") || $account
->hasPermission('administer schedule entities');
$rows = [];
$vids = $schedule_storage
->revisionIds($business_rules_schedule);
$latest_revision = TRUE;
foreach (array_reverse($vids) as $vid) {
/** @var \Drupal\business_rules\ScheduleInterface $revision */
$revision = $schedule_storage
->loadRevision($vid);
// Only show revisions that are affected by the language that is being
// displayed.
if ($revision
->hasTranslation($langcode) && $revision
->getTranslation($langcode)
->isRevisionTranslationAffected()) {
$username = [
'#theme' => 'username',
'#account' => $revision
->getRevisionUser(),
];
// Use revision link to link to revisions that are not active.
$time = $revision
->getRevisionCreationTime();
if (is_numeric($time)) {
$date = \Drupal::service('date.formatter')
->format($revision
->getRevisionCreationTime(), 'short');
if ($vid != $business_rules_schedule
->getRevisionId()) {
$link = Link::fromTextAndUrl($date, Url::fromRoute('entity.business_rules_schedule.revision', [
'schedule' => $business_rules_schedule
->id(),
'schedule_revision' => $vid,
]));
}
else {
$link = $business_rules_schedule
->toLink($date)
->toString();
}
}
else {
$link = '';
}
$row = [];
$column = [
'data' => [
'#type' => 'inline_template',
'#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
'#context' => [
'date' => $link,
'username' => \Drupal::service('renderer')
->renderPlain($username),
'message' => [
'#markup' => $revision
->getRevisionLogMessage(),
'#allowed_tags' => Xss::getHtmlTagList(),
],
],
],
];
$row[] = $column;
if ($latest_revision) {
$row[] = [
'data' => [
'#prefix' => '<em>',
'#markup' => $this
->t('Current revision'),
'#suffix' => '</em>',
],
];
foreach ($row as &$current) {
$current['class'] = [
'revision-current',
];
}
$latest_revision = FALSE;
}
else {
$links = [];
if ($revert_permission) {
$links['revert'] = [
'title' => $this
->t('Revert'),
'url' => Url::fromRoute('entity.business_rules_schedule.revision_revert', [
'schedule' => $business_rules_schedule
->id(),
'schedule_revision' => $vid,
]),
];
}
if ($delete_permission) {
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.business_rules_schedule.revision_delete', [
'schedule' => $business_rules_schedule
->id(),
'schedule_revision' => $vid,
]),
];
}
$row[] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
];
}
$rows[] = $row;
}
}
$build['schedule_revisions_table'] = [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
];
return $build;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
ScheduleController:: |
private | property | A dateFormatter object. | |
ScheduleController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
ScheduleController:: |
public | function | Manual execution of a scheduled item. | |
ScheduleController:: |
public | function | Generates an overview table of older revisions of a Schedule . | |
ScheduleController:: |
public | function | Page title callback for a Schedule revision. | |
ScheduleController:: |
public | function | Displays a Schedule revision. | |
ScheduleController:: |
public | function | View schedule canonical. | |
ScheduleController:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |