You are here

viewsphpfilter.module in Views PHP Filter 5

Same filename and directory in other branches
  1. 6 viewsphpfilter.module
  2. 7 viewsphpfilter.module

File

viewsphpfilter.module
View source
<?php

/**
 * Implementation of hook_help().
 */
function viewsphpfilter_help($section) {
  $output = '';
  switch ($section) {
    case "admin/help#viewsphpfilter":
      $output = '<p>' . t("A filter for the Views module.") . '</p>';
      $output = '<p>' . t("This filter will appear in the Views Filter dialog as a \"Node: Node ID\".  This filter takes PHP code as its value (with no open/close brackets), which should return an array of integers representing Node IDs to include or exclude.  The filter will evaluate the PHP and filter on the resulting array.  It can also (experimental) take a list of node IDs, separated by commas.") . '</p>';
      break;
  }
  return $output;
}

/**
 * Implementation of hook_disable().
 */
function viewsphpfilter_disable() {
}

/**
 * Implementation of hook_views_tables_alter() from Views API
 *
 */
function viewsphpfilter_views_tables_alter(&$table_data) {
  if (isset($table_data['node']['filters'])) {
    $table_data['node']['filters']['nid'] = array(
      'name' => t('Node: Node ID'),
      'operator' => 'views_handler_operator_or',
      'cacheable' => 'no',
      'handler' => 'views_handler_filter_nid',
      'value' => array(
        '#type' => 'textarea',
        '#process' => array(
          'views_filter_nid_process_form' => array(),
        ),
      ),
      'option' => array(
        '#type' => 'select',
        '#options' => array(
          'php' => 'PHP code',
          'id' => 'ID list',
        ),
      ),
      'help' => t('This filter allows nodes to be filtered by Node ID.  PHP code should return an array with node IDs.  ID lists should be separated by commas.'),
    );
  }
}
function views_handler_filter_nid($op, $filter, $filterinfo, &$query) {

  //as with filter_nid_process_form below -- the first if should NOT be necessary - 5.x only
  if (is_array($filter['value'])) {
    $filter['value'] = implode(',', $filter['value']);
  }
  if (isset($filter['value'])) {
    if ($filter['options'] == 'php') {
      $result = eval($filter['value']);

      //can't use drupal_eval(), because it only returns strings -- what good is that?
      if ($result === FALSE) {
        watchdog('viewsphpfilter', "Parse error in PHP: {$filter['value']}; filter ignored", WATCHDOG_ERROR);
        return;
      }
    }
    else {
      $result = $filter['value'];
    }
    if ($result == NULL) {
      watchdog('viewsphpfilter', "PHP returned null; filter ignored", WATCHDOG_NOTICE);
      return;
    }
    if (!is_array($result)) {
      $result = explode(',', $result);
    }
    if (empty($result) || count($result) == 1 && $result[0] == NULL) {
      if ($filter['operator'] == 'OR') {
        $query
          ->add_where("node.nid IS NULL");
      }
      return;
    }
    foreach ($result as $i => $val) {
      $result[$i] = trim($val, " \t\n\r\0\v,");
    }

    //error_log('result: ' . print_r($nids,TRUE));
    $allints = true;
    foreach ($result as $retval) {
      if ((string) (int) $retval != $retval) {
        $allints = false;
      }
    }
    if ($allints) {
      if ($filter['operator'] == 'OR') {
        $query
          ->add_where("node.nid IN (%s)", implode(",", $result));
      }
      else {

        // $filter['operator'] == 'NOR'
        $query
          ->add_where("node.nid NOT IN (%s)", implode(",", $result));
      }
    }
    else {
      watchdog('viewsphpfilter', "Invalid return value in PHP: " . implode(",", $result) . "; filter ignored", WATCHDOG_ERROR);
    }
  }
}
function views_filter_nid_process_form($element) {

  //the if should NOT be needed -- why is it turning my strings into arrays? Drupal 5.x only
  if (is_array($element['#value'])) {
    $element['#value'] = implode(',', $element['#value']);
  }

  //error_log("form: " . print_r($element['#value'], TRUE));
  return $element;
}

Functions

Namesort descending Description
viewsphpfilter_disable Implementation of hook_disable().
viewsphpfilter_help Implementation of hook_help().
viewsphpfilter_views_tables_alter Implementation of hook_views_tables_alter() from Views API
views_filter_nid_process_form
views_handler_filter_nid