View source
<?php
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;
}
function viewsphpfilter_disable() {
}
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) {
if (is_array($filter['value'])) {
$filter['value'] = implode(',', $filter['value']);
}
if (isset($filter['value'])) {
if ($filter['options'] == 'php') {
$result = eval($filter['value']);
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,");
}
$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 {
$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) {
if (is_array($element['#value'])) {
$element['#value'] = implode(',', $element['#value']);
}
return $element;
}