You are here

public function SchedulerHandlerFieldSchedulerCountdown::render in Scheduler 7

Renders the countdown value in the units required.

Overrides views_handler_field::render

File

./scheduler_handler_field_scheduler_countdown.inc, line 106
Display scheduled dates in 'countdown' format for use in Views.

Class

SchedulerHandlerFieldSchedulerCountdown
Field handler to display a countdown until a scheduled action.

Code

public function render($values) {
  $countdown_display = $this->options['countdown_display'];
  $this->raw_value = $values->{$this->field_alias};
  $scales = array(
    'weeks' => self::WEEK_SCALE,
    'days' => self::DAY_SCALE,
    'hours' => self::HOUR_SCALE,
    'minutes' => self::MINUTE_SCALE,
    'seconds' => self::SECOND_SCALE,
  );

  // If the field has been set to 'Smart', determine the right timescale.
  if ($countdown_display == 'smart') {
    $scales = array_filter($scales, array(
      $this,
      'scaleFilterCallback',
    ));
    $scale = empty($scales) ? self::SECOND_SCALE : reset($scales);
  }
  else {
    $scale = $scales[$countdown_display];
  }

  // Get the display value by dividing the original value by the scale.
  $scaled_value = round($this->raw_value / $scale);
  switch ($scale) {
    case self::SECOND_SCALE:
      $long = format_plural($scaled_value, '1 second', '@count seconds', array(
        '@count' => $scaled_value,
      ));
      $short = t('@counts', array(
        '@count' => $scaled_value,
      ));
      break;
    case self::MINUTE_SCALE:
      $long = format_plural($scaled_value, '1 minute', '@count minutes', array(
        '@count' => $scaled_value,
      ));
      $short = t('@countm', array(
        '@count' => $scaled_value,
      ));
      break;
    case self::HOUR_SCALE:
      $long = format_plural($scaled_value, '1 hour', '@count hours', array(
        '@count' => $scaled_value,
      ));
      $short = t('@counth', array(
        '@count' => $scaled_value,
      ));
      break;
    case self::DAY_SCALE:
      $long = format_plural($scaled_value, '1 day', '@count days', array(
        '@count' => $scaled_value,
      ));
      $short = t('@countd', array(
        '@count' => $scaled_value,
      ));
      break;
    case self::WEEK_SCALE:
      $long = format_plural($scaled_value, '1 week', '@count weeks', array(
        '@count' => $scaled_value,
      ));
      $short = t('@countw', array(
        '@count' => $scaled_value,
      ));
      break;
  }
  switch ($this->options['units_display']) {
    case 'long':
      return $long;
    case 'short':
      return $short;
    default:
      return $scaled_value;
  }
}