You are here

class views_php_handler_filter in Views PHP 6

Same name and namespace in other branches
  1. 7.2 plugins/views/views_php_handler_filter.inc \views_php_handler_filter
  2. 7 plugins/views/views_php_handler_filter.inc \views_php_handler_filter

A handler to filter a view using PHP defined by the administrator.

Hierarchy

Expanded class hierarchy of views_php_handler_filter

1 string reference to 'views_php_handler_filter'
views_php_views_data in ./views_php.views.inc
Implements hook_views_data().

File

plugins/views/views_php_handler_filter.inc, line 8

View source
class views_php_handler_filter extends views_handler_filter {
  protected $php_static_variable = NULL;

  /**
   * Implements views_handler#can_expose().
   */
  function can_expose() {
    return FALSE;
  }

  /**
   * Implements views_object#admin_summary().
   */
  function admin_summary() {
    return t('PHP');
  }

  /**
   * Implements views_object#option_definition().
   */
  function option_definition() {
    $options = parent::option_definition();
    $options['use_php_setup'] = array(
      'default' => FALSE,
    );
    $options['php_setup'] = array(
      'default' => '',
    );
    $options['php_filter'] = array(
      'default' => '',
    );
    $options['sql_filter'] = array(
      'default' => '',
    );
    return $options;
  }

  /**
   * Implements views_handler#option_definition().
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form += views_php_form_element($this, FALSE, array(
      'sql_filter',
      t('SQL filter code'),
      t('Return a valid SQL WHERE clause.'),
      FALSE,
    ), array(
      '$view',
      '$handler',
    ));
    $form += views_php_form_element($this, array(
      'use_php_setup',
      t('Use setup code'),
      t('If checked, you can provide PHP code to be run once right before execution of the view. This may be useful to define functions to be re-used in the value and/or output code.'),
    ), array(
      'php_setup',
      t('Setup code'),
      t('Code to run right before execution of the view.'),
      FALSE,
    ), array(
      '$view',
      '$handler',
      '$static',
    ));
    $form += views_php_form_element($this, FALSE, array(
      'php_filter',
      t('Filter code'),
      t('If the code returns TRUE the current row is removed from the results.'),
      FALSE,
    ), array(
      '$view',
      '$handler',
      '$static',
      '$row',
      '$data',
    ));
  }

  /**
   * Implements views_handler_filter#query().
   */
  function query() {

    // Inform views_php_views_pre_execute() to seize control over the query.
    $this->view->views_php = TRUE;

    // Add the SQL sort clause if any.
    if (!empty($this->options['sql_filter'])) {
      $function = create_function('$view, $handler, &$static', $this->options['sql_filter'] . ';');
      ob_start();
      $clause = $function($this->view, $this, $this->php_static_variable);
      ob_end_clean();
      if (!empty($clause)) {
        $this->query
          ->add_where($this->options['group'], $clause);
      }
    }
  }

  /**
   *
   * @see views_php_views_pre_execute()
   */
  function php_pre_execute() {

    // Don't do anything if displaying a summary.
    if (!empty($this->view->build_info['summary'])) {
      return;
    }

    // Ecexute static PHP code.
    if (!empty($this->options['php_setup'])) {
      $function = create_function('$view, $handler, &$static', $this->options['php_setup'] . ';');
      ob_start();
      $function($this->view, $this, $this->php_static_variable);
      ob_end_clean();
    }
    if (!empty($this->options['php_filter'])) {
      $this->view->use_php_pager = TRUE;
    }
  }

  /**
   *
   * @see views_php_views_post_execute()
   */
  function php_post_execute() {

    // Don't do anything if displaying a summary.
    if (!empty($this->view->build_info['summary'])) {
      return;
    }

    // Evaluate the PHP code.
    if (!empty($this->options['php_filter'])) {
      $function = create_function('$view, $handler, &$static, $row, $data', $this->options['php_filter'] . ';');
      ob_start();
      foreach ($this->view->result as $i => $row) {
        $normalized_row = new stdClass();
        foreach ($this->view->display_handler
          ->get_handlers('field') as $field => $handler) {
          $normalized_row->{$field} = isset($row->{$handler->field_alias}) ? $row->{$handler->field_alias} : NULL;
        }

        // Add base_field if found.
        if (!empty($this->view->base_field) && isset($row->{$this->view->base_field})) {
          $normalized_row->{$this->view->base_field} = $row->{$this->view->base_field};
        }
        if ($function($this->view, $this, $this->php_static_variable, $normalized_row, $row)) {
          unset($this->view->result[$i]);
          $this->view->total_rows--;
        }
      }
      ob_end_clean();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_php_handler_filter::$php_static_variable protected property
views_php_handler_filter::admin_summary function Implements views_object#admin_summary().
views_php_handler_filter::can_expose function Implements views_handler#can_expose().
views_php_handler_filter::options_form function Implements views_handler#option_definition().
views_php_handler_filter::option_definition function Implements views_object#option_definition().
views_php_handler_filter::php_post_execute function
views_php_handler_filter::php_pre_execute function
views_php_handler_filter::query function Implements views_handler_filter#query().