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

Contents of /contributions/modules/taxonomy_user/taxonomy_user.module

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


Revision 1.4 - (show annotations) (download) (as text)
Sat Dec 16 17:02:13 2006 UTC (2 years, 11 months ago) by alexb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +85 -4 lines
File MIME type: text/x-php
taxonomy_user/term page allows to list only nodes tagged by the current user
1 <?php
2
3 /**
4 * taxonomy_user - stores which users tagged what with a shared free tagging vocabulary
5 */
6 define('TU_POS_LINKS', 0);
7 define('TU_POS_TOP', 2);
8 define('TU_POS_BOTTOM', 4);
9
10 /**
11 * implementation of hook_help()
12 */
13 function taxonomy_user_help($section) {
14
15 switch ($section) {
16 case 'admin/modules#description':
17 return t('Relates users to tags on nodes. The tags of the current user will be highlighted. Requires taxonomy module.
18 Once installed, go to admin/categories and enable "Relate terms and users" for the vocabularies of your choice.
19 WARNING: Use fresh vocabulary. For more information, consult README file.');
20 }
21 }
22
23 /**
24 * settings hook
25 */
26 function taxonomy_user_settings() {
27 $form = array();
28
29 $form['taxonomy_user_tag_pos'] = array(
30 '#type' => 'select',
31 '#title' => t('Position of terms'),
32 '#default_value' => variable_get('taxonomy_user_tag_pos', TU_POS_BOTTOM),
33 '#options' => array(TU_POS_LINKS => 'With all other terms', TU_POS_TOP => 'Text top', TU_POS_BOTTOM => 'Text bottom'),
34 '#description' => t('Select where user taxonomy terms should show up.
35 Note that if you select "With all other terms", terms of the current user and terms of all
36 other users will not be set apart.'),
37 );
38 return $form;
39 }
40
41 /**
42 * Implementation of hook_taxonomy().
43 */
44 function taxonomy_user_taxonomy($op, $type, $object = NULL) {
45
46 switch ($op) {
47 case 'form':
48 switch ($type) {
49 case 'vocabulary':
50 return array('userterms' => array('#type' => 'checkboxes',
51 '#title' => t('User terms'),
52 '#default_value' => variable_get('taxonomy_user_vocab_'.$object['vid'], 0),
53 '#options' => array(1 => t('Relate terms and users (Only works if free tagging enabled)')),
54 '#description' => t('Relates terms in the vocabulary to the users that associated them to a node.'),
55 '#weight' => 1,
56 ));
57 break;
58 }
59 break;
60 case 'update':
61 case 'insert':
62 switch ($type) {
63 case 'vocabulary':
64 if ($object['tags'] == 1) {
65 variable_set('taxonomy_user_vocab_'.$object['vid'], $object['userterms'][1]);
66 if ($object['userterms'][1]) {
67 db_query('UPDATE {vocabulary} SET module = "taxonomy_user" WHERE vid = %d', $object['vid']);
68 }
69 else {
70 db_query('UPDATE {vocabulary} SET module = "taxonomy" WHERE vid = %d', $object['vid']);
71 }
72 }
73 else {
74 variable_set('taxonomy_user_vocab_'.$object['vid'], 0);
75 db_query('UPDATE {vocabulary} SET module = "taxonomy" WHERE vid = %d', $object['vid']);
76 }
77 break;
78 }
79 break;
80 case 'delete':
81 switch ($type) {
82 case 'vocabulary':
83 variable_del('taxonomy_user_vocab_'.$object['vid']);
84 break;
85 }
86 break;
87 }
88 }
89
90 /**
91 * implementation of hook_form_alter()
92 */
93 function taxonomy_user_form_alter($form_id, &$form) {
94
95 if ($form_id == 'taxonomy_form_vocabulary') {
96 $form['submit']['#weight'] = 2;
97 $form['delete']['#weight'] = 2;
98 }
99 else if (strpos($form_id, '_node_form') !== false) {
100
101 global $user;
102
103 if (isset($form['taxonomy']['tags'])) {
104
105 // check each free tagging category, if marked for user taxonomy -
106 // if so, get all according terms from term_node_user and sort out those that are not this user's terms
107 foreach ($form['taxonomy']['tags'] as $vid => $tags) {
108
109 if (variable_get('taxonomy_user_vocab_'.$vid, 0)) {
110
111 if (isset($form['nid']['#value'])) {
112 if ($uterms = taxonomy_user_load(array('nid' => $form['nid']['#value'], 'vid' => $vid))) {
113
114 $this_users_tags = array();
115 $tags_others = array();
116 $other_users_tags = array();
117
118 foreach ($uterms as $uterm) {
119 // if a tag is from this user, show it in textarea, if not, hide it in a hidden form field
120 if ($uterm->uid == $user->uid) {
121 $this_users_tags[] = $uterm->name;
122 }
123 else {
124 $other_users_tags[$uterm->tid] = $uterm->name;
125 $tags_others[] = array( 'uid' => $uterm->uid,
126 'tid' => $uterm->tid,
127 'name' => $uterm->name);
128 }
129 }
130
131 $form['taxonomy']['tags_others'][$vid] = array( '#type' => 'value',
132 '#value' => $tags_others,
133 );
134
135 $form['taxonomy']['tags'][$vid]['#default_value'] = implode(", ", $this_users_tags);
136
137 if (count($other_users_tags) > 0) {
138 $form['taxonomy']['tags'][$vid]['#description'] .= '<div class="tags others">'.t('Other people tagged').': '.implode(', ', $other_users_tags).'</div>';
139 }
140 }
141 }
142 }
143 }
144 }
145 }
146 }
147
148 /**
149 * Implementation of hook_nodeapi().
150 */
151 function taxonomy_user_nodeapi(&$node, $op, $arg = 0) {
152
153 switch ($op) {
154 case 'load':
155 taxonomy_user_node_load($node);
156 break;
157 case 'submit':
158 taxonomy_user_node_submit($node);
159 break;
160 case 'insert':
161 case 'update':
162 taxonomy_user_node_save($node);
163 break;
164 case 'delete':
165 taxonomy_user_node_delete($node->nid);
166 break;
167 case 'view':
168 taxonomy_user_node_view($node);
169 break;
170 }
171 }
172
173 /**
174 * implementation of hook_block
175 */
176 function taxonomy_user_block($op = 'list', $delta = O, $edit = array()) {
177
178 if ($op == 'view') {
179 switch($delta)
180 {
181 case 0:
182 if (module_exist('tagadelic')) {
183 $blocks['subject'] = t('My tags');
184 $blocks['content'] = taxonomy_user_my_tag_cloud();
185 }
186 break;
187 }
188 }
189 elseif ($op == 'list') {
190 $blocks[0]['info'] = t('taxonomy_user user tag cloud - requires tagadelic module');
191 }
192 return $blocks;
193 }
194
195 /**
196 * loads a tid/nid/uid triple
197 * $args can contain one or several of the following values
198 * Array(
199 * 'tid' => int(11),
200 * 'nid' => int(11),
201 * 'uid' => int(11),
202 * )
203 */
204 function taxonomy_user_load($args = array()) {
205
206 if (count($args) < 1) {
207 return FALSE;
208 }
209 if (isset($args['tid'])) {
210 $where[] = 'tnu.tid = %d';
211 $whereargs[] = $args['tid'];
212 }
213 if (isset($args['nid'])) {
214 $where[] = 'tnu.nid = %d';
215 $whereargs[] = $args['nid'];
216 }
217 if (isset($args['uid']) && ($args['uid'] != 0)) {
218 $where[] = 'tnu.uid = %d';
219 $whereargs[] = $args['uid'];
220 }
221 if (isset($args['vid'])) {
222 $where[] = 'td.vid = %d';
223 $whereargs[] = $args['vid'];
224 }
225 $result = db_query('SELECT tnu.tid, td.vid, tnu.uid, tnu.nid,
226 td.name, td.description, td.weight
227 FROM {term_node_user} tnu
228 JOIN {term_data} td ON td.tid = tnu.tid
229 WHERE '.implode(' AND ', $where).' ORDER BY td.vid ASC, td.name ASC', $whereargs );
230 while ($tnu = db_fetch_object($result)) {
231 $return[] = $tnu;
232 }
233 return $return;
234 }
235
236 /**
237 * save node's user terms
238 */
239 function taxonomy_user_node_save(&$node) {
240 taxonomy_user_node_delete($node->nid);
241
242 foreach($node->taxonomy_user as $uterm) {
243 if (!$inserted[$uterm->tid][$uterm->uid]) {
244 $inserted[$uterm->tid][$uterm->uid] = TRUE;
245 db_query('INSERT INTO {term_node_user} (nid, tid, uid) VALUES (%d, %d, %d)', $node->nid, $uterm->tid, $uterm->uid);
246 }
247 }
248 }
249
250 /**
251 * intercept node here before it goes to taxonomy.module
252 */
253 function taxonomy_user_node_submit(&$node) {
254
255 global $user;
256
257 if (isset($node->taxonomy['tags'])) {
258 foreach ($node->taxonomy['tags'] as $vid => $vid_value) {
259 if (variable_get('taxonomy_user_vocab_'.$vid, 0)) {
260
261 // the following is taken from taxonomy.module - duplication - not the ideal solution, alas.
262
263 // This regexp allows the following types of user input:
264 // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar
265 $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
266 preg_match_all($regexp, $vid_value, $matches);
267 $typed_terms = array_unique($matches[1]);
268
269 $inserted = array();
270 foreach ($typed_terms as $typed_term) {
271 // If a user has escaped a term (to demonstrate that it is a group,
272 // or includes a comma or quote character), we remove the escape
273 // formatting so to save the term into the DB as the user intends.
274 $typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $typed_term));
275 $typed_term = trim($typed_term);
276 if ($typed_term == "") { continue; }
277
278 // See if the term exists in the chosen vocabulary
279 // and return the tid, otherwise, add a new record.
280 $possibilities = taxonomy_get_term_by_name($typed_term);
281 $typed_term_tid = NULL; // tid match if any.
282 foreach ($possibilities as $possibility) {
283 if ($possibility->vid == $vid) {
284 $typed_term_tid = $possibility->tid;
285 }
286 }
287
288 if (!$typed_term_tid) {
289 $edit = array('vid' => $vid, 'name' => $typed_term);
290 $status = taxonomy_save_term($edit);
291 $typed_term_tid = $edit['tid'];
292 }
293
294 // stick stub of term on node for saving in taxonomy_user_save_node()
295 $new_term = new stdClass();
296 $new_term->tid = $typed_term_tid;
297 $new_term->uid = $user->uid;
298 $node->taxonomy_user[] = $new_term;
299 }
300 // now pluck off other people's tags and stick it onto the node->taxonomy_user variable as well
301 if (is_array($node->taxonomy['tags_others'][$vid])) {
302 foreach($node->taxonomy['tags_others'][$vid] as $term) {
303 $new_term = new stdClass();
304 $new_term->tid = $term['tid'];
305 $new_term->uid = $term['uid'];
306 $node->taxonomy_user[] = $new_term;
307 // merge with original 'tags' array, so that taxonomy.module saves those tags as well
308 $node->taxonomy['tags'][$vid] .= ",".$term['name'];
309 }
310 }
311 // this would hide user terms from taxonomy module - buggy
312 // unset($node->taxonomy['tags'][$vid]);
313 }
314 }
315 }
316 // unset our temp variable here, otherwise taxonomy.module takes it as yet another term category
317 unset($node->taxonomy['tags_others']);
318 }
319
320 /**
321 * delete all term/user associations for a given node
322 */
323 function taxonomy_user_node_delete($nid) {
324
325 db_query('DELETE FROM {term_node_user} WHERE nid = %d', $nid);
326 }
327
328 /**
329 * Renders user taxonomy for a given node
330 */
331 function taxonomy_user_node_view(&$node) {
332
333 if (!is_array($node->taxonomy_user)) {
334 return;
335 }
336
337 // TU_SHOW_LINKS_INLINE options needs to get a settings page implemented....
338 $tagpos = variable_get('taxonomy_user_tag_pos', TU_POS_BOTTOM);
339 if (($tagpos == TU_POS_TOP) || ($tagpos == TU_POS_BOTTOM)) {
340 // take out those terms from taxonomy link list, that are already
341 // in taxonomy user
342 if (is_array($node->taxonomy)) {
343 foreach($node->taxonomy_user as $term) {
344 if (array_key_exists($term->tid, $node->taxonomy)) {
345 unset($node->taxonomy[$term->tid]);
346 }
347 }
348 }
349 $themed_links = theme('taxonomy_user_inline_link', $node);
350 if (isset($node->teaser)) {
351 if ($tagpos == TU_POS_TOP) {
352 $node->teaser = $themed_links.$node->teaser;
353 }
354 else {
355 $node->teaser = $node->teaser.$themed_links;
356 }
357 }
358 if (isset($node->body)) {
359 if ($tagpos == TU_POS_TOP) {
360 $node->body = $themed_links.$node->body;
361 }
362 else {
363 $node->body = $node->body.$themed_links;
364 }
365 }
366 }
367 }
368
369 /**
370 * Load user taxonomy for a given node
371 */
372 function taxonomy_user_node_load(&$node) {
373
374 $node->taxonomy_user = array();
375 if ($uterms = taxonomy_user_load(array('nid' => $node->nid))) {
376 global $user;
377 foreach ($uterms as $uterm) {
378 // do not user $uterm->tid as key, two users could have the tagged with the same $uterm->tid!
379 $node->taxonomy_user[] = $uterm;
380 }
381 }
382 }
383
384 /**
385 * callback function for taxonomy_term_path()
386 */
387 function taxonomy_user_term_path($term) {
388 global $user;
389 if ($term->uid == $user->uid) {
390 return 'taxonomy_user/term/'.$term->tid;
391 }
392 return 'taxonomy/term/'.$term->tid;
393 }
394
395 /**
396 * theme function for links
397 */
398 function theme_taxonomy_user_inline_link($node) {
399 global $user;
400 if (count($node->taxonomy_user) != 0) {
401 $myterms = array();
402 $otherterms = array();
403 foreach ($node->taxonomy_user as $uterm) {
404 if ($uterm->uid == $user->uid) {
405 $myterms[] = l($uterm->name, taxonomy_term_path($uterm), array('rel' => 'tag', 'title' => strip_tags($uterm->description), 'class' => 'myterm'));
406 }
407 else {
408 $otherterms[$uterm->tid] = l($uterm->name, taxonomy_term_path($uterm), array('rel' => 'tag', 'title' => strip_tags($uterm->description), 'class' => 'otherterm'));
409 }
410 }
411 }
412 if (count($myterms)) {
413 $output = '<div class="links myterms">';
414 $output .= '<div class="title">'.t('My tags:').'</div>';
415 $output .= implode(' | ', $myterms);
416 $output .= '</div>';
417 }
418 if (count($otherterms)) {
419 $output .= '<div class="links otherterms">';
420 if ($user->uid != 0) {
421 $output .= '<div class="title">'.t('Other people tagged').':</div>';
422 }
423 else {
424 $output .= '<div class="title">'.t('Tags').':</div>';
425 }
426 $output .= implode(' | ', $otherterms);
427 $output .= '</div>';
428 }
429
430 return $output;
431 }
432
433 /**
434 * builds a tag cloud of tags of current user
435 */
436 function taxonomy_user_my_tag_cloud() {
437
438 global $user;
439 if ($user->uid == 0) {
440 return;
441 }
442
443 $result = db_query("SELECT COUNT( * ) AS count, td.tid, td.name, td.vid, tnu.uid
444 FROM {term_data} td
445 INNER JOIN {term_node_user} tnu ON tnu.tid = td.tid
446 INNER JOIN {node} n ON n.nid = tnu.nid
447 WHERE tnu.uid = %d
448 GROUP BY td.tid, td.vid
449 ORDER BY count DESC", $user->uid);
450
451 $tags = tagadelic_build_weighted_tags($result);
452 return theme('tagadelic_weighted',tagadelic_sort_tags($tags));
453 }
454
455 /**
456 * menu callback - shows a list of nodes tagged by the current user
457 */
458 function taxonomy_user_page() {
459
460 $tid = func_get_arg(0);
461 if (!is_numeric($tid)) {
462 return $tid;
463 return drupal_not_found();
464 }
465
466 global $user;
467
468 if ($term = taxonomy_get_term($tid)) {
469 $query = db_rewrite_sql( 'SELECT n.nid
470 FROM {node} n
471 JOIN {term_node_user} tnu ON tnu.nid = n.nid
472 WHERE n.status = 1
473 AND tnu.uid = %d
474 AND tnu.tid = %d
475 ORDER BY n.sticky DESC, n.created DESC',
476 'n', 'nid'
477 );
478 $result = pager_query( $query, variable_get('default_nodes_main', 10), 0, NULL, $user->uid, $tid);
479 if (db_num_rows($result) > 0) {
480 while ($node = db_fetch_object($result)) {
481 $output .= node_view(node_load(array('nid' => $node->nid)), true);
482 }
483 }
484 else {
485 $output .= t('There are currently no posts in this category.');
486 }
487 if ($user->uid != 0) {
488 drupal_set_title(t('Content you tagged %termname', array('%termname' => $term->name)));
489 }
490 else {
491 drupal_set_title(t('Content tagged %termname', array('%termname' => $term->name)));
492 }
493 }
494 else {
495 $output .= t('Given category does not exist.');
496 drupal_set_title(t('No such category'));
497 }
498 return $output;
499 }
500
501 /**
502 * implementation of hook_menu()
503 */
504 function taxonomy_user_menu($may_cache) {
505 $items = array();
506
507 if ($may_cache) {
508 $items[] = array('path' => 'taxonomy_user/term',
509 'title' => t('Taxonomy user'),
510 'callback' => 'taxonomy_user_page',
511 'access' => user_access('access content'),
512 'type' => MENU_CALLBACK,
513 );
514 }
515 return $items;
516 }

  ViewVC Help
Powered by ViewVC 1.1.2