Index: community_tags.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/community_tags/community_tags.module,v
retrieving revision 1.31
diff -u -p -r1.31 community_tags.module
--- community_tags.module	2 Nov 2006 20:24:03 -0000	1.31
+++ community_tags.module	28 Nov 2006 17:38:14 -0000
@@ -2,87 +2,43 @@
 // $Id: community_tags.module,v 1.31 2006/11/02 20:24:03 webchick Exp $
 
 /**
- * Implementation of hook_help().
- */
-function community_tags_help($section) {
-  switch ($section) {
-    case 'admin/modules#description':
-      return t("Allows users to 'tag' other users' content. Weighted tag lists or 'tag clouds' are generated for each piece of content to show the popularity of tags.");
-  }
-}
-
-/**
- * Implementation of hook_menu().
- */
+* Implementation of hook_menu().
+*/
 function community_tags_menu($may_cache) {
   $items = array();
-
-  if (!$may_cache) {
-    if (arg(0) == 'node' && is_numeric(arg(1))) {
-      $node = node_load(arg(1));
-      $items[] = array(
-        'path' => 'node/'. arg(1) .'/tag',
-        'title' => t('Tag it!'),
-        'callback' => 'community_tags_node_view',
-        'callback arguments' => array(arg(1), FALSE),
-        'access' => (user_access('tag content') && $node->community_tags_form),
+  
+  
+  if ($may_cache) {
+    $items[] = array(
+      'path' => 'admin/settings/community_tags',
+      'title' => t('Community Tags'),
+      'description' => t('Administer community tags.'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => 'community_tags_admin_settings',
+      'access' => user_access('administer site configuration'),
       );
-    }
   }
-
+  
   return $items;
 }
 
 /**
- * Community tags callback.
- */
-function community_tags_node_view($nid, $inline = TRUE) {
-  global $user;
-  $node = node_load($nid);
-
-  if (!$inline) {
-    drupal_set_title(t('Tagging %type %title:', array('%type' => node_get_name($node->type), '%title' => theme('placeholder', $node->title))));
-  }
-
-  $output = theme('community_tags', 'node', $node->nid);
-
-  $vid = $node->tags ? array_shift(array_keys($node->tags)) : variable_get('community_tags_default_vocabulary', NULL);
-  $tags = community_tags_get_user_node_tags($user->uid, $node->nid);
-  if (!count($tags)) {
-    // User has not yet added tags to this node yet. Show form.
-    $output .= community_tags_form(array('nid' => $node->nid, 'vid' => $vid, 'tags' => NULL));
-  }
-  elseif (user_access('edit own tags')) {
-    // User has already tagged this node, but can edit their tags. Show form
-    // with the user's tags pre-populated.
-    $tags = implode(', ', $tags);
-    $output .= community_tags_form(array('nid' => $node->nid, 'vid' => $vid, 'tags' => $tags));
-  }
-  else {
-    // Sorry, no more adding tags for you!
-    $output .= '<p>'. t('You have already tagged this post. Your tags: ') . theme('community_tags', 'user_node', $user->uid, $node->nid) .'</p>';
-  }
-
-  return $output;
-}
-
-/**
- * Implementation of hook_perm().
- */
+* Implementation of hook_perm().
+*/
 function community_tags_perm() {
-  return array('tag content', 'edit own tags');
+  return array('tag content');
 }
 
 /**
- * Implementation of hook_nodeapi().
- */
-function community_tags_nodeapi(&$node, $op, $teaser) {
+* Implementation of hook_nodeapi().
+*/
+function community_tags_nodeapi(&$node, $op, $teaser, $page) {
   switch ($op) {
     case 'load':
       $node->tags = tagadelic_node_get_terms($node);
-      $node->community_tags_form = variable_get('community_tags_'. $node->type, FALSE);
+      $node->community_tags_form = variable_get('community_tags_nodeapi_'. $node->type, FALSE);
       break;
-
+      
     case 'insert':
     case 'update':
       // If this node has tags associated with it, we want to save these in
@@ -96,32 +52,27 @@ function community_tags_nodeapi(&$node, 
         }
       }
       break;
-
+      
     case 'view':
       global $user;
       // Show quick tag form for this node if we're on a node page view and the
       // form is enabled for this node and the default quick tag vocab is set.
       $vid = $node->tags ? array_shift(array_keys($node->tags)) : variable_get('community_tags_default_vocabulary', NULL);
-      if (!$teaser && $node->community_tags_form && $vid) {
+      if ($node->community_tags_form && $vid) {
         // Display a tag cloud of the overall community tags for this node.
-        $node->body .= theme('community_tags', 'node', $node->nid);
-
-        if (user_access('tag content') && $node->uid != $user->uid) {
+        $node->content['tags_list'] = array(
+          '#value' => theme('community_tags', 'node', 20, $node->nid),
+          '#weight' => 20,
+          );
+        
+        if (user_access('tag content')) {
           $tags = community_tags_get_user_node_tags($user->uid, $node->nid);
-          if (!count($tags)) {
-            // User has not yet added tags to this node yet. Show form.
-            $node->body .= community_tags_form(array('nid' => $node->nid, 'vid' => $vid, 'tags' => NULL));
-          }
-          elseif (user_access('edit own tags')) {
-            // User has already tagged this node, but can edit their tags. Show form
-            // with the user's tags pre-populated.
-            $tags = implode(', ', $tags);
-            $node->body .= community_tags_form(array('nid' => $node->nid, 'vid' => $vid, 'tags' => $tags));
-          }
-          else {
-            // Sorry, no more adding tags for you!
-            $node->body .= '<p>'. t('You have already tagged this post. Your tags: ') . theme('community_tags', 'user_node', $user->uid, $node->nid) .'</p>';
-          }
+          $tags = count($tags) ? implode(', ', $tags) : NULL;
+          
+          $node->content['tags_form'] = array(
+            '#value' => drupal_get_form('community_tags_form_' . $node->nid, array('nid' => $node->nid, 'vid' => $vid, 'tags' => $tags)),
+            '#weight' => 21,
+            );
         }
       }
       break;
@@ -129,9 +80,9 @@ function community_tags_nodeapi(&$node, 
 }
 
 /**
- * Implementation of hook_settings().
- */
-function community_tags_settings() {
+* Implementation of hook_settings().
+*/
+function community_tags_admin_settings() {
   // Build list of available free-tagging vocabularies
   $vocabs = db_query('SELECT v.vid, v.name FROM {vocabulary} v WHERE v.tags = 1 ORDER BY v.weight, v.name');
   $options = array(0 => t('--'));
@@ -140,45 +91,35 @@ function community_tags_settings() {
   }
   if ($options) {
     $form['community_tags_default_vocabulary'] = array(
-    '#type' => 'select',
-    '#title' => t('Default free tagging vocabulary'),
-    '#default_value' => variable_get('community_tags_default_vocabulary', NULL),
-    '#options' => $options,
-    '#description' => t('Which vocabulary should the quick tagging forms use?')
-    );
+      '#type' => 'select',
+      '#title' => t('Default free tagging vocabulary'),
+      '#default_value' => variable_get('community_tags_default_vocabulary', NULL),
+      '#options' => $options,
+      '#description' => t('Which vocabulary should the quick tagging forms use?')
+      );
   }
-  return $form;
+  return system_settings_form($form);
 }
 
 /**
- * Implementation of hook_form_alter().
- */
+* Implementation of hook_form_alter().
+*/
 function community_tags_form_alter($form_id, &$form) {
   // Provide option to enable Community Tags per node type.
-  if (isset($form['type']) && $form['type']['#value'] .'_node_settings' == $form_id) {
-    $form['workflow']['community_tags_'. $form['type']['#value']] = array( 	 
-      '#type' => 'radios', 	 
-      '#title' => t('Enable community tags'), 	 
-      '#default_value' => variable_get('community_tags_'. $form['type']['#value'], FALSE), 	 
-      '#options' => array(t('Disabled'), t('Enabled')), 	 
-      '#description' => t('An "add a tag" form will be added to the bottom of nodes of this type.'), 	 
-    ); 	 
-  }
-  elseif ($form_id == 'system_modules' && !$_POST) {
-    // Dependency checking.
-    if (!in_array('tagadelic', $form['status']['#default_value']) || !function_exists('tagadelic_build_weighted_tags')) {
-      $module = 'community_tags';
-      db_query("UPDATE {system} SET status = 0 WHERE type = 'module' AND name = '%s'", $module);
-      $key = array_search($module, $form['status']['#default_value']);
-      unset($form['status']['#default_value'][$key]);
-      drupal_set_message(t('Community Tags module was deactivated -- it requires the HEAD version of Tagadelic module.'), 'error');
-    }
+  if ($form_id == 'node_type_form'){
+    $form['workflow']['community_tags_nodeapi'] = array(
+      '#type' => 'radios',
+      '#title' => t('Enable community tags'),
+      '#default_value' => variable_get('community_tags_nodeapi_'. $form['old_type']['#value'], 0),
+      '#options' => array('1' => t('Enabled'), '0' => t('Disabled')), 	 
+      '#description' => t('An "add a tag" form will be added to the bottom of nodes of this type.'), 
+      );
   }
 }
 
 /**
- * Retrieve list of tags for a given node that belong to a user.
- */
+* Retrieve list of tags for a given node that belong to a user.
+*/
 function community_tags_get_user_node_tags($uid, $nid) {
   $tags = array();
   $result = db_query("SELECT t.name FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid WHERE c.nid = %d AND c.uid = %d", $nid, $uid);
@@ -190,106 +131,129 @@ function community_tags_get_user_node_ta
     }
     $tags[] = $term->name;
   }
-
+  
   // Sort the list.
   sort($tags);
-
+  
   return $tags;
 }
 
 /**
- * Quick tag form
- */
-function community_tags_form($edit, $title = NULL) {
+* hook_forms()
+*/
+function community_tags_forms($args) {
+  $form_id = array_shift($args);
+  if (preg_match('/^community_tags_form_\d+$/', $form_id)){
+    $forms[$form_id] = array(
+      'callback' => 'community_tags_tag_form',
+      );
+  }
+  return $forms;
+}
+
+/**
+* Quick tag form
+*/
+function community_tags_tag_form($edit) {
   $form = array();
+  
+  $form['#base'] = 'community_tags_tag_form';
+  
+  $form['#attributes'] = array(
+    'class' => 'community-tags-form',
+    'onsubmit' => 'community_tags_submit_tags(' . $edit['nid'] . '); return false;',
+    );
+  
+  if (! $edit['tags']){
+    $form['#attributes']['class'] .= ' hidden';
+  }
+  
   $form['tags'] = array(
     '#type' => 'textfield',
-    '#title' => t('Add tags'),
+    '#title' => t('Your tags'),
     '#maxlength' => 100,
     '#default_value' => $edit['tags'],
     '#required' => FALSE,
     '#autocomplete_path' => 'taxonomy/autocomplete/'. $edit['vid'],
     '#description' => t('A comma-separated list of terms describing this content.  Example: funny, bungee jumping, "Company, Inc.".'),
-  );
-
+    );
+  
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Save'),
-  );
-
+    );
+  
   $form['nid'] = array(
     '#type' => 'value',
     '#value' => $edit['nid'],
-  );
-
+    );
+  
   $form['vid'] = array(
     '#type' => 'value',
     '#value' => $edit['vid'],
-  );
-
-  return drupal_get_form('community_tags_form', $form);
+    );
+  
+  return $form;
 }
 
 /**
- * Validate the quick tag form.
- */
-function community_tags_form_validate($form_id, $form_values) {
+* Validate the quick tag form.
+*/
+function community_tags_tag_form_validate($form_id, $form_values) {
   if ($form_values['tags'] == '') {
     form_set_error('tags', t('You must enter at least one tag.'));
   }
 }
 
 /**
- * Submit callback for quick tag form.
- */
-function community_tags_form_submit($form_id, $form_values) {
+* Submit callback for quick tag form.
+*/
+function community_tags_tag_form_submit($form_id, $form_values) {
   global $user;
-
+  
   community_tags_taxonomy_node_save($form_values['nid'], array('tags' => array($form_values['vid'] => $form_values['tags'])), FALSE, $user->uid);
-
-  return array('node/'. $form_values['nid']);
 }
 
 /**
- * Save community_tags term associations and counts for a given node.
- */
+* Save community_tags term associations and counts for a given node.
+*/
 function community_tags_taxonomy_node_save($nid, $terms, $remove_omitted_tags, $uid) {
-
+  
   // If we're adding tags from the node add/edit page, then we want to delete
   // any tags that aren't entered to allow admins to remove node tags.
   // If not, we at least want the existing counts to update them.
-
+  
   $old_tags_by_uid = array();
   $result = db_query('SELECT * FROM {community_tags} ttn WHERE nid = %d', $nid);
   while ($old_tag_node = db_fetch_object($result)) {
     $old_tags_by_uid[$old_tag_node->uid][$old_tag_node->tid] = $old_tag_node;
   }
-
+  
   $tag_nodes = array();
   $inserted_tids = array();
-
+  
   $tids = array();
   // Free tagging vocabularies do not send their tids in the form,
   // so we'll detect them here and process them independently.
   if (isset($terms['tags'])) {
     $typed_input = $terms['tags'];
     unset($terms['tags']);
-
+    
     foreach ($typed_input as $vid => $vid_value) {
       // This regexp allows the following types of user input:
       // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar
       $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
       preg_match_all($regexp, $vid_value, $matches);
       $typed_terms = $matches[1];
-
+      
       foreach ($typed_terms as $typed_term) {
         // If a user has escaped a term (to demonstrate that it is a group,
-        // or includes a comma or quote character), we remove the escape
+          // or includes a comma or quote character), we remove the escape
         // formatting so to save the term into the DB as the user intends.
         $typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $typed_term));
         $typed_term = trim($typed_term);
         if ($typed_term == '') { continue; }
-
+        
         // See if the term exists in the chosen vocabulary
         // and return the tid, otherwise, add a new record.
         $possibilities = taxonomy_get_term_by_name($typed_term);
@@ -299,35 +263,35 @@ function community_tags_taxonomy_node_sa
             $typed_term_tid = $possibility->tid;
           }
         }
-
+        
         if (!$typed_term_tid) {
           $edit = array('vid' => $vid, 'name' => $typed_term);
           $status = taxonomy_save_term($edit);
           $typed_term_tid = $edit['tid'];
-
+          
           // Add only new terms to return value
           $tids[$typed_term_tid] = $typed_term;
         }
-
+        
         // If we already added this term in this cycle, skip this duplicate.
         if (isset($inserted_tids[$typed_term_tid])) {
           continue;
         }
-
+        
         // If there was an existing tag by this user, load that data.
         $tag_node = new StdClass();
         $tag_node->tid = $typed_term_tid;
         $tag_node->nid = $nid;
         $tag_node->uid = $uid;
         $tag_node->date = time();
-
+        
         // So we don't add this tag again on this run or re-insert the old tag for this user.
         $inserted_tids[$typed_term_tid] = $typed_term_tid;
         $tag_nodes[] = $tag_node;
       }
     }
   }
-
+  
   // If not on node edit page (removing all omitted tags), then re-insert the existing tags
   // that we haven't already added back in.
   foreach($old_tags_by_uid as $old_uid => $old_tags) {
@@ -343,7 +307,7 @@ function community_tags_taxonomy_node_sa
       }
     }
   }
