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

Contents of /contributions/modules/user_tags/user_tags.module

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


Revision 1.15 - (show annotations) (download) (as text)
Mon Feb 12 08:57:52 2007 UTC (2 years, 9 months ago) by stevemckenzie
Branch: MAIN
CVS Tags: DRUPAL-5--2-0, HEAD
Branch point for: DRUPAL-5--2, DRUPAL-6--1
Changes since 1.14: +12 -4 lines
File MIME type: text/x-php
info file for Drupal 5 version
1 <?php // $id:$
2
3 /**
4 * Implementation of hook_help().
5 */
6 function user_tags_help($section) {
7 switch ($section) {
8 case 'admin/help#user_tags':
9 case 'admin/modules#description':
10 return t('Allows users to free tag themselves.');
11 }
12 }
13
14 /**
15 * Implementation of hook_perm().
16 */
17 function user_tags_perm() {
18 return array('administer user tags', 'tag users');
19 }
20
21 function user_tags_menu($may_cache) {
22 $access = 'tag users';
23 $items = array();
24 if ($may_cache) {
25 $items[] = array(
26 'path' => 'tag/user',
27 'title' => t('tag'),
28 'callback' => 'user_tags_page',
29 'access' => $access,
30 'type' => MENU_CALLBACK,
31 );
32 $items[] = array(
33 'path' => 'admin/settings/user_tags',
34 'title' => t('User Tags'),
35 'callback' => 'drupal_get_form',
36 'callback arguments' => array('user_tags_settings'),
37 'access' => user_access('administer user tags'),
38 'type' => MENU_NORMAL_ITEM,
39 );
40 }
41 return $items;
42 }
43
44
45 function user_tags_settings() {
46 if ($_POST['edit']['user_tags_vocabulary_list']) {
47 db_query('DELETE FROM {user_tags_vocabulary}');
48 foreach ($_POST['edit']['user_tags_vocabulary_list'] as $vid) {
49 db_query("INSERT INTO {user_tags_vocabulary} (vid) VALUES (%d)", $vid);
50 }
51 }
52
53 $vocab = db_query("SELECT vid, name, description FROM {vocabulary}");
54 if (db_affected_rows() > 0) {
55 while($vocab_list = db_fetch_object($vocab)) {
56 $options[$vocab_list->vid] = $vocab_list->name;
57 if ($vocab_list->description) {
58 $options[$vocab_list->vid] .=" - ". $vocab_list->description;
59 }
60 }
61 $enabled_vocab = db_query("SELECT vu.vid, v.name FROM {user_tags_vocabulary} vu INNER JOIN {vocabulary} v ON v.vid = vu.vid");
62 while($enabled = db_fetch_object($enabled_vocab)) {
63 $match_options[] = $enabled->vid;
64 }
65 $form["user_tags_vocabulary_list"] = array("#type" => "select",
66 "#multiple" => true,
67 "#required" => true,
68 "#title" => t("Vocabularies Available"),
69 "#description" => t("To select multiple items, hold ctrl (command) and click on the items"),
70 "#options" => $options,
71 "#default_value" => $match_options
72 );
73
74 $form["user_tags_display_options"] = array("#type" => "fieldset",
75 "#collapsible" => true,
76 "#title" => t("Display Options")
77 );
78
79 $form["user_tags_display_options"]["user_tags_user_page"] = array("#type" => "checkbox",
80 "#title" => t("Display tags on user's profile pages (user/username)"),
81 "#default_value" => variable_get("user_tag_user_page", true),
82 "#return_value" => true,
83 );
84
85 } else {
86 drupal_set_message(t("You must add a ". l(t("vocabulary"), "admin/taxonomy") ."before using this module."), "error");
87 }
88
89 return system_settings_form($form);
90 }
91
92
93 function user_tags_user($op, &$edit, &$account, $category = NULL) {
94 switch ($op) {
95 case 'view':
96 $list = array();
97 foreach ($account->taxonomy as $tid => $term) {
98 $list[] = array('value' => l($term->name, "tag/user/$tid"));
99 }
100 if (!empty($list)) {
101 return array('Tags' => $list);
102 }
103 break;
104
105 case 'delete':
106 return user_tags_delete($account->uid);
107 break;
108
109 case 'load':
110 $result = db_query('SELECT t.* FROM {term_data} t INNER JOIN {term_user} tu WHERE tu.tid = t.tid AND tu.uid = %d ORDER BY weight, name', $account->uid);
111 $account->taxonomy = array();
112 while ($term = db_fetch_object($result)) {
113 $account->taxonomy[$term->tid] = $term;
114 }
115 return $account->taxonomy;
116 break;
117
118 case 'categories':
119 return array(array('name' =>'tags',
120 'title' => 'tags',
121 'weight' => 10));
122 break;
123
124
125 case 'form':
126 if ($category != 'tags') {
127 return;
128 }
129
130 if (!isset($account->taxonomy)) {
131 if ($account->uid) {
132 $terms = user_tags_get_terms($account->uid);
133 }
134 else {
135 $terms = array();
136 }
137 }
138 else {
139 $terms = $account->taxonomy;
140 }
141
142 $c = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {user_tags_vocabulary} u ON v.vid = u.vid ORDER BY v.weight, v.name", 'v', 'vid'));
143
144 while ($vocabulary = db_fetch_object($c)) {
145 if ($vocabulary->tags) {
146 $typed_terms = array();
147 foreach ($terms as $term) {
148 // Extract terms belonging to the vocabulary in question.
149 if ($term->vid == $vocabulary->vid) {
150
151 // Commas and quotes in terms are special cases, so encode 'em.
152 if (preg_match('/,/', $term->name) || preg_match('/"/', $term->name)) {
153 $term->name = '"'.preg_replace('/"/', '""', $term->name).'"';
154 }
155 $typed_terms[] = $term->name;
156 }
157 }
158 $typed_string = implode(', ', $typed_terms) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);
159
160 if ($vocabulary->help) {
161 $help = $vocabulary->help;
162 }
163 else {
164 $help = t('A comma-separated list of terms describing this content. Example: funny, bungee jumping, "Company, Inc.".');
165 }
166 $form['taxonomy']['freetags'][$vocabulary->vid] = array('#type' => 'textfield',
167 '#title' => $vocabulary->name,
168 '#description' => $help,
169 '#required' => $vocabulary->required,
170 '#default_value' => $typed_string,
171 '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid,
172 '#weight' => $vocabulary->weight,
173 '#maxlength' => 100,
174 );
175 }
176 else {
177 // Extract terms belonging to the vocabulary in question.
178 $default_terms = array();
179 foreach ($terms as $term) {
180 if ($term->vid == $vocabulary->vid) {
181 $default_terms[$term->tid] = $term;
182 }
183 }
184 $form['taxonomy']['tags'][$vocabulary->vid] = taxonomy_form($vocabulary->vid, array_keys($default_terms), $vocabulary->help);
185 $form['taxonomy']['tags'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
186 $form['taxonomy']['tags'][$vocabulary->vid]['#required'] = $vocabulary->required;
187 }
188
189 if (isset($form['taxonomy'])) {
190 $form['taxonomy'] += array('#type' => 'fieldset', '#title' => t('Categories'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, '#weight' => -3);
191 }
192 }
193 return $form;
194 break;
195
196 case 'submit':
197 if ($category != 'tags') {
198 return;
199 }
200 user_tags_delete($account->uid);
201
202 if (is_array($edit['taxonomy'])) {
203 if (isset($edit['taxonomy']['freetags'])) {
204 foreach ($edit['taxonomy']['freetags'] as $key => $value){
205 $vid = $key;
206 $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
207 preg_match_all($regexp, $value, $matches);
208 $typed_terms = $matches[1];
209 foreach ($typed_terms as $typed_term) {
210 $typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $typed_term));
211 $typed_term = trim($typed_term);
212 if ($typed_term == '') continue;
213 $possibilities = taxonomy_get_term_by_name($typed_term);
214 $typed_term_tid = NULL; // tid match if any.
215 foreach ($possibilities as $possibility) {
216 if ($possibility->vid == $vid) {
217 $typed_term_tid = $possibility->tid;
218 }
219 }
220 if (!$typed_term_tid) {
221 $save_terms = array('vid' => $vid, 'name' => $typed_term);
222 $status = taxonomy_save_term($save_terms);
223 $typed_term_tid = $save_terms['tid'];
224 }
225 db_query('INSERT INTO {term_user} (uid, tid, vid) VALUES (%d, %d, %d)', $account->uid, $typed_term_tid, $vid);
226 }
227 }
228 }
229 if (isset($edit['taxonomy']['tags'])) {
230 foreach ($edit['taxonomy']['tags'] as $vid => $term) {
231 if (is_array($term)) {
232 foreach ($term as $tid) {
233 if ($tid) {
234 db_query('INSERT INTO {term_user} (uid, tid, vid) VALUES (%d, %d, %d)', $account->uid, $tid, $vid);
235 }
236 }
237 }
238 else if ($term) {
239 db_query('INSERT INTO {term_user} (uid, tid, vid) VALUES (%d, %d, %d)', $account->uid, $term, $vid);
240 }
241 }
242 }
243 }
244 unset($edit['taxonomy']);
245 break;
246 }
247 }
248
249 /**
250 * Find all terms associated to the given user, ordered by vocabulary and term weight.
251 */
252 function user_tags_get_terms($uid, $key = 'tid') {
253 static $terms;
254
255 if (!isset($terms[$uid])) {
256 $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_user} r INNER JOIN {term_data} t ON (r.tid = t.tid AND r.vid = t.vid ) WHERE r.uid = %d ORDER BY t.weight, t.name', 't', 'tid'), $uid);
257 $terms[$uid] = array();
258 while ($term = db_fetch_object($result)) {
259 $terms[$uid][$term->$key] = $term;
260 }
261 }
262 return $terms[$uid];
263 }
264
265 function user_tags_delete($uid) {
266 db_query('DELETE FROM {term_user} WHERE uid = %d', $uid);
267 }
268
269 function user_tags_page($tid = null) {
270 global $user;
271 $row = array();
272 $rows = array();
273 $term = taxonomy_get_term($tid);
274
275 if (!$tid || $term->tid != $tid) {
276
277 //print all the tags currently in use.
278 $query = db_query('SELECT DISTINCT( td.name ) as termname, tu.tid FROM {term_data} td INNER JOIN {term_user} tu ON td.tid = tu.tid
279 ORDER BY td.weight, td.name');
280 while ($term = db_fetch_object($query)) {
281 $rows[] = array(l($term->termname, "tag/user/$term->tid"));
282 }
283 return theme('table', array(t('Tags')), $rows);
284 }
285 else {
286 drupal_set_title($term->name);
287 $result = db_query('SELECT tu.tid, td.name as termname, u.uid, u.name as username FROM {users} u INNER JOIN {term_user} t ON u.uid = t.uid
288 AND t.tid = %d INNER JOIN {term_user} tu ON u.uid = tu.uid INNER JOIN {term_data} td ON td.tid = tu.tid
289 ORDER BY u.name, td.weight, td.name', $tid);
290 $old = '';
291 while ($term = db_fetch_object($result)) {
292 if ($term->username != $old->username && !empty($row)) {
293 $rows[] = array(l($old->username, "user/$old->uid"), implode(', ', $row));
294 unset($row); // Cleaning and reinitializing to avoid duplicates.
295 $row = array();
296 }
297 $old = $term;
298 $row[] = l($term->termname, "tag/user/$term->tid");
299 }
300 $rows[] = array(l($old->username, "user/$old->uid"), implode(', ', $row));
301 return theme('table', array(t('Name'), t('Tags')), $rows);
302 }
303 }
304
305 /**
306 * Find all terms associated to the user of a given vocabulary.
307 */
308 function user_tags_get_terms_by_vocabulary($uid, $vid, $key = 'tid') {
309 $result = db_query('SELECT t.* FROM {term_data} t INNER JOIN {term_user} tu WHERE tu.tid = t.tid AND tu.vid = %d AND tu.uid =%d', $vid, $uid);
310 $terms = array();
311 while ($term = db_fetch_object($result)) {
312 $terms[$term->$key] = $term;
313 }
314 return $terms;
315 }

  ViewVC Help
Powered by ViewVC 1.1.2