? test.patch
Index: weight.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/weight/weight.info,v
retrieving revision 1.1.2.2
diff -u -p -r1.1.2.2 weight.info
--- weight.info	19 Nov 2006 20:21:34 -0000	1.1.2.2
+++ weight.info	6 Mar 2007 08:15:48 -0000
@@ -1,4 +1,3 @@
 ; $Id: weight.info,v 1.1.2.2 2006/11/19 20:21:34 hlslaughter Exp $
 name = Weight
-description = Add weight-based sorting to nodes.
-package = Content
+description = Add weight-based sorting to nodes.
\ No newline at end of file
Index: weight.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/weight/weight.module,v
retrieving revision 1.17.2.1
diff -u -p -r1.17.2.1 weight.module
--- weight.module	19 Nov 2006 20:21:34 -0000	1.17.2.1
+++ weight.module	6 Mar 2007 08:15:50 -0000
@@ -3,34 +3,22 @@
 
 /**
  * @abstract
- * This module uses the sticky column of the node table
- * to add weighting to nodes
+ * This module uses the sticky column of the node table to add weighting to
+ * nodes.
  */
 
-define(USER_HELP,
-	'In a node list context (such as the front page or term pages), list items
-	(aka "teasers") will be ordered by "stickiness" then by "node weight" then by
-	"authored on" datestamp. Items with a higher node weight value will appear
-	above those with a lower value.'
-);
-
 /**
  * Implementation of hook_help()
- *
  */
 function weight_help($section) {
   switch($section) {
-    case 'admin/modules#name':
-      return t('Weight');
-    case 'admin/modules#description':
-      return t('Add weight-based sorting to nodes.');
     case 'admin/help#weight':
       return t('
-				<p><strong>Description:</strong> The weight module adds a weight option
-				to enabled node types. It uses the "sticky" field in the database to
-				store weights as well as sticky information (so that feature is not
-				lost). Nodes will be sorted first by stickiness, then by weight
-				(lightest to heaviest), then by creation date.</p>
+        <p><strong>Description:</strong> The weight module adds a weight option
+        to enabled node types. It uses the "sticky" field in the database to
+        store weights as well as sticky information (so that feature is not
+        lost). Nodes will be sorted first by stickiness, then by weight
+        (lightest to heaviest), then by creation date.</p>
 
         <p><strong>Setup:</strong> To enable weight sorting on existing nodes,
         visit the <a href="@setup">weight db setup page</a> and click
@@ -43,15 +31,15 @@ function weight_help($section) {
         to adjust weight on their own nodes. Find these settings <a
         href="@access">here</a>.</p>
 
-				<p><strong>Bulk weight management:</strong> You may easily manage the
-				weight of multiple nodes simultaneously by using the <a href="@node_admin">
+        <p><strong>Bulk weight management:</strong> You may easily manage the
+        weight of multiple nodes simultaneously by using the <a href="@node_admin">
         node admin page</a>.</p>
       ', array(
-					'@setup'          => url('admin/settings/weight/setup'),
-					'@access'         => url('admin/user/access'),
-					'@node_admin'     => url('admin/content/node')
-				)
-			);
+          '@setup'          => url('admin/settings/weight/setup'),
+          '@access'         => url('admin/user/access'),
+          '@node_admin'     => url('admin/content/node')
+        )
+      );
   }
 }
 
@@ -66,98 +54,67 @@ function weight_menu($may_cache){
   $items = array();
   if ($may_cache){
     $access = user_access('administer site configuration');
-    // bulk manage node weights
-    /* TODO: remove this
-    $items[] = array(
-      'path'     => 'admin/content/node/weight',
-      'title'    => t('Node Weights'),
-      'callback' => 'weight_manager',
-      'access'   => user_access('administer nodes'),
-      'type'     => MENU_LOCAL_TASK,
-      'weight'   => -1
-    );
-    */
-    // ajax callback for weight changer page
     $items[] = array(
-      'path'     => 'admin/node/weight/_change_weight',
-      'callback' => '_change_weight',
-      'access'   => user_access('administer nodes'),
-      'type'     => MENU_CALLBACK
+      'title'    => t('Weight'),
+      'path'     => 'admin/settings/weight',
+      'access'   => $access,
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('weight_settings_form')
     );
-    // top level settings
-    $items[] = array(
-			'title'    => t('Weight'),
-			'path'     => 'admin/settings/weight',
-			'access'   => $access,
-			'callback' =>'weight_settings_page'
-		);
 
     // 2nd level nav (tabs)
     $items[] = array(
-			'title'    => t('settings'),
-			'path'     => 'admin/settings/weight/settings',
-			'access'   => $access,
-			'callback' => 'weight_settings_page',
-			'type'     => MENU_DEFAULT_LOCAL_TASK
-		);
+      'title'    => t('Settings'),
+      'path'     => 'admin/settings/weight/settings',
+      'access'   => $access,
+      'callback' => 'weight_settings_page',
+      'type'     => MENU_DEFAULT_LOCAL_TASK
+    );
     $items[] = array(
-			'title'    => t('db setup'),
-			'path'     => 'admin/settings/weight/setup',
-			'access'   => $access,
-			'callback' => 'weight_enable_page',
-			'type'     => MENU_LOCAL_TASK,
-			'weight'   => 4
-		);
+      'title'    => t('DB setup'),
+      'path'     => 'admin/settings/weight/setup',
+      'access'   => $access,
+      'callback' => 'weight_enable_page',
+      'type'     => MENU_LOCAL_TASK,
+      'weight'   => 1
+    );
 
     // 3rd level
     $items[] = array(
-			'title'    => t('enable'),
-			'path'     => 'admin/settings/weight/setup/enable',
-			'access'   => $access,
-			'callback' => 'weight_enable_page',
-			'type'     => MENU_DEFAULT_LOCAL_TASK,
-			'weight'   => -2
-		);
+      'title'    => t('Enable'),
+      'path'     => 'admin/settings/weight/setup/enable',
+      'access'   => $access,
+      'callback' => 'weight_enable_page',
+      'type'     => MENU_DEFAULT_LOCAL_TASK,
+      'weight'   => -2
+    );
     $items[] = array(
-			'title'    => t('disable'),
-			'path'     => 'admin/settings/weight/setup/disable',
-			'access'   => $access,
-			'callback' => 'weight_disable_page',
-			'type'     => MENU_LOCAL_TASK,
-			'weight'   => 2
-		);
+      'title'    => t('Disable'),
+      'path'     => 'admin/settings/weight/setup/disable',
+      'access'   => $access,
+      'callback' => 'weight_disable_page',
+      'type'     => MENU_LOCAL_TASK,
+      'weight'   => 2
+    );
   }
   return $items;
 }
 
 function weight_nodeapi(&$node, $op) {
-	switch($op) {
-    case 'validate':
-    case 'submit':
-      if ($_POST['op'] == t('Submit')) {
-        // non-weighted nodes have a weight of zero
+  $types = variable_get('weight_node_types', array());
+  if (in_array($node->type, $types)) {
+    switch($op) {
+      case 'submit':
         if (is_null($node->node_weight)){
           $node->node_weight = 0;
         }
-
-        // here we're 'encoding' weight into the sticky value for the database
-        // stickiness is the inverse of weight
-        // - stickiness is sorted DESC while weight is sorted ASC so we invert
-        // the weight before saving... if sticky box is checked, add 100 to
-        // weight unweighted sticky nodes will have a value of 100
-        if($node->sticky) {
-          $node->sticky = (-1 * $node->node_weight) + 100;
-        }
-        // unweighted non-sticky nodes will have a value of -100
-        else {
-          $node->sticky = (-1 * $node->node_weight) - 100;
-        }
-      }
-      break;
-    case 'load':
-      $node->sticky = ($sticky > 0) ? 1 : 0;
-      $node->node_weight = _real_sticky2weight($sticky);
-      break;
+        $node->sticky = weight_real_to_encoded($node->node_weight, $node->sticky);
+        break;
+      case 'load':
+        $node->node_weight = weight_encoded_to_real($node->sticky);
+        $node->sticky = ($node->sticky > 0) ? 1 : 0;
+        break;
+    }
   }
 }
 
@@ -169,72 +126,37 @@ function weight_nodeapi(&$node, $op) {
  * and add some help text to the form.
  */
 function weight_form_alter($form_id, &$form){
-
   $weight_node_types = variable_get('weight_node_types', array_flip(node_get_types('names')));
 
   // The admin node page does not use the nodeapi for getting lists of nodes, so
   // i never have a chance to convert the sticky flag to 0/1. and trying to
   // filter on that field will not work. therefore, i am going to hide the
   // 'filter on sticky status' altogether when weight_module is enabled
-  if ( $form_id == 'node_filter_form' ) {
+  if ($form_id == 'node_filter_form') {
     // no comment
     unset($form['filters']['status']['status']['#options']['sticky-1']);
     unset($form['filters']['status']['status']['#options']['sticky-0']);
-    $form['weight_help'] = array(
-      '#type'  => 'markup',
-      '#value' => t('<strong>Note:</strong> When the weight module is enabled,
-        it is not possible to filter based on sticky status.')
-    );
-  }
-
-  if ( $form_id == 'node_admin_nodes' ) {
-
-    // i can't add a table header for weight, so instead i'm going to explain
-    // the weight dropdown to the user. Also, to position my help text
-    // appropriately, i'm using this '#suffix' hack rather than adding
-    // a form property as i'd like to do.
-    $form['options']['#suffix'] = t('
-      <strong>Weight:</strong> To change the weight of a node, select a value
-      from the corresponding dropdown box under <i>@operations</i>. Node weights
-      are submitted immediately. Selectors are only available for node types
-      configured on the <a href="@weight_admin">weight admin page</a>.
-      ', array(
-                '@weight_admin' => url('admin/settings/weight'),
-                '@operations'   => 'Operations'
-              )
-    );
-
-    // add my weight selector under the operations section of the admin node
-    // overview page (admin/content/node)
-    if ( !empty($form['operations']) ) {
-      foreach ( $form['operations'] as $nid => $title ) {
-        // only add weight selector if weight is enabled for this node type
-        if ( in_array($form['name'][$nid]['#value'], $weight_node_types) ) {
-          $form['operations'][$nid]['weight_selector']['#value'] =  weight_node_selector($nid);
-        }
-      }
-    }
   }
 
   // node edit page weight selector
   if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
-    if (user_access('assign node weight') || user_access('administer nodes')) {
+    if (user_access('assign node weight')) {
       $node = $form['#node'];
       if (in_array($node->type, $weight_node_types)) {
         $range = variable_get('weight_range', 20);
         $form["weight_fieldset"] = array(
-          '#type'        => 'fieldset',
-          '#title'       => t('Node Weight'),
+          '#type' => 'fieldset',
+          '#title' => t('Weight'),
           '#collapsible' => true,
-          '#collapsed'   => ($node->node_weight == 0),
-          '#weight'      => 2,
+          '#collapsed' => ($node->node_weight == 0),
+          '#weight' => 2
         );
         $form["weight_fieldset"]["node_weight"] = array(
-          '#type'          => 'weight',
-          '#title'         => t("Node Weight"),
-          '#default_value' => $node->node_weight,
-          '#delta'         => $range,
-          '#description'   => t(USER_HELP),
+          '#type' => 'weight',
+          '#title' => t("Display order"),
+          '#default_value' => isset($node->node_weight) ? $node->node_weight : 0,
+          '#description' => t('Lighter elements float to the top; Heavier elements sink to the bottom.'),
+          '#delta' => $range
         );
       }
     }
@@ -247,106 +169,86 @@ function weight_enable_page(){
     drupal_goto('admin/settings/weight');
   }
   $count = db_num_rows(db_query('SELECT DISTINCT nid FROM {node} WHERE sticky IN (0,1)'));
-	$output = t('<p>The weight module uses the node table\'s "sticky" column to
-		store weight information for each node. New and updated nodes will
-		automatically have their sticky and weight information remapped. However if
-		you have pre-existing nodes, you will need to update your database so that
-		these nodes sort correctly with new nodes.</p> <p>%count nodes need to be
-		updated.</p>',
-		array('%count' => $count));
-	$output .= drupal_get_form('weight_setup_form');
+  $output = t('<p>The weight module uses the node table\'s "sticky" column to
+    store weight information for each node. New and updated nodes will
+    automatically have their sticky and weight information remapped. However if
+    you have pre-existing nodes, you will need to update your database so that
+    these nodes sort correctly with new nodes.</p> <p>%count nodes need to be
+    updated.</p>',
+    array('%count' => $count));
+  $output .= drupal_get_form('weight_setup_form');
   return $output;
 }
 
 function weight_setup_form() {
-	$form[] = array(
-		'#type'  => 'submit',
-		'#value' => t('Setup Database'),
-	);
+  $form[] = array('#type' => 'submit', '#value' => t('Setup Database'));
+
   return($form);
 }
 
 function weight_disable_page() {
   if ($_POST['op'] == t('Remove weights')) {
     weight_disable();
-    drupal_goto('admin/modules');
+    drupal_goto('admin/build/modules');
   }
-	$output .= t('<p>Before disabling the weight module, you will want to click
-		this button to change the database back to Drupal\'s conventional sticky
-		system.</p> <p><strong>NOTE: Clicking this button will erase any node weights
-		that have been set.</strong></p>');
-	$output .= drupal_get_form('weight_disable_form');
+  $output .= t('<p>Before disabling the weight module, you will want to click
+    this button to change the database back to Drupal\'s conventional sticky
+    system.</p> <p><strong>NOTE: Clicking this button will erase any node weights
+    that have been set.</strong></p>');
+  $output .= drupal_get_form('weight_disable_form');
+
   return $output;
 }
 
 function weight_disable_form() {
-	$form[] = array(
-		'#type'  => 'submit',
-		'#value' => t('Remove weights'),
-	);
-  return($form);
-}
-function weight_settings_page() {
-
-  if ($_POST['op'] == t('Update')) {
-/*
-    $edit = $_POST['edit'];
-    foreach($edit as $key => $item) {
-drupal_set_message("got $key -> $item");
-      if( is_array($item) ){
-        $item = array_keys($item);
-      }
-      variable_set($key, $item);
-    }
-*/
-    $weight_range = check_plain($_POST['weight_range']);
-    $weight_node_types = array_keys($_POST['weight_node_types']);
-
-    variable_set('weight_range', $weight_range);
-    variable_set('weight_node_types', $weight_node_types);
+  $form[] = array('#type' => 'submit', '#value' => t('Remove weights'));
 
-    drupal_set_message(t('Settings updated.'));
-  }
-	$output .= drupal_get_form('weight_settings_form');
-  return $output;
+  return($form);
 }
 
 function weight_settings_form() {
   $form = array();
-	$types = node_get_types('names');
-
-	$form['weight_range'] = array(
-		'#type'          => 'select',
-		'#title'         => t('Node Weight Range'),
-		'#default_value' => variable_get('weight_range', 20),
-		'#options'       => array(5=>5, 10=>10, 20=>20, 30=>30, 40=>40),
-		'#description'   => t('<p>This will be the +/- range for node weight.</p>'),
-	);
-	$form['weight_node_types'] = array(
-		'#type'          => 'checkboxes',
-		'#title'         => t('Display On'),
-		'#default_value' => variable_get('weight_node_types', $types),
-		'#options'       => $types,
-		'#description'   => t('<p>Add node weighting to these content types.</p>
-      <p><i>Note:</i> Unselecting a node type after having changed weights
-      for nodes of that type will leave these nodes with the current weight.
-      You may want to check the <a href="@posts_page">Post page</a>. Before
-      unsetting any node types.',
-      array( '@posts_page' => url('admin/content/node'))
+  $types = node_get_types('names');
+  $form['weight_range'] = array(
+    '#type' => 'select',
+    '#title' => t('Node Weight Range'),
+    '#default_value' => variable_get('weight_range', 20),
+    '#options' => array(
+      5 => 5,
+      10 => 10,
+      20 => 20,
+      30 => 30,
+      40 => 40
     ),
-	);
-	$form[] = array(
-		'#type'  => 'submit',
-		'#value' => t('Update'),
-	);
+    '#description' => t('<p>This will be the +/- range for node weight.</p>')
+  );
+  $form['weight_node_types'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Display On'),
+    '#default_value' => variable_get('weight_node_types', $types),
+    '#options' => $types,
+    '#description' => t('<p>Add node weighting to these content types.</p>', array( '@posts_page' => url('admin/content/node'))
+    )
+  );
+  $form[] = array('#type' => 'submit', '#value' => t('Update'));
+
   return $form;
 }
 
+function weight_settings_form_submit($form_id, $form_values) {
+  $weight_range = $form_values['weight_range'];
+  $weight_node_types = array_keys(array_filter($form_values['weight_node_types']));
+
+  variable_set('weight_range', $weight_range);
+  variable_set('weight_node_types', $weight_node_types);
+
+  drupal_set_message(t('Settings updated.'));
+}
+
 /**
  * Update "old" nodes where sticky is 1 or 0
  * And resave them with new values so that they will sort correctly
  * with new and updated nodes
- *
  */
 function weight_old_nodes(){
   $count = db_num_rows(db_query('SELECT DISTINCT nid FROM {node} WHERE sticky IN (0,1)'));
@@ -372,160 +274,35 @@ function weight_disable(){
 }
 
 /**
- * Allow for bulk editing of node weights via admin/node page (aka 'content')
+ * Convert encoded value to actual submitted weight.
  */
-
-/* TODO: remove this
-
-function weight_manager() {
-
-  drupal_set_html_head('<script type="text/javascript" src="' . base_path() .
-    drupal_get_path('module', 'weight') . "/httpRequest.js" . '"></script>');
-
-  $destination = drupal_get_destination();
-  $header = array();
-  $anon_username = variable_get('anonymous', 'Anonymous');
-  $output = '';
-
-  $output .= t('<p>Node weight changes are submitted automatically. There is no
-    need to submit this page after changing node weights. To re-sort nodes after
-    making changes, simply <a href="javascript:location.reload()">reload this
-    page</a></p>');
-
-  $output .= drupal_get_form('node_filter_form');
-
-  // basic queries
-  $sql = "
-    SELECT n.nid, n.title, n.type, n.uid, u.name author, n.sticky stored_sticky,
-      ABS(n.sticky) -100 weight, n.created
-    FROM {node} n
-    LEFT JOIN users u ON u.uid = n.uid
-  ";
-
-  $header = array(
-    array('data' => t('Weight'),  'field'  => 'weight'),
-    array('data' => t('Title'),   'field'  => 'title'),
-    array('data' => t('Type'),    'field'  => 'type'),
-    array('data' => t('Author'),  'field'  => 'author'),
-    array('data' => t('Created'), 'field'  => 'created'),
-  );
-
-  $sql .= tablesort_sql($header);
-  $count = db_num_rows(db_query($sql));
-
-  if ( $count ) {
-    $count_query = "SELECT $count";
-    $result = pager_query($sql, 50, 0, $count_query);
-
-		// ugly bit of javascript we use for each dropdown to submit weight changes
-		// in the background. Relies on even uglier httpRequest.js file that comes
-		// with this module. Ironically, Ajax makes me feel dirty
-    $selector_template = "\n" . "<form><select style=\"margin: 0;\" onchange='httpRequest(\"GET\", \"" .
-			base_path() . "admin/node/weight/_change_weight/\" + [NID] + \"/\" + this.options[this.selectedIndex].value,true)' >";
-
-    $weight_range = variable_get('weight_range', 20);
-    for ($i=0-$weight_range; $i <= $weight_range; $i++ ) {
-      $selector_template .= "\n" . "<option value='$i'>$i</option>";
-    }
-    $selector_template .= "\n" . '</select></form>' . "\n";
-
-    while ($data = db_fetch_object($result)) {
-      $weight_selector = $selector_template;
-      $weight_selector = preg_replace("/(value='{$data->weight}')/", "$1 selected='selected'", $weight_selector);
-      $weight_selector = preg_replace("/\[NID\]/", $data->nid, $weight_selector);
-      $weight_selector = '<span style="height: 50%;" id="weight_selector">' . $weight_selector . '</span>';
-
-      $rows[] = array(
-        $weight_selector,
-        l($data->title, 'node/'. $data->nid),
-        $data->type,
-        !empty($data->author) ? l($data->author, 'user/' . $data->uid) : $anon_username,
-        format_date($data->created, 'small'),
-      );
-    }
-  } else {
-    $node_list .= "<h2>No results found</h2>";
+function weight_encoded_to_real($sticky) {
+  if ($sticky > 0) {
+    $weight = $sticky - 100;
+  }
+  else {
+    $weight = $sticky + 100;
   }
-  $output .= theme('table', $header, $rows) . theme('pager', NULL, 50);
-  return $output;
-}
 
-*/
+  return($weight);
+}
 
 /**
- * Generate JS code for selecting individual node wieghts on admin page
+ * Convert submitted weight to an encoded value incorporating the sticky status.
  */
-function weight_node_selector($nid) {
-
-  static $js_included;
-
-  if ( !$js_included ) {
-    drupal_set_html_head('<script type="text/javascript" src="' . base_path() .
-      drupal_get_path('module', 'weight') . "/httpRequest.js" . '"></script>');
-    $js_included = TRUE;
-  }
-
-  $selector_template = "\n" . "<form><select style=\"margin: 0;\"
-    onchange='httpRequest(\"GET\", \"" . base_path() .
-    "admin/node/weight/_change_weight/\" + [NID] + \"/\" +
-    this.options[this.selectedIndex].value,true)' >";
-
-  // i don't have easy access to weight or promote, so i'll need to fetch that
-  $sticky = db_result(db_query('SELECT sticky FROM {node} WHERE nid = %d', $nid));
-  $weight = _real_sticky2weight($sticky) - 200; // convert to our weight range
-
-  // ugly bit of javascript we use for each dropdown to submit weight changes
-  // in the background. Relies on even uglier httpRequest.js file that comes
-  // with this module. Ironically, Ajax makes me feel dirty
-
-  $weight_range = variable_get('weight_range', 20);
-  for ($i=0-$weight_range; $i <= $weight_range; $i++ ) {
-    $selector_template .= "<option value='$i'>$i</option>";
-  }
-  $selector_template .= '</select></form>';
-  $weight_selector = $selector_template;
-  // TODO: need node weight to be able to choose this!
-  $weight_selector = preg_replace("/(value='$weight')/", "$1 selected='selected'", $weight_selector);
-  $weight_selector = preg_replace("/\[NID\]/", $nid, $weight_selector);
-  $weight_selector = '<div class="weight_selector" style="display: inline; float: left;
-    margin: -18px 0 0 35px; position: relative;">' . $weight_selector . '</div>';
-  return($weight_selector);
-}
-
-// ajax callback for weight manager page
-function _change_weight($nid, $weight) {
-  // this is a lot of unnecessary overhead. TODO: just do DB calls
-  $node = node_load($nid);
-
-  // duplicate code
-  if($node->sticky) {
-    $node->sticky = (-1 * $weight) + 100;
+function weight_real_to_encoded($weight, $sticky = 0) {
+  // here we're 'encoding' weight into the sticky value for the database
+  // stickiness is the inverse of weight
+  // - stickiness is sorted DESC while weight is sorted ASC so we invert
+  // the weight before saving... if sticky box is checked, add 100 to
+  // weight unweighted sticky nodes will have a value of 100
+  if ($sticky) {
+    $sticky = $weight + 100;
   }
   // unweighted non-sticky nodes will have a value of -100
   else {
-    $node->sticky = (-1 * $weight) - 100;
+    $sticky = $weight - 100;
   }
-  node_save($node);
-}
 
-// convert our weight to 'encoded' sticky value for DB
-function _weight2encoded_sticky($weight) {
-  if( $weight > 0 ) {
-    $sticky = (-1 * $node->node_weight) + 100;
-  }
-  // unweighted non-sticky nodes will have a value of -100
-  else {
-    $node->sticky = (-1 * $node->node_weight) - 100;
-  }
+  return $sticky;
 }
-function _real_sticky2weight($sticky) {
-  if ( $sticky ) {
-    $weight = ($sticky - 100) * -1;
-  }
-  else {
-    $weight = ($sticky + 100) * -1;
-  }
-  return($weight);
-}
-
-?>
