views_user.inc 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
<?php
// $Id$

/**
 * This include file implements views functionality on behalf of user.module
 */

function user_views_tables() {
  $tables['users'] = array(
    'name' => 'users',
    'provider' => 'internal', // won't show up in external list.
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'uid'
      ),
      'right' => array(
        'field' => 'uid'
      ),
    ),
    'fields' => array(
      'name' => array(
        'name' => t('Node: Author Name'),
        'handler' => 'views_handler_field_username',
        'sortable' => true,
        'uid' => 'uid',
        'addlfields' => array('uid'),
        'help' => t('This will display the author of the node.'),
      ),
      'uid' => array(
        'name' => t('User: Author Picture'),
        'handler' => 'views_handler_field_userpic',
        'sortable' => false,
        'help' => t('Display the user picture of the author.'),
      ),
    ),
    'sorts' => array(
      'name' => array(
        'name' => t('Node: Author Name'),
        'help' => t('This allows you to sort alphabetically by author.'),
      )
    ),
    'filters' => array(
      'uid' => array(
        'name' => t('Node: Author Name'),
        'operator' => 'views_handler_operator_or',
        'list' => 'views_handler_filter_username',
        'value-type' => 'array',
        'help' => t('This allows you to filter by a particular user. You might not find this useful if you have a lot of users.'),
      ),
    )
  );

  $tables['users_roles'] = array(
    'name' => 'users_roles',
    'provider' => 'internal', // won't show up in external list.
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'uid'
      ),
      'right' => array(
        'field' => 'uid'
      ),
    ),
    'filters' => array(
      'rid' => array(
        'name' => t('Role: Author Role'),
        'operator' => 'views_handler_operator_andor',
        'list' => 'views_handler_filter_role',
        'value-type' => 'array',
        'help' => t('Include the node only if the author is a member of the selected role.'),
      ),
    )
  );
  return $tables;
}

function user_views_arguments() {
  $arguments = array(
    'uid' => array(
      'name' => t('User: UID is Author'),
      'handler' => 'views_handler_arg_uid',
      'help' => t('The User ID argument allows users to filter to nodes authored by the specified user ID.'),
    ),
    'uidtouch' => array(
      'name' => t('User: UID Authored or Commented'),
      'handler' => 'views_handler_arg_uid_touch',
      'help' => t('The User ID argument allows users to filter a to nodes authored or commented on the specified user ID.'),
    ),
91 92 93 94 95
    'username' => array(
      'name' => t('User: Username is Author'),
      'handler' => t('views_handler_arg_username'),
      'help' => t('The Username argument allows users to file to a nodes authored by the specified username.'),
    ),
96 97 98 99 100 101 102 103 104
  );
  return $arguments;
}


function user_views_default_views() {
  $view = new stdClass();
  $view->name = 'tracker';
  $view->requires = array('node_comment_statistics');
105
  $view->description = t('Shows all new activity on system.');
106 107
  $view->page = true;
  $view->url = 'tracker';
108
  $view->page_title = t('recent posts');
109 110 111 112 113 114 115 116 117 118 119
  $view->page_type = 'table';
  $view->use_pager = true;
  $view->nodes_per_page = '25';

  $view->menu = true;
  $view->menu_title = 'recent posts';
  $view->block = false;
  $view->field = array (
    array (
      'tablename' => 'node',
      'field' => 'type',
120
      'label' => t('Type'),
121 122 123 124
    ),
    array (
      'tablename' => 'node',
      'field' => 'title',
125
      'label' => t('Title'),
126 127 128 129 130
      'handler' => 'views_handler_field_nodelink_with_mark',
    ),
    array (
      'tablename' => 'users',
      'field' => 'name',
131
      'label' => t('Author'),
132 133 134 135
    ),
    array (
      'tablename' => 'node_comment_statistics',
      'field' => 'comment_count',
136
      'label' => t('Replies'),
137 138 139 140 141
      'handler' => 'views_handler_comments_with_new',
    ),
    array (
      'tablename' => 'node_comment_statistics',
      'field' => 'last_comment_timestamp',
142
      'label' => t('Last Post'),
143 144 145 146 147 148 149 150 151 152 153 154 155 156
      'handler' => 'views_handler_field_since',
    ),
  );
  $view->sort = array (
    array (
      'tablename' => 'node_comment_statistics',
      'field' => 'last_comment_timestamp',
      'sortorder' => 'DESC',
    ),
  );
  $view->argument = array (
    array (
      'type' => 'uid',
      'argdefault' => '2',
157
      'title' => t('recent posts for %1'),
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    ),
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'value' => '1',
    ),
  );

  $views[$view->name] = $view;

  return $views;
}

174 175 176 177 178 179 180 181
/**
 * Allow replacement of current userid so we can cache these queries
 */
