/[drupal]/contributions/modules/userlink/userlink.module
ViewVC logotype

Contents of /contributions/modules/userlink/userlink.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Mon Feb 23 18:32:09 2009 UTC (9 months ago) by marcp
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.5: +32 -24 lines
File MIME type: text/x-php
Fixed a few menu issues.
1 <?php
2 // $Id: userlink.module,v 1.4.2.3 2007/06/20 17:32:47 marcp Exp $
3
4 /*
5 * @file
6 * Enables storing and retrieving of userlinks (or bookmarks).
7 */
8
9 /**
10 * Implementation of hook_node_info().
11 */
12 function userlink_node_info() {
13 return array(
14 'userlink' => array(
15 'name' => t('Userlink'),
16 'module' => 'userlink',
17 'description' => t("Enables storing and retrieving of userlinks (or bookmarks)."),
18 )
19 );
20 }
21
22 /**
23 * Implementation of hook_perm().
24 */
25 function userlink_perm() {
26 return array(
27 'create userlinks',
28 'view all userlinks',
29 'edit all userlinks',
30 'view own userlinks',
31 'edit own userlinks'
32 );
33 }
34
35 /**
36 * Implementation of hook_access().
37 */
38 function userlink_access($op, $node, $account) {
39 if ($op == 'create') {
40 return user_access('create userlinks', $account);
41 }
42
43 if ($op == 'update' || $op == 'delete') {
44 if (user_access('edit all userlinks', $account) || (user_access('edit own userlinks', $account) && ($account->uid == $node->uid))) {
45 return TRUE;
46 }
47 }
48 }
49
50 /**
51 * Implementation of hook_user().
52 */
53 function userlink_user($type, &$edit, &$user) {
54 if ($type == 'view' && user_access('view all userlinks', $user)) {
55 $items['userlinks'] = array(
56 'title' => t('Links'),
57 'value' => l(t("View recent links"), "userlink/$user->uid/recent", array('title' => t("Read @username's links.", array('@username' => $user->name)))),
58 'class' => 'userlink',
59 );
60 return array(t('History') => $items);
61 }
62 }
63
64 /**
65 * Implementation of hook_help().
66 */
67 function userlink_help($path, $arg) {
68 switch ($path) {
69 case 'admin/help#userlink':
70 return t("
71 <p>The userlink module allows registered users to maintain a list of links (or bookmarks).</p>
72 <p>A userlink is simply a URL along with a title and description.</p>");
73 case 'node/add#userlink':
74 return t("A userlink is a URL along with a title and description.");
75 }
76 }
77
78 /**
79 * Menu callback; displays a Drupal page containing recent blog entries.
80 */
81 function userlink_page($a = NULL, $b = NULL, $c = NULL) {
82
83 if (is_numeric($a)) { // $a is a user ID
84 return userlink_page_user($a, $b, $c);
85 }
86 else {
87 return userlink_page_last();
88 }
89 }
90
91 function userlink_recent() {
92 if (arg(1)) {
93 if (is_numeric(arg(1))) {
94 return userlink_page_user(arg(1));
95 }
96 else if (arg(1) == 'all') {
97 return userlink_page_last();
98 }
99 }
100
101 return 'no links to display';
102 }
103
104 /**
105 *
106 */
107 function userlink_homepage() {
108 global $user;
109 if ($user->uid == 0) {
110 return userlink_page_last();
111 }
112 else {
113 drupal_goto("userlink/$user->uid");
114 }
115 }
116
117 function userlink_page_termlinks($user, $tid) {
118 if (is_string($user) && ($user == 'all')) {
119 return userlink_page_user('all', 'term', $tid);
120 }
121 else {
122 return userlink_page_user($user->uid, 'term', $tid);
123 }
124 }
125
126 /**
127 * Displays a Drupal page containing userlink entries of a given user.
128 */
129 function userlink_page_user($uid, $command = NULL, $tid = NULL) {
130 global $user;
131
132 if ($uid) {
133 if (is_numeric($uid)) {
134 $name = userlink_name_from_uid($uid);
135 }
136
137 $output = '';
138
139 if ($tid == NULL) {
140 $tid = arg(3);
141 }
142
143 if ($command == 'term') {
144 $nodes_main = variable_get('default_nodes_main', 10);
145 if (is_numeric($uid)) {
146 $result = pager_query(db_rewrite_sql("SELECT n.nid, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = %d AND n.type = 'userlink' AND n.uid = %d ORDER BY n.sticky DESC, n.created DESC"), $nodes_main, 0, NULL, $tid, $uid);
147 }
148 else {
149 $result = pager_query(db_rewrite_sql("SELECT n.nid, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = %d AND n.type = 'userlink' ORDER BY n.sticky DESC, n.created DESC"), $nodes_main, 0, NULL, $tid);
150 }
151
152 $num_rows = 0;
153 while ($node = db_fetch_object($result)) {
154 $num_rows++;
155 $output .= node_view(node_load($node->nid), 1);
156 }
157 if ($num_rows > 0) {
158 $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
159 }
160 else {
161 $output = $name ? t("@name has not created any userlinks.", array('@name' => $name)) : t("There are no userlinks in the system.");
162 }
163 }
164 else {
165 if (($uid == $user->uid) && user_access('create userlinks')) {
166 $output .= '<ul><li>'. l(t('Add new link.'), "node/add/userlink") .'</li></ul>';
167 }
168
169 $result = pager_query(db_rewrite_sql("SELECT n.nid, n.sticky, n.created FROM {node} n WHERE type = 'userlink' AND n.uid = %d ORDER BY n.sticky DESC, n.created DESC"), variable_get('default_nodes_main', 10), 0, NULL, $uid);
170
171 $num_rows = 0;
172 while ($node = db_fetch_object($result)) {
173 $num_rows++;
174 $output .= node_view(node_load($node->nid), 1);
175 }
176
177 if ($num_rows > 0) {
178 $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
179 }
180 else {
181 $output = t("@name has not created any userlinks.", array('@name' => $name));
182 }
183 }
184
185 return $output;
186 }
187 else {
188 drupal_not_found();
189 }
190 }
191
192 /**
193 * Displays a Drupal page listing all users along with a link to their userlinks...
194 */
195 function userlink_page_everyone() {
196 $output = "<div class=\"help\"><p>Click on a user's name to see his/her links, or ". l('click here', 'userlink/all') ." to see all links in the system.</p></div>\n";
197
198 $header = array(
199 array('data' => t('Username'), 'field' => 'u.name', 'sort' => 'asc'),
200 array('data' => t('# of links'), 'field' => 'numlinks')
201 );
202 $sql = 'SELECT u.uid AS uid, u.name AS name, COUNT(*) AS numlinks FROM {node} n INNER JOIN {userlink} ul ON n.vid = ul.vid INNER JOIN {users} u ON n.uid = u.uid GROUP BY u.uid';
203 $sql .= tablesort_sql($header);
204 $sqlcount = 'SELECT COUNT(DISTINCT n.uid) FROM {node} n INNER JOIN {userlink} ul ON n.vid = ul.vid';
205 $result = pager_query($sql, 50, 0, $sqlcount);
206
207 while ($account = db_fetch_object($result)) {
208 $rows[] = array(l($account->name, 'userlink/'. $account->uid), $account->numlinks);
209 }
210
211 $output .= theme('table', $header, $rows);
212 $output .= theme('pager', NULL, 50, 0);
213 return $output;
214 }
215
216 /**
217 * Displays a Drupal page containing recent userlink entries of all users.
218 */
219 function userlink_page_last() {
220 global $user;
221
222 $output = '';
223
224 $result = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'userlink' ORDER BY n.created DESC"), variable_get('default_nodes_main', 10));
225
226 $num_rows = 0;
227 while ($node = db_fetch_object($result)) {
228 $num_rows++;
229 $output .= node_view(node_load($node->nid), 1);
230 }
231
232 if ($num_rows > 0) {
233 $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
234 }
235
236 if (drupal_strlen($output) == 0) {
237 $output = 'There are no links in the system.';
238 }
239
240 return $output;
241 }
242
243 function userlink_page_links() {
244 $output = '';
245
246 // Add the javascript for collapsible fieldsets...
247 drupal_add_js('misc/collapse.js');
248
249 drupal_add_css(drupal_get_path('module', 'userlink') .'/userlink.css');
250
251 if (arg(1)) {
252 $uid = arg(1);
253
254 $basesql = "SELECT n.nid AS nid, tn.tid AS tid, td.name AS name FROM {node} n
255 INNER JOIN {term_node} tn ON n.nid = tn.nid
256 INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink'";
257
258 if (arg(1) == 'all') {
259 $sql = $basesql ." ORDER BY name";
260 $result = db_query(db_rewrite_sql($sql));
261 }
262 else {
263 $sql = $basesql ." AND n.uid = %d ORDER BY name";
264 $result = db_query(db_rewrite_sql($sql), $uid);
265 $name = userlink_name_from_uid($uid);
266 }
267
268 $group = '';
269 $last_group = '';
270 $num_in_group = 0;
271
272 while ($row = db_fetch_object($result)) {
273 $node = node_load($row->nid);
274
275 if ($row->name != $last_group) {
276 if ($last_group != '') {
277 $group .= "</ul>";
278 $output .= theme('fieldset', array('#title' => $last_group, '#children' => $group, '#attributes' => array('class' => 'collapsible userlink-group')));
279 }
280
281 $group = "<ul>\n";
282
283 $last_group = $row->name;
284 $num_in_group = 0;
285 }
286 $group .= '<li>'. l($node->title, 'node/'. $node->nid) ."</li>\n";
287 $num_in_group++;
288 }
289 if ($num_in_group > 0) {
290 $group .= "</ul>";
291 $output .= theme('fieldset', array('#title' => $last_group, '#children' => $group, '#attributes' => array('class' => 'collapsible userlink-group')));
292 }
293 }
294
295 if (drupal_strlen($output) == 0) {
296 $output = t('there are no links to display');
297 }
298
299 $output = '<form>'. $output .'</form>';
300 return $output;
301 }
302
303 function userlink_page_category() {
304 $header = array(
305 array('data' => t('Category'), 'field' => 'name'),
306 array('data' => t('# of links'), 'field' => 'numlinks', 'sort' => 'desc')
307 );
308
309 $uid = 0;
310
311 if (arg(1) && is_numeric(arg(1))) {
312 $uid = arg(1);
313
314 $sql = "SELECT tn.tid AS tid, td.name AS name, COUNT(*) AS numlinks FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink' AND n.uid = $uid GROUP BY tn.tid";
315 $sql .= tablesort_sql($header);
316 $sqlcount = "SELECT COUNT(DISTINCT tn.tid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink' AND n.uid = $uid";
317
318 $name = userlink_name_from_uid($uid);
319 }
320 else {
321 $sql = "SELECT tn.tid AS tid, td.name AS name, COUNT(*) AS numlinks FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink' GROUP BY tn.tid";
322 $sql .= tablesort_sql($header);
323 $sqlcount = "SELECT COUNT(DISTINCT tn.tid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink'";
324 }
325
326 $result = pager_query($sql, 50, 0, $sqlcount);
327
328 while ($category = db_fetch_object($result)) {
329 if ($uid) {
330 $url = 'userlink/'. $uid .'/term/'. $category->tid;
331 }
332 else {
333 $url = 'userlink/all/term/'. $category->tid;
334 }
335
336 $rows[] = array(l($category->name, $url), $category->numlinks);
337 }
338
339 $output = theme('table', $header, $rows);
340 $output .= theme('pager', NULL, 50, 0);
341 return $output;
342 }
343
344 /**
345 * Implementation of hook_form().
346 */
347 function userlink_form(&$node) {
348 $form['url'] = array('#type' => 'textfield', '#title' => t('URL'), '#required' => TRUE, '#default_value' => $node->url ? $node->url : 'http://', '#description' => t('The URL for this link (usually begins with http://).'), '#weight' => -50);
349 $form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title, '#description' => t('The title of this link.'), '#weight' => -30);
350 $form['body'] = array(
351 '#type' => 'textarea', '#title' => t('Description'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE, '#weight' => -20
352 );
353
354 return $form;
355 }
356
357 /**
358 * Implementation of hook_view().
359 */
360 function userlink_view(&$node, $teaser = FALSE, $page = FALSE) {
361 drupal_add_css(drupal_get_path('module', 'userlink') .'/userlink.css');
362
363 if ($page) {
364 // Breadcrumb navigation
365 $breadcrumb[] = l(t('Home'), NULL);
366 $breadcrumb[] = l(t('Links'), 'userlink/all' );
367 $breadcrumb[] = l(t("@name's links", array('@name' => $node->name)), 'userlink/'. $node->uid);
368
369 drupal_set_breadcrumb($breadcrumb);
370 }
371
372 $node = node_prepare($node, $teaser);
373
374 // If the url is longer than X characters, just display the first X...
375 $url = userlink_trim_url($node->url);
376
377 // The link should always set a target window -- we are saving it
378 // on a site-wide basis at this point...
379 $atts = array('target' => variable_get('userlink_behavior', '_self'));
380
381 // If there is a globally defined prefix, put it in the node's content...
382 if (variable_get('userlink_prefix_yesno', 0)) {
383 $prefix_name = variable_get('userlink_prefix_name', 'Full story: ');
384 $node->content['url-prefix'] = array(
385 '#weight' => 0,
386 '#prefix' => "<div class=\"userlink-prefix\">",
387 '#value' => $prefix_name,
388 '#suffix' => "</div>");
389 }
390
391 // By default, put the url above the body...
392 $node->content['url'] = array(
393 '#weight' => 1,
394 '#prefix' => "<div class=\"userlink-url\">",
395 '#value' => l($url, $node->url, array('attributes' => $atts)),
396 '#suffix' => "</div>");
397
398 // Modify the weight of the body to drop it down...
399 $node->content['body']['#weight'] = 2;
400
401 return $node;
402 }
403
404 /**
405 * Implementation of hook_menu().
406 */
407 function userlink_menu() {
408 global $user;
409 $items = array();
410
411 $items['admin/settings/userlink'] = array(
412 'title' => 'userlink',
413 'description' => 'Describes what the settings generally do.',
414 'page callback' => 'drupal_get_form',
415 'page arguments' => array('userlink_admin_settings'),
416 'access arguments' => array('administer site configuration'),
417 'type' => MENU_NORMAL_ITEM
418 );
419
420
421 $items['links'] = array(
422 'title' => 'links',
423 'page callback' => 'userlink_page',
424 'access callback' => TRUE,
425 'access arguments' => array('access content'),
426 'type' => MENU_SUGGESTED_ITEM
427 );
428 $items['userlink/homepage'] = array(
429 'title' => 'recent links',
430 'page callback' => 'userlink_homepage',
431 'access arguments' => array('view all userlinks') /*|| user_access('view own userlinks')*/,
432 'type' => MENU_CALLBACK
433 );
434
435 $items['userlink/%user' ] = array(
436 'title callback' => '_userlink_term_title',
437 'title arguments' => array(1),
438 'page callback' => 'userlink_page_links',
439 'access arguments' => array('view all userlinks'),
440 'type' => MENU_CALLBACK
441 );
442
443 $items['userlink/all'] = array(
444 'title' => 'all links...',
445 'page callback' => 'userlink_page_links',
446 'access arguments' => array('view all userlinks'),
447 'type' => MENU_CALLBACK
448 );
449
450 $items['userlink/%user/list'] = array(
451 'title' => 'by category',
452 'page callback' => 'userlink_page_links',
453 'access arguments' => array('view all userlinks'),
454 'type' => MENU_DEFAULT_LOCAL_TASK,
455 'weight' => -20
456 );
457
458 $items['userlink/all/list'] = array(
459 'title' => 'by category',
460 'page callback' => 'userlink_page_links',
461 'access arguments' => array('view all userlinks'),
462 'type' => MENU_DEFAULT_LOCAL_TASK,
463 'weight' => -20
464 );
465
466 $items['userlink/%user/categories'] = array(
467 'title' => 'just categories',
468 'page callback' => 'userlink_page_category',
469 'access callback' => TRUE,
470 'type' => MENU_LOCAL_TASK,
471 'weight' => -1
472 );
473
474 $items['userlink/all/categories'] = array(
475 'title' => 'just categories',
476 'page callback' => 'userlink_page_category',
477 'access callback' => TRUE,
478 'type' => MENU_LOCAL_TASK,
479 'weight' => -1
480 );
481
482 $items['userlink/all/everyone'] = array(
483 'title' => 'other users',
484 'page callback' => 'userlink_page_everyone',
485 'access arguments' => array('view all userlinks'),
486 'type' => MENU_LOCAL_TASK,
487 'weight' => 20
488 );
489
490 $items['userlink/%user/recent'] = array(
491 'title' => 'recent links',
492 'page callback' => 'userlink_recent',
493 'access arguments' => array('view all userlinks'),
494 'type' => MENU_LOCAL_TASK,
495 'weight' => 10
496 );
497
498 //
499 // If we are looking at a listing of links for a given term, add
500 // another tab for the listing...
501 //
502 //if (arg(2) == 'term' && is_numeric(arg(3)))
503 $items['userlink/%user/term/%'] = array(
504 'title callback' => '_userlink_term_title',
505 'title arguments' => array(1, 3),
506 'page callback' => 'userlink_page_termlinks',
507 'page arguments' => array(1, 3),
508 'access arguments' => array('view all userlinks'),
509 'type' => MENU_LOCAL_TASK,
510 'weight' => 80
511 );
512
513 $items['userlink/all/term/%'] = array(
514 'title callback' => '_userlink_all_term_title',
515 'title arguments' => array(3),
516 'page callback' => 'userlink_page_termlinks',
517 'page arguments' => array('all', 3),
518 'access arguments' => array('view all userlinks'),
519 'type' => MENU_LOCAL_TASK,
520 'weight' => 85
521 );
522
523 return $items;
524 }
525
526 function _userlink_term_title($user, $tid = NULL) {
527 if ($tid) {
528 $term = taxonomy_get_term($tid);
529 return t("@who's @termname links", array('@who' => $user->name, '@termname' => $term->name));
530 }
531 else {
532 return t("@who's links", array('@who' => $user->name));
533 }
534 }
535
536 function _userlink_all_term_title($tid) {
537 $term = taxonomy_get_term($tid);
538 return t("All @termname links", array('@termname' => $term->name));
539 }
540
541 /**
542 * Implementation of hook_block().
543 *
544 */
545 function userlink_block($op = 'list', $delta = 0) {
546 global $user;
547 if ($op == 'list') {
548 $block[0]['info'] = t('All Links');
549 $block[1]['info'] = t('My Links');
550 $block[2]['info'] = t('Recent Links');
551 $block[3]['info'] = t('Popular Link Categories');
552 $block[4]['info'] = t("User's Link Categories");
553 $block[5]['info'] = t("Link Menu");
554 return $block;
555 }
556 else if ($op == 'view') {
557 if (($delta == 0) && user_access('view all userlinks')) {
558 $block['content'] = userlink_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, ul.url FROM {node} n INNER JOIN {userlink} ul ON n.vid = ul.vid WHERE n.type = 'userlink' ORDER BY n.title"), 0, 10));
559 $block['content'] .= '<div class="more-link">'.
560 l(t('more'), 'links', array('title' => t('See all the links.'))) .'</div>';
561 $block['subject'] = t('All links');
562 return $block;
563 }
564 else if (($delta == 1) && $user->uid && user_access('view own userlinks')) {
565 $block['content'] = userlink_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, ul.url FROM {node} n INNER JOIN {userlink} ul ON n.vid = ul.vid WHERE n.type = 'userlink' AND n.uid = $user->uid ORDER BY n.title"), 0, 10));
566 $block['content'] .= '<div class="more-link">'.
567 l(t('more'), 'userlink/'. $user->uid, array('title' => t('See all the links.'))) .'</div>';
568 $block['subject'] = t('My Links');
569 return $block;
570 }
571 else if (($delta == 2) && user_access('view all userlinks')) {
572 $block['content'] = userlink_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, ul.url FROM {node} n INNER JOIN {userlink} ul ON n.vid = ul.vid WHERE n.type = 'userlink' ORDER BY n.changed DESC"), 0, 10));
573 $block['content'] .= '<div class="more-link">'.
574 l(t('more'), 'links', array('title' => t('See all the links.'))) .'</div>';
575 $block['subject'] = t('Recent Links');
576 return $block;
577 }
578 else if (($delta == 3) && user_access('view all userlinks')) {
579 $block['content'] = userlink_category_list(db_query_range(db_rewrite_sql("SELECT tn.tid AS tid, td.name AS name, COUNT(*) AS numlinks FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink' GROUP BY tn.tid ORDER BY numlinks DESC"), 0, 10));
580 $block['content'] .= '<div class="more-link">'.
581 l(t('more'), 'userlink/all/categories', array('title' => t('See all the link categories.'))) .'</div>';
582 $block['subject'] = t('Popular Link Categories');
583 return $block;
584 }
585 else if (($delta == 4) && user_access('view all userlinks')) {
586 // This block shows "the target user's" userlinks. If we
587 // are on a page looking at some user's links, by category
588 // or otherwise, we want to show that user's categories.
589 //
590 // Also, if we are looking at a single userlink node, we
591 // want to show that node's owner's userlink categories.
592
593 if (arg(0) == 'userlink') {
594 if (is_numeric(arg(1)) || arg(1) == 'homepage') {
595 $uid = arg(1) == 'homepage' ? $user->uid : arg(1);
596 }
597 }
598 else if ($node = menu_get_object()) {//if ((arg(0) == 'node') && is_numeric(arg(1))) {
599 //$node = node_load(arg(1));
600 if ($node->type == 'userlink') {
601 $uid = $node->uid;
602 }
603 }
604
605 if (!$uid || ($uid == 'all')) {
606 if ($user->uid) {
607 $uid = $user->uid;
608 }
609 }
610
611 if ($uid) {
612 $name = userlink_name_from_uid($uid);
613 $block['content'] = userlink_category_list(db_query_range(db_rewrite_sql("SELECT tn.tid AS tid, td.name AS name, COUNT(*) AS numlinks FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.uid = $uid AND n.type = 'userlink' GROUP BY tn.tid ORDER BY numlinks DESC"), 0, 5), $uid, $name);
614 $block['content'] .= '<div class="more-link">'.
615 l(t('more'), 'userlink/'. $uid, array('title' => t("See @name's link categories.", array('@name' => $name)))) .'</div>';
616
617 if ($uid == $user->uid) {
618 $block['subject'] = t("My Link Categories");
619 }
620 else {
621 $block['subject'] = t("@name's Link Categories", array('@name' => $name));
622 }
623 return $block;
624 }
625 }
626 else if (($delta == 5) && user_access('access content')) {
627 if (user_access('create userlinks')) {
628 $items[] = l("Add a link", 'node/add/userlink');
629 }
630 if ($user->uid) {
631 $items[] = l('View my links', "userlink/$user->uid");
632 $items[] = l('View my categories', "userlink/$user->uid/categories");
633 }
634 if (user_access('view all userlinks')) {
635 $items[] = l("View everyone's links", 'userlink/all');
636 }
637 if (user_access('view all userlinks')) {
638 $items[] = l("View everyone's categories", 'userlink/all/categories');
639 }
640 $items[] = l('View all users', "userlink/all/everyone");
641 $block['content'] = theme('item_list', $items);
642 $block['subject'] = t('Link Menu');
643 return $block;
644 }
645 }
646 }
647
648 /**
649 * Implementation of hook_load().
650 */
651 function userlink_load($node) {
652 $userlink = db_fetch_object(db_query("SELECT url, private FROM {userlink} WHERE vid = %d", $node->vid));
653 return $userlink;
654 }
655
656 /**
657 * Implementation of hook_delete().
658 */
659 function userlink_delete($node) {
660 db_query("DELETE FROM {userlink} WHERE nid = %d", $node->nid);
661 }
662
663 /**
664 * Implementation of hook_insert().
665 */
666 function userlink_insert($node) {
667 db_query("INSERT INTO {userlink} (nid, vid, url, private) VALUES (%d, %d, '%s', %d)",
668 $node->nid, $node->vid, $node->url, $node->private);
669 }
670
671 /**
672 * Implementation of hook_update().
673 */
674 function userlink_update($node) {
675 if ($node->revision) {
676 db_query("INSERT INTO {userlink} (nid, vid, url, private) VALUES (%d, %d, '%s', %d)",
677 $node->nid, $node->vid, $node->url, $node->private);
678 }
679 else {
680 db_query("UPDATE {userlink} SET url = '%s', private = %d WHERE vid = %d",
681 $node->url, $node->private, $node->vid);
682 }
683 }
684
685 /**
686 * Gather a listing of links to userlinks.
687 *
688 * @param $result
689 * A DB result object from a query to fetch node objects.
690 * @param $title
691 * A heading for the resulting list.
692 *
693 * @return
694 * An HTML list suitable as content for a block.
695 */
696 function userlink_list($result, $title = NULL) {
697 // The link should always set a target window -- we are saving it
698 // on a site-wide basis at this point...
699 $atts = array('target' => variable_get('userlink_behavior', '_self'));
700
701 while ($node = db_fetch_object($result)) {
702 $atts['title'] = $node->url;
703 $items[] = l($node->title, $node->url, array('attributes' => $atts));
704 }
705
706 return theme('item_list', $items, $title);
707 }
708
709 /**
710 * Gather a listing of links to userlink categories.
711 *
712 * @param $result
713 * A DB result object from a query to fetch node objects.
714 * @param $title
715 * A heading for the resulting list.
716 *
717 * @return
718 * An HTML list suitable as content for a block.
719 */
720 function userlink_category_list($result, $uid = 0, $name = NULL) {
721 while ($category = db_fetch_object($result)) {
722 if ($uid) {
723 $url = 'userlink/'. $uid .'/term/'. $category->tid;
724 }
725 else {
726 $url = 'userlink/all/term/'. $category->tid;
727 }
728 $items[] = l($category->name, $url) ." <span class=\"userlink-count\">($category->numlinks)</span>";
729 }
730
731 return theme('item_list', $items);
732 }
733
734 /**
735 * Return list of vids that describe userlink entries...
736 *
737 */
738 function userlink_vids() {
739 $a = array();
740
741 $result = db_query("SELECT vid FROM {vocabulary_node_types} WHERE type = 'userlink'");
742 while ($row = db_fetch_array($result)) {
743 $a[] = $row['vid'];
744 }
745
746 return $a;
747 }
748
749 function userlink_name_from_uid($uid) {
750 $result = db_query("SELECT name FROM {users} u WHERE u.uid = '%i' ", $uid);
751 return db_result($result);
752 }
753
754 function userlink_trim_url($url, $len = 70) {
755 if (drupal_strlen($url) > $len) {
756 $url = substr($url, 0, $len) .'...';
757 }
758
759 return $url;
760 }
761
762 function userlink_admin_settings() {
763
764 $form['userlink_behavior'] = array('#type' => 'select',
765 '#options' => array('_self' => 'link appears in the same window',
766 '_blank' => 'link appears in its own target window',
767 'link-target' => 'link appears in a single target window'),
768 '#title' => t('What happens when a link is clicked'),
769 '#default_value' => variable_get('userlink_behavior', '_self'),
770 '#description' => t("The window where the linked page will appear after it is clicked on a user's page."));
771
772 $form['userlink_prefix_yesno'] = array('#type' => 'checkbox',
773 '#title' => t('Show a link prefix?'),
774 '#default_value' => variable_get('userlink_prefix_yesno', 0),
775 '#description' => t("Select to prefix the userlink with a caption."));
776
777 $form['userlink_prefix_name'] = array('#type' => 'textfield',
778 '#title' => t('Prefix caption text'),
779 '#size' => 25,
780 '#default_value' => variable_get('userlink_prefix_name', 'Full story: '),
781 '#description' => t("Enter the prefix text that will be displayed before the link text."),
782 '#maxlength' => 25,
783 '#required' => FALSE,
784 '#field_prefix' => 'Text: ');
785
786 return system_settings_form($form);
787 }
788
789 /**
790 * Implementation of hook_views_tables().
791 *
792 */
793 function userlink_views_tables() {
794 $tables['userlink'] = array(
795 'name' => 'userlink',
796 'provider' => 'internal',
797 'join' => array(
798 'left' => array(
799 'table' => 'node',
800 'field' => 'vid'
801 ),
802 'right' => array(
803 'field' => 'vid'
804 )
805 ),
806 'fields' => array(
807 'url' => array(
808 'name' => t('Userlink: url'),
809 'handler' => array(
810 'userlink_views_handler_url_as_link' => t('As Link'),
811 'userlink_views_handler_url_not_as_link' => t('Without Link'),
812 ),
813 'sortable' => TRUE,
814 'help' => t('Display the url for this userlink'),
815 ),
816 ),
817 'sorts' => array(
818 'url' => array(
819 'name' => t('Userlink: url'),
820 'help' => t('Sort by url'),
821 ),
822 ),
823 );
824
825 return $tables;
826 }
827
828 function userlink_views_handler_url_as_link($fieldinfo, $fielddata, $value, $data) {
829 return l($value, $value);
830 }
831
832 function userlink_views_handler_url_not_as_link($fieldinfo, $fielddata, $value, $data) {
833 return check_plain($value);
834 }

  ViewVC Help
Powered by ViewVC 1.1.2