You are here

public function Instances::listInstancesOutput in Smart Date 8.2

Same name and namespace in other branches
  1. 3.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::listInstancesOutput()
  2. 3.0.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::listInstancesOutput()
  3. 3.1.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::listInstancesOutput()
  4. 3.2.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::listInstancesOutput()
  5. 3.3.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::listInstancesOutput()
  6. 3.4.x modules/smart_date_recur/src/Controller/Instances.php \Drupal\smart_date_recur\Controller\Instances::listInstancesOutput()

Provide a list of rule items with operations to change rule items.

Return value

array A render array of list of instances, with actions/operations.

2 calls to Instances::listInstancesOutput()
Instances::listInstances in modules/smart_date_recur/src/Controller/Instances.php
Preparing output of instance listing either modal/Ajax or default.
Instances::revertAjax in modules/smart_date_recur/src/Controller/Instances.php
Reverting a rule instance in an Ajax confirm dialog.

File

modules/smart_date_recur/src/Controller/Instances.php, line 72

Class

Instances
Provides listings of instances (with overrides) for a specified rule.

Namespace

Drupal\smart_date_recur\Controller

Code

public function listInstancesOutput() {
  if (!($entity = $this->rrule
    ->getParentEntity())) {
    return $this
      ->returnError();
  }
  $field_name = $this->rrule->field_name
    ->getString();
  if ($this->rrule->limit
    ->isEmpty()) {
    $month_limit = $this->rrule
      ->getFieldSettings('month_limit');
    $before = strtotime('+' . (int) $month_limit . ' months');
  }
  else {
    $before = NULL;
  }

  // Use generated instances so we have a full list, and override as we go.
  $gen_instances = $this->rrule
    ->makeRuleInstances($before)
    ->toArray();
  $instances = [];
  foreach ($gen_instances as $gen_instance) {
    $gen_index = $gen_instance
      ->getIndex();
    $instances[$gen_index] = [
      'value' => $gen_instance
        ->getStart()
        ->getTimestamp(),
      'end_value' => $gen_instance
        ->getEnd()
        ->getTimestamp(),
    ];
  }
  if (empty($instances)) {
    return $this
      ->returnError();
  }
  $overrides = $this->rrule
    ->getRuleOverrides();

  // Build headers.
  // Iterate through rows and check for existing overrides.
  foreach ($instances as $index => &$instance) {
    $row_class = '';

    // Check for an override.
    if (isset($overrides[$index])) {

      // Check for rescheduled, overridden, or cancelled
      // add an appropriate class for each, and actions.
      $override = $overrides[$index];
      if ($override->entity_id
        ->getString()) {

        // Overridden, retrieve appropriate entity.
        $override_type = 'overridden';
        $override = $entity_storage
          ->load($override->entity_id
          ->getString());
        $field = $override
          ->get($field_name);

        // TODO: drill down and retrieve, replace values.
        // TODO: drop in the URL to edit.
      }
      elseif ($override->value
        ->getString()) {

        // Rescheduled, use values from override.
        $override_type = 'rescheduled';

        // TODO: drill down and retrieve, replace values.
        $instance['value'] = $override->value
          ->getString();
        $instance['end_value'] = $override->end_value
          ->getString();
      }
      else {

        // Cancelled, so change class and actions.
        $override_type = 'cancelled';
      }
      $instance['class'] = $override_type;
      $instance['override'] = $override;
    }
    else {
    }
    $instance['rrule'] = $this->rrule
      ->id();
    $instance['rrule_index'] = $index;
  }
  return $this
    ->render($instances);
}