You are here

function rules_save_variables in Rules 6

Saves variables, which are returned by an action

1 call to rules_save_variables()
rules_execute_action in rules/rules.module
Execution handler for actions

File

rules/rules.variables.inc, line 179
Provides functions and classes for handling variables

Code

function rules_save_variables($element, $result, &$state) {
  $element_info = rules_get_element_info($element);
  $element['#settings'] += array(
    '#argument map' => array(),
  );
  $map = $element['#settings']['#argument map'];
  foreach (element_children($result) as $argument_name) {
    $map += array(
      $argument_name => $argument_name,
    );
    if (isset($state['variables'][$map[$argument_name]])) {

      //variable exists, so update it
      $variable =& $state['variables'][$map[$argument_name]];
      $variable
        ->update($result[$argument_name]);
      $variable
        ->save();
    }
    else {
      if (isset($element_info['new variables'][$map[$argument_name]])) {

        //it is a new variable, so add it
        $info = $element_info['new variables'][$map[$argument_name]];
        $variable = new rules_variable();
        $variable
          ->construct($state, $map[$argument_name], $result[$argument_name], $info);
        if (isset($info['save']) && $info['save']) {
          $variable
            ->save();
        }
        rules_log(t('Successfully added the new variable "@arg"', array(
          '@arg' => $info['label'],
        )));
      }
      else {
        rules_log(t('Unknown variable name "@var" return by action "@name".', array(
          '@var' => $argument_name,
          '@name' => $element['#name'],
        )), TRUE);
      }
    }
  }
}