You are here

protected function RulesAction::executeCallback in Rules 7.2

Execute the callback and update/save data as specified by the action.

Overrides RulesAbstractPlugin::executeCallback

File

includes/rules.plugins.inc, line 21
Contains plugin info and implementations not needed for rule evaluation.

Class

RulesAction
Implements a rules action.

Code

protected function executeCallback(array $args, RulesState $state = NULL) {
  rules_log('Evaluating the action %name.', array(
    '%name' => $this
      ->label($this->elementName),
  ), RulesLog::INFO, $this);
  $return = $this
    ->__call('execute', empty($this->info['named parameter']) ? $args : array(
    $args,
  ));

  // Get the (partially) wrapped arguments.
  $args = $state->currentArguments;
  if (is_array($return)) {
    foreach ($return as $name => $data) {

      // Add provided variables.
      if (isset($this->info['provides'][$name])) {
        $var_name = isset($this->settings[$name . ':var']) ? $this->settings[$name . ':var'] : $name;
        if (!$state
          ->varInfo($var_name)) {
          $state
            ->addVariable($var_name, $data, $this->info['provides'][$name]);
          rules_log('Added the provided variable %name of type %type', array(
            '%name' => $var_name,
            '%type' => $this->info['provides'][$name]['type'],
          ), RulesLog::INFO, $this);
          if (!empty($this->info['provides'][$name]['save']) && $state->variables[$var_name] instanceof EntityMetadataWrapper) {
            $state
              ->saveChanges($var_name, $state->variables[$var_name]);
          }
        }
      }
      elseif (!isset($this->info['provides'][$name])) {

        // Update the data value using the wrapper.
        if (isset($args[$name]) && $args[$name] instanceof EntityMetadataWrapper) {
          try {
            $args[$name]
              ->set($data);
          } catch (EntityMetadataWrapperException $e) {
            throw new RulesEvaluationException('Unable to update the argument for parameter %name: %error', array(
              '%name' => $name,
              '%error' => $e
                ->getMessage(),
            ), $this);
          }
        }
        elseif (array_key_exists($name, $args)) {

          // Map back to the source variable name and update it.
          $var_name = !empty($this->settings[$name . ':select']) ? str_replace('-', '_', $this->settings[$name . ':select']) : $name;
          $state->variables[$var_name] = $data;
        }
      }
    }
  }

  // Save parameters as defined in the parameter info.
  if ($return !== FALSE) {
    foreach ($this->info['parameter'] as $name => $info) {
      if (!empty($info['save']) && $args[$name] instanceof EntityMetadataWrapper) {
        if (isset($this->settings[$name . ':select'])) {
          $state
            ->saveChanges($this->settings[$name . ':select'], $args[$name]);
        }
        else {

          // Wrapper has been configured via direct input, so just save.
          rules_log('Saved argument of type %type for parameter %name.', array(
            '%name' => $name,
            '%type' => $args[$name]
              ->type(),
          ));
          $args[$name]
            ->save();
        }
      }
    }
  }
}