You are here

function hansel_switch_node_age_compare in Hansel breadcrumbs 7

Same name and namespace in other branches
  1. 8 hansel.switches.inc \hansel_switch_node_age_compare()

Callback for "node age" switch to compare a given value.

Parameters

array $arguments:

string $value:

Return value

boolean

1 string reference to 'hansel_switch_node_age_compare'
hansel_hansel_switch_types in ./hansel.module
Implements hook_hansel_switch_types().

File

./hansel.switches.inc, line 124
Hansel switches

Code

function hansel_switch_node_age_compare($arguments, $value) {
  if (drupal_strtolower(hansel_arg(0)) == 'node' && is_numeric(hansel_arg(1))) {
    if ($node = node_load(hansel_arg(1))) {
      $unit = empty($arguments['unit']) ? 86400 : $arguments['unit'];
      $age = (REQUEST_TIME - $node->created) / $unit;

      // Check for ranges (e.g. "0-100" or "0.5-1").
      if (preg_match('/^([0-9\\.]+)\\-([0-9\\.]+)$/si', $value, $match)) {
        $min = (double) $match[1];
        $max = (double) $match[2];
        return $age >= $min && $age < $max;
      }
      elseif (preg_match('/^(\\<|\\>|\\<\\=|\\>\\=) *([0-9\\.]+)$/si', $value, $match)) {
        $operator = $match[1];
        $check_value = (double) $match[2];
        switch ($operator) {
          case '<':
            return $age < $check_value;
          case '>':
            return $age > $check_value;
          case '<=':
            return $age <= $check_value;
          case '>=':
            return $age >= $check_value;
        }
      }
    }
  }
  return FALSE;
}