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

Diff of /contributions/modules/user_tags/user_tags.module

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

revision 1.8.2.1, Sun Dec 10 23:46:05 2006 UTC revision 1.8.2.2, Tue May 6 17:35:27 2008 UTC
# Line 1  Line 1 
1  <?php // $id:$  <?php
2    // $Id$
3    
4  /**  /**
5  * Implementation of hook_help().   * Implementation of hook_help().
6   */   */
7  function user_tags_help($section) {  function user_tags_help($section) {
8    switch ($section) {    switch ($section) {
9      case 'admin/help#user_tags':      case 'admin/help#user_tags':
10      case 'admin/modules#description':      case 'admin/modules#description':
11        return t('Allows free tagging of users');        return t('Allows free tagging of users');
12    }    }
# Line 23  function user_tags_menu($may_cache) { Line 24  function user_tags_menu($may_cache) {
24    $items = array();    $items = array();
25    if ($may_cache) {    if ($may_cache) {
26      $items[] = array(      $items[] = array(
27      'path' => 'tag/user',        'path' => 'tag/user',
28      'title' => t('tag'),        'title' => t('tag'),
29      'callback' => 'user_tags_page',        'callback' => 'user_tags_page',
30      'access' => $access,        'access' => $access,
31      'type' => MENU_CALLBACK,        'type' => MENU_CALLBACK,
32      );      );
33    }    }
34    return $items;    return $items;
# Line 44  function user_tags_settings() { Line 45  function user_tags_settings() {
45    
46    $vocab = db_query("SELECT vid, name, description FROM {vocabulary}");    $vocab = db_query("SELECT vid, name, description FROM {vocabulary}");
47    if (db_affected_rows() > 0) {    if (db_affected_rows() > 0) {
48      while($vocab_list = db_fetch_object($vocab)) {      while ($vocab_list = db_fetch_object($vocab)) {
49        $options[$vocab_list->vid] = $vocab_list->name;        $options[$vocab_list->vid] = $vocab_list->name;
50        if ($vocab_list->description) {        if ($vocab_list->description) {
51          $options[$vocab_list->vid] .=" - ". $vocab_list->description;          $options[$vocab_list->vid] .= " - ". $vocab_list->description;
52        }        }
53      }      }
54      $enabled_vocab = db_query("SELECT vu.vid, v.name FROM {user_tags_vocabulary} vu INNER JOIN {vocabulary} v ON v.vid = vu.vid");      $enabled_vocab = db_query("SELECT vu.vid, v.name FROM {user_tags_vocabulary} vu INNER JOIN {vocabulary} v ON v.vid = vu.vid");
55      while($enabled = db_fetch_object($enabled_vocab)) {      while($enabled = db_fetch_object($enabled_vocab)) {
56        $match_options[] = $enabled->vid;        $match_options[] = $enabled->vid;
57      }      }
58      $form["user_tags_vocabulary_list"] = array("#type" => "select",      $form["user_tags_vocabulary_list"] = array(
59       "#multiple" => true,        "#type" => 'select',
60          "#multiple" => true,
61        "#required" => true,        "#required" => true,
62        "#title" => t("Vocabularies Available"),        "#title" => t("Vocabularies Available"),
63        "#description" => t("To select multiple items, hold ctrl (command) and click on the items"),        "#description" => t("To select multiple items, hold ctrl (command) and click on the items"),
# Line 63  function user_tags_settings() { Line 65  function user_tags_settings() {
65        "#default_value" => $match_options        "#default_value" => $match_options
66      );      );
67    
68      $form["user_tags_display_options"] = array("#type" => "fieldset",      $form["user_tags_display_options"] = array(
69          "#type" => 'fieldset',
70        "#collapsible" => true,        "#collapsible" => true,
71        "#title" => t("Display Options")        "#title" => t("Display Options")
72      );      );
73    
74      $form["user_tags_display_options"]["user_tags_user_page"] = array("#type" => "checkbox",      $form["user_tags_display_options"]["user_tags_user_page"] = array(
75          "#type" => 'checkbox',
76        "#title" => t("Display tags on user's profile pages (user/username)"),        "#title" => t("Display tags on user's profile pages (user/username)"),
77        "#default_value" => variable_get("user_tag_user_page", true),        "#default_value" => variable_get("user_tags_user_page", true),
78        "#return_value" => true,        "#return_value" => true,
79      );      );
80    
81    } else {    } else {
82      drupal_set_message(t("You must add a ". l(t("vocabulary"), "admin/taxonomy") ."before using this module."), "error");      drupal_set_message(t('You must add a !link before using this module.', array('!link' => l(t('vocabulary'), 'admin/taxonomy'))), "error");
83    }    }
84    
85    return $form;    return $form;
# Line 83  function user_tags_settings() { Line 87  function user_tags_settings() {
87    
88    
89  function user_tags_user($op, &$edit, &$account, $category = NULL) {  function user_tags_user($op, &$edit, &$account, $category = NULL) {
90    switch ($op) {    switch ($op) {
91      case 'view':      case 'view':
92        $list = array();        $list = array();
93        foreach ($account->taxonomy as $tid => $term) {        foreach ($account->taxonomy as $tid => $term) {
# Line 92  function user_tags_user($op, &$edit, &$a Line 96  function user_tags_user($op, &$edit, &$a
96        if (!empty($list)) {        if (!empty($list)) {
97          return array('Tags' => $list);          return array('Tags' => $list);
98        }        }
99      break;        break;
100    
101      case 'delete':      case 'delete':
102        return user_tags_delete($account->uid);        return user_tags_delete($account->uid);
103      break;        break;
104    
105      case 'load':      case 'load':
106        $result = db_query('SELECT t.* FROM {term_data} t INNER JOIN {term_user} tu WHERE tu.tid = t.tid AND tu.vid = t.vid AND tu.uid = %d ORDER BY weight, name', $account->uid);        $result = db_query('SELECT t.* FROM {term_data} t INNER JOIN {term_user} tu WHERE tu.tid = t.tid AND tu.vid = t.vid AND tu.uid = %d ORDER BY weight, name', $account->uid);
107        $account->taxonomy = array();        $account->taxonomy = array();
# Line 105  function user_tags_user($op, &$edit, &$a Line 109  function user_tags_user($op, &$edit, &$a
109          $account->taxonomy[$term->tid] = $term;          $account->taxonomy[$term->tid] = $term;
110        }        }
111        return $account->taxonomy;        return $account->taxonomy;
112      break;        break;
113    
114      case 'categories':      case 'categories':
115        return array(array('name' =>'tags',        return array(
116                           'title' => 'tags',          array('name' =>'tags',
117                           'weight' => 10));            'title' => 'tags',
118      break;            'weight' => 10)
119          );
120          break;
121    
122    
123      case 'form':      case 'form':
124        if ($category != 'tags') {        if ($category != 'tags') {
125          return;          return;
126        }        }
127    
128        if (!isset($account->taxonomy)) {        if (!isset($account->taxonomy)) {
129             if ($account->uid) {             if ($account->uid) {
130               $terms = user_tags_get_terms($account->uid);               $terms = user_tags_get_terms($account->uid);
# Line 130  function user_tags_user($op, &$edit, &$a Line 136  function user_tags_user($op, &$edit, &$a
136           else {           else {
137             $terms = $account->taxonomy;             $terms = $account->taxonomy;
138           }           }
139    
140          $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'));          $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'));
141    
142          while ($vocabulary = db_fetch_object($c)) {          while ($vocabulary = db_fetch_object($c)) {
143             if ($vocabulary->tags) {             if ($vocabulary->tags) {
144               $typed_terms = array();               $typed_terms = array();
145               foreach ($terms as $term) {               foreach ($terms as $term) {
146                 // Extract terms belonging to the vocabulary in question.                 // Extract terms belonging to the vocabulary in question.
147                 if ($term->vid == $vocabulary->vid) {                 if ($term->vid == $vocabulary->vid) {
148    
149                   // Commas and quotes in terms are special cases, so encode 'em.                   // Commas and quotes in terms are special cases, so encode 'em.
150                   if (preg_match('/,/', $term->name) || preg_match('/"/', $term->name)) {                   if (preg_match('/,/', $term->name) || preg_match('/"/', $term->name)) {
151                     $term->name = '"'.preg_replace('/"/', '""', $term->name).'"';                     $term->name = '"'.preg_replace('/"/', '""', $term->name).'"';
# Line 148  function user_tags_user($op, &$edit, &$a Line 154  function user_tags_user($op, &$edit, &$a
154                 }                 }
155               }               }
156               $typed_string = implode(', ', $typed_terms) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);               $typed_string = implode(', ', $typed_terms) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);
157    
158               if ($vocabulary->help) {               if ($vocabulary->help) {
159                 $help = $vocabulary->help;                 $help = $vocabulary->help;
160               }               }
# Line 177  function user_tags_user($op, &$edit, &$a Line 183  function user_tags_user($op, &$edit, &$a
183               $form['taxonomy']['tags'][$vocabulary->vid]['#weight'] = $vocabulary->weight;               $form['taxonomy']['tags'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
184               $form['taxonomy']['tags'][$vocabulary->vid]['#required'] = $vocabulary->required;               $form['taxonomy']['tags'][$vocabulary->vid]['#required'] = $vocabulary->required;
185             }             }
186    
187           if (isset($form['taxonomy'])) {           if (isset($form['taxonomy'])) {
188             $form['taxonomy'] += array('#type' => 'fieldset', '#title' => t('Categories'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, '#weight' => -3);             $form['taxonomy'] += array('#type' => 'fieldset', '#title' => t('Categories'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, '#weight' => -3);
189            }            }
190          }          }
191        return $form;        return $form;
192      break;        break;
193    
194      case 'submit':      case 'submit':
195        if ($category != 'tags') {        if ($category != 'tags') {
196          return;          return;
197        }        }
198        user_tags_delete($account->uid);        user_tags_delete($account->uid);
199    
200        if (is_array($edit['taxonomy'])) {        if (is_array($edit['taxonomy'])) {
201          if (isset($edit['taxonomy']['freetags'])) {          if (isset($edit['taxonomy']['freetags'])) {
202            foreach ($edit['taxonomy']['freetags'] as $key => $value){            foreach ($edit['taxonomy']['freetags'] as $key => $value){
203              $vid = $key;              $vid = $key;
204              $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';              $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
205              preg_match_all($regexp, $value, $matches);              preg_match_all($regexp, $value, $matches);
206              $typed_terms = $matches[1];              $typed_terms = $matches[1];
# Line 233  function user_tags_user($op, &$edit, &$a Line 239  function user_tags_user($op, &$edit, &$a
239            }            }
240          }          }
241        }        }
242        unset($edit['taxonomy']);        unset($edit['taxonomy']);
243      break;        break;
244    }    }
245  }  }
246    
# Line 261  function user_tags_delete($uid) { Line 267  function user_tags_delete($uid) {
267  function user_tags_page($tid = null) {  function user_tags_page($tid = null) {
268    global $user;    global $user;
269    $row = array();    $row = array();
270    $rows = array();    $rows = array();
271    $term = taxonomy_get_term($tid);    $term = taxonomy_get_term($tid);
272    
273    if (!$tid || $term->tid != $tid) {    if (!$tid || $term->tid != $tid) {
274    
275      //print all the tags currently in use.      //print all the tags currently in use.
276      $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      $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
277                         ORDER BY td.weight, td.name');                         ORDER BY td.weight, td.name');
# Line 284  function user_tags_page($tid = null) { Line 290  function user_tags_page($tid = null) {
290        if ($term->username != $old->username && !empty($row)) {        if ($term->username != $old->username && !empty($row)) {
291          $rows[] = array(l($old->username, "user/$old->uid"), implode(', ', $row));          $rows[] = array(l($old->username, "user/$old->uid"), implode(', ', $row));
292          unset($row); // Cleaning and reinitializing to avoid duplicates.          unset($row); // Cleaning and reinitializing to avoid duplicates.
293          $row = array();          $row = array();
294        }        }
295        $old = $term;        $old = $term;
296        $row[] = l($term->termname, "tag/user/$term->tid");        $row[] = l($term->termname, "tag/user/$term->tid");
# Line 295  function user_tags_page($tid = null) { Line 301  function user_tags_page($tid = null) {
301  }  }
302    
303  /**  /**
304  * Find all terms associated to the user of a given vocabulary.   * Find all terms associated to the user of a given vocabulary.
305   */   */
306  function user_tags_get_terms_by_vocabulary($uid, $vid, $key = 'tid') {  function user_tags_get_terms_by_vocabulary($uid, $vid, $key = 'tid') {
307    $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);    $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);
308    $terms = array();    $terms = array();
309    while ($term = db_fetch_object($result)) {    while ($term = db_fetch_object($result)) {
310      $terms[$term->$key] = $term;      $terms[$term->$key] = $term;
311    }    }
312    return $terms;    return $terms;
313  }  }

Legend:
Removed from v.1.8.2.1  
changed lines
  Added in v.1.8.2.2

  ViewVC Help
Powered by ViewVC 1.1.2