Fixed update weights callback. 7.x-1.0-rc2
authorMatthias Hutterer
Wed, 20 Feb 2013 08:58:29 +0000 (09:58 +0100)
committerMatthias Hutterer
Wed, 20 Feb 2013 08:58:29 +0000 (09:58 +0100)
js/updateWeight.js
taxonomy_manager.admin.inc

index c55aec8..34881d7 100644 (file)
@@ -6,13 +6,16 @@
 (function ($) {
 
 //object to store weights (tid => weight)
-var weights = new Object();
+var termWeightsData = new Object();
 
 Drupal.behaviors.TaxonomyManagerWeights = {
   attach: function(context, settings) {
     var weightSettings = settings.updateWeight || [];
     if (!$('#edit-toolbar.tm-weights-processed').length) {
       $('#edit-toolbar').addClass('tm-weights-processed');
+      termWeightsData['form_token'] = $('input[name=form_token]').val();
+      termWeightsData['form_id'] = $('input[name=form_id]').val();
+      termWeightsData['weights'] = new Object();
       Drupal.attachUpdateWeightToolbar(weightSettings['up'], weightSettings['down']);
       Drupal.attachUpdateWeightTerms();
     }
@@ -36,7 +39,7 @@ Drupal.attachUpdateWeightToolbar = function(upButton, downButton) {
       Drupal.orderTerms(upTerm, downTerm);
     }
     if (selected.length > 0) {
-      $.post(url, weights);
+      $.post(url, termWeightsData);
     }
   });
 
@@ -50,7 +53,7 @@ Drupal.attachUpdateWeightToolbar = function(upButton, downButton) {
       Drupal.orderTerms(upTerm, downTerm);
     }
     if (selected.length > 0) {
-      $.post(url, weights);
+      $.post(url, termWeightsData);
     }
   });
 }
@@ -92,7 +95,7 @@ Drupal.attachUpdateWeightTerms = function(parent, currentIndex) {
       var downTerm = $(upTerm).prev();
 
       Drupal.orderTerms(upTerm, downTerm);
-      $.post(url, weights);
+      $.post(url, termWeightsData);
 
       $(downTerm).find(termLineClass).unbind('mouseover');
       setTimeout(function() {
@@ -110,7 +113,7 @@ Drupal.attachUpdateWeightTerms = function(parent, currentIndex) {
       var upTerm = $(downTerm).next();
 
       Drupal.orderTerms(upTerm, downTerm);
-      $.post(url, weights);
+      $.post(url, termWeightsData);
 
       $(upTerm).find(termLineClass).unbind('mouseover');
       setTimeout(function() {
@@ -180,12 +183,12 @@ Drupal.swapWeights = function(upTerm, downTerm) {
 
   //same weight, decrease upTerm
   if (upWeight == downWeight) {
-    weights[upTid] = --upWeight;
+    termWeightsData['weights'][upTid] = --upWeight;
   }
   //different weights, swap
   else {
-    weights[upTid] = downWeight;
-    weights[downTid] = upWeight;
+    termWeightsData['weights'][upTid] = downWeight;
+    termWeightsData['weights'][downTid] = upWeight;
   }
 
   //update prev siblings if necessary
@@ -194,7 +197,7 @@ Drupal.swapWeights = function(upTerm, downTerm) {
       $(upTerm).prevAll().each(function() {
         var id = Drupal.getTermId(this);
         var weight = Drupal.getWeight(this);
-        weights[id] = --weight;
+        termWeightsData['weights'][id] = --weight;
       });
     }
   } catch(e) {
@@ -207,7 +210,7 @@ Drupal.swapWeights = function(upTerm, downTerm) {
       $(downTerm).nextAll().each(function() {
         var id = Drupal.getTermId(this);
         var weight = Drupal.getWeight(this);
-        weights[id] = ++weight;
+        termWeightsData['weights'][id] = ++weight;
       });
     }
   } catch(e) {
@@ -223,8 +226,8 @@ Drupal.getWeight = function(li) {
   var id = Drupal.getTermId(li);
   var weight;
 
-  if (weights[id] != null) {
-    weight = weights[id];
+  if (termWeightsData['weights'][id] != null) {
+    weight = termWeightsData['weights'][id];
   }
   else {
     weight = $(li).find("input:hidden[class=weight-form]").attr("value");
index c60244b..024bdff 100644 (file)
@@ -2491,14 +2491,15 @@ function taxonomy_manager_autocomplete_search_terms($typed_input, $vid, $include
  *
  */
 function taxonomy_manager_update_weights() {
-  $weights = $_POST;
-  if (is_array($weights)) {
-    foreach ($weights as $tid => $weight) {
-      if (is_numeric($tid) && is_numeric($weight)) {
-        $term = taxonomy_term_load($tid);
-        $term->weight = $weight;
-        taxonomy_term_save($term);
-        //db_query("UPDATE {term_data} SET weight = %d WHERE tid = %d", $weight, $tid);
+  $submitted_data = $_POST;
+  if (isset($submitted_data['form_token']) && isset($submitted_data['form_id']) && !empty($submitted_data['weights']) && is_array($submitted_data['weights'])) {
+    if (drupal_valid_token($submitted_data['form_token'], $submitted_data['form_id'])) {
+      foreach ($submitted_data['weights'] as $tid => $weight) {
+        if (is_numeric($tid) && is_numeric($weight)) {
+          $term = taxonomy_term_load($tid);
+          $term->weight = $weight;
+          taxonomy_term_save($term);
+        }
       }
     }
   }