From 14e8ba664b0299ec0eeee155ca0ae8d1a4f466b5 Mon Sep 17 00:00:00 2001 From: plegall Date: Wed, 11 Oct 2023 16:58:14 +0200 Subject: [PATCH] fixes #1735 new API method pwg.images.setCategory to associate/dissociate/move a list of images --- admin/batch_manager_global.php | 25 ++------------- admin/include/functions.php | 37 +++++++++++++++++++++ include/ws_functions/pwg.images.php | 50 +++++++++++++++++++++++++++++ tools/ws/ws.css | 10 +++++- ws.php | 14 ++++++++ 5 files changed, 112 insertions(+), 24 deletions(-) diff --git a/admin/batch_manager_global.php b/admin/batch_manager_global.php index 2d5b53791..38d2f5006 100644 --- a/admin/batch_manager_global.php +++ b/admin/batch_manager_global.php @@ -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') ); diff --git a/admin/include/functions.php b/admin/include/functions.php index 25be5ab4d..c0dc4c6c3 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -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. diff --git a/include/ws_functions/pwg.images.php b/include/ws_functions/pwg.images.php index cb71da75b..46c64a6b3 100644 --- a/include/ws_functions/pwg.images.php +++ b/include/ws_functions/pwg.images.php @@ -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(); +} ?> diff --git a/tools/ws/ws.css b/tools/ws/ws.css index 58ce93bf4..bdf485d98 100644 --- a/tools/ws/ws.css +++ b/tools/ws/ws.css @@ -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); -} \ No newline at end of file +} diff --git a/ws.php b/ws.php index c205c84a3..e792c6bfe 100644 --- a/ws.php +++ b/ws.php @@ -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. action can be:', + $ws_functions_root . 'pwg.images.php', + array('admin_only'=>true, 'post_only'=>true) + ); + $service->addMethod( 'pwg.rates.delete', 'ws_rates_delete',