From b0512b37b4a70b3ea4ab58f2e78446aef663a7a1 Mon Sep 17 00:00:00 2001 From: mistic100 Date: Mon, 6 Aug 2012 09:04:46 +0000 Subject: bug 2716: modifying a photo reset it's rank modify associate_images_to_categories() and move_images_to_categories() to preserve ranks please test before 2.4.4 ! :-D git-svn-id: http://piwigo.org/svn/trunk@17424 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/include/functions.php | 66 ++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 21 deletions(-) (limited to 'admin/include/functions.php') diff --git a/admin/include/functions.php b/admin/include/functions.php index cb735d70f..41dadfdcb 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -1485,7 +1485,7 @@ DELETE /** * Associate a list of images to a list of categories. * - * The function will not duplicate links + * The function will not duplicate links and will preserve ranks * * @param array images * @param array categories @@ -1498,15 +1498,25 @@ function associate_images_to_categories($images, $categories) { return false; } - + + // get existing associations $query = ' -DELETE +SELECT + image_id, + category_id FROM '.IMAGE_CATEGORY_TABLE.' WHERE image_id IN ('.implode(',', $images).') AND category_id IN ('.implode(',', $categories).') ;'; - pwg_query($query); + $result = pwg_query($query); + + $existing = array(); + while ($row = pwg_db_fetch_assoc($result)) + { + $existing[ $row['category_id'] ][] = $row['image_id']; + } + // get max rank of each categories $query = ' SELECT category_id, @@ -1523,6 +1533,7 @@ SELECT 'max_rank' ); + // associate only not already associated images $inserts = array(); foreach ($categories as $category_id) { @@ -1530,35 +1541,47 @@ SELECT { $current_rank_of[$category_id] = 0; } + if (!isset($existing[$category_id])) + { + $existing[$category_id] = array(); + } foreach ($images as $image_id) { - $rank = ++$current_rank_of[$category_id]; + if (!in_array($image_id, $existing[$category_id])) + { + $rank = ++$current_rank_of[$category_id]; - array_push( - $inserts, - array( - 'image_id' => $image_id, - 'category_id' => $category_id, - 'rank' => $rank, - ) - ); + array_push( + $inserts, + array( + 'image_id' => $image_id, + 'category_id' => $category_id, + 'rank' => $rank, + ) + ); + } } } - mass_inserts( - IMAGE_CATEGORY_TABLE, - array_keys($inserts[0]), - $inserts - ); + if (count($inserts)) + { + mass_inserts( + IMAGE_CATEGORY_TABLE, + array_keys($inserts[0]), + $inserts + ); - update_category($categories); + update_category($categories); + } } /** - * Disssociate images from all categories except their storage category and + * Dissociate images from all old categories except their storage category and * associate to new categories. * + * This function will preserve ranks + * * @param array images * @param array categories * @return void @@ -1570,12 +1593,13 @@ function move_images_to_categories($images, $categories) return false; } - // let's first break links with all albums but their "storage album" + // let's first break links with all old albums but their "storage album" $query = ' DELETE '.IMAGE_CATEGORY_TABLE.'.* FROM '.IMAGE_CATEGORY_TABLE.' JOIN '.IMAGES_TABLE.' ON image_id=id WHERE id IN ('.implode(',', $images).') + '.((is_array($categories) and count($categories)>0) ? 'AND category_id NOT IN ('.implode(',', $categories).')' : null).' AND (storage_category_id IS NULL OR storage_category_id != category_id) ;'; pwg_query($query); -- cgit v1.2.3