| 1 |
<?php
|
| 2 |
// $Id: favorite_nodes.module,v 1.5.2.7 2008/03/14 15:27:27 kbahey Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Favorite node module
|
| 7 |
*
|
| 8 |
* This module allows users to add certain nodes to a favorite list. Each node has an "add to favorites" link at the bottom.
|
| 9 |
* This favorite list is visible to others viewing the site.
|
| 10 |
*/
|
| 11 |
|
| 12 |
define('FAVORITE_NODES_NODE_TYPE', 'favorite_nodes_node_type_');
|
| 13 |
define('FAVORITE_NODES_PERM_ADD', 'create favorite nodes');
|
| 14 |
define('FAVORITE_NODES_PERM_VIEW', 'view favorite nodes');
|
| 15 |
define('FAVORITE_NODES_PERM_ADMINISTER', 'administer favorite nodes');
|
| 16 |
define('FAVORITE_NODES_BLOCK', 'favorite_nodes_block_type_');
|
| 17 |
define('FAVORITE_NODES_BLOCK_LIMIT', 'favorite_nodes_block_limit');
|
| 18 |
define('FAVORITE_NODES_BLOCK_TITLE', 'favorite_nodes_block_title');
|
| 19 |
define('FAVORITE_NODES_PAGE_LIMIT', 'favorite_nodes_page_limit');
|
| 20 |
define('FAVORITE_NODES_PROFILE_LIMIT', 'favorite_nodes_profile_limit');
|
| 21 |
define('FAVORITE_NODES_PAGE_TYPE', 'favorite_nodes_page_type');
|
| 22 |
define('FAVORITE_NODES_MENUS', 'favorite_nodes_menu');
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_help().
|
| 26 |
*/
|
| 27 |
function favorite_nodes_help($path, $arg) {
|
| 28 |
switch ($path) {
|
| 29 |
case 'admin/help#favorite_nodes':
|
| 30 |
case 'admin/modules#description':
|
| 31 |
return t('Allows users to manage a favorite list of nodes.');
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
| 35 |
function favorite_nodes_perm() {
|
| 36 |
return array(FAVORITE_NODES_PERM_ADD, FAVORITE_NODES_PERM_VIEW, FAVORITE_NODES_PERM_ADMINISTER);
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Implementation of hook_menu().
|
| 41 |
*/
|
| 42 |
function favorite_nodes_menu() {
|
| 43 |
global $user;
|
| 44 |
$items = array();
|
| 45 |
|
| 46 |
$items['admin/settings/favorite_nodes'] = array(
|
| 47 |
'title' => 'Favorite nodes',
|
| 48 |
'description' => 'Settings for favorite nodes',
|
| 49 |
'page callback' => 'drupal_get_form',
|
| 50 |
'page arguments' => array('favorite_nodes_settings'),
|
| 51 |
'type' => MENU_NORMAL_ITEM,
|
| 52 |
'access arguments' => array(FAVORITE_NODES_PERM_ADMINISTER),
|
| 53 |
);
|
| 54 |
|
| 55 |
$items['favorite_nodes/add'] = array(
|
| 56 |
'page callback' => 'favorite_nodes_add_page',
|
| 57 |
'type' => MENU_CALLBACK,
|
| 58 |
'access arguments' => array(FAVORITE_NODES_PERM_ADD),
|
| 59 |
);
|
| 60 |
|
| 61 |
$items['favorite_nodes/delete'] = array(
|
| 62 |
'page callback' => 'favorite_nodes_delete_page',
|
| 63 |
'type' => MENU_CALLBACK,
|
| 64 |
'access arguments' => array(FAVORITE_NODES_PERM_ADD),
|
| 65 |
);
|
| 66 |
|
| 67 |
$items['favorite_nodes/view'] = array(
|
| 68 |
'page callback' => 'favorite_nodes_view_page',
|
| 69 |
'title' => 'Favorites',
|
| 70 |
'type' => MENU_CALLBACK,
|
| 71 |
'access arguments' => array(FAVORITE_NODES_PERM_VIEW),
|
| 72 |
);
|
| 73 |
|
| 74 |
$items['favorites/view'] = array(
|
| 75 |
'page callback' => 'favorite_nodes_view_page',
|
| 76 |
'type' => MENU_CALLBACK,
|
| 77 |
'access arguments' => array(FAVORITE_NODES_PERM_VIEW),
|
| 78 |
);
|
| 79 |
|
| 80 |
if ($user->uid && variable_get(FAVORITE_NODES_MENUS, 0)) {
|
| 81 |
foreach (_node_types_natcasesort() as $type) {
|
| 82 |
if (variable_get(FAVORITE_NODES_NODE_TYPE . $type->type, 0)) {
|
| 83 |
$items["favorites/view/$user->uid/$type->type"] = array(
|
| 84 |
'title' => 'Favorite @type', array('@type' => $type->name),
|
| 85 |
'page callback' => 'favorite_nodes_view_page',
|
| 86 |
'type' => MENU_NORMAL_ITEM,
|
| 87 |
'access arguments' => array(FAVORITE_NODES_PERM_VIEW),
|
| 88 |
);
|
| 89 |
}
|
| 90 |
}
|
| 91 |
}
|
| 92 |
return $items;
|
| 93 |
}
|
| 94 |
|
| 95 |
/**
|
| 96 |
* Implementation of hook_theme().
|
| 97 |
*/
|
| 98 |
function favorite_nodes_theme() {
|
| 99 |
return array(
|
| 100 |
'favorite_nodes_view_table' => array(
|
| 101 |
'arguments' => array('list' => array(), 'uid' => NULL, 'type' => NULL),
|
| 102 |
),
|
| 103 |
'favorite_nodes_view_teasers' => array(
|
| 104 |
'arguments' => array('list' => array(), 'uid' => NULL, 'type' => NULL),
|
| 105 |
)
|
| 106 |
);
|
| 107 |
}
|
| 108 |
/**
|
| 109 |
* Implementation of hook_xmlrpc().
|
| 110 |
*/
|
| 111 |
function favorite_nodes_xmlrpc() {
|
| 112 |
$items = array();
|
| 113 |
|
| 114 |
$items[] = array(
|
| 115 |
'favorite_nodes.add',
|
| 116 |
'favorite_nodes_add',
|
| 117 |
array('boolean', 'int'),
|
| 118 |
t('Add a favorite node to the current user\'s list.')
|
| 119 |
);
|
| 120 |
}
|
| 121 |
|
| 122 |
function _node_types_natcasesort() {
|
| 123 |
static $node_types;
|
| 124 |
|
| 125 |
if (!isset($node_types)) {
|
| 126 |
$node_types = node_get_types();
|
| 127 |
uasort($node_types, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);'));
|
| 128 |
}
|
| 129 |
return $node_types;
|
| 130 |
}
|
| 131 |
|
| 132 |
/**
|
| 133 |
* Implementation of hook_user().
|
| 134 |
*/
|
| 135 |
function favorite_nodes_user($op, &$edit, &$user, $category = null) {
|
| 136 |
|
| 137 |
switch ($op) {
|
| 138 |
case 'view':
|
| 139 |
$user->content['faveliste'] = array(
|
| 140 |
'#type' => 'user_profile_category',
|
| 141 |
'#title' => t('My favorites'),
|
| 142 |
'#value' => theme('item_list', $items),
|
| 143 |
);
|
| 144 |
$fav_list = array();
|
| 145 |
foreach (_node_types_natcasesort() as $type) {
|
| 146 |
if (variable_get(FAVORITE_NODES_NODE_TYPE . $type->type, 0)) {
|
| 147 |
$uid = intval($user->uid);
|
| 148 |
$favorites = favorite_nodes_get($uid, $type->type, variable_get(FAVORITE_NODES_BLOCK_LIMIT, 5));
|
| 149 |
$items = array();
|
| 150 |
if (!empty($favorites)) {
|
| 151 |
foreach ($favorites as $favorite) {
|
| 152 |
$items[] = l($favorite->title, "node/$favorite->nid");
|
| 153 |
}
|
| 154 |
$user->content['faveliste'][$type->name] = array(
|
| 155 |
'#type' => 'user_profile_item',
|
| 156 |
'#title' => l($type->name, "favorite_nodes/view/$user->uid/$type->type"),
|
| 157 |
'#value' => theme('item_list', $items),
|
| 158 |
);
|
| 159 |
}
|
| 160 |
}
|
| 161 |
}
|
| 162 |
break;
|
| 163 |
|
| 164 |
case 'delete':
|
| 165 |
favorite_nodes_delete_favorites($user->uid);
|
| 166 |
break;
|
| 167 |
}
|
| 168 |
}
|
| 169 |
|
| 170 |
/**
|
| 171 |
* Implementation of hook_nodeapi().
|
| 172 |
*/
|
| 173 |
function favorite_nodes_nodeapi(&$node, $op, $teaser = null, $page = null) {
|
| 174 |
switch ($op) {
|
| 175 |
case 'delete':
|
| 176 |
// Delete all favorite entries of the node being deleted.
|
| 177 |
db_query("DELETE FROM {favorite_nodes} WHERE nid = %d", $node->nid);
|
| 178 |
break;
|
| 179 |
}
|
| 180 |
}
|
| 181 |
|
| 182 |
/**
|
| 183 |
* Implementation of hook_block().
|
| 184 |
*/
|
| 185 |
function favorite_nodes_block($op = 'list', $delta = 0, $edit = array()) {
|
| 186 |
global $user;
|
| 187 |
|
| 188 |
$node_types = _node_types_natcasesort();
|
| 189 |
|
| 190 |
switch ($op) {
|
| 191 |
case 'list':
|
| 192 |
return array(array('info' => t('Favorite nodes')));
|
| 193 |
|
| 194 |
case 'view':
|
| 195 |
if (user_access(FAVORITE_NODES_PERM_VIEW)) {
|
| 196 |
$block = array();
|
| 197 |
$block['subject'] = variable_get(FAVORITE_NODES_BLOCK_TITLE, t('Favorites'));
|
| 198 |
$block['content'] = '';
|
| 199 |
|
| 200 |
foreach ($node_types as $type) {
|
| 201 |
if (variable_get(FAVORITE_NODES_NODE_TYPE . $type->type, 0)) {
|
| 202 |
$uid = intval($user->uid);
|
| 203 |
$favorites = favorite_nodes_get($uid, $type->type, variable_get(FAVORITE_NODES_BLOCK_LIMIT, 5));
|
| 204 |
$items = array();
|
| 205 |
if (!empty($favorites)) {
|
| 206 |
foreach ($favorites as $favorite) {
|
| 207 |
$items[] = l($favorite->title, "node/$favorite->nid");
|
| 208 |
}
|
| 209 |
$block['content'] .= theme('item_list', $items, variable_get(FAVORITE_NODES_BLOCK . $type->type, $type->name));
|
| 210 |
}
|
| 211 |
$sql = "SELECT COUNT(*) FROM {node} n INNER JOIN {favorite_nodes} f USING(nid) WHERE n.type = '%s' AND f.uid = %d ORDER by f.last DESC";
|
| 212 |
$count = db_result(db_query($sql, $type->type, $user->uid));
|
| 213 |
if ($count > variable_get(FAVORITE_NODES_BLOCK_LIMIT, 5)) {
|
| 214 |
$block['content'] .= "<div class=\"more\">\n";
|
| 215 |
$block['content'] .= l(t('More Favorite %types', array('%types' => variable_get(FAVORITE_NODES_BLOCK . $type->type, $type->name))), "favorite_nodes/view/$user->uid/$type->type");
|
| 216 |
$block['content'] .= "</div>\n";
|
| 217 |
}
|
| 218 |
}
|
| 219 |
}
|
| 220 |
}
|
| 221 |
return $block;
|
| 222 |
|
| 223 |
case 'configure':
|
| 224 |
$form = array();
|
| 225 |
$form['titles'] = array(
|
| 226 |
'#type' => 'fieldset',
|
| 227 |
'#title' => t('Titles'),
|
| 228 |
'#collapsible' => true,
|
| 229 |
'#collapsed' => false,
|
| 230 |
);
|
| 231 |
$form['titles']['title'] = array(
|
| 232 |
'#type' => 'textfield',
|
| 233 |
'#title' => t('Block title'),
|
| 234 |
'#size' => 60,
|
| 235 |
'#description' => t('This title will be displayed at the top of the block.'),
|
| 236 |
'#default_value' => variable_get(FAVORITE_NODES_BLOCK_TITLE, t('Favorites')),
|
| 237 |
);
|
| 238 |
$form['limit'] = array(
|
| 239 |
'#type' => 'textfield',
|
| 240 |
'#title' => t('Number of favorites to display'),
|
| 241 |
'#size' => 4,
|
| 242 |
'#description' => t('Up to this many favorites of each type of content will be displayed in the block. If there are no marked favorites of a type, then that type won\'t show up.'),
|
| 243 |
'#default_value' => variable_get(FAVORITE_NODES_BLOCK_LIMIT, 5),
|
| 244 |
);
|
| 245 |
$form['titles']['subtitles'] = array(
|
| 246 |
'#type' => 'fieldset',
|
| 247 |
'#title' => t('Type subtitles'),
|
| 248 |
'#collapsible' => true,
|
| 249 |
'#collapsed' => true,
|
| 250 |
);
|
| 251 |
foreach ($node_types as $type) {
|
| 252 |
if (variable_get(FAVORITE_NODES_NODE_TYPE . $type->type, 0)) {
|
| 253 |
$form['titles']['subtitles'][FAVORITE_NODES_BLOCK . $type->type] = array(
|
| 254 |
'#type' => 'textfield',
|
| 255 |
'#title' => t('Subtitle for the %name content type', array('%name' => $type->name)),
|
| 256 |
'#size' => 60,
|
| 257 |
'#description' => t('Within the block, any links to content of the %name type will be categorized under this subtitle.', array('%name' => $name)),
|
| 258 |
'#default_value' => variable_get(FAVORITE_NODES_BLOCK . $type->type, $type->name),
|
| 259 |
);
|
| 260 |
}
|
| 261 |
}
|
| 262 |
return $form;
|
| 263 |
|
| 264 |
case 'save':
|
| 265 |
variable_set(FAVORITE_NODES_BLOCK_TITLE, $edit['title']);
|
| 266 |
variable_set(FAVORITE_NODES_BLOCK_LIMIT, $edit['limit']);
|
| 267 |
foreach ($node_types as $type) {
|
| 268 |
if (variable_get(FAVORITE_NODES_NODE_TYPE . $type->type, 0)) {
|
| 269 |
variable_set(FAVORITE_NODES_BLOCK . $type->type, $edit[FAVORITE_NODES_BLOCK . $type->type]);
|
| 270 |
}
|
| 271 |
}
|
| 272 |
break;
|
| 273 |
|
| 274 |
default:
|
| 275 |
break;
|
| 276 |
}
|
| 277 |
}
|
| 278 |
|
| 279 |
/**
|
| 280 |
* Implementation of hook_links().
|
| 281 |
*/
|
| 282 |
function favorite_nodes_link($type, $node = null, $teaser = false) {
|
| 283 |
global $user;
|
| 284 |
$links = array();
|
| 285 |
if ($type == 'node' && !$teaser) {
|
| 286 |
if (variable_get(FAVORITE_NODES_NODE_TYPE . $node->type, 0)) {
|
| 287 |
if (user_access(FAVORITE_NODES_PERM_ADD)) {
|
| 288 |
if (!_favorite_nodes_check($node->nid)) {
|
| 289 |
$links[] = array('title' => t('add to favorites'), 'href' => 'favorite_nodes/add/'. $node->nid);
|
| 290 |
}
|
| 291 |
else {
|
| 292 |
if (user_access(FAVORITE_NODES_PERM_VIEW)) {
|
| 293 |
$links[] = array('title' => t('in favorites'));
|
| 294 |
$links[] = array('title' => t('remove from favorites'), 'href' => 'favorite_nodes/delete/'. $node->nid);
|
| 295 |
}
|
| 296 |
}
|
| 297 |
}
|
| 298 |
}
|
| 299 |
}
|
| 300 |
return $links;
|
| 301 |
}
|
| 302 |
|
| 303 |
/**
|
| 304 |
* Settings page for this module.
|
| 305 |
*/
|
| 306 |
function favorite_nodes_settings() {
|
| 307 |
$set = 'page';
|
| 308 |
$form[$set] = array(
|
| 309 |
'#type' => 'fieldset',
|
| 310 |
'#title' => t('Favorites Page Settings'),
|
| 311 |
'#collapsible' => true,
|
| 312 |
'#collapsed' => false,
|
| 313 |
);
|
| 314 |
|
| 315 |
$form[$set][FAVORITE_NODES_PAGE_LIMIT] = array(
|
| 316 |
'#type' => 'textfield',
|
| 317 |
'#title' => t('Favorite Nodes Page Limit'),
|
| 318 |
'#default_value' => variable_get(FAVORITE_NODES_PAGE_LIMIT, 10),
|
| 319 |
'#description' => t('How many items to display on a single page of marked favorites.'),
|
| 320 |
);
|
| 321 |
$form[$set][FAVORITE_NODES_PROFILE_LIMIT] = array(
|
| 322 |
'#type' => 'textfield',
|
| 323 |
'#title' => t('Favorite Nodes Profile Limit'),
|
| 324 |
'#default_value' => variable_get(FAVORITE_NODES_PROFILE_LIMIT, 3),
|
| 325 |
'#description' => t('How many items per type to display on the profile page.'),
|
| 326 |
);
|
| 327 |
$form[$set][FAVORITE_NODES_PAGE_TYPE] = array(
|
| 328 |
'#type' => 'select',
|
| 329 |
'#title' => t('Type of Page Display for Favorite Nodes'),
|
| 330 |
'#options' => array(
|
| 331 |
'table' => 'Table',
|
| 332 |
'teasers' => 'Teasers',
|
| 333 |
),
|
| 334 |
'#default_value' => variable_get(FAVORITE_NODES_PAGE_TYPE, 'table'),
|
| 335 |
'#description' => t('How should favorites be displayed on the favorite nodes page?'),
|
| 336 |
);
|
| 337 |
|
| 338 |
$form[$set][FAVORITE_NODES_MENUS] = array(
|
| 339 |
'#type' => 'checkbox',
|
| 340 |
'#title' => t('Navigation menu items'),
|
| 341 |
'#return_value' => 1,
|
| 342 |
'#default_value' => variable_get(FAVORITE_NODES_MENUS, 0),
|
| 343 |
'#description' => t('Whether to show a menu item in the navigation block for each node type?'),
|
| 344 |
);
|
| 345 |
|
| 346 |
$set = 'types';
|
| 347 |
$form[$set] = array(
|
| 348 |
'#type' => 'fieldset',
|
| 349 |
'#title' => t('Enable favorites for these node types'),
|
| 350 |
'#collapsible' => true,
|
| 351 |
'#collapsed' => false,
|
| 352 |
);
|
| 353 |
foreach (_node_types_natcasesort() as $type) {
|
| 354 |
$form[$set][FAVORITE_NODES_NODE_TYPE . $type->type] = array(
|
| 355 |
'#type' => 'checkbox',
|
| 356 |
'#title' => $type->name,
|
| 357 |
'#return_value' => 1,
|
| 358 |
'#default_value' => variable_get(FAVORITE_NODES_NODE_TYPE . $type->type, 0),
|
| 359 |
);
|
| 360 |
}
|
| 361 |
// $form['#submit'][] = 'example_admin_settings_submit';
|
| 362 |
return system_settings_form($form);
|
| 363 |
|
| 364 |
}
|
| 365 |
|
| 366 |
/**
|
| 367 |
* Add a favorite node.
|
| 368 |
*/
|
| 369 |
function favorite_nodes_add($nid) {
|
| 370 |
global $user;
|
| 371 |
|
| 372 |
$node = node_load($nid);
|
| 373 |
|
| 374 |
if ($node->nid) {
|
| 375 |
db_query("DELETE FROM {favorite_nodes} WHERE nid = %d AND uid = %d", $nid, $user->uid);
|
| 376 |
db_query("INSERT INTO {favorite_nodes} (nid, uid, last) VALUES (%d, %d, %d)", $nid, $user->uid, time());
|
| 377 |
|
| 378 |
return true;
|
| 379 |
}
|
| 380 |
else {
|
| 381 |
return false;
|
| 382 |
}
|
| 383 |
}
|
| 384 |
|
| 385 |
/**
|
| 386 |
* Delete a favorite node.
|
| 387 |
*/
|
| 388 |
function favorite_nodes_delete($nid) {
|
| 389 |
global $user;
|
| 390 |
db_query("DELETE FROM {favorite_nodes} WHERE nid = %d AND uid = %d", $nid, $user->uid);
|
| 391 |
}
|
| 392 |
|
| 393 |
/**
|
| 394 |
* Select all the favorite nodes a user has.
|
| 395 |
* TODO why don't return all node if tpye are nul. So we can display all favorite node on path favorite_nodes/view/"uid"
|
| 396 |
*/
|
| 397 |
function favorite_nodes_get($uid, $type = null, $limit = null) {
|
| 398 |
if (is_null($limit)) {
|
| 399 |
$limit = variable_get(FAVORITE_NODES_PAGE_LIMIT, 10);
|
| 400 |
}
|
| 401 |
$row = array();
|
| 402 |
if ($type && variable_get(FAVORITE_NODES_NODE_TYPE . $type, 0)) {
|
| 403 |
$sql = "SELECT n.nid, n.title, f.uid, f.last FROM {node} n INNER JOIN {favorite_nodes} f ON n.nid = f.nid WHERE n.type = '%s' AND f.uid = %d ORDER by f.last DESC";
|
| 404 |
|
| 405 |
$result = pager_query($sql, $limit, 0, null, $type, $uid);
|
| 406 |
if ($result) {
|
| 407 |
while ($data = db_fetch_object($result)) {
|
| 408 |
$row[$data->nid] = $data;
|
| 409 |
}
|
| 410 |
}
|
| 411 |
}
|
| 412 |
return $row;
|
| 413 |
}
|
| 414 |
|
| 415 |
/**
|
| 416 |
* Page to add a favorite node.
|
| 417 |
*/
|
| 418 |
function favorite_nodes_add_page() {
|
| 419 |
$nid = arg(2);
|
| 420 |
if (is_numeric($nid)) {
|
| 421 |
$result = favorite_nodes_add($nid);
|
| 422 |
if ($result) {
|
| 423 |
drupal_set_message(t('The node was added to your favorites.'));
|
| 424 |
}
|
| 425 |
else {
|
| 426 |
|
| 427 |
}
|
| 428 |
drupal_goto('node/'. $nid);
|
| 429 |
}
|
| 430 |
|
| 431 |
drupal_not_found();
|
| 432 |
}
|
| 433 |
|
| 434 |
/**
|
| 435 |
* Page to delete a favorite node.
|
| 436 |
*/
|
| 437 |
function favorite_nodes_delete_page() {
|
| 438 |
global $user;
|
| 439 |
$nid = arg(2);
|
| 440 |
|
| 441 |
favorite_nodes_delete($nid);
|
| 442 |
drupal_set_message(t('The node was deleted from your favorites'));
|
| 443 |
drupal_goto("user/$user->uid");
|
| 444 |
}
|
| 445 |
|
| 446 |
/**
|
| 447 |
* Page to display a user's favorite list.
|
| 448 |
*/
|
| 449 |
function favorite_nodes_view_page() {
|
| 450 |
|
| 451 |
$uid = arg(2);
|
| 452 |
$type = arg(3);
|
| 453 |
$output = theme('favorite_nodes_view_'. variable_get(FAVORITE_NODES_PAGE_TYPE, 'table'), favorite_nodes_get($uid, $type), $uid, $type);
|
| 454 |
print theme('page', $output);
|
| 455 |
}
|
| 456 |
|
| 457 |
/**
|
| 458 |
* Delete favorite nodes a user has.
|
| 459 |
*/
|
| 460 |
function favorite_nodes_delete_favorites($uid) {
|
| 461 |
db_query("DELETE FROM {favorite_nodes} WHERE uid = %d", $uid);
|
| 462 |
}
|
| 463 |
|
| 464 |
/**
|
| 465 |
* Get all the favorite nodes inner joining the users.
|
| 466 |
* TODO: currently not being used.. do we need this?
|
| 467 |
*/
|
| 468 |
function _favorite_nodes_get_users($nid) {
|
| 469 |
$sql = "SELECT u.*, f.last FROM {users} u INNER JOIN {favorite_nodes} f USING(uid) WHERE f.nid = %d ORDER by f.last DESC";
|
| 470 |
$result = db_query($sql, $nid);
|
| 471 |
$row = array();
|
| 472 |
while ($data = db_fetch_object($result)) {
|
| 473 |
$row[$data->uid] = $data;
|
| 474 |
}
|
| 475 |
|
| 476 |
return $row;
|
| 477 |
}
|
| 478 |
|
| 479 |
/**
|
| 480 |
* Check if a user already has a node in their favorite list.
|
| 481 |
*/
|
| 482 |
function _favorite_nodes_check($nid) {
|
| 483 |
global $user;
|
| 484 |
$sql = "SELECT COUNT(*) FROM {favorite_nodes} WHERE uid = %d AND nid = %d";
|
| 485 |
return db_result(db_query($sql, $user->uid, $nid));
|
| 486 |
}
|
| 487 |
|
| 488 |
/**
|
| 489 |
* TODO: This is not being used.. do we need it?
|
| 490 |
*/
|
| 491 |
function theme_favorite_nodes_view_teasers($list = array(), $uid = null, $type = null) {
|
| 492 |
$type_desc = variable_get(FAVORITE_NODES_BLOCK . $type, $type);
|
| 493 |
|
| 494 |
$user = user_load(array('uid' => $uid));
|
| 495 |
|
| 496 |
if (!$type) {
|
| 497 |
$output .= t('Please select favorite type.');
|
| 498 |
}
|
| 499 |
else {
|
| 500 |
if (!empty($list)) {
|
| 501 |
$output .= '<h2>'. t('Favorite !type for !user', array('!type' => $type_desc, '!user' => theme('username', $user))) ."</h2>\n";
|
| 502 |
foreach ($list as $nid => $data) {
|
| 503 |
$node = node_load($nid);
|
| 504 |
$output .= node_view($node, true);
|
| 505 |
}
|
| 506 |
}
|
| 507 |
else {
|
| 508 |
$output .= t('No favorites of type !type.', array('!type' => $type_desc));
|
| 509 |
}
|
| 510 |
}
|
| 511 |
$output .= theme('pager');
|
| 512 |
|
| 513 |
return $output;
|
| 514 |
}
|
| 515 |
|
| 516 |
/**
|
| 517 |
* Table which displays favorite node lists.
|
| 518 |
*/
|
| 519 |
function theme_favorite_nodes_view_table($list = array(), $uid = null, $type = null) {
|
| 520 |
|
| 521 |
global $user;
|
| 522 |
$account = user_load(array('uid' => $uid));
|
| 523 |
$header = array(t('Title'), t('Added'), t('Operations'));
|
| 524 |
|
| 525 |
$rows = array();
|
| 526 |
if (isset($list)) {
|
| 527 |
foreach ($list as $nid => $data) {
|
| 528 |
$title = array(l($data->title, "node/$nid"), format_date($data->last, 'custom', 'Y-m-d H:i'));
|
| 529 |
if ($user->uid == $data->uid || $user->uid == 1) {
|
| 530 |
$delete = array(l(t('delete'), "favorite_nodes/delete/$nid"));
|
| 531 |
$result = array_merge($title, $delete);
|
| 532 |
}
|
| 533 |
else {
|
| 534 |
$result = $title;
|
| 535 |
}
|
| 536 |
$rows[] = array('data' => $result);
|
| 537 |
}
|
| 538 |
}
|
| 539 |
|
| 540 |
$output .= '<h2>'. t('Favorite !types for !user', array('!type' => variable_get(FAVORITE_NODES_BLOCK . $type, node_get_types('name', $type)), '!user' => theme('username', $account))) ."</h2>\n";
|
| 541 |
$output .= theme('table', $header, $rows);
|
| 542 |
$output .= theme('pager');
|
| 543 |
|
| 544 |
return $output;
|
| 545 |
}
|
| 546 |
|
| 547 |
function favorite_nodes_views_tables() {
|
| 548 |
$tables['favorite_nodes'] = array(
|
| 549 |
'name' => 'favorite_nodes',
|
| 550 |
'provider' => 'internal',
|
| 551 |
'join' => array(
|
| 552 |
'left' => array(
|
| 553 |
'table' => 'node',
|
| 554 |
'field' => 'nid',
|
| 555 |
),
|
| 556 |
'right' => array(
|
| 557 |
'field' => 'nid',
|
| 558 |
),
|
| 559 |
),
|
| 560 |
'fields' => array(
|
| 561 |
'last' => array(
|
| 562 |
'name' => t('Favorite Nodes: Time Added'),
|
| 563 |
'sortable' => true,
|
| 564 |
'handler' => views_handler_field_dates(),
|
| 565 |
'option' => 'string',
|
| 566 |
'help' => t('Display the date/time the favorite node was added.'),
|
| 567 |
),
|
| 568 |
'count' => array(
|
| 569 |
'name' => t('Favorite Nodes: Count'),
|
| 570 |
'handler' => 'favorite_nodes_handler_user_count',
|
| 571 |
'help' => t('Number of times this node was added to favorites by any user.'),
|
| 572 |
'sortable' => FALSE,
|
| 573 |
'notafield' => TRUE,
|
| 574 |
),
|
| 575 |
),
|
| 576 |
'sorts' => array(
|
| 577 |
'last' => array(
|
| 578 |
'name' => t('Favorite Nodes: Time Added'),
|
| 579 |
'help' => t('Sort by the date/time the favorite node was added.'),
|
| 580 |
),
|
| 581 |
),
|
| 582 |
'filters' => array(
|
| 583 |
'uid' => array(
|
| 584 |
'field' => 'uid',
|
| 585 |
'name' => t('Favorite Nodes: User ID'),
|
| 586 |
'operator' => 'views_handler_operator_eqneq',
|
| 587 |
'handler' => 'views_handler_filter_favorite_nodes',
|
| 588 |
'list-type' => 'select',
|
| 589 |
'list' => array(
|
| 590 |
'uid_current' => t('Currently Logged In User'),
|
| 591 |
'uid_all' => t('All Users'),
|
| 592 |
),
|
| 593 |
'help' => t('This allows you to filter based on favorites nodes.'),
|
| 594 |
),
|
| 595 |
'last' => array(
|
| 596 |
'name' => t('Favorite Nodes: Time Added'),
|
| 597 |
'operator' => 'views_handler_operator_gtlt',
|
| 598 |
'value' => views_handler_filter_date_value_form(),
|
| 599 |
'handler' => 'views_handler_filter_timestamp',
|
| 600 |
'option' => 'string',
|
| 601 |
'help' => t('This filter allows favorite nodes to be filtered by the date and time the user added them. 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.'),
|
| 602 |
),
|
| 603 |
),
|
| 604 |
);
|
| 605 |
return $tables;
|
| 606 |
}
|
| 607 |
|
| 608 |
function views_handler_filter_favorite_nodes($op, $filter, $filterinfo, &$query) {
|
| 609 |
global $user;
|
| 610 |
|
| 611 |
$table_data = _views_get_tables();
|
| 612 |
$joininfo = $table_data['favorite_nodes']['join'];
|
| 613 |
$joininfo['type'] = 'inner';
|
| 614 |
$table_num = $query->add_table('favorite_nodes', FALSE, 1, $joininfo);
|
| 615 |
$table = $query->get_table_name('favorite_nodes', $table_num);
|
| 616 |
$query->set_distinct();
|
| 617 |
|
| 618 |
switch ($filter['value']) {
|
| 619 |
case 'uid_current':
|
| 620 |
if ($user->uid) {
|
| 621 |
$query->add_where("$table.uid = $user->uid");
|
| 622 |
}
|
| 623 |
break;
|
| 624 |
|
| 625 |
case 'uid_all':
|
| 626 |
// No special processing needed, since we do an inner join anyways
|
| 627 |
break;
|
| 628 |
}
|
| 629 |
}
|
| 630 |
|
| 631 |
function favorite_nodes_views_arguments() {
|
| 632 |
$args['user_id'] = array(
|
| 633 |
'name' => t("Favorite Nodes: User ID"),
|
| 634 |
'handler' => 'favorite_nodes_handler_argument_uid',
|
| 635 |
'help' => t('User ID for Favorite node.'),
|
| 636 |
);
|
| 637 |
return $args;
|
| 638 |
}
|
| 639 |
|
| 640 |
function favorite_nodes_handler_argument_uid($op, &$query, $argtype, $arg = '') {
|
| 641 |
switch ($op) {
|
| 642 |
case 'sort':
|
| 643 |
// no luck using add_orderby method.
|
| 644 |
$query->orderby[] = ' num_nodes '. $argtype;
|
| 645 |
break;
|
| 646 |
case 'filter':
|
| 647 |
list($and_or, $uids) = _views_break_phrase($arg);
|
| 648 |
$and_or = drupal_strtoupper($and_or);
|
| 649 |
// Similar to taxonomy AND/OR query.
|
| 650 |
|
| 651 |
if ($and_or == 'OR') {
|
| 652 |
$query->ensure_table('favorite_nodes');
|
| 653 |
$cond = array_fill(0, count($uids), "favorite_nodes.uid = %d");
|
| 654 |
$query->add_where(implode(" $and_or ", $cond), $uids);
|
| 655 |
}
|
| 656 |
else {
|
| 657 |
foreach ((array)$uids as $uid) {
|
| 658 |
$num = $query->add_table('favorite_nodes');
|
| 659 |
$tablename = $query->get_table_name('favorite_nodes', $num);
|
| 660 |
$query->add_where("$tablename.uid = %d", $uid);
|
| 661 |
}
|
| 662 |
}
|
| 663 |
break;
|
| 664 |
}
|
| 665 |
}
|
| 666 |
|
| 667 |
function favorite_nodes_handler_user_count($fieldinfo, $fielddata, $value, $data) {
|
| 668 |
return db_result(db_query("SELECT COUNT(*) FROM {favorite_nodes} WHERE nid = %d", $data->nid));
|
| 669 |
}
|
| 670 |
|