You are here

public function CompareNumberOfResultsBetweenTwoViews::process in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesCondition/CompareNumberOfResultsBetweenTwoViews.php \Drupal\business_rules\Plugin\BusinessRulesCondition\CompareNumberOfResultsBetweenTwoViews::process()

Process the condition.

Parameters

\Drupal\business_rules\ConditionInterface $condition: The configured condition.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event that has triggered the condition.

Return value

bool Boolean value that indicates if the condition is true.

Overrides BusinessRulesConditionPlugin::process

File

src/Plugin/BusinessRulesCondition/CompareNumberOfResultsBetweenTwoViews.php, line 94

Class

CompareNumberOfResultsBetweenTwoViews
Class CompareNumberOfResultsBetweenTwoViews.

Namespace

Drupal\business_rules\Plugin\BusinessRulesCondition

Code

public function process(ConditionInterface $condition, BusinessRulesEvent $event) {

  // Get settings.
  $defined_view1 = $condition
    ->getSettings('view_1');
  $args1 = $condition
    ->getSettings('arguments_1');
  $operator = $condition
    ->getSettings('operator');
  $defined_view2 = $condition
    ->getSettings('view_2');
  $args2 = $condition
    ->getSettings('arguments_2');
  $event_variables = $event
    ->getArgument('variables');

  // Process settings.
  $defined_view1 = explode(':', $defined_view1);
  $view_id1 = $defined_view1[0];
  $display1 = $defined_view1[1];
  $defined_view2 = explode(':', $defined_view2);
  $view_id2 = $defined_view2[0];
  $display2 = $defined_view2[1];
  $args1 = explode(chr(10), $args1);
  $args1 = array_map('trim', $args1);
  $args1 = array_filter($args1, 'strlen');
  $args2 = explode(chr(10), $args2);
  $args2 = array_map('trim', $args2);
  $args2 = array_filter($args2, 'strlen');

  // Process variables.
  foreach ($args1 as $key => $value) {
    $args1[$key] = $this
      ->processVariables($value, $event_variables);
  }
  foreach ($args2 as $key => $value) {
    $args2[$key] = $this
      ->processVariables($value, $event_variables);
  }

  // Execute view 1.
  $view1 = Views::getView($view_id1);
  $view1
    ->setArguments($args1);
  $view1
    ->setDisplay($display1);
  $view1
    ->preExecute();
  $view1
    ->build();
  if ($view1
    ->execute()) {
    $view_result1 = $view1->result;
    $result1 = count($view_result1);
  }
  else {
    $result1 = 0;
  }

  // Execute view 2.
  $view2 = Views::getView($view_id2);
  $view2
    ->setArguments($args2);
  $view2
    ->setDisplay($display2);
  $view2
    ->preExecute();
  $view2
    ->build();
  if ($view2
    ->execute()) {
    $view_result2 = $view2->result;
    $result2 = count($view_result2);
  }
  else {
    $result2 = 0;
  }

  // Check the condition.
  if ($this->util
    ->criteriaMet($result1, $operator, $result2)) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}