You are here

function feeds_tamper_math_callback in Feeds Tamper 7

Same name and namespace in other branches
  1. 6 plugins/math.inc \feeds_tamper_math_callback()
1 string reference to 'feeds_tamper_math_callback'
math.inc in plugins/math.inc

File

plugins/math.inc, line 76

Code

function feeds_tamper_math_callback($result, $item_key, $element_key, &$field, $settings, $source) {
  if ($field === TRUE || $field === FALSE || $field === NULL) {
    $field = (int) $field;
  }
  if (!is_numeric($field)) {
    if (!empty($settings['log'])) {
      $source
        ->log('feeds_tamper:math', 'Math plugin failed because @key was not numeric. Value: @field', array(
        '@key' => $element_key,
        '@field' => $field,
      ));
      drupal_set_message(t('Math plugin failed because @key was not numeric. Value: @field', array(
        '@key' => $element_key,
        '@field' => $field,
      )));
    }
    return;
  }
  if (!empty($settings['flip'])) {
    switch ($settings['operation']) {
      case 'subtraction':
        $field = $settings['value'] - $field;
        return;
      case 'division':

        // Avoid divide by zero.
        if (empty($field)) {
          if (!empty($settings['log'])) {
            $source
              ->log('feeds_tamper:math', 'Math plugin failed because @key was zero.', array(
              '@key' => $element_key,
            ));
            drupal_set_message(t('Math plugin failed because @key was zero.', array(
              '@key' => $element_key,
            )));
          }
          return;
        }
        $field = $settings['value'] / $field;
        return;
    }
  }
  switch ($settings['operation']) {
    case 'addition':
      $field = $field + $settings['value'];
      return;
    case 'subtraction':
      $field = $field - $settings['value'];
      return;
    case 'multiplication':
      $field = $field * $settings['value'];
      return;
    case 'division':
      $field = $field / $settings['value'];
      return;
  }
}