| 1 |
<?php
|
| 2 |
// $Id: usernode.module,v 1.33 2007/09/11 13:29:13 fago Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Automatic creation of usernodes - one node for each user.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Defines the used content type for the nodes.
|
| 11 |
*
|
| 12 |
* You may change this to your custom nodetype, if you do so you may also remove
|
| 13 |
* the next 6 functions, however reimplementing hook_delete would be suggested.
|
| 14 |
*/
|
| 15 |
define('USERNODE_CONTENT_TYPE', "usernode");
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Usernode custom nodetype
|
| 19 |
*/
|
| 20 |
function usernode_node_info() {
|
| 21 |
return array(
|
| 22 |
'usernode' => array(
|
| 23 |
'name' => t('Usernode'),
|
| 24 |
'module' => 'usernode',
|
| 25 |
'description' => t('Nodes of this type are automatically generated for each user.'),
|
| 26 |
'locked' => TRUE,
|
| 27 |
'has_body' => FALSE,
|
| 28 |
'has_title' => FALSE,
|
| 29 |
)
|
| 30 |
);
|
| 31 |
}
|
| 32 |
|
| 33 |
/*
|
| 34 |
* Implementation of hook_cron()
|
| 35 |
*/
|
| 36 |
function usernode_cron() {
|
| 37 |
if (variable_get('usernode_check_more', FALSE)) {
|
| 38 |
usernode_check_all();
|
| 39 |
}
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Implementation of hook_form(): Construct the usernode form.
|
| 44 |
*/
|
| 45 |
function usernode_form(&$node) {
|
| 46 |
$form['title'] = array(
|
| 47 |
'#type' => 'hidden',
|
| 48 |
'#value' => isset($node->user) ? $node->user->name : $node->title,
|
| 49 |
);
|
| 50 |
|
| 51 |
// temporarily store a newly registering user's user object,
|
| 52 |
// so that usernode_submit() can set the user's actual uid on node creation.
|
| 53 |
if (isset($node->user)) {
|
| 54 |
$form['user'] = array(
|
| 55 |
'#type' => 'value',
|
| 56 |
'#value' => $node->user,
|
| 57 |
);
|
| 58 |
}
|
| 59 |
|
| 60 |
return $form;
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Implementation of hook_access().
|
| 65 |
*/
|
| 66 |
function usernode_access($op, $node) {
|
| 67 |
global $user;
|
| 68 |
|
| 69 |
if ($op == 'create' || $op == 'delete') {
|
| 70 |
return FALSE;
|
| 71 |
}
|
| 72 |
|
| 73 |
if ($op == 'update') {
|
| 74 |
if ( (user_access('edit own usernode') && is_usernode($node))
|
| 75 |
|| user_access('edit usernodes') ) {
|
| 76 |
return TRUE;
|
| 77 |
}
|
| 78 |
}
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
* Implementation of hook_perm().
|
| 83 |
*/
|
| 84 |
function usernode_perm() {
|
| 85 |
return array('edit usernodes', 'edit own usernode');
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Implementation of hook_view().
|
| 90 |
*/
|
| 91 |
function usernode_view($node, $teaser = FALSE, $page = FALSE) {
|
| 92 |
$node = node_prepare($node, $teaser);
|
| 93 |
|
| 94 |
$member = '<dl><dt class="user-member">';
|
| 95 |
$member .= t('Member for') .'</dt><dd class="user-member">';
|
| 96 |
$member .= format_interval(time() - $node->user->created) .'</dl>';
|
| 97 |
|
| 98 |
$node->content['user_member'] = array(
|
| 99 |
'#value' => $member,
|
| 100 |
'#weight' => 0,
|
| 101 |
);
|
| 102 |
|
| 103 |
if (module_exists('nodeprofile') && function_exists('nodeprofile_show_profiles')) {
|
| 104 |
$node->content['nodeprofile'] = nodeprofile_show_profiles($node->uid);
|
| 105 |
$node->content['nodeprofile']['#weight'] = -2;
|
| 106 |
}
|
| 107 |
|
| 108 |
return $node;
|
| 109 |
}
|
| 110 |
|
| 111 |
/**
|
| 112 |
* Implementation of hook_delete().
|
| 113 |
*/
|
| 114 |
function usernode_delete(&$node) {
|
| 115 |
// prevent deletion of usernodes, if a module directly calls node_delete()
|
| 116 |
drupal_set_message('You mustn\'t delete a usernode, so the deletion has been prevented! Delete the '.
|
| 117 |
'according user and the usernode is going to be deleted automatically.', 'error');
|
| 118 |
|
| 119 |
$user = usernode_get_user($node);
|
| 120 |
|
| 121 |
// the node has already been deleted, so we create a new one instead
|
| 122 |
usernode_create_node($user);
|
| 123 |
}
|
| 124 |
|
| 125 |
/**
|
| 126 |
* End of usernode content type implementation
|
| 127 |
*/
|
| 128 |
|
| 129 |
/**
|
| 130 |
* Implementation of hook_user().
|
| 131 |
*/
|
| 132 |
function usernode_user($op, &$edit, &$user, $category = NULL) {
|
| 133 |
switch ($op) {
|
| 134 |
case 'load':
|
| 135 |
usernode_get_node_id($user);
|
| 136 |
break;
|
| 137 |
case 'insert':
|
| 138 |
return usernode_create_node($user);
|
| 139 |
case 'after_update':
|
| 140 |
return $category == 'account' ? usernode_update_node($user) : '';
|
| 141 |
case 'delete':
|
| 142 |
return usernode_delete_node($user);
|
| 143 |
}
|
| 144 |
}
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Implementation of hook_menu().
|
| 148 |
*/
|
| 149 |
function usernode_menu($may_cache) {
|
| 150 |
global $user;
|
| 151 |
|
| 152 |
if ($may_cache) {
|
| 153 |
// prevent manual adding of usernodes
|
| 154 |
$items[] = array('path' => 'node/add/'. USERNODE_CONTENT_TYPE,
|
| 155 |
'callback' => 'usernode_prevent_manual_adding',
|
| 156 |
'access' => user_access('administer nodes'),
|
| 157 |
'type' => MENU_CALLBACK);
|
| 158 |
|
| 159 |
$items[] = array('path' => 'usernode',
|
| 160 |
'callback' => 'usernode_view_own_usernode',
|
| 161 |
'title' => t('My usernode'),
|
| 162 |
'access' => $user->uid && user_access('access content'),
|
| 163 |
'type' => MENU_SUGGESTED_ITEM);
|
| 164 |
return $items;
|
| 165 |
}
|
| 166 |
|
| 167 |
if (!$may_cache && arg(0) == 'node' && is_numeric(arg(1))) {
|
| 168 |
$node = node_load(arg(1));
|
| 169 |
if ($node->nid && $node->type == USERNODE_CONTENT_TYPE && arg(2) == 'delete') {
|
| 170 |
// somebody tries to delete a usernode!
|
| 171 |
drupal_set_message(t("It's not possible to delete a usernode!"), 'error');
|
| 172 |
drupal_goto('');
|
| 173 |
}
|
| 174 |
}
|
| 175 |
}
|
| 176 |
|
| 177 |
/**
|
| 178 |
* Delete the associated node(s). Called by hook_user().
|
| 179 |
*/
|
| 180 |
function usernode_delete_node(&$user) {
|
| 181 |
$node = usernode_get_node($user);
|
| 182 |
|
| 183 |
db_query('DELETE FROM {node} WHERE nid = %d', $node->nid);
|
| 184 |
db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid);
|
| 185 |
db_query('DELETE FROM {usernode} WHERE nid = %d', $node->nid);
|
| 186 |
|
| 187 |
// Don't call hook_delete as we prevent deletion there
|
| 188 |
node_invoke_nodeapi($node, 'delete');
|
| 189 |
|
| 190 |
// Clear the cache so an anonymous poster can see the node being deleted.
|
| 191 |
cache_clear_all();
|
| 192 |
|
| 193 |
// Remove this node from the search index if needed.
|
| 194 |
if (function_exists('search_wipe')) {
|
| 195 |
search_wipe($node->nid, 'node');
|
| 196 |
}
|
| 197 |
}
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Create an associated node. Called by hook_user().
|
| 201 |
*/
|
| 202 |
function usernode_create_node($user) {
|
| 203 |
$node = array(
|
| 204 |
'type' => USERNODE_CONTENT_TYPE,
|
| 205 |
'title' => check_plain($user->name),
|
| 206 |
'user' => $user,
|
| 207 |
);
|
| 208 |
usernode_save_node($node);
|
| 209 |
}
|
| 210 |
|
| 211 |
/*
|
| 212 |
* Saves a usernode by using drupal_execute.
|
| 213 |
* This allows modules like auto_nodetitle to work and makes sure all hooks are invoked properly
|
| 214 |
*/
|
| 215 |
function usernode_save_node($node) {
|
| 216 |
$values = array();
|
| 217 |
|
| 218 |
// workaround to disable drupal message "Your usernode has been created"
|
| 219 |
$messages = drupal_get_messages();
|
| 220 |
|
| 221 |
// create the usernode
|
| 222 |
drupal_execute(USERNODE_CONTENT_TYPE .'_node_form', $values, $node);
|
| 223 |
|
| 224 |
// write back the old messages
|
| 225 |
$_SESSION['messages'] = $messages;
|
| 226 |
}
|
| 227 |
|
| 228 |
/**
|
| 229 |
* Implementation of usernode_submit():
|
| 230 |
* Use the temporarily stored user object of a newly registering user,
|
| 231 |
* so that the usernode is owned by the actual user instead of Anonymous.
|
| 232 |
*/
|
| 233 |
function usernode_submit(&$node) {
|
| 234 |
if (isset($node->user)) {
|
| 235 |
$node->uid = $node->user->uid;
|
| 236 |
}
|
| 237 |
//unpublish the usernode if the user is blocked
|
| 238 |
$node_options = variable_get('node_options_'. USERNODE_CONTENT_TYPE, array('status', 'promote'));
|
| 239 |
if (in_array('status', $node_options)) {
|
| 240 |
$node->status = $node->user->status;
|
| 241 |
}
|
| 242 |
}
|
| 243 |
|
| 244 |
/**
|
| 245 |
* Implementation of hook_insert():
|
| 246 |
* Insert a node/user relationship into the {usernode} table.
|
| 247 |
*/
|
| 248 |
function usernode_insert($node) {
|
| 249 |
db_query("INSERT INTO {usernode} (nid,uid) VALUES(%d,%d)",
|
| 250 |
$node->nid, $node->uid);
|
| 251 |
}
|
| 252 |
|
| 253 |
/**
|
| 254 |
* Update an associated node. Called by hook_user().
|
| 255 |
*/
|
| 256 |
function usernode_update_node($user) {
|
| 257 |
$node = usernode_get_node($user);
|
| 258 |
//make sure the node is associated with the updated $user and save it again,
|
| 259 |
//so the usernode is updated
|
| 260 |
$node->user = $user;
|
| 261 |
usernode_save_node($node);
|
| 262 |
}
|
| 263 |
|
| 264 |
/**
|
| 265 |
* Implementation of hook_load():
|
| 266 |
* Load the user object of the corresponding user into $node->user.
|
| 267 |
*/
|
| 268 |
function usernode_load($node) {
|
| 269 |
$sql = "SELECT uid FROM {usernode} WHERE nid = %d";
|
| 270 |
$uid = db_result(db_query($sql, $node->nid));
|
| 271 |
|
| 272 |
if ($uid) {
|
| 273 |
$additions = array('user' => user_load(array('uid' => $uid)));
|
| 274 |
return $additions;
|
| 275 |
}
|
| 276 |
}
|
| 277 |
|
| 278 |
/**
|
| 279 |
* Return the usernode of the user.
|
| 280 |
* @param $user The user object or the user's uid
|
| 281 |
*/
|
| 282 |
function usernode_get_node(&$user) {
|
| 283 |
if (!is_object($user)) {
|
| 284 |
$nid = usernode_get_node_id($user);
|
| 285 |
return $nid ? node_load($nid) : FALSE;
|
| 286 |
}
|
| 287 |
if (!isset($user->node)) {
|
| 288 |
$nid = usernode_get_node_id($user);
|
| 289 |
$user->node = $nid ? node_load($nid) : FALSE;
|
| 290 |
}
|
| 291 |
return $user->node;
|
| 292 |
}
|
| 293 |
|
| 294 |
/**
|
| 295 |
* Return the id of the usernode of the user.
|
| 296 |
* @param $user The user object or the user's uid
|
| 297 |
*/
|
| 298 |
function usernode_get_node_id(&$user) {
|
| 299 |
static $history = array();
|
| 300 |
|
| 301 |
if (!is_object($user)) {
|
| 302 |
if (!$history[$user]) {
|
| 303 |
$history[$user] = db_result(db_query("SELECT nid FROM {usernode} WHERE uid = %d", $user));
|
| 304 |
}
|
| 305 |
return $history[$user];
|
| 306 |
}
|
| 307 |
|
| 308 |
if (!isset($user->node_id)) {
|
| 309 |
$user->node_id = usernode_get_node_id($user->uid);
|
| 310 |
}
|
| 311 |
return $user->node_id;
|
| 312 |
}
|
| 313 |
|
| 314 |
|
| 315 |
/**
|
| 316 |
* Return the user object of the usernode.
|
| 317 |
*/
|
| 318 |
function usernode_get_user($node) {
|
| 319 |
return $node->user;
|
| 320 |
}
|
| 321 |
|
| 322 |
/**
|
| 323 |
* Return true if the node is the usernode of the given or the current user,
|
| 324 |
* and false otherwise.
|
| 325 |
*/
|
| 326 |
function is_usernode($node, $account = NULL) {
|
| 327 |
global $user;
|
| 328 |
|
| 329 |
if (is_null($account)) {
|
| 330 |
$account = $user;
|
| 331 |
}
|
| 332 |
|
| 333 |
return $account->uid == $node->user->uid;
|
| 334 |
}
|
| 335 |
|
| 336 |
/**
|
| 337 |
* Implementation of hook_enable().
|
| 338 |
*/
|
| 339 |
function usernode_enable() {
|
| 340 |
//when the module is activated, we check the usernodes
|
| 341 |
usernode_check_all();
|
| 342 |
// set the flag to re-check usernodes on cron - so we verfiy usernode creating actually worked
|
| 343 |
variable_set('usernode_check_more', TRUE);
|
| 344 |
if (module_exists('nodefamily')) {
|
| 345 |
nodefamily_content_type_set_max(USERNODE_CONTENT_TYPE, 1);
|
| 346 |
}
|
| 347 |
}
|
| 348 |
|
| 349 |
/**
|
| 350 |
* Check all users for an existing usernode, and create one if necessary.
|
| 351 |
*/
|
| 352 |
function usernode_check_all($limit = 100) {
|
| 353 |
// find usernodes that have been deleted while the module was deactivated
|
| 354 |
$result = db_query("SELECT un.* FROM {usernode} un ".
|
| 355 |
"LEFT JOIN {node} n ON un.nid = n.nid ".
|
| 356 |
"WHERE n.nid IS NULL");
|
| 357 |
|
| 358 |
while ($row = db_fetch_object($result)) {
|
| 359 |
db_query("DELETE FROM {usernode} WHERE nid = %d", $row->nid);
|
| 360 |
}
|
| 361 |
|
| 362 |
// create usernodes for all existing users without a usernode
|
| 363 |
$result = db_query_range("SELECT u.* FROM {users} u ".
|
| 364 |
"LEFT JOIN {usernode} un ON u.uid = un.uid ".
|
| 365 |
"WHERE un.nid IS NULL AND u.uid != 0", 0, $limit);
|
| 366 |
$processed = 0;
|
| 367 |
while ($user = db_fetch_object($result)) {
|
| 368 |
usernode_create_node($user);
|
| 369 |
$processed ++;
|
| 370 |
}
|
| 371 |
if ($processed < $limit) {
|
| 372 |
variable_del('usernode_check_more');
|
| 373 |
return TRUE;
|
| 374 |
}
|
| 375 |
return FALSE;
|
| 376 |
}
|
| 377 |
|
| 378 |
/**
|
| 379 |
* Implementation of hook_form_alter().
|
| 380 |
*/
|
| 381 |
function usernode_form_alter($form_id, &$form) {
|
| 382 |
if ($form_id == USERNODE_CONTENT_TYPE .'_node_form' && !$_POST) {
|
| 383 |
unset($form['delete']);
|
| 384 |
}
|
| 385 |
//If the nodefamily module is activated, make sure that it knows that usernodes are lonely nodes
|
| 386 |
else if ($form_id == 'node_type_form' && module_exists('nodefamily') && $form['#node_type']->type == USERNODE_CONTENT_TYPE ) {
|
| 387 |
nodefamily_content_type_set_max(USERNODE_CONTENT_TYPE, 1);
|
| 388 |
}
|
| 389 |
}
|
| 390 |
|
| 391 |
/**
|
| 392 |
* Implementation of hook_link().
|
| 393 |
*/
|
| 394 |
function usernode_link($type, &$node, $teaser = FALSE) {
|
| 395 |
$links = array();
|
| 396 |
|
| 397 |
if ($type == 'node' && $node->type == USERNODE_CONTENT_TYPE && !$teaser) {
|
| 398 |
if ($node->user->uid && $node->user->name && user_access('access user profiles')) {
|
| 399 |
$links['usernode_user_profile'] = array(
|
| 400 |
'title' => t('View user page'),
|
| 401 |
'href' => 'user/'. $node->user->uid,
|
| 402 |
'attributes' => array('title' => t('View user page.')),
|
| 403 |
);
|
| 404 |
}
|
| 405 |
}
|
| 406 |
return $links;
|
| 407 |
}
|
| 408 |
|
| 409 |
function usernode_prevent_manual_adding() {
|
| 410 |
drupal_set_message(t('You mustn\'t manually add a usernode!'), 'error');
|
| 411 |
drupal_goto('node/add');
|
| 412 |
}
|
| 413 |
|
| 414 |
function usernode_view_own_usernode() {
|
| 415 |
global $user;
|
| 416 |
|
| 417 |
$node = usernode_get_node($user);
|
| 418 |
return node_view($node, FALSE, TRUE, TRUE);
|
| 419 |
}
|
| 420 |
|
| 421 |
/**
|
| 422 |
* @ingroup views
|
| 423 |
*/
|
| 424 |
|
| 425 |
/**
|
| 426 |
* Implementation of hook_views_tables():
|
| 427 |
* Present fields and filters for user data.
|
| 428 |
*/
|
| 429 |
function usernode_views_tables() {
|
| 430 |
$tables['usernode'] = array(
|
| 431 |
'name' => 'usernode',
|
| 432 |
'provider' => 'internal', // won't show up in external list.
|
| 433 |
'join' => array(
|
| 434 |
'left' => array(
|
| 435 |
'table' => 'node',
|
| 436 |
'field' => 'nid'
|
| 437 |
),
|
| 438 |
'right' => array(
|
| 439 |
'field' => 'nid'
|
| 440 |
),
|
| 441 |
'type' => 'inner',
|
| 442 |
),
|
| 443 |
);
|
| 444 |
|
| 445 |
$tables['usernode_users'] = array(
|
| 446 |
'name' => 'users',
|
| 447 |
'provider' => 'internal', // won't show up in external list.
|
| 448 |
'join' => array(
|
| 449 |
'left' => array(
|
| 450 |
'table' => 'usernode',
|
| 451 |
'field' => 'uid'
|
| 452 |
),
|
| 453 |
'right' => array(
|
| 454 |
'field' => 'uid'
|
| 455 |
),
|
| 456 |
),
|
| 457 |
'fields' => array(
|
| 458 |
'name' => array(
|
| 459 |
'name' => t('Usernode: Name'),
|
| 460 |
'handler' => array(
|
| 461 |
'usernode_views_handler_field_username_text' => t('normal text'),
|
| 462 |
'usernode_views_handler_field_username_link' => t('themed userlink'),
|
| 463 |
),
|
| 464 |
'sortable' => true,
|
| 465 |
'uid' => 'uid',
|
| 466 |
'addlfields' => array('uid'),
|
| 467 |
'help' => t('This will display the name of the user.'),
|
| 468 |
),
|
| 469 |
'uid' => array(
|
| 470 |
'name' => t('Usernode: User ID'),
|
| 471 |
'handler' => 'views_handler_field_int',
|
| 472 |
'sortable' => true,
|
| 473 |
'help' => t('Display the user id.'),
|
| 474 |
),
|
| 475 |
'picture' => array(
|
| 476 |
'field' => 'uid',
|
| 477 |
'name' => t('Usernode: Picture'),
|
| 478 |
'handler' => 'views_handler_field_userpic',
|
| 479 |
'sortable' => false,
|
| 480 |
'help' => t('Display the user picture.'),
|
| 481 |
),
|
| 482 |
'mail' => array(
|
| 483 |
'name' => t('Usernode: Email'),
|
| 484 |
'handler' => 'usernode_views_handler_field_email',
|
| 485 |
'sortable' => true,
|
| 486 |
'help' => t('This will display the email address of the user.'),
|
| 487 |
),
|
| 488 |
'status' => array(
|
| 489 |
'name' => t('Usernode: Status'),
|
| 490 |
'handler' => 'usernode_views_handler_field_status',
|
| 491 |
'sortable' => true,
|
| 492 |
'help' => t('This will display whether user is active or blocked.'),
|
| 493 |
),
|
| 494 |
'created' => array(
|
| 495 |
'name' => t('Usernode: Created Time'),
|
| 496 |
'sortable' => true,
|
| 497 |
'handler' => views_handler_field_dates(),
|
| 498 |
'option' => 'string',
|
| 499 |
'help' => t('Display the registration time of a user.'),
|
| 500 |
),
|
| 501 |
'access' => array(
|
| 502 |
'name' => t('Usernode: Last Access Time'),
|
| 503 |
'sortable' => true,
|
| 504 |
'handler' => views_handler_field_dates(),
|
| 505 |
'option' => 'string',
|
| 506 |
'help' => t('Displays when the user has visited the site the last time.'),
|
| 507 |
),
|
| 508 |
'login' => array(
|
| 509 |
'name' => t('Usernode: Last Login Time'),
|
| 510 |
'sortable' => true,
|
| 511 |
'handler' => views_handler_field_dates(),
|
| 512 |
'option' => 'string',
|
| 513 |
'help' => t('Displays when the user has logged in the last time.'),
|
| 514 |
),
|
| 515 |
),
|
| 516 |
'sorts' => array(
|
| 517 |
'name' => array(
|
| 518 |
'name' => t('Usernode: Name'),
|
| 519 |
'help' => t('This allows you to sort alphabetically by usernames.'),
|
| 520 |
),
|
| 521 |
'uid' => array(
|
| 522 |
'name' => t('Usernode: User ID'),
|
| 523 |
'help' => t('This allows you to sort by user id.'),
|
| 524 |
),
|
| 525 |
'mail' => array(
|
| 526 |
'name' => t('Usernode: Email'),
|
| 527 |
'help' => t('This allows you to sort alphabetically by email.'),
|
| 528 |
),
|
| 529 |
'status' => array(
|
| 530 |
'name' => t('Usernode: Status'),
|
| 531 |
'help' => t('This allows you to sort by user status (active/blocked).'),
|
| 532 |
),
|
| 533 |
'created' => array(
|
| 534 |
'name' => t('Usernode: Created Time'),
|
| 535 |
'help' => t('Sort by the registration time of a user.'),
|
| 536 |
),
|
| 537 |
'access' => array(
|
| 538 |
'name' => t('Usernode: Last Access Time'),
|
| 539 |
'help' => t('Sorts by the last access time of a user.'),
|
| 540 |
),
|
| 541 |
'login' => array(
|
| 542 |
'name' => t('Usernode: Last Login Time'),
|
| 543 |
'help' => t('Sorts by the last login time of a user.'),
|
| 544 |
),
|
| 545 |
),
|
| 546 |
'filters' => array(
|
| 547 |
'name' => array(
|
| 548 |
'name' => t('Usernode: Name'),
|
| 549 |
'operator' => 'views_handler_operator_or',
|
| 550 |
'list' => 'views_handler_filter_username',
|
| 551 |
'value-type' => 'array',
|
| 552 |
'help' => t('This allows you to filter by a particular user. You might not find this useful if you have a lot of users.'),
|
| 553 |
),
|
| 554 |
'uid' => array(
|
| 555 |
'name' => t('Usernode: User ID'),
|
| 556 |
'operator' => 'views_handler_operator_gtlt',
|
| 557 |
'help' => t('This allows you to filter by user id.'),
|
| 558 |
),
|
| 559 |
'mail' => array(
|
| 560 |
'name' => t('Usernode: Email'),
|
| 561 |
'operator' => 'views_handler_operator_like',
|
| 562 |
'handler' => 'views_handler_filter_like',
|
| 563 |
'help' => t('This allows you to filter by email address.'),
|
| 564 |
),
|
| 565 |
'status' => array(
|
| 566 |
'name' => t('Usernode: Active'),
|
| 567 |
'operator' => 'views_handler_operator_eqneq',
|
| 568 |
'list' => array(0 => t('blocked'), 1 => t('active')),
|
| 569 |
'help' => t('This allows you to filter by user status (active/blocked).'),
|
| 570 |
),
|
| 571 |
'created' => array(
|
| 572 |
'name' => t('Usernode: Created Time'),
|
| 573 |
'operator' => 'views_handler_operator_gtlt',
|
| 574 |
'value' => views_handler_filter_date_value_form(),
|
| 575 |
'handler' => 'views_handler_filter_timestamp',
|
| 576 |
'option' => 'string',
|
| 577 |
'help' => t('This filter allows usernodes to be filtered by their users\'s creation date. Enter dates in the format: CCYY-MM-DD HH:MM:SS. Enter \'now\' to use the current time. You may enter a delta (in seconds) to the option that will be added to the time; this is most useful when combined with now. If you have the jscalendar module from jstools installed, you can use a popup date picker here.'),
|
| 578 |
),
|
| 579 |
'access' => array(
|
| 580 |
'name' => t('Usernode: Last Access Time'),
|
| 581 |
'operator' => 'views_handler_operator_gtlt',
|
| 582 |
'value' => views_handler_filter_date_value_form(),
|
| 583 |
'handler' => 'views_handler_filter_timestamp',
|
| 584 |
'option' => 'string',
|
| 585 |
'help' => t('This filter allows usernodes to be filtered by their users\'s last access time. Enter dates in the format: CCYY-MM-DD HH:MM:SS. Enter \'now\' to use the current time. You may enter a delta (in seconds) to the option that will be added to the time; this is most useful when combined with now. If you have the jscalendar module from jstools installed, you can use a popup date picker here.'),
|
| 586 |
),
|
| 587 |
'login' => array(
|
| 588 |
'name' => t('Usernode: Last Login Time'),
|
| 589 |
'operator' => 'views_handler_operator_gtlt',
|
| 590 |
'value' => views_handler_filter_date_value_form(),
|
| 591 |
'handler' => 'views_handler_filter_timestamp',
|
| 592 |
'option' => 'string',
|
| 593 |
'help' => t('This filter allows usernodes to be filtered by their users\'s last login time. Enter dates in the format: CCYY-MM-DD HH:MM:SS. Enter \'now\' to use the current time. You may enter a delta (in seconds) to the option that will be added to the time; this is most useful when combined with now. If you have the jscalendar module from jstools installed, you can use a popup date picker here.'),
|
| 594 |
),
|
| 595 |
),
|
| 596 |
);
|
| 597 |
|
| 598 |
$tables['usernode_users_roles'] = array(
|
| 599 |
'name' => 'users_roles',
|
| 600 |
'provider' => 'internal', // won't show up in external list.
|
| 601 |
'join' => array(
|
| 602 |
'left' => array(
|
| 603 |
'table' => 'usernode',
|
| 604 |
'field' => 'uid'
|
| 605 |
),
|
| 606 |
'right' => array(
|
| 607 |
'field' => 'uid'
|
| 608 |
),
|
| 609 |
),
|
| 610 |
'filters' => array(
|
| 611 |
'rid' => array(
|
| 612 |
'name' => t('Usernode: Role'),
|
| 613 |
'operator' => 'views_handler_operator_andor',
|
| 614 |
'list' => 'views_handler_filter_role',
|
| 615 |
'value-type' => 'array',
|
| 616 |
'help' => t('Include the user only if the user is a member of the selected role.'),
|
| 617 |
),
|
| 618 |
),
|
| 619 |
);
|
| 620 |
|
| 621 |
$tables['usernode_role'] = array(
|
| 622 |
'name' => 'role',
|
| 623 |
'provider' => 'internal', // won't show up in external list.
|
| 624 |
'join' => array(
|
| 625 |
'left' => array(
|
| 626 |
'table' => 'usernode_users_roles',
|
| 627 |
'field' => 'rid'
|
| 628 |
),
|
| 629 |
'right' => array(
|
| 630 |
'field' => 'rid'
|
| 631 |
),
|
| 632 |
),
|
| 633 |
);
|
| 634 |
return $tables;
|
| 635 |
}
|
| 636 |
|
| 637 |
/**
|
| 638 |
* Implementation of hook_views_arguments():
|
| 639 |
* Present an argument Role ID.
|
| 640 |
*/
|
| 641 |
function usernode_views_arguments() {
|
| 642 |
$arguments = array(
|
| 643 |
'usernode_rid' => array(
|
| 644 |
'name' => t('Usernode: Role ID'),
|
| 645 |
'handler' => 'views_handler_arg_usernode_rid',
|
| 646 |
'help' => t('The Role ID argument allows to filter usernodes by the specified Role.'),
|
| 647 |
),
|
| 648 |
);
|
| 649 |
return $arguments;
|
| 650 |
}
|
| 651 |
|
| 652 |
/**
|
| 653 |
* Callback for usernode_views_arguments().
|
| 654 |
*/
|
| 655 |
function views_handler_arg_usernode_rid($op, &$query, $argtype, $arg = '') {
|
| 656 |
switch ($op) {
|
| 657 |
case 'summary':
|
| 658 |
$table_data = _views_get_tables();
|
| 659 |
$joininfo = $table_data['usernode_role']['join'];
|
| 660 |
$joininfo['type'] = 'inner';
|
| 661 |
$query->add_table('usernode_role', true, 1, $joininfo);
|
| 662 |
|
| 663 |
$query->add_field('name', 'usernode_role');
|
| 664 |
$query->add_field('rid', 'usernode_role');
|
| 665 |
$fieldinfo['field'] = "usernode_role.name";
|
| 666 |
return $fieldinfo;
|
| 667 |
break;
|
| 668 |
|
| 669 |
case 'sort':
|
| 670 |
$query->add_orderby('usernode_role', 'name', $argtype);
|
| 671 |
break;
|
| 672 |
|
| 673 |
case 'filter':
|
| 674 |
$rid = intval($arg);
|
| 675 |
$table_data = _views_get_tables();
|
| 676 |
$joininfo = $table_data['usernode_users_roles']['join'];
|
| 677 |
$joininfo['type'] = 'inner';
|
| 678 |
$joininfo['extra'] = array('rid' => $rid);
|
| 679 |
|
| 680 |
$query->add_table("usernode_users_roles", true, 1, $joininfo);
|
| 681 |
break;
|
| 682 |
|
| 683 |
case 'link':
|
| 684 |
return l($query->name, "$arg/" . intval($query->rid));
|
| 685 |
|
| 686 |
case 'title':
|
| 687 |
$role = db_fetch_object(db_query("SELECT name FROM {role} WHERE rid = '%d'", $query));
|
| 688 |
return $role->name;
|
| 689 |
}
|
| 690 |
}
|
| 691 |
|
| 692 |
/**
|
| 693 |
* Callback for usernode_views_tables(): user name as plaintext.
|
| 694 |
*/
|
| 695 |
function usernode_views_handler_field_username_text($fieldinfo, $fielddata, $value, $data) {
|
| 696 |
return check_plain($value);
|
| 697 |
}
|
| 698 |
|
| 699 |
/**
|
| 700 |
* Callback for usernode_views_tables(): user name as a link to the usernode.
|
| 701 |
*/
|
| 702 |
function usernode_views_handler_field_username_link($fieldinfo, $fielddata, $value, $data) {
|
| 703 |
if ($value) {
|
| 704 |
$obj->name = $value;
|
| 705 |
$uidfield = $fielddata['tablename'] ."_". $fieldinfo['uid'];
|
| 706 |
$obj->uid = $data->$uidfield;
|
| 707 |
return theme('username', $obj);
|
| 708 |
}
|
| 709 |
}
|
| 710 |
|
| 711 |
/**
|
| 712 |
* Callback for usernode_views_tables(): email address as a link.
|
| 713 |
*/
|
| 714 |
function usernode_views_handler_field_email($fieldinfo, $fielddata, $value, $data) {
|
| 715 |
return l($value, 'mailto:'. $value);
|
| 716 |
}
|
| 717 |
|
| 718 |
/**
|
| 719 |
* Callback for usernode_views_tables(): render status: active or blocked.
|
| 720 |
*/
|
| 721 |
function usernode_views_handler_field_status($fieldinfo, $fielddata, $value, $data) {
|
| 722 |
return $value ? t('active') : t('blocked');
|
| 723 |
}
|
| 724 |
|
| 725 |
/**
|
| 726 |
* Generate a default view: a simple userlisting.
|
| 727 |
*/
|
| 728 |
function usernode_views_default_views() {
|
| 729 |
$view = new stdClass();
|
| 730 |
$view->name = 'userlist';
|
| 731 |
$view->description = 'Provides a user listing using usernodes.';
|
| 732 |
$view->access = array();
|
| 733 |
$view->view_args_php = '';
|
| 734 |
$view->sort = array();
|
| 735 |
$view->argument = array();
|
| 736 |
$view->field = array(
|
| 737 |
array(
|
| 738 |
'tablename' => 'usernode_users',
|
| 739 |
'field' => 'uid',
|
| 740 |
'label' => 'user id',
|
| 741 |
'sortable' => '1',
|
| 742 |
'defaultsort' => 'ASC',
|
| 743 |
),
|
| 744 |
array(
|
| 745 |
'tablename' => 'node',
|
| 746 |
'field' => 'title',
|
| 747 |
'label' => 'username',
|
| 748 |
'handler' => 'views_handler_field_nodelink',
|
| 749 |
'sortable' => '1',
|
| 750 |
),
|
| 751 |
array(
|
| 752 |
'tablename' => 'usernode_users',
|
| 753 |
'field' => 'mail',
|
| 754 |
'label' => 'email',
|
| 755 |
'sortable' => '1',
|
| 756 |
),
|
| 757 |
array(
|
| 758 |
'tablename' => 'usernode_users',
|
| 759 |
'field' => 'created',
|
| 760 |
'label' => 'created',
|
| 761 |
'handler' => 'views_handler_field_date_small',
|
| 762 |
'sortable' => '1',
|
| 763 |
),
|
| 764 |
array(
|
| 765 |
'tablename' => 'usernode_users',
|
| 766 |
'field' => 'access',
|
| 767 |
'label' => 'access',
|
| 768 |
'handler' => 'views_handler_field_date',
|
| 769 |
'sortable' => '1',
|
| 770 |
),
|
| 771 |
array(
|
| 772 |
'tablename' => 'usernode_users',
|
| 773 |
'field' => 'login',
|
| 774 |
'label' => 'login',
|
| 775 |
'handler' => 'views_handler_field_date_large',
|
| 776 |
'sortable' => '1',
|
| 777 |
),
|
| 778 |
);
|
| 779 |
$view->filter = array(
|
| 780 |
array(
|
| 781 |
'tablename' => 'node',
|
| 782 |
'field' => 'type',
|
| 783 |
'operator' => 'OR',
|
| 784 |
'options' => '',
|
| 785 |
'value' => array(
|
| 786 |
0 => 'usernode',
|
| 787 |
),
|
| 788 |
),
|
| 789 |
array(
|
| 790 |
'tablename' => 'usernode_users',
|
| 791 |
'field' => 'status',
|
| 792 |
'operator' => '=',
|
| 793 |
'options' => '',
|
| 794 |
'value' => '1',
|
| 795 |
),
|
| 796 |
array(
|
| 797 |
'tablename' => 'usernode_users_roles',
|
| 798 |
'field' => 'rid',
|
| 799 |
'operator' => 'AND',
|
| 800 |
'options' => '',
|
| 801 |
'value' => array(
|
| 802 |
),
|
| 803 |
),
|
| 804 |
);
|
| 805 |
$view->exposed_filter = array(
|
| 806 |
array(
|
| 807 |
'tablename' => 'usernode_users_roles',
|
| 808 |
'field' => 'rid',
|
| 809 |
'label' => 'User Roles',
|
| 810 |
'optional' => 1,
|
| 811 |
'is_default' => 0,
|
| 812 |
'operator' => 0,
|
| 813 |
'single' => 0,
|
| 814 |
),
|
| 815 |
);
|
| 816 |
$view->requires = array(node, usernode_users, usernode_users_roles);
|
| 817 |
$views[$view->name] = $view;
|
| 818 |
|
| 819 |
return $views;
|
| 820 |
}
|