-
+  
   // Get existing term-node associations to see if we need to re-insert a term-node
   // relation for each old tag we're putting back in since we're acting after
   // taxonomy.module has done its business.
@@ -353,13 +317,13 @@ function community_tags_taxonomy_node_sa
   while ($term_node = db_fetch_object($result)) {
     $existing_term_nodes[$term_node->tid] = TRUE;
   }
-
+  
   // Re-insert tag-node-user associations and term-node if not existing.
   db_lock_table('{community_tags}');
   db_query('DELETE FROM {community_tags} WHERE nid = %d', $nid);
   foreach ($tag_nodes as $tag_node) {
     db_query('INSERT INTO {community_tags} (tid, nid, uid, date) VALUES (%d, %d, %d, %d)', $tag_node->tid, $tag_node->nid, $tag_node->uid, $tag_node->date);
-
+    
     // Remember to insert term-node relation into term_node table if it doesn't exist already.
     if (!isset($existing_term_nodes[$tag_node->tid])) {
       $add_term_nodes[] = $tag_node;
@@ -367,7 +331,7 @@ function community_tags_taxonomy_node_sa
     }
   }
   db_unlock_tables();
-
+  
   // Avoid locking term_node so we loop again.
   foreach ($add_term_nodes as $tag_node) {
     // Insert term-node relation into term_node table since it doesn't exist already.
@@ -377,34 +341,34 @@ function community_tags_taxonomy_node_sa
 }
 
 /**
- * Helper function for retrieving a query result to pass along to the Tagadelic 
- * functions prior to theming.
- *
- * @param $type
- *   The type of query to perform. Possible values:
- *   - node: get tag count for a given node.
- *   - type: get tag count for a given node type.
- *   - user: get tag count for a given user.
- *   - user_node: get tag count for a given user on a given node.
- *   - global: get tag count across entire site (default).
- * @param $args
- *   An array of arguments that correspond to the result type:
- *   - If type is 'node', $args[1] is a node ID.
- *   - If type is 'type', $args[1] is a node type.
- *   - If type is 'user', $args[1] is a user ID.
- *   - If type is 'user_node', $args[1] is a user ID, and $args[2] is a node ID.
- *   - If type is 'global', args is not used.
- * @param $limit
- *   Only display a certain number of tags.
- * @return $result
- *  A database result set.
- */
+* Helper function for retrieving a query result to pass along to the Tagadelic 
+* functions prior to theming.
+*
+* @param $type
+*   The type of query to perform. Possible values:
+*   - node: get tag count for a given node.
+*   - type: get tag count for a given node type.
+*   - user: get tag count for a given user.
+*   - user_node: get tag count for a given user on a given node.
+*   - global: get tag count across entire site (default).
+* @param $args
+*   An array of arguments that correspond to the result type:
+*   - If type is 'node', $args[1] is a node ID.
+*   - If type is 'type', $args[1] is a node type.
+*   - If type is 'user', $args[1] is a user ID.
+*   - If type is 'user_node', $args[1] is a user ID, and $args[2] is a node ID.
+*   - If type is 'global', args is not used.
+* @param $limit
+*   Only display a certain number of tags.
+* @return $result
+*  A database result set.
+*/
 function _community_tags_get_tag_result($type = 'global', $limit = NULL, $args) {
   // Initialize variables.
   $arg1 = NULL;
   $arg2 = NULL;
   $sql = '';
-
+  
   switch ($type) {
     case 'node':
       $arg1 = (int)$args[2];
@@ -425,25 +389,25 @@ function _community_tags_get_tag_result(
     default:
       $sql = "SELECT COUNT(c.tid) AS count, t.tid, t.name, t.vid FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid GROUP BY c.tid ORDER BY count DESC";
   }
-
+  
   if ($limit) {
     $limit = (int)$limit;
     $sql .= " LIMIT $limit";
   }
-
+  
   return db_query($sql, $arg1, $arg2);
 }
 
 /**
- * Theme function to display community tags. See _community_tags_get_tag_result() 
- * for definitions of $type and $args.
- *
- * @ingroup themeable
- */
+* Theme function to display community tags. See _community_tags_get_tag_result() 
+* for definitions of $type and $args.
+*
+* @ingroup themeable
+*/
 function theme_community_tags($type = 'global', $limit = NULL) {
   $args = func_get_args();
   $result = _community_tags_get_tag_result($type, $limit, $args);
   $weighted_tags = tagadelic_build_weighted_tags($result);
   $sorted_tags = tagadelic_sort_tags($weighted_tags);
   return theme('tagadelic_weighted', $sorted_tags);
-}
+}
\ No newline at end of file
