You are here

function media_gallery_ajax_sort in Media Gallery 7.2

Same name and namespace in other branches
  1. 7 media_gallery.pages.inc \media_gallery_ajax_sort()

AJAX callback for drag-and-drop gallery sorting.

Parameters

string $type: The type of item being sorted: 'gallery' for an individual gallery or 'collection' for a gallery collection.

mixed $item: For a media gallery, the $node object for that gallery; for gallery collections, the taxonomy term corresponding to the collection.

1 string reference to 'media_gallery_ajax_sort'
media_gallery_menu in ./media_gallery.module
Implements hook_menu().

File

./media_gallery.pages.inc, line 161
Common pages for the Media Gallery module.

Code

function media_gallery_ajax_sort($type, $item) {
  $order = $_POST['order'];
  $result = FALSE;
  $id_prefix = '';
  switch ($type) {
    case 'collection':

      // There are some themes, which set a specific html-prefix for the node
      // id. Parse the id to be compatible with all themes.
      // @see drupal_html_id()
      $search = empty($order[0]) ? '' : $order[0];
      if (preg_match('/^([A-Za-z0-9\\-_]+?-)[0-9]+(--[0-9]+)?$/', $search, $match)) {
        $id_prefix = $match[1];
      }
      else {
        $id_prefix = 'node-';
      }
      $order = _media_gallery_sanitize_ids($id_prefix, $order);
      $result = media_gallery_reorder_collection($item, $order);
      break;
    case 'gallery':
      $id_prefix = 'media-gallery-media-';
      $order = _media_gallery_sanitize_ids($id_prefix, $order);
      $result = media_gallery_reorder_gallery($item, $order);
      break;
  }
  drupal_json_output(array(
    'result' => $result,
    'order' => $_POST['order'],
    'idPrefix' => $id_prefix,
  ));
}