You are here

function media_gallery_reorder_collection in Media Gallery 7.2

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

Reorder a gallery collection.

1 call to media_gallery_reorder_collection()
media_gallery_ajax_sort in ./media_gallery.pages.inc
AJAX callback for drag-and-drop gallery sorting.

File

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

Code

function media_gallery_reorder_collection($collection, $order) {

  // Get a complete ordered list of the galleries in this collection.
  $galleries = db_query('SELECT ti.nid FROM {taxonomy_index} ti LEFT JOIN {media_gallery_weight} mgw ON (ti.nid = mgw.nid AND ti.tid = mgw.tid) WHERE ti.tid = :tid ORDER BY mgw.weight ASC, ti.nid ASC', array(
    ':tid' => $collection->tid,
  ))
    ->fetchCol();

  // Sort the list.
  $galleries = _media_gallery_reorder($galleries, $order);

  // Resave gallery weights for the entire collection.
  db_delete('media_gallery_weight')
    ->condition('tid', $collection->tid)
    ->execute();
  $query = db_insert('media_gallery_weight')
    ->fields(array(
    'tid',
    'nid',
    'weight',
  ));
  foreach ($galleries as $weight => $nid) {
    $query
      ->values(array(
      'tid' => $collection->tid,
      'nid' => $nid,
      'weight' => $weight,
    ));
  }
  $query
    ->execute();
  return TRUE;
}