diff options
Diffstat (limited to 'admin/include')
-rw-r--r-- | admin/include/functions.php | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/admin/include/functions.php b/admin/include/functions.php index 9a827d1d6..030e7fa31 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -1519,6 +1519,8 @@ function add_tags($tags, $images) return; } + $taglist_before = get_image_tag_ids($images); + // we can't insert twice the same {image_id,tag_id} so we must first // delete lines we'll insert later $query = ' @@ -1545,6 +1547,11 @@ DELETE array_keys($inserts[0]), $inserts ); + + $taglist_after = get_image_tag_ids($images); + $images_to_update = compare_image_tag_lists($taglist_before, $taglist_after); + update_images_lastmodified($images_to_update); + invalidate_user_cache_nb_tags(); } @@ -1565,6 +1572,15 @@ function delete_tags($tag_ids) return false; } + // we need the list of impacted images, to update their lastmodified + $query = ' +SELECT + image_id + FROM '.IMAGE_TAG_TABLE.' + WHERE tag_id IN ('.implode(',', $tag_ids).') +;'; + $image_ids = query2array($query, null, 'image_id'); + $query = ' DELETE FROM '.IMAGE_TAG_TABLE.' @@ -1579,6 +1595,7 @@ DELETE ;'; pwg_query($query); + update_images_lastmodified($image_ids); invalidate_user_cache_nb_tags(); } @@ -1662,6 +1679,9 @@ function set_tags_of($tags_of) { if (count($tags_of) > 0) { + $taglist_before = get_image_tag_ids(array_keys($tags_of)); + global $logger; $logger->debug('taglist_before', $taglist_before); + $query = ' DELETE FROM '.IMAGE_TAG_TABLE.' @@ -1691,11 +1711,83 @@ DELETE ); } + $taglist_after = get_image_tag_ids(array_keys($tags_of)); + global $logger; $logger->debug('taglist_after', $taglist_after); + $images_to_update = compare_image_tag_lists($taglist_before, $taglist_after); + global $logger; $logger->debug('$images_to_update', $images_to_update); + + update_images_lastmodified($images_to_update); invalidate_user_cache_nb_tags(); } } /** + * Get list of tag ids for each image. Returns an empty list if the image has + * no tags. + * + * @since 2.9 + * @param array $image_ids + * @return associative array, image_id => list of tag ids + */ +function get_image_tag_ids($image_ids) +{ + if (!is_array($image_ids) and is_int($image_ids)) + { + $images_ids = array($image_ids); + } + + if (count($image_ids) == 0) + { + return array(); + } + + $query = ' +SELECT + image_id, + tag_id + FROM '.IMAGE_TAG_TABLE.' + WHERE image_id IN ('.implode(',', $image_ids).') +;'; + + $tags_of = array_fill_keys($image_ids, array()); + $image_tags = query2array($query); + foreach ($image_tags as $image_tag) + { + $tags_of[ $image_tag['image_id'] ][] = $image_tag['tag_id']; + } + + return $tags_of; +} + +/** + * Compare the list of tags, for each image. Returns image_ids where tag list has changed. + * + * @since 2.9 + * @param array $taglist_before - for each image_id (key), list of tag ids + * @param array $taglist_after - for each image_id (key), list of tag ids + * @return array - image_ids where the list has changed + */ +function compare_image_tag_lists($taglist_before, $taglist_after) +{ + $images_to_update = array(); + + foreach ($taglist_after as $image_id => $list_after) + { + sort($list_after); + + $list_before = isset($taglist_before[$image_id]) ? $taglist_before[$image_id] : array(); + sort($list_before); + + if ($list_after != $list_before) + { + $images_to_update[] = $image_id; + } + } + + return $images_to_update; +} + +/** * Associate a list of images to a list of categories. * The function will not duplicate links and will preserve ranks. * @@ -2887,3 +2979,30 @@ function save_images_order($category_id, $images) ); mass_updates(IMAGE_CATEGORY_TABLE, $fields, $datas); } + +/** + * Force update on images.lastmodified column. Useful when modifying the tag + * list. + * + * @since 2.9 + * @param array $image_ids + */ +function update_images_lastmodified($image_ids) +{ + if (!is_array($image_ids) and is_int($image_ids)) + { + $images_ids = array($image_ids); + } + + if (count($image_ids) == 0) + { + return; + } + + $query = ' +UPDATE '.IMAGES_TABLE.' + SET lastmodified = NOW() + WHERE id IN ('.implode(',', $image_ids).') +;'; + pwg_query($query); +} |