| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
require_once(drupal_get_path('module', 'simplelist') .'/SimpleListFilterParent.php');
|
| 5 |
|
| 6 |
/*
|
| 7 |
* This class filters nodes by type and/or taxonomy.
|
| 8 |
*/
|
| 9 |
class SimpleListNodeFilter extends SimpleListFilterParent {
|
| 10 |
|
| 11 |
/**
|
| 12 |
* The main workhorse of the class, this function gets the list of node ids from the database, and then gets the loaded nodes out of the cache_engine.
|
| 13 |
*
|
| 14 |
* @param stdClass $simple_list
|
| 15 |
* SimpleList object from controller.
|
| 16 |
* @param int $count
|
| 17 |
* The number of nodes to return.
|
| 18 |
* @param int $offset
|
| 19 |
* The offset from the start - 0 means start at 1. Basically, query starts at item 1+$offset and goes on to $count+$offset
|
| 20 |
* @return array
|
| 21 |
* Array of loaded node objects.
|
| 22 |
*/
|
| 23 |
public function get_node_list($simple_list, $count, $offset, $paged) {
|
| 24 |
if (count($simple_list->node_types) == 0 && count($simple_list->terms) == 0) {
|
| 25 |
return array();
|
| 26 |
}
|
| 27 |
$nodes = array();
|
| 28 |
$query_args = array();
|
| 29 |
$where_args = array();
|
| 30 |
$query = '';
|
| 31 |
$where = '';
|
| 32 |
$order = '';
|
| 33 |
|
| 34 |
if (count($simple_list->node_types) > 0 && count($simple_list->terms) > 0) {
|
| 35 |
$query = "SELECT DISTINCT n.nid FROM {node} n INNER JOIN {term_node} tn ON (tn.tid IN (". db_placeholders($simple_list->terms) .") AND n.vid = tn.vid)";
|
| 36 |
$where = " WHERE n.type IN (". db_placeholders($simple_list->node_types, "varchar") .")";
|
| 37 |
foreach($simple_list->terms as $term) {
|
| 38 |
$query_args[] = $term->tid;
|
| 39 |
}
|
| 40 |
/*foreach($simple_list->node_types as $type) {
|
| 41 |
$where_args[] = $type;
|
| 42 |
}*/
|
| 43 |
$where_args = array_merge($where_args, $simple_list->node_types);
|
| 44 |
}
|
| 45 |
else if (count($simple_list->node_types) > 0) {
|
| 46 |
$query = "SELECT n.nid FROM {node} n";
|
| 47 |
$where = " WHERE n.type IN (". db_placeholders($simple_list->node_types, "varchar") .")";
|
| 48 |
$where_args = $simple_list->node_types;
|
| 49 |
}
|
| 50 |
else {
|
| 51 |
$query = "SELECT DISTINCT n.nid FROM {node} n INNER JOIN {term_node} tn ON (tn.tid IN (". db_placeholders($simple_list->terms) .") AND n.vid = tn.vid)";
|
| 52 |
foreach($simple_list->terms as $term) {
|
| 53 |
$query_args[] = $term->tid;
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
if ($simple_list->published == SIMPLELIST_PUBLISHED_NODES || $simple_list->published == SIMPLELIST_UNPUBLISHED_NODES) {
|
| 58 |
if ($where != '') {
|
| 59 |
$where .= " AND";
|
| 60 |
}
|
| 61 |
else {
|
| 62 |
$where .= " WHERE";
|
| 63 |
}
|
| 64 |
$where .= " n.status = %d";
|
| 65 |
$where_args[] = $simple_list->published;
|
| 66 |
}
|
| 67 |
|
| 68 |
$dir = $this->get_sort_order_from_sort_data($simple_list->sort_data);
|
| 69 |
switch ($simple_list->sort_name) {
|
| 70 |
case 'created':
|
| 71 |
|
| 72 |
$order = ' ORDER BY n.created '. $dir;
|
| 73 |
break;
|
| 74 |
case 'title':
|
| 75 |
$order = ' ORDER BY n.title '. $dir;
|
| 76 |
break;
|
| 77 |
case 'node_id':
|
| 78 |
$order = ' ORDER BY n.nid '. $dir;
|
| 79 |
break;
|
| 80 |
case 'updated':
|
| 81 |
$order = ' ORDER BY n.changed '. $dir;
|
| 82 |
break;
|
| 83 |
case 'type':
|
| 84 |
$order = ' ORDER BY n.type '. $dir .', created DESC';
|
| 85 |
break;
|
| 86 |
case 'comment_count':
|
| 87 |
if (db_table_exists('node_comment_statistics')) {
|
| 88 |
$query .= ' INNER JOIN {node_comment_statistics} ncs ON (ncs.nid = n.nid)';
|
| 89 |
$order = ' ORDER BY ncs.comment_count '. $dir . ', n.created DESC';
|
| 90 |
}
|
| 91 |
break;
|
| 92 |
case 'most_popular':
|
| 93 |
if (db_table_exists('votingapi_cache')) {
|
| 94 |
$query .= ' LEFT OUTER JOIN {votingapi_cache} vap ON (vap.content_id = n.nid AND vap.content_type = \'node\' AND vap.function = \'average\')';
|
| 95 |
$order = ' ORDER BY vap.value '. $dir . ', n.created DESC';
|
| 96 |
}
|
| 97 |
break;
|
| 98 |
case 'user_name':
|
| 99 |
$query .= ' INNER JOIN {users} u ON (u.uid = n.uid)';
|
| 100 |
$order = ' ORDER BY u.name '. $dir .', created DESC';
|
| 101 |
break;
|
| 102 |
default:
|
| 103 |
$order = '';
|
| 104 |
break;
|
| 105 |
}
|
| 106 |
|
| 107 |
if ($paged) {
|
| 108 |
$result = pager_query(db_rewrite_sql($query . $where . $order), $count, 0, NULL, array_merge($query_args, $where_args));
|
| 109 |
}
|
| 110 |
else {
|
| 111 |
$result = db_query_range(db_rewrite_sql($query . $where . $order), array_merge($query_args, $where_args), $offset, $count);
|
| 112 |
}
|
| 113 |
while ($node_id = db_fetch_object($result)) {
|
| 114 |
$nodes[] = $this->cache_engine->fetch_node($node_id->nid);
|
| 115 |
}
|
| 116 |
|
| 117 |
return $nodes;
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* This is the form for the class paramters.
|
| 122 |
*
|
| 123 |
* @param unknown_type $simplelist
|
| 124 |
* @return unknown
|
| 125 |
*/
|
| 126 |
public static function get_filter_form($simplelist) {
|
| 127 |
$form = array();
|
| 128 |
$nodes = array();
|
| 129 |
$selected_terms = array();
|
| 130 |
|
| 131 |
foreach (node_get_types() as $type => $info) {
|
| 132 |
$nodes[$type] = $info->name;
|
| 133 |
}
|
| 134 |
|
| 135 |
$form['node_types'] = array(
|
| 136 |
'#type' => 'checkboxes',
|
| 137 |
'#title' => t('Node Types'),
|
| 138 |
'#default_value' => (isset($simplelist->node_types) ? $simplelist->node_types : array()),
|
| 139 |
'#options' => $nodes,
|
| 140 |
'#description' => t('Check each node type to display in the list.'),
|
| 141 |
'#weight' => -6
|
| 142 |
);
|
| 143 |
|
| 144 |
$terms = array();
|
| 145 |
$query = 'SELECT v.vid, v.name, td.tid, td.name AS term_name FROM {vocabulary} v INNER JOIN {term_data} td ON (v.vid = td.vid) ORDER BY v.weight, v.name, td.weight, td.name';
|
| 146 |
$result = db_query(db_rewrite_sql($query, 'v', 'vid'));
|
| 147 |
while ($term_row = db_fetch_object($result)) {
|
| 148 |
$terms[$term_row->tid] = $term_row->name .': '. $term_row->term_name;
|
| 149 |
}
|
| 150 |
|
| 151 |
foreach($simplelist->terms as $term) {
|
| 152 |
$selected_terms[] = $term->tid;
|
| 153 |
}
|
| 154 |
|
| 155 |
|
| 156 |
$form['taxonomy_tids'] = array(
|
| 157 |
'#type' => 'select',
|
| 158 |
'#title' => t('Taxonomy terms'),
|
| 159 |
'#default_value' => (isset($selected_terms) ? $selected_terms : array()),
|
| 160 |
'#options' => $terms,
|
| 161 |
'#multiple' => TRUE,
|
| 162 |
'#size' => 8,
|
| 163 |
'#description' => t('Select any taxonomy terms to restrict by.'),
|
| 164 |
'#weight' => -4,
|
| 165 |
);
|
| 166 |
|
| 167 |
$form_options = array(
|
| 168 |
'created' => t('Date Created'),
|
| 169 |
'updated' => t('Date Updated'),
|
| 170 |
'title' => t('Title'),
|
| 171 |
'node_id' => t('Node ID'),
|
| 172 |
'user_name' => t('Author Name'),
|
| 173 |
'type' => t('Node Type'),
|
| 174 |
'comment_count' => t('Comment Count'),
|
| 175 |
);
|
| 176 |
|
| 177 |
if (db_table_exists('votingapi_cache')) {
|
| 178 |
$form_options['most_popular'] = t('Most Popular');
|
| 179 |
}
|
| 180 |
|
| 181 |
$form['sort_name'] = array(
|
| 182 |
'#type' => 'select',
|
| 183 |
'#title' => t('Sort Order'),
|
| 184 |
'#default_value' => $simplelist->sort_name,
|
| 185 |
'#options' => $form_options,
|
| 186 |
'#description' => 'The order to display nodes in.',
|
| 187 |
'#weight' => -2,
|
| 188 |
);
|
| 189 |
|
| 190 |
$form['sort_data'] = array(
|
| 191 |
'#type' => 'radios',
|
| 192 |
'#title' => t('Sort Direction'),
|
| 193 |
'#default_value' => $simplelist->sort_data,
|
| 194 |
'#options' => array('ASC' => t('Ascending'), 'DESC' => t('Descending')),
|
| 195 |
'#weight' => 2,
|
| 196 |
);
|
| 197 |
return $form;
|
| 198 |
}
|
| 199 |
|
| 200 |
/**
|
| 201 |
* This is the validation for the class parameters.
|
| 202 |
*
|
| 203 |
* @param unknown_type $form
|
| 204 |
* @param unknown_type $form_state
|
| 205 |
*/
|
| 206 |
public static function get_filter_form_validate(&$form, &$form_state) {
|
| 207 |
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* This is the submit function for the class parameters.
|
| 212 |
*
|
| 213 |
* @param unknown_type $form_id
|
| 214 |
* @param unknown_type $form_state
|
| 215 |
*/
|
| 216 |
public static function get_filter_form_submit($form_id, &$form_state) {
|
| 217 |
$old_simplelist = $form_state['values']['simplelist'];
|
| 218 |
|
| 219 |
$node_types = array();
|
| 220 |
foreach ($form_state['values']['node_types'] as $key => $value) {
|
| 221 |
if ($value) {
|
| 222 |
$node_types[] = $key;
|
| 223 |
}
|
| 224 |
}
|
| 225 |
|
| 226 |
$delete_type_query = "DELETE FROM {simplelist_types} WHERE slid = %d AND node_type = '%s'";
|
| 227 |
$insert_type_query = "INSERT INTO {simplelist_types} (slid, node_type) VALUES (%d, '%s')";
|
| 228 |
$old_types = $old_simplelist->node_types;
|
| 229 |
foreach ($old_types as $type) {
|
| 230 |
if (($index = array_search($type, $node_types)) !== FALSE) {
|
| 231 |
unset($node_types[$index]);
|
| 232 |
}
|
| 233 |
else {
|
| 234 |
db_query($delete_type_query, $form_state['values']['slid'], $type);
|
| 235 |
}
|
| 236 |
}
|
| 237 |
foreach ($node_types as $type) {
|
| 238 |
db_query($insert_type_query, $form_state['values']['slid'], $type);
|
| 239 |
}
|
| 240 |
|
| 241 |
$delete_term_query = "DELETE FROM {simplelist_terms} WHERE slid = %d AND tid = %d";
|
| 242 |
$insert_term_query = "INSERT INTO {simplelist_terms} (slid, tid) VALUES (%d, %d)";
|
| 243 |
$old_terms = $old_simplelist->terms;
|
| 244 |
foreach ($old_terms as $term) {
|
| 245 |
if (isset($form_state['values']['taxonomy_tids'][$term->tid])) {
|
| 246 |
unset($form_state['values']['taxonomy_tids'][$term->tid]);
|
| 247 |
}
|
| 248 |
else {
|
| 249 |
db_query($delete_term_query, $form_state['values']['slid'], $term->tid);
|
| 250 |
}
|
| 251 |
}
|
| 252 |
foreach ($form_state['values']['taxonomy_tids'] as $tid => $data) {
|
| 253 |
db_query($insert_term_query, $form_state['values']['slid'], $tid);
|
| 254 |
}
|
| 255 |
}
|
| 256 |
|
| 257 |
/**
|
| 258 |
* Clean up old settings from this simplelist
|
| 259 |
*
|
| 260 |
* Here we go through and clean up the settings specific to this filter for this simplelist. This gets called by
|
| 261 |
* the form_submit if the user has switched from this filter to a different one, to make sure no leftover data
|
| 262 |
* is left behind. We also clean out the old node_types data so that if the new filter uses it, then it's not
|
| 263 |
* confused by thinking there's old data left behind that's not there.
|
| 264 |
*
|
| 265 |
* This should also be called by contrib modules building on SimpleList on uninstall, so that data is deleted from
|
| 266 |
* lists which the contrib filter wrote.
|
| 267 |
*
|
| 268 |
* @param unknown_type $slid
|
| 269 |
* @param unknown_type $form_id
|
| 270 |
* @param unknown_type $form_state
|
| 271 |
*/
|
| 272 |
public static function clear_existing_settings($slid, $form_id='', &$form_state=NULL) {
|
| 273 |
$delete_term_query = "DELETE FROM {simplelist_terms} WHERE slid = %d";
|
| 274 |
db_query($delete_term_query, $slid);
|
| 275 |
if (is_array($form_state)) {
|
| 276 |
if ($form_state['values']['simplelist']) {
|
| 277 |
$form_state['values']['simplelist']->terms = array();
|
| 278 |
}
|
| 279 |
}
|
| 280 |
|
| 281 |
$delete_type_query = "DELETE FROM {simplelist_types} WHERE slid = %d";
|
| 282 |
db_query($delete_type_query, $slid);
|
| 283 |
if (is_array($form_state)) {
|
| 284 |
if ($form_state['values']['simplelist']) {
|
| 285 |
$form_state['values']['simplelist']->node_types = array();
|
| 286 |
}
|
| 287 |
}
|
| 288 |
}
|
| 289 |
}
|
| 290 |
?>
|