aboutsummaryrefslogtreecommitdiffstats
path: root/admin/include/functions.php
diff options
context:
space:
mode:
authormistic100 <mistic@piwigo.org>2012-08-06 09:04:46 +0000
committermistic100 <mistic@piwigo.org>2012-08-06 09:04:46 +0000
commitb0512b37b4a70b3ea4ab58f2e78446aef663a7a1 (patch)
tree3bd8f8938af14db6ba787acc150a85b70afa6a45 /admin/include/functions.php
parent12758e0b8f6c46014042a315a5d1e1ef9069bdd0 (diff)
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
Diffstat (limited to 'admin/include/functions.php')
-rw-r--r--admin/include/functions.php66
1 files changed, 45 insertions, 21 deletions
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);