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

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')
);

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.

View file

@ -2657,4 +2657,54 @@ function ws_images_deleteOrphans($params, $service)
'nb_orphans' => count(get_orphans()),
);
}
/**
* API method
* Associate/Dissociate/Move photos with an album.
*
* @since 14
* @param mixed[] $params
* @option int[] image_id
* @option int category_id
* @option string action
* @option string pwg_token
*/
function ws_images_setCategory($params, $service)
{
if (get_pwg_token() != $params['pwg_token'])
{
return new PwgError(403, 'Invalid security token');
}
// does the category really exist?
$query = '
SELECT
id
FROM '.CATEGORIES_TABLE.'
WHERE id = '.$params['category_id'].'
;';
$categories = query2array($query);
if (count($categories) == 0)
{
return new PwgError(404, 'category_id not found');
}
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
if ('associate' == $params['action'])
{
associate_images_to_categories($params['image_id'], array($params['category_id']));
}
elseif ('dissociate' == $params['action'])
{
dissociate_images_from_category($params['image_id'], $params['category_id']);
}
elseif ('move' == $params['action'])
{
move_images_to_categories($params['image_id'], array($params['category_id']));
}
invalidate_user_cache();
}
?>

View file

@ -295,6 +295,14 @@ input[type="text"] {
grid-template-columns: 66% 34%;
}
#methodDescription blockquote ul {
margin-top: 10px;
}
#methodDescription blockquote ul li {
list-style-type: disc;
}
.card, .card-2 {
margin: 10px;
background-color: var(--color-bg-secondary);
@ -598,4 +606,4 @@ div.onlys {padding:.25em .5em;display:table;border-radius:4px;margin-bottom:0.5e
/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
background: var(--color-action);
}
}

14
ws.php
View file

@ -296,6 +296,20 @@ function ws_addDefaultMethods( $arr )
array('admin_only'=>true, 'post_only'=>true)
);
$service->addMethod(
'pwg.images.setCategory',
'ws_images_setCategory',
array(
'image_id' => array('flags'=>WS_PARAM_FORCE_ARRAY, 'type'=>WS_TYPE_ID),
'category_id' => array('type'=>WS_TYPE_ID),
'action' => array('default'=>'associate', 'info' => 'associate/dissociate/move'),
'pwg_token' => array(),
),
'Manage associations of images with an album. <b>action</b> can be:<ul><li><i>associate</i> : add photos to this album</li><li><i>dissociate</i> : remove photos from this album</li><li><i>move</i> : dissociate photos from any other album and adds photos to this album</li></ul>',
$ws_functions_root . 'pwg.images.php',
array('admin_only'=>true, 'post_only'=>true)
);
$service->addMethod(
'pwg.rates.delete',
'ws_rates_delete',