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