fixes #1735 new API method pwg.images.setCategory to associate/dissociate/move a list of images

This commit is contained in:
plegall
2023-10-11 16:58:14 +02:00
parent 71164f80d6
commit 14e8ba664b
5 changed files with 112 additions and 24 deletions
+2 -23
View File
@@ -207,31 +207,10 @@ DELETE
else if ('dissociate' == $action)
{
// physical links must not be broken, so we must first retrieve image_id
// which create virtual links with the category to "dissociate from".
$query = '
SELECT id
FROM '.IMAGE_CATEGORY_TABLE.'
INNER JOIN '.IMAGES_TABLE.' ON image_id = id
WHERE category_id = '.$_POST['dissociate'].'
AND id IN ('.implode(',', $collection).')
AND (
category_id != storage_category_id
OR storage_category_id IS NULL
)
;';
$dissociables = array_from_query($query, 'id');
$nb_dissociated = dissociate_images_from_category($collection, $_POST['dissociate']);
if (!empty($dissociables))
if ($nb_dissociated > 0)
{
$query = '
DELETE
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE category_id = '.$_POST['dissociate'].'
AND image_id IN ('.implode(',', $dissociables).')
';
pwg_query($query);
$_SESSION['page_infos'] = array(
l10n('Information data registered in database')
);
+37
View File
@@ -2102,6 +2102,43 @@ SELECT
}
}
/**
* Dissociate a list of images from a category.
*
* @param int[] $images
* @param int $categories
*/
function dissociate_images_from_category($images, $category)
{
// physical links must not be broken, so we must first retrieve image_id
// which create virtual links with the category to "dissociate from".
$query = '
SELECT id
FROM '.IMAGE_CATEGORY_TABLE.'
INNER JOIN '.IMAGES_TABLE.' ON image_id = id
WHERE category_id ='.$category.'
AND id IN ('.implode(',', $images).')
AND (
category_id != storage_category_id
OR storage_category_id IS NULL
)
;';
$dissociables = array_from_query($query, 'id');
if (!empty($dissociables))
{
$query = '
DELETE
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE category_id = '.$category.'
AND image_id IN ('.implode(',', $dissociables).')
';
pwg_query($query);
}
return count($dissociables);
}
/**
* Dissociate images from all old categories except their storage category and
* associate to new categories.