Index: weblinks.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/weblinks/weblinks.module,v
retrieving revision 1.1.4.54
diff -u -r1.1.4.54 weblinks.module
--- weblinks.module	16 Jul 2008 16:46:01 -0000	1.1.4.54
+++ weblinks.module	17 Jul 2008 18:33:10 -0000
@@ -203,20 +203,21 @@
       '#required' => true,
       '#prefix' => '<div class="weblinks-radios">',
       '#suffix' => '</div><div class="clear-block"></div>',
-      '#weight' => -20,
+      '#weight' => -40,
       );
   }
   $form['weblinks_external'] = array(
     '#type' => 'radios',
     '#title' => t('Open links in a new window'),
+    '#default_value' => variable_get("weblinks_external", true),
     '#options' => array(1 => 'yes', 0 => 'no'),
     '#required' => FALSE,
-    '#default_value' => variable_get("weblinks_external", true),
     '#description' => t('Recommended to keep your vistors on your site.'),
     '#prefix' => '<div class="weblinks-radios">',
     '#suffix' => '</div><div class="clear-block"></div>',
-    '#weight' => -17,
+    '#weight' => -36,
     );
+
   $form['weblinks_pagedesc'] = array(
     '#type' => 'textarea',
     '#title' => t('Page information'),
@@ -224,16 +225,27 @@
     '#default_value' => variable_get("weblinks_pagedesc", null),
     '#description' => t('This description will appear at the top of the links page.  It will be filtered by your default input format.'),
     '#required' => false,
-    '#weight' => -16,
+    '#weight' => -32,
     );
+
   $form['weblinks_simplelinks'] = array(
     '#type' => 'checkbox',
     '#title' => t('Use Simple Links'),
-    '#default_value' => variable_get("weblinks_simplelinks", false),
-    '#description' => t('Enable this if you want the links to appear on the main page, otherwise, your users will have to click on the group to view the links. The depth of the links to be displayed is specified below.'),
-    '#required' => FALSE,
-    '#weight' => -15,
+    '#default_value' => variable_get('weblinks_simplelinks', false),
+    '#description' => t('Enable this if you want the links to appear on the main page, otherwise, your users will have to click on the category to view the links. The depth of the links to be displayed is specified below.'),
+    '#required' => false,
+    '#weight' => -30,
+  );
+
+  $form['weblinks_collapsible'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Make categories collapsible'),
+    '#default_value' => variable_get('weblinks_collapsible', false),
+    '#description' => t('Enable this if you want the categories to be collapsible on the main page. If selected, only the top level categories will be uncollapsed; all child categories will be collapsed by default.'),
+    '#required' => false,
+    '#weight' => -30,
   );
+
   $form['weblinks_empty_text'] = array(
     '#type' => 'textarea',
     '#title' => t('Empty category text'),
@@ -241,8 +253,9 @@
     '#default_value' => variable_get('weblinks_empty_text', null),
     '#description' => t('This text will appear when a category is empty. Leave it blank for no text. It will be filtered by your default input format.'),
     '#required' => false,
-    '#weight' => -14,
+    '#weight' => -28,
     );
+
   $sort_options = array(
     'standard' => 'Normal Drupal - by weight, sticky, created date.',
     'title' => 'Legacy - by weight and title.',
@@ -256,18 +269,20 @@
     '#required' => false,
     '#prefix' => '<div class="weblinks-radios">',
     '#suffix' => '</div><div class="clear-block"></div>',
-    '#weight' => -12,
+    '#weight' => -24,
   );
+
+  // @TODO: move this to block settings.
   $form['weblinks_block_sort'] = array(
     '#type' => 'radios',
     '#options' => $sort_options,
     '#title' => t('Block Sort order'),
     '#default_value' => variable_get('weblinks_block_sort', 'title'),
-    '#description' => t('This determines how the links blocks will sort the links.'),
+    '#description' => t('This determines how the links block will sort the links.'),
     '#required' => false,
     '#prefix' => '<div class="weblinks-radios">',
     '#suffix' => '</div><div class="clear-block"></div>',
-    '#weight' => -11,
+    '#weight' => -22,
   );
   $form['weblinks_urlnode'] = array(
     '#type' => 'radios',
@@ -278,42 +293,44 @@
     '#required' => FALSE,
     '#prefix' => '<div class="weblinks-radios">',
     '#suffix' => '</div><div class="clear-block"></div>',
-    '#weight' => -10,
+    '#weight' => -20,
   );
+
+  // @TODO: move this to block settings.
   $form['weblinks_block_urlnode'] = array(
     '#type' => 'radios',
     '#title' => t('Block links lead URL or to Web Link node'),
     '#default_value' => variable_get("weblinks_block_urlnode", 'url'),
-    '#description' => t('See the previous description.'),
+    '#description' => t('Most people will want to leave this as URL. Specifying "node" makes it easier to edit the links, but requires an additional click to follow the link.'),
     '#options' => array('url' => 'URL', 'node' => 'node'),
     '#required' => FALSE,
     '#prefix' => '<div class="weblinks-radios">',
     '#suffix' => '</div><div class="clear-block"></div>',
-    '#weight' => -9,
+    '#weight' => -18,
   );
   $form['weblinks_catdesc'] = array(
     '#type' => 'checkbox',
-    '#title' => t('Display link group descriptions'),
+    '#title' => t('Display link category descriptions'),
     '#default_value' => variable_get("weblinks_catdesc", true),
-    '#required' => FALSE,
     '#description' => t('Generally recommended.'),
-    '#weight' => -8,
+    '#required' => FALSE,
+    '#weight' => -16,
   );
   $form['weblinks_subcatdesc'] = array(
     '#type' => 'checkbox',
-    '#title' => t('Display link sub-group descriptions'),
+    '#title' => t('Display link sub-category descriptions'),
     '#default_value' => variable_get("weblinks_subcatdesc", false),
-    '#required' => FALSE,
     '#description' => t('Generally recommended if you are showing more than the top level of links..'),
-    '#weight' => -7,
+    '#required' => FALSE,
+    '#weight' => -14,
   );
   $form['weblinks_linkdesc'] = array(
     '#type' => 'checkbox',
     '#title' => t('Display individual link descriptions'),
     '#default_value' => variable_get("weblinks_linkdesc", true),
-    '#required' => FALSE,
     '#description' => t('Generally recommended.'),
-    '#weight' => -6,
+    '#required' => FALSE,
+    '#weight' => -12,
   );
   $form['weblinks_linkinfo'] = array(
     '#type' => 'checkbox',
@@ -321,37 +338,38 @@
     '#default_value' => variable_get("weblinks_linkinfo", true),
     '#description' => t('This displays link author, publish date, and revision date'),
     '#required' => FALSE,
-    '#weight' => -5,
+    '#weight' => -10,
   );
   $form['weblinks_nofollow'] = array(
     '#type' => 'checkbox',
     '#title' => t('Spam link deterrent'),
     '#default_value' => variable_get('weblinks_nofollow', false),
     '#description' => t('If enabled, Drupal will add rel="nofollow" to all links, as a measure to reduce the effectiveness of spam links. Note: this will also prevent valid links from being followed by search engines, therefore it is likely most effective when enabled for anonymous users.'),
-    '#weight' => -4,
+    '#weight' => -8,
     );
 
   $mfd_default = variable_get("weblinks_maxfrontdepth", 1);
-  if (!$mfd_default) {
+  if (!is_numeric($mfd_default)) {
     $mfd_default = 1;
   }
   $form['weblinks_maxfrontdepth'] = array(
     '#type' => 'textfield',
-    '#title' => t('Maximum group depth (front page)'),
+    '#title' => t('Maximum category depth (front page)'),
     '#default_value' => $mfd_default,
-    '#description' => t('This controls how many subcategories, if any, will display on the main links page'),
+    '#description' => t('This controls how many subcategories, if any, will display on the main links page. Blank, zero (0), and one (1) will all show only the top level.'),
     '#required' => FALSE,
     '#size' => 2,
-    '#weight' => -3,
+    '#weight' => -6,
   );
+
   $form['weblinks_maxdepth'] = array(
     '#type' => 'textfield',
-    '#title' => t('Maximum group depth'),
+    '#title' => t('Maximum category depth'),
     '#default_value' => variable_get("weblinks_maxdepth", true),
     '#description' => t('This controls how many subcategories, if any, will display on other links pages.'),
     '#required' => FALSE,
     '#size' => 2,
-    '#weight' => -2
+    '#weight' => -4,
   );
   $form['weblinks_clickthru'] = array(
     '#type' => 'textfield',
@@ -360,8 +378,9 @@
     '#description' => t('How many clicks a user can make before all the menus are automatically expanded. Leave blank if you do not want to set a limit.'),
     '#required' => FALSE,
     '#size' => 2,
-    '#weight' => 1,
+    '#weight' => -2,
   );
+
   $form['categories'] = array(
     '#type' => 'fieldset',
     '#title' => t('Category settings'),
@@ -370,30 +389,28 @@
     '#weight' => 0,
   );
   // Get the weblinks categories to add to the blogroll
-  $result = db_query('SELECT tid, name '.
-   'FROM {term_data} '.
-   'WHERE vid = %d '.
-   'ORDER BY weight ASC, name ASC', _weblinks_get_vid());
+  $result = db_query('SELECT tid, name FROM {term_data} WHERE vid = %d ORDER BY weight ASC, name ASC', _weblinks_get_vid());
 
   while ($row = db_fetch_object($result)) {
     // Set which link categories get displayed on the links page
 
     $form['categories'][$row->tid] = array(
       '#type' => 'fieldset',
-      '#title' => t($row->name),
+      '#title' => $row->name,
       '#collapsible' => TRUE,
       '#collapsed' => TRUE,
+      '#weight' => 0,
     );
 
     $form['categories'][$row->tid]['weblinks_page_'. $row->tid] = array(
       '#type' => 'checkbox',
-      '#title' => t($row->name),
+      '#title' => $row->name,
       '#default_value' => variable_get('weblinks_page_'. $row->tid, 1),
       '#required' => FALSE,
-      '#description' => t('Check to enable this group to display on the main weblinks page.'),
+      '#description' => t('Check to enable this category to display on the main weblinks page.'),
     );
 
-    // Set the maximum number of links to display for each group
+    // Set the maximum number of links to display for each category
     $form['categories'][$row->tid]['weblinks_maxdisp_'. $row->tid] = array(
       '#type' => 'textfield',
       '#title' => t('Maximum links to display'),
@@ -895,34 +912,45 @@
 }
 
 /**
- *  Process child terms. Called by main page display.
- */
-function _weblinks_recurse_children($tid, $max_depth = null, $withlinks = false, $bypass_empty = true) {
-  global $_weblinks_depth;
-  $description = variable_get('weblinks_subcatdesc', FALSE); // see if we're supposed to display cat descriptions
-
-  if ($withlinks) {
-    $output .= _weblinks_links($tid, true, $bypass_empty);
-  }
-
-  if ($_weblinks_depth <= $max_depth) {
-    $children = taxonomy_get_children($tid, _weblinks_get_vid());
-    if ($children) {
-      $_weblinks_depth++;
-      $output .= '<ul class="subcat">';
-      foreach ($children as $child) {
-        $output .= '<li class="subcatItem">'. l(t($child->name), 'weblinks/'. $child->tid);
-        if ($description) {
-          $output .= check_markup($child->description);
-        }
-        if ($_weblinks_depth < $max_depth) {
-          $output .= _weblinks_recurse_children($child->tid, $max_depth, $withlinks, false);
-        }
-        $output .= '</li>';
-      }
-      $output .= '</ul>';
+ * Display the category. This function is recursive to handle children.
+ *
+ * @param $term
+ *   The term object for the category. Child terms will also be followed.
+ * @return HTML
+ */
+function weblinks_display_category($term) {
+  static $collapsible, $display_links, $taxo_img, $max_depth;
+  if (!isset($collapsible)) {
+    $collapsible = variable_get('weblinks_collapsible', true);
+    $display_links = variable_get('weblinks_simplelinks', true);
+    $taxo_img = module_exists('taxonomy_image');
+    $max_depth = variable_get("weblinks_maxfrontdepth", 1) - 1;
+    if (!$display_links) {
+      $collapsible = false;
+    }
+  }
+
+  $tree = taxonomy_get_tree($term->vid, $term->tid, -1, 1);
+  $child_depth = $term->depth + 1;
+  $links = $display_links ? _weblinks_links($term->tid, false, $tree): null;
+  if ($tree) {
+    $child_links = null;
+    foreach ($tree as $key => $child) {
+      $child->depth = $child_depth;
+      $child_links .= weblinks_display_category($child);
     }
   }
+  $data = ($taxo_img ? taxonomy_image_display($term->tid, array('hspace' => '5', 'align' => 'left')) : null)
+    . '<div class="weblinks-cat-desc">'. check_markup($term-> description) .'</div>' 
+    . $links
+    . $child_links;
+  $fieldset = array(
+    '#title' => $collapsible ? check_plain($term->name) : l($term->name, 'weblinks/'. $term->tid),
+    '#collapsible' => $collapsible,
+    '#collapsed' => $term->depth > $max_depth,
+    '#value' => $data,
+    );
+  $output .=  '<div class="weblinkCat weblinkCat-depth-'. $term->depth .'">'. theme('fieldset', $fieldset) ."</div>\n";
   return $output;
 }
 
@@ -939,123 +967,35 @@
   // Get the page description, if there is one
   $output = theme('weblinks_pagedesc');
 
-  $description = variable_get('weblinks_catdesc', FALSE); // See if we're supposed to display cat descriptions.
-  $withlinks = variable_get('weblinks_simplelinks', FALSE); // See if we're just using cat -> link; simple.
-  $maxdepth = variable_get('weblinks_maxfrontdepth', 1); // See how many subcats to display on front page.
-  $clickthru = variable_get('weblinks_clickthru', 2); // Maximum number of click to get at links (0 is front page).
-  $taxo_img = module_exists('taxonomy_image');
-
-  $bwrow = array();
+  $output = null;
   if ($tid == 0) {
-    $output .= _weblinks_links(0);
-    $tree = taxonomy_get_tree(_weblinks_get_vid(), $tid, $_weblinks_depth = -1, $max_depth = 1); //get top-level cats
-    if ($tree) {
+    // We don't specify depth here because taxonomy will cache the terms
+    // and we will need them all eventually. 
+    if ($tree = taxonomy_get_tree(_weblinks_get_vid())) {
       foreach ($tree as $term) {
-        if (variable_get('weblinks_page_'. $term->tid, true)) {
-          $img = $taxo_img ? taxonomy_image_display($term->tid) : null;
-          $output .= '<div class="catbox"><h3 class="weblinks">'. $img . l($term->name, 'weblinks/'. $term->tid) .'</h3>';
-          if ($description) {
-            $output .= '<p>'. check_plain($term->description) .'</p>';
-          }
-          if ($img) {
-            $output .= '<div class="clear-block"></div>';
-          }
-          global $_weblinks_depth; // @@@ Bdragon: wtf?
-          $_weblinks_depth = 0;
-          $output .= _weblinks_recurse_children($term->tid, $maxdepth, $withlinks);
-          $output .= '</div>';
+        if ($term->depth == 0) {
+          $output .= weblinks_display_category($term);
         }
       }
     }
-    else {
-      $output .= t('There are no existing categories or web links.');
-    }
+    // Need to dummy a term object for unclassified links.
+    $noclass = new stdClass();
+    $noclass->tid = 0;
+    $noclass->name = t('Unclassified');
+    $noclass->description = t('These links have not been assigned a category yet.');
+    $noclass->depth = 0;
+    $output .= weblinks_display_category($noclass);
   }
   else {
-    $maxdepth = variable_get('weblinks_maxdepth', 0); // See how many subcats to display on front page
-    $thisterm = taxonomy_get_term($tid);
-    $img = $taxo_img ? taxonomy_image_display($thisterm->tid) : null;
-    $output .= '<h3 class="weblinks">'. $img . check_plain($thisterm->name) .'</h3>';
-    if ($description) {
-      $output .= '<p>'. check_plain($thisterm->description) .'</p>';
-    }
-    $clickindex = count(taxonomy_get_parents_all($tid));
-    $clickthruindex = $clickindex - variable_get('weblinks_maxfrontdepth', 1);
-    $withlinks = TRUE;
-    //print "CTI is {$clickThruIndex}; CT id {$clickthru}";
-    if ($clickthruindex >= $clickthru) {
-      $maxdepth = NULL;
-    } // if we've reached the click through limit, display links
-    global $_weblinks_depth; // @@@ Bdragon: wtf?
-    $_weblinks_depth = 0;
-    $links = _weblinks_recurse_children($tid, $maxdepth, $withlinks);
-    if ($links == '') {
-      $output .= t('There are no existing categories or web links.');
-    }
-    else {
-      $output .= $links;
-    }
-  }
-
-  // Breadcrumb navigation
-  $breadcrumb = array();
-  $breadcrumb[] = array('path' => 'weblinks', 'title' => 'Web Links');
-  if ($parents = taxonomy_get_parents_all($tid)) {
-    $parents = array_reverse($parents);
-    foreach ($parents as $p) {
-      $breadcrumb[] = array('path' => 'weblinks/'. $p->tid, 'title' => $p->name);
+    $term = taxonomy_get_term($tid);
+    if (!isset($term->depth)) {
+      $term->depth = 0;
     }
+    $output .= weblinks_display_category($term);
   }
-  $breadcrumb[] = array('path' => 'weblinks/'. $tid);
-  menu_set_location($breadcrumb); // @@@ Bdragon: Probabaly a poor idea.
-
-  //return theme("page", $output);
   return $output;
 }
 
-function weblinks_user_form() {
-  global $user;
-  $filter = weblinks_build_filter_query();
-  $vid = _weblinks_get_vid();
-
-  // @@@ Bdragon: Danger, will robinson!
-  $result = db_query('SELECT n.* FROM {node} n '. $filter['join'] .
-    ' INNER JOIN {users} u ON n.uid = '. $user->uid .
-    ' INNER JOIN {term_node} tn ON tn.nid = n.nid'.
-    ' INNER JOIN {term_data} t ON t.tid = tn.tid'.
-    ' '. $filter['where'] .' AND t.vid = '. $vid .
-    ' ORDER BY n.changed DESC', $filter['args']);
-
-  $destination = drupal_get_destination();
-  $form['options'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Update options'),
-    '#prefix' => '<div class="container-inline">',
-    '#suffix' => '</div>',
-  );
-  $options = array();
-  $weblinks_options = array('delete'); // Should we allow "unpublish"?
-  $node_ops = module_invoke_all('node_operations');
-  foreach ($node_ops as $operation => $array) {
-    if (in_array($operation, $weblinks_options)) {
-      $options[$operation] = $array['label'];
-    }
-  }
-  $form['options']['operation'] = array('#type' => 'select', '#options' => $options,  '#default_value' => 'approve');
-  $form['options']['submit'] = array('#type' => 'submit', '#value' => t('Update'));
-
-  while ($node = db_fetch_object($result)) {
-    $nodes[$node->nid] = '';
-    $form['title'][$node->nid] = array('#value' => l($node->title, 'node/'. $node->nid) .' '. theme('mark', node_mark($node->nid, $node->changed)));
-    $form['status'][$node->nid] =  array('#value' =>  ($node->status ? t('published') : t('not published')));
-    $form['operations'][$node->nid] = array('#value' => l(t('edit'), 'node/'. $node->nid .'/edit', array(), $destination));
-  }
-  $form['nodes'] = array('#type' => 'checkboxes', '#options' => $nodes);
-  $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
-
-  return $form;
-}
-
 /**
  * Displays a Drupal page containing weblinks submitted by a given user.
  */
@@ -1454,16 +1394,26 @@
 
     case 'configure':
       list($type, $tid) = explode('-', $delta);
-      $form['weblinks_maxdisp_block'] = array(
+
+      $form['weblinks'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Web Links'),
+        '#collapsible' => true,
+        '#collapsed' => false,
+        );
+      
+      $disp = variable_get('weblinks_maxdisp_block_'. $tid, 10);
+      $disp = empty($disp) ? 10 : $disp;
+      $form['weblinks']['weblinks_maxdisp_block'] = array(
         '#type' => 'textfield',
         '#title' => t('Maximum weblinks to display'),
         '#size' => 3,
-        '#default_value' => variable_get('weblinks_maxdisp_block_'. $tid, 10),
+        '#default_value' => $disp,
         '#description' => t('The number of links to show in this block.'),
         );
 
       if ($tid == 'popular') {
-        $form['weblinks_popular_list_type'] = array(
+        $form['weblinks']['weblinks_popular_list_type'] = array(
           '#type' => 'radios',
           '#title' => t('List type'),
           '#options' => array('ul' => 'simple list', 'ol' => 'numbered list'),
@@ -1473,6 +1423,35 @@
           '#suffix' => '</div>',
           );
       }
+
+      if ($tid != 'recent' && $tid != 'popular') {
+        $sort_options = array(
+          'standard' => 'Normal Drupal - by weight, sticky, created date.',
+          'title' => 'Legacy - by weight and title.',
+          );
+        $form['weblinks']['weblinks_block_sort'] = array(
+          '#type' => 'radios',
+          '#options' => $sort_options,
+          '#title' => t('Block Sort order'),
+          '#default_value' => variable_get('weblinks_block_sort_'. $tid, 'title'),
+          '#description' => t('This determines how the links block will sort the links.'),
+          '#required' => false,
+          '#prefix' => '<div class="weblinks-radios">',
+          '#suffix' => '</div>',
+          );
+      }
+
+      $form['weblinks']['weblinks_block_urlnode'] = array(
+        '#type' => 'radios',
+        '#title' => t('Block links lead URL or to Web Link node'),
+        '#default_value' => variable_get('weblinks_block_urlnode_'. $tid, 'url'),
+        '#description' => t('Most people will want to leave this as URL. Specifying "node" makes it easier to edit the links, but requires an additional click to follow the link.'),
+        '#options' => array('url' => 'URL', 'node' => 'node'),
+        '#required' => FALSE,
+        '#prefix' => '<div class="weblinks-radios">',
+        '#suffix' => '</div>',
+        );
+
       return $form;
 
     case 'save':
