? .buildpath
? .cvsignore
? .project
? .settings
Index: weight.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/weight/weight.module,v
retrieving revision 1.23.2.12
diff -u -p -r1.23.2.12 weight.module
--- weight.module	23 Jul 2010 02:27:14 -0000	1.23.2.12
+++ weight.module	3 Nov 2010 18:45:43 -0000
@@ -107,12 +107,6 @@ function weight_nodeapi(&$node, $op) {
  * @ingroup themeable
  */
 function theme_weight_node_admin_nodes($form) {
-  $weight_node_types = variable_get('weight_node_types', array_flip(node_get_types('names')));
-
-  $weight_node_type_names = array();
-  foreach ($weight_node_types as $type) {
-    $weight_node_type_names[] = node_get_types('name', $type);
-  }
 
   // If there are rows in this form, then $form['title'] contains a list of
   // the title form elements.
@@ -129,13 +123,6 @@ function theme_weight_node_admin_nodes($
   $output .= drupal_render($form['options']);
   if ($has_posts) {
     foreach (element_children($form['title']) as $key) {
-      $one_of_ours = in_array($form['name'][$key]['#value'], $weight_node_type_names);
-
-      // Add a weight element by copying and modifying the title element.
-      $s = weight_node_selector($key);
-      $form['weight'][$key] = $form['title'][$key];
-      $form['weight'][$key]['#value'] = $one_of_ours ? $s['selector'] : '';
-      $form['status'][$key]['#value'] = $s['status'];
 
       $row = array();
       $row[] = drupal_render($form['nodes'][$key]);
@@ -165,54 +152,6 @@ function theme_weight_node_admin_nodes($
 }
 
 /**
- * Generate JS code for selecting individual node weights on admin page
- */
-function weight_node_selector($nid) {
-  static $js_included;
-
-  if (!$js_included) {
-    $path = drupal_get_path('module', 'weight');
-    drupal_add_js($path .'/httpRequest.js', 'module', 'header', TRUE);
-    $js_included = TRUE;
-    drupal_add_css($path .'/weight.css');
-  }
-
-  $selector_template = "<select style=\"margin: 0;\"
-    onchange='httpRequest(\"GET\", \"" . base_path() .
-    "?q=admin/node/weight/_weight_change/\" + [NID] + \"/\" +
-    this.options[this.selectedIndex].value,true)' >";
-
-  // Get more stuff about the node.
-  $node = db_fetch_object(db_query("SELECT nid, `status`, sticky, promote, translate, moderate FROM {node} WHERE nid = %d", $nid));
-
-  // Convert to our weight range.
-  _weight_decode($node);
-  $weight = $node->node_weight;
-
-  // 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 = - $weight_range; $i <= $weight_range; ++$i) {
-    $selector_template .= "<option value='$i'". ($i == $node->node_weight ? " selected='selected'" : '') .">$i</option>";
-  }
-  $selector_template .= '</select>';
-  $weight_selector = str_replace('[NID]', $nid, $selector_template);
-
-  $status = $node->status ? t('published') : NULL;
-  $status .= $node->sticky ? '<br />'. t('sticky') : NULL;
-  $status .= $node->promote ? '<br />'. t('promoted') : NULL;
-  $status .= $node->translate ? '<br />'. t('translate') : NULL;
-  $status .= $node->moderate ? '<br />'. t('moderated') : NULL;
-
-  return array(
-    'selector' => '<div class="weight-selector">'. $weight_selector .'</div>',
-    'status' => $status,
-    );
-}
-
-/**
  * Implementation of hook_form_alter().
  *
  * This is where we tweak the admin/content/node to include our weight
@@ -271,13 +210,80 @@ function weight_form_alter(&$form, $form
 }
 
 /**
+ * Implementation of hook_form_FORM_ID_alter().
+ */
+function weight_form_node_admin_content_alter(&$form, $form_state) {
+  $weight_node_types = variable_get('weight_node_types', array_flip(node_get_types('names')));
+
+  $weight_node_type_names = array();
+  foreach ($weight_node_types as $type) {
+    $weight_node_type_names[] = node_get_types('name', $type);
+  }
+  
+  $weight_range = variable_get('weight_range', 20);
+  $weights = array();
+  for ($i = - $weight_range; $i <= $weight_range; ++$i) {
+    $weights[$i] = $i;
+  }
+  $form['admin']['weight']['#tree'] = TRUE;
+  foreach($form['admin']['title'] as $nid => $value) {
+    $one_of_ours = in_array($form['admin']['name'][$nid]['#value'], $weight_node_type_names);
+    if ($one_of_ours) {
+      // Get more stuff about the node.
+      $node = db_fetch_object(db_query("SELECT nid, status, sticky, promote, translate, moderate FROM {node} WHERE nid = %d", $nid));
+
+      // Convert to our weight range.
+      _weight_decode($node);
+      $weight = $node->node_weight;
+      $form['admin']['weight'][$nid] = array(
+        '#type' => 'select',
+        '#options' => $weights,
+        '#default_value' => $weight,
+        '#ahah' => array(
+          'path' => 'admin/node/weight/_weight_change/'. $nid,
+          'event' => 'change',
+        )
+      );
+      $status = $node->status ? t('published') : NULL;
+      $status .= $node->sticky ? '<br />'. t('sticky') : NULL;
+      $status .= $node->promote ? '<br />'. t('promoted') : NULL;
+      $status .= $node->translate ? '<br />'. t('translate') : NULL;
+      $status .= $node->moderate ? '<br />'. t('moderated') : NULL;
+      $form['admin']['status'][$nid]['#value'] = $status;
+    }
+    else {
+      $form['admin']['weight'][$nid]['#value'] = '';
+    }
+  }
+}
+
+/**
  * Ajax callback for weight manager page.
  */
-function _weight_change($nid, $weight) {
+function _weight_change($nid) {
+  $form_state = array('storage' => NULL, 'rebuild' => TRUE);
+  $form_build_id = $_POST['form_build_id'];
+  include_once 'modules/node/node.admin.inc';
+  // Step #4.
+  $form = form_get_cache($form_build_id, $form_state);
+
+  // Preparing for #5.
+  $args = $form['#parameters'];
+  $form_id = array_shift($args);
+  $form_state['post'] = $form['#post'] = $_POST;
+  $form['#programmed'] = $form['#redirect'] = FALSE;
+
+  // Step #5.
+  drupal_process_form($form_id, $form, $form_state);
+  // Step #6 and #7 and #8.
+  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
   // Doing it this way preserves the revision information.
   $node = node_load($nid);
-  $node->node_weight = $weight;
+  $node->node_weight = $form_state['post']['weight'][$nid];
   node_save($node);
+  $output = $form_state['post']['weight'][$nid];
+  // Final rendering callback.
+  drupal_json(array('status' => TRUE, 'data' => $output));
 }
 
 /**