function user_views_query_substitutions($view) {
  global $user;
  return array('***CURRENT_USER***' => intval($user->uid));
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/*
 * Format as a field as a username. Needs to also have a 'uid' available,
 * specified in the field info.
 */
function views_handler_field_username($fieldinfo, $fielddata, $value, $data) {
  $obj->name = $value;
  $uidfield = $fielddata['tablename'] . "_"  . $fieldinfo['uid'];
  $obj->uid = $data->$uidfield;
  return theme('username', $obj);
}

/*
 * Format as a field as a username. Needs to also have a 'uid' available,
 * specified in the field info.
 */
function views_handler_field_userpic($fieldinfo, $fielddata, $value, $data) {
  $account = user_load(array('uid' => $value));
  if ($account !== FALSE) {
200
    return theme('user_picture', $account);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  }
}

function views_handler_arg_uid($op, &$query, $argtype, $arg = '') {
  switch($op) {
    case 'summary':
      $query->add_table('users', true);
      $query->add_field('name', 'users');
      $query->add_field('uid', 'users');
      $fieldinfo['field'] = "users.name";
      return $fieldinfo;
      break;
    case 'sort':
      $query->add_orderby('users', 'name', $argtype);
      break;
    case 'filter':
      $uid = intval($arg);
      $query->add_where("node.uid = $uid");
      break;
    case 'link':
      $name = ($query->name ? $query->name : variable_get('anonymous', 'Anonymous'));
      return l($name, "$arg/" . intval($query->uid));
    case 'title':
      if (!$query) {
        return variable_get('anonymous', 'Anonymous');
226
      }
227
      $user = db_fetch_object(db_query("SELECT name FROM {users} WHERE uid = '%d'", $query));
228
      return check_plain($user->name);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  }
}

function views_handler_arg_uid_touch($op, &$query, $argtype, $arg = '') {
  switch($op) {
    case 'summary':
      $query->add_table('users', true);
      $query->add_field('name', 'users');
      $query->add_field('uid', 'users');
      $fieldinfo['field'] = "users.name";
      return $fieldinfo;
      break;
    case 'sort':
      $query->add_orderby('users', 'name', $argtype);
      break;
    case 'filter':
      $uid = intval($arg);
      $table_data = _views_get_tables();
      $joininfo = $table_data['comments']['join'];
      $joininfo['extra'] = array('uid' => $uid);
      $tblnum = $query->add_table("comments", false, 1, $joininfo);
      $table = $query->get_table_name('comments', $tblnum);

      $query->set_distinct(); // once you do this, there's just one node.
      $query->add_where("node.uid = '%s' OR $table.uid = '%s'", $uid, $uid);
      break;
    case 'link':
      $name = ($query->name ? $query->name : variable_get('anonymous', 'Anonymous'));
      return l($name, "$arg/" . intval($query->uid));
    case 'title':
      if (!$query) {
        return variable_get('anonymous', 'Anonymous');
261
      }
262
      $user = db_fetch_object(db_query("SELECT name FROM {users} WHERE uid = '%d'", $query));
263
      return check_plain($user->name);
264 265 266
  }
}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
function views_handler_arg_username($op, &$query, $argtype, $arg = '') {
  switch($op) {
    case 'summary':
      $query->add_table('users', true);
      $query->add_field('name', 'users');
      $query->add_field('uid', 'users');
      $fieldinfo['field'] = "users.name";
      return $fieldinfo;
      break;
    case 'sort':
      $query->add_orderby('users', 'name', $argtype);
      break;
    case 'filter':
      $name = check_plain($arg);
      $table_data = _views_get_tables();
      $joininfo = $table_data['users']['join'];
      $query->add_table('users', false, 1, $joininfo);
      drupal_set_message($name);
      $query->add_where("users.name = '%s'", $name);
      break;
    case 'link':
      $name = ($query->name ? $query->name : variable_get('anonymous', 'Anonymous'));
      return l($name, "$arg/" . check_plain($name));
    case 'title':
      if (!$query) {
        return variable_get('anonymous', 'Anonymous');
      } 
      $user = db_fetch_object(db_query("SELECT name FROM {users} WHERE name = '%s'", $query));
      return check_plain($user->name);
  }
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
/*
 * Create a list of usernames.
 * I worry this will really, really suck for large sites.
 * But then, why would large sites create a filter for one particular user?
 * Better to go with roles.
 */
function views_handler_filter_username() {
  $uids = array();
  $result = db_query("SELECT u.uid, u.name FROM {users} u WHERE uid != 0 ORDER BY u.name");
  while ($obj = db_fetch_object($result)) {
    $uids[$obj->uid] = $obj->name;
  }
  return $uids;
}

/*
 * Simple array for anonymous..
 */
function views_handler_filter_useranon() {
  return array("0" => variable_get('anonymous', 'Anonymous'));
}

/*
 * Simple array for current user.
 */
function views_handler_filter_usercurrent() {
  return array('***CURRENT_USER***' => t('Currently Logged In User'));
}

/*
 * Custom filter for current user.
 */
function views_handler_filter_usercurrent_custom($op, $filter, $filterinfo, &$query) {
  global $user;
  $query->ensure_table('users');
  $query->add_where("users.uid $filter[operator] '$user->uid'");
}

/*
 * Add special info to see if a user either authored OR commented on a post
 */
function views_handler_filter_uid_touched($op, $filter, $filterinfo, &$query) {
  if (!module_exist('comment')) { // not much to do in this case
    $query->add_where("node.uid = '%s'", $filter['value']);
    return;
  }

  $table_data = _views_get_tables();
  $joininfo = $table_data['comments']['join'];
  $joininfo['extra'] = array('uid' => $filter['value']);
  $tblnum = $query->add_table('comments', false, 1, $joininfo);
  $table = $query->get_table_name('comments', $tblnum);

  $query->set_distinct(); // once you do this, there's just one node.
  $query->add_where("node.uid = '%s' OR $table.uid = '%s'", $filter['value'], $filter['value']);
}