? ng3-sort-images-presets.patch
Index: node_gallery.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_gallery/node_gallery.inc,v
retrieving revision 1.2.2.72
diff -u -p -r1.2.2.72 node_gallery.inc
--- node_gallery.inc	23 Nov 2010 00:14:53 -0000	1.2.2.72
+++ node_gallery.inc	23 Nov 2010 14:06:49 -0000
@@ -338,27 +338,6 @@ function node_gallery_get_image_nids($gi
 }
 
 /**
- * Builds an array of image nids, regardless of publish status and permissions.
- * 
- * @param object $gallery
- *   A populated gallery node object.
- *
- * @param bool $sorted
- *   When true, sorts the nids based on weight and nid.
- * 
- * @return
- *   An array of nids.
- */
-function node_gallery_get_all_image_nids($gallery, $sorted = FALSE) {
-  $items = array();
-  
-  if (in_array($gallery->type, node_gallery_get_types('gallery'))) {
-    $items = node_gallery_get_image_nids($gallery->nid, $sorted, FALSE);
-  }
-  return $items;
-}
-
-/**
  * Given a gallery, returns it's cover image.
  * 
  * @param object $gallery
@@ -439,7 +418,7 @@ function node_gallery_clear_gallery_cach
  * @param $gid
  *   The nid of the gallery to build caches for.
  */
-function node_gallery_cache_image_lists($gid) {
+function _node_gallery_cache_sorted_image_nids($gid) {
   $image_list = node_gallery_get_image_nids($gid, TRUE, TRUE);
   $image_postition = array();
   $position = 1;
@@ -470,7 +449,7 @@ function node_gallery_get_image_position
       $image_position[$gid] = $cache->data;
     }
     else {
-      node_gallery_cache_image_lists($gid);
+      _node_gallery_cache_sorted_image_nids($gid);
       $cache =  cache_get('node_gallery:image_position:'. $gid);
       $image_position[$gid] = $cache->data;
     }    
@@ -530,7 +509,7 @@ function node_gallery_get_image_list($gi
       $image_list[$gid] = $cache->data;
     }
     else {
-      node_gallery_cache_image_lists($gid);
+      _node_gallery_cache_sorted_image_nids($gid);
       $cache = cache_get('node_gallery:image_list:'. $gid);
       $image_list[$gid] = $cache->data;
     }    
Index: node_gallery.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_gallery/node_gallery.module,v
retrieving revision 1.18.2.127
diff -u -p -r1.18.2.127 node_gallery.module
--- node_gallery.module	23 Nov 2010 01:49:44 -0000	1.18.2.127
+++ node_gallery.module	23 Nov 2010 11:12:07 -0000
@@ -79,7 +79,8 @@ function node_gallery_help($path, $arg) 
       }
       break;
     case 'node/%/sort':
-      $output = '<p>'. t('To sort the gallery, drag the images below. When done, be sure to save your changes.  ');
+      $output = '<div>' . t('This page can be used to override the image sorting in this gallery with a static order. ') . '</div>';
+      $output .= '<p>'. t('To sort the gallery, drag the images below. When done, be sure to save your changes.  ');
       if (module_exists('jquery_ui') && variable_get('node_gallery_jquery_ui_integration', TRUE)) {
         if (arg(3) == 'no_jquery') {
           $output .= t('If you wish to use the jquery drag & drop sorting, <a href="!jquery">click here</a>.', array('!jquery' => url('node/'. $arg[1] .'/sort'))) .'</p>';
@@ -175,6 +176,14 @@ function node_gallery_menu() {
     'weight' => -4,
     'type' => MENU_LOCAL_TASK,
   );
+  $items['node_gallery_ajax_get_sort/%node_gallery_gallery'] = array(
+      'page callback' => 'node_gallery_sort_images_ajax_get_sort',
+      'page arguments' => array(1,2,3),
+      'access callback' => 'node_gallery_user_access',
+      'access arguments' => array('edit image', 1),
+      'file' => 'node_gallery.pages.inc',
+      'type' => MENU_CALLBACK,
+  );
   $items['node/%node_gallery_gallery/images'] = array(
     'title' => 'Manage Images',
     'page callback' => 'drupal_get_form',
@@ -601,7 +610,7 @@ function _node_gallery_delete(&$node) {
  */
 function _node_gallery_set_publish(&$node, $status) {
   $gid = $node->nid;
-  $imagenids = node_gallery_get_all_image_nids($node);
+  $imagenids = node_gallery_get_image_nids($node, FALSE, FALSE);
   // Split our operations into NODE_GALLERY_BATCH_CHUNK_SIZE a time
   $node_updates = array_chunk($imagenids, NODE_GALLERY_BATCH_CHUNK_SIZE);
   array_walk_recursive($node_updates,
Index: node_gallery.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_gallery/node_gallery.pages.inc,v
retrieving revision 1.12.2.61
diff -u -p -r1.12.2.61 node_gallery.pages.inc
--- node_gallery.pages.inc	22 Nov 2010 19:20:15 -0000	1.12.2.61
+++ node_gallery.pages.inc	23 Nov 2010 14:36:49 -0000
@@ -154,7 +154,7 @@ function node_gallery_manage_images_form
     unset($_SESSION['node_gallery_plupload_nids'][$gallery->nid]);
   }
   else {
-    $images = node_gallery_get_all_image_nids($gallery, TRUE);
+    $images = node_gallery_get_image_nids($gallery, TRUE, FALSE);
     $chunks = array_chunk($images, $items_per_page);
     $images = $chunks[$pager_page_array[$element]];
   }
@@ -517,7 +517,12 @@ function node_gallery_sort_images_form($
   );
   $form['submit'] = array(
     '#type' => 'submit',
-    '#value' => t('Save')
+    '#value' => t('Save sorting')
+  );
+  $form['remove'] = array(
+    '#type' => 'submit',
+    '#value' => t('Delete sorting'),
+    '#submit' => array('node_gallery_sort_images_form_remove_weights_submit'),
   );
   $form['jquery_ui_integration'] = array(
     '#type' => 'value',
@@ -526,6 +531,63 @@ function node_gallery_sort_images_form($
   return $form;
 }
 
+function node_gallery_sort_images_ajax_get_sort($gallery, $op = '', $toggle = FALSE) {
+  $image_nids = node_gallery_get_image_nids($gallery->nid, TRUE, FALSE);
+  $image_count = count($image_nids);
+  $relationship = node_gallery_get_relationship($gallery->type);
+  $field_name = $relationship['imagefield_name'];
+  $filepaths = node_gallery_get_image_filepath($image_nids, $field_name);
+  $sql = 'SELECT n.nid,n.title,n.status,n.created,n.changed,ngi.weight FROM {node} n INNER JOIN {node_revisions} r ON r.vid = n.vid INNER JOIN {node_gallery_images} ngi ON ngi.nid = n.nid WHERE n.nid IN ('. db_placeholders($image_nids) . ')';
+  $node_result = db_query($sql, $image_nids);
+  while ($node = db_fetch_object($node_result)) {
+    $nodes_unsorted[$node->nid] = $node;
+  }
+  for ($i = 0; $i < $image_count; ++$i) {
+    $nid = $image_nids[$i];
+    $nodes_unsorted[$nid]->filepath = $filepaths[$i];
+    $nodes[] = $nodes_unsorted[$nid];
+  }
+  $sort_function = NULL;
+  switch ($op) {
+    case 'title':
+      $sort_function = create_function('$a,$b', 'return strnatcasecmp($a->title, $b->title);');
+      break;
+
+    case 'filename':
+      $sort_function = create_function('$a,$b', 'return strnatcasecmp(basename($a->filepath), basename($b->filepath));');
+      break;
+
+    case 'reset':
+    default:
+  }
+  if ($sort_function && uasort($nodes, $sort_function) === FALSE) {
+    $image_nids = FALSE;
+  } else {
+    $image_nids = array();
+    foreach ($nodes as $node) {
+      $image_nids[] = $node->nid;
+    }
+    if ($op != 'reset' && $toggle) {
+      $image_nids = array_reverse($image_nids);
+    }
+  }
+  return drupal_json($image_nids);
+}
+
+
+/**
+ * Submit function to reset all image weights in a gallery to 0, effectively removing any custom sort.
+ */
+function node_gallery_sort_images_form_remove_weights_submit($form, &$form_state) {
+  $result = db_query('UPDATE {node_gallery_images} SET weight = 0 WHERE gid = %d', $form_state['values']['gid']);
+  if ($result === FALSE) {
+    $message = t("There was a problem updating you're images.");
+  } else {
+    $message = t("All image weights have been reset. The global sorting is now active again.");
+  }
+  drupal_set_message($message);
+}
+
 function node_gallery_sort_images_form_submit($form, &$form_state) {
   $images = array();
   $gid = $form_state['values']['gid'];
Index: js/ng_sort_jqueryui.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_gallery/js/Attic/ng_sort_jqueryui.js,v
retrieving revision 1.1.2.2
diff -u -p -r1.1.2.2 ng_sort_jqueryui.js
--- js/ng_sort_jqueryui.js	18 Nov 2010 20:13:00 -0000	1.1.2.2
+++ js/ng_sort_jqueryui.js	23 Nov 2010 13:53:00 -0000
@@ -1,16 +1,41 @@
-Drupal.behaviors.initSorting = function(context) {
+Drupal.behaviors.nodeGalleryInitSorting = function(context) {
   // hide the weight dropdowns
   $('*[id^=edit-images-sort][id$=-wrapper]').each(function() {
     $(this).hide();
   });
-  $(function() {
-    $("div.node-gallery-sort-images-grid-sortable").sortable({
-      update : function(event, ui) {
-        $("div.node-gallery-sort-images-grid-sortable").children().each(function(index, element) {
-          $('#edit-'+element.id+' option[value='+index+']').attr("selected", true);
-        });
-      }
-    });
-    $("div.node-gallery-sort-images-grid-sortable").disableSelection();
+  // enable drag and drop
+  $("div.node-gallery-sort-images-grid-sortable").sortable({
+    update: nodeGalleryUpdatePositions
   });
+  $("div.node-gallery-sort-images-grid-sortable").disableSelection();
+  // enable presets
+  $('div.node-gallery-sort-presets a:not(.ng3-processed)', context).addClass('ng3-processed').bind('click', nodeGalleryPresetClickHandler);
 };
+
+var nodeGalleryUpdatePositions = function() {
+  $("div.node-gallery-sort-images-grid-sortable").children().each(function(index, element) {
+    $('#edit-'+element.id+' option[value='+index+']').attr("selected", true);
+  });
+}
+
+var nodeGalleryPresetClickHandler = function() {
+  op = this.id.substring(this.id.lastIndexOf('-')+1);
+  url = Drupal.settings.basePath+'node_gallery_ajax_get_sort/'+Drupal.settings.node_gallery.gid+'/'+op;
+  if ($(this).hasClass("node-gallery-sort-active")) {
+    url += '/1';
+  }
+  $(this).toggleClass("node-gallery-sort-active").siblings().removeClass("node-gallery-sort-active");
+  $.get(url, null, nodeGallerySortByNidArray);
+  return false;
+}
+
+var nodeGallerySortByNidArray = function(response) {
+  var result, index;
+  result = Drupal.parseJson(response);
+  for (key in result) {
+    index = jQuery.inArray(result[key], Drupal.settings.node_gallery.original_sort);
+    $("div.node-gallery-sort-images-grid-sortable").append($('#images-sort-'+index));
+  }
+  nodeGalleryUpdatePositions();
+  $("div.node-gallery-sort-images-grid-sortable").sortable("refreshPositions");
+}
Index: theme/node-gallery-sort-images-grid.tpl.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_gallery/theme/Attic/node-gallery-sort-images-grid.tpl.php,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 node-gallery-sort-images-grid.tpl.php
--- theme/node-gallery-sort-images-grid.tpl.php	18 Nov 2010 20:13:00 -0000	1.1.2.1
+++ theme/node-gallery-sort-images-grid.tpl.php	23 Nov 2010 13:37:29 -0000
@@ -9,6 +9,12 @@
 ?>
 <div id="node-gallery-sort-images-grid">
   <?php if($items): ?>
+  <div class="node-gallery-sort-presets">
+    <span class="node-gallery-sort-presets-description description">Sort items by: </span>
+    <a href="#" id="node-gallery-sort-title">Title &uarr;&darr;</a> | 
+    <a href="#" id="node-gallery-sort-filename">Filename &uarr;&darr;</a> | 
+    <a href="#" id="node-gallery-sort-reset">Restore last saved order</a> 
+  </div>
   <div class="clear-block node-gallery-sort-images-grid-sortable">
    <?php print $items; ?>
   </div>
Index: theme/theme.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_gallery/theme/theme.inc,v
retrieving revision 1.2.2.37
diff -u -p -r1.2.2.37 theme.inc
--- theme/theme.inc	18 Nov 2010 20:13:00 -0000	1.2.2.37
+++ theme/theme.inc	23 Nov 2010 12:58:49 -0000
@@ -60,9 +60,12 @@ function template_preprocess_node_galler
  * 
  */
 function template_preprocess_node_gallery_sort_images_grid(&$variables) {
+  $original_sort = array();
   foreach ($variables['images'] as $image) {
+    $original_sort[] = $image['nid'];
     $variables['items'] .= theme('node_gallery_sort_images_grid_item', $image);
   }
+  drupal_add_js(array('node_gallery' => array('original_sort' => $original_sort)), 'setting');
 }
 
 /**
@@ -78,7 +81,8 @@ function theme_node_gallery_sort_images_
   $output = '';
   if ($form['jquery_ui_integration']['#value']) {
     jquery_ui_add(array('ui.draggable', 'ui.droppable', 'ui.sortable'));
-    drupal_add_js(drupal_get_path('module', 'node_gallery') .'/js/ng_sort_jqueryui.js');    
+    drupal_add_js(drupal_get_path('module', 'node_gallery') .'/js/ng_sort_jqueryui.js');
+    drupal_add_js(array('node_gallery' => array('gid' => $form['gid']['#value'])), 'setting');
     $output .= theme('node_gallery_sort_images_grid', $form['#images']);
   }
   else {
