diff --git a/manualcrop.helpers.inc b/manualcrop.helpers.inc index 08a83ef..74a819f 100644 --- a/manualcrop.helpers.inc +++ b/manualcrop.helpers.inc @@ -642,24 +642,37 @@ function manualcrop_save_crop_data($file, $data) { * - "height": An integer representing the height in pixels. */ function manualcrop_load_crop_selection($file, $style_name = NULL) { - $query = db_select('manualcrop', 'mc'); - if (module_exists('file_entity_revisions')) { + + $use_revisions = module_exists('file_entity_revisions'); + if ($use_revisions) { $query->join('file_managed_revisions', 'r', 'mc.vid = r.vid AND r.uri = :uri', array(':uri' => $file)); - $query->orderBy('r.vid', 'DESC'); - $query->range(0, 1); } else { $query->join('file_managed', 'f', 'mc.fid = f.fid'); $query->condition('f.uri', $file); } + + $query->fields('mc', array('x', 'y', 'width', 'height', 'style_name')); + if (isset($style_name)) { - $query->fields('mc', array('x', 'y', 'width', 'height', 'style_name')); $query->condition('mc.style_name', $style_name); + if ($use_revisions) { + // Order by file revision ID descending to use the most recent manual + // crop settings associated with the provided file URI. + $query->orderBy('r.vid', 'DESC'); + $query->range(0, 1); + } return $query->execute()->fetchObject(); } else { - $query->fields('mc', array('x', 'y', 'width', 'height')); + if ($use_revisions) { + // The fetchAllAssoc() call below will use the last result found by this + // query for each style name, so order it by file revision ID ascending + // to use the most recent manual crop settings associated with the + // provided file URL for each style. + $query->orderBy('r.vid', 'ASC'); + } return $query->execute()->fetchAllAssoc('style_name'); } } diff --git a/manualcrop.install b/manualcrop.install index 9f96ccc..27ef2f0 100644 --- a/manualcrop.install +++ b/manualcrop.install @@ -456,7 +456,7 @@ function manualcrop_update_7110() { /** * Add index on vid. */ -function manualcrop_update_7110() { +function manualcrop_update_7111() { db_add_index('manualcrop', 'vid', array('vid')); }