You are here

public function ViewsConditionalHandler::render in Views Conditional 7

Renders the final output based on conditional input.

Overrides views_handler_field::render

File

includes/views/handlers/views_conditional_handler.inc, line 199
Handles conditionals in Views. IF xxx THEN yyy...

Class

ViewsConditionalHandler
@file Handles conditionals in Views. IF xxx THEN yyy...

Code

public function render($values) {
  $if = $this->options['if'];
  $condition = $this->options['condition'];
  $equalto = $this->options['equalto'];
  $then = $this->options['then'];
  $or = $this->options['or'] ? $this->options['or'] : '';

  // Gather field information.
  $fields = $this->view->display_handler
    ->get_handlers('field');

  // Search through field information for possible replacement variables.
  foreach ($this->view->display_handler
    ->get_field_labels() as $key => $var) {

    // If we find a replacement variable, replace it.
    if (strpos($equalto, "[{$key}]") !== FALSE) {
      $field = $this
        ->clean_var($fields[$key]);
      $equalto = str_replace("[{$key}]", $field, $equalto);
    }
    if (strpos($then, "[{$key}]") !== FALSE) {
      $field = $this
        ->clean_var($fields[$key]);
      $then = str_replace("[{$key}]", $field, $then);
    }
    if (strpos($or, "[{$key}]") !== FALSE) {
      $field = $this
        ->clean_var($fields[$key]);
      $or = str_replace("[{$key}]", $field, $or);
    }
  }

  // If we find a date stamp replacement, replace that.
  if (strpos($equalto, 'DATE_STAMP') !== FALSE) {
    $equalto = str_replace('DATE_STAMP', format_date(REQUEST_TIME), $equalto);
  }
  if (strpos($then, 'DATE_STAMP') !== FALSE) {
    $then = str_replace('DATE_STAMP', format_date(REQUEST_TIME), $then);
  }
  if (strpos($or, 'DATE_STAMP') !== FALSE) {
    $or = str_replace('DATE_STAMP', format_date(REQUEST_TIME), $or);
  }

  // If we find a unix date stamp replacement, replace that.
  if (strpos($equalto, 'DATE_UNIX') !== FALSE) {
    $equalto = str_replace('DATE_UNIX', REQUEST_TIME, $equalto);
  }
  if (strpos($then, 'DATE_UNIX') !== FALSE) {
    $then = str_replace('DATE_UNIX', REQUEST_TIME, $then);
  }
  if (strpos($or, 'DATE_UNIX') !== FALSE) {
    $or = str_replace('DATE_UNIX', REQUEST_TIME, $or);
  }

  // Strip tags on the "if" field.  Otherwise it appears to
  // output as <div class="xxx">Field data</div>...
  // ...which of course makes it difficult to compare.
  $r = isset($fields["{$if}"]->last_render) ? trim(strip_tags($fields["{$if}"]->last_render, '<img>')) : NULL;

  // Run conditions.
  switch ($condition) {

    // Equal to.
    case 1:
      if ($r == $equalto) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Not equal to.
    case 2:
      if ($r !== $equalto) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Greater than.
    case 3:
      if ($r > $equalto) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Less than.
    case 4:
      if ($r < $equalto) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Empty.
    case 5:
      if (empty($r)) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Not empty.
    case 6:
      if (!empty($r)) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Contains
    case 7:
      if (mb_stripos($r, $equalto) !== FALSE) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Does NOT contain
    case 8:
      if (mb_stripos($r, $equalto) === FALSE) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Length Equal to.
    case 9:
      if (mb_strlen($r) == $equalto) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Length Not equal to.
    case 10:
      if (mb_strlen($r) !== $equalto) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Length Greater than.
    case 11:
      if (mb_strlen($r) > $equalto) {
        return $then;
      }
      else {
        return $or;
      }
      break;

    // Length Less than.
    case 12:
      if (mb_strlen($r) < $equalto) {
        return $then;
      }
      else {
        return $or;
      }
      break;
  }
}