From 61b7974a8d86f22b6ce9f9cf53503f0c2c8c3336 Mon Sep 17 00:00:00 2001 From: Teatek <38403802+Teatek@users.noreply.github.com> Date: Wed, 13 Feb 2019 15:00:22 +0100 Subject: [PATCH] ability to set album order with web API * add method pwg.categories.setRank in Piwigo web API --- admin/cat_list.php | 42 +------------ admin/include/functions.php | 42 +++++++++++++ include/ws_functions/pwg.categories.php | 82 +++++++++++++++++++++++++ ws.php | 21 ++++++- 4 files changed, 145 insertions(+), 42 deletions(-) diff --git a/admin/cat_list.php b/admin/cat_list.php index 9e061dd7d..c0e6b2ab9 100644 --- a/admin/cat_list.php +++ b/admin/cat_list.php @@ -53,54 +53,14 @@ $sort_orders = array( // | functions | // +-----------------------------------------------------------------------+ -/** - * save the rank depending on given categories order - * - * The list of ordered categories id is supposed to be in the same parent - * category - * - * @param array categories - * @return void - */ -function save_categories_order($categories) -{ - $current_rank_for_id_uppercat = array(); - $current_rank = 0; - - $datas = array(); - foreach ($categories as $category) - { - if (is_array($category)) - { - $id = $category['id']; - $id_uppercat = $category['id_uppercat']; - if (!isset($current_rank_for_id_uppercat[$id_uppercat])) - { - $current_rank_for_id_uppercat[$id_uppercat] = 0; - } - $current_rank = ++$current_rank_for_id_uppercat[$id_uppercat]; - } - else - { - $id = $category; - $current_rank++; - } - - $datas[] = array('id' => $id, 'rank' => $current_rank); - } - $fields = array('primary' => array('id'), 'update' => array('rank')); - mass_updates(CATEGORIES_TABLE, $fields, $datas); - - update_global_rank(); -} function get_categories_ref_date($ids, $field='date_available', $minmax='max') { // we need to work on the whole tree under each category, even if we don't // want to sort sub categories $category_ids = get_subcat_ids($ids); - + // search for the reference date of each album $query = ' SELECT diff --git a/admin/include/functions.php b/admin/include/functions.php index 6621b2a72..2ca1697b6 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -637,6 +637,48 @@ function get_fs_directories($path, $recursive = true) return $dirs; } +/** + * save the rank depending on given categories order + * + * The list of ordered categories id is supposed to be in the same parent + * category + * + * @param array categories + * @return void + */ +function save_categories_order($categories) +{ + $current_rank_for_id_uppercat = array(); + $current_rank = 0; + + $datas = array(); + foreach ($categories as $category) + { + if (is_array($category)) + { + $id = $category['id']; + $id_uppercat = $category['id_uppercat']; + + if (!isset($current_rank_for_id_uppercat[$id_uppercat])) + { + $current_rank_for_id_uppercat[$id_uppercat] = 0; + } + $current_rank = ++$current_rank_for_id_uppercat[$id_uppercat]; + } + else + { + $id = $category; + $current_rank++; + } + + $datas[] = array('id' => $id, 'rank' => $current_rank); + } + $fields = array('primary' => array('id'), 'update' => array('rank')); + mass_updates(CATEGORIES_TABLE, $fields, $datas); + + update_global_rank(); +} + /** * Orders categories (update categories.rank and global_rank database fields) * so that rank field are consecutive integers starting at 1 for each child. diff --git a/include/ws_functions/pwg.categories.php b/include/ws_functions/pwg.categories.php index 2eab7b654..327ee3c1b 100644 --- a/include/ws_functions/pwg.categories.php +++ b/include/ws_functions/pwg.categories.php @@ -585,6 +585,88 @@ function ws_categories_add($params, &$service) return $creation_output; } +/** + * API method + * Set the rank of a category + * @param mixed[] $params + * @option int cat_id + * @option int rank + */ +function ws_categories_setRank($params, &$service) +{ + // does the category really exist? + $query = ' +SELECT id, id_uppercat, rank + FROM '.CATEGORIES_TABLE.' + WHERE id IN ('.implode(',',$params['category_id']).') +;'; + $categories = query2array($query); + + if (count($categories) == 0) + { + return new PwgError(404, 'category_id not found'); + } + + $category = $categories[0]; + + //check the number of category given by the user + if(count($params['category_id']) > 1) + { + $order_new = $params['category_id']; + $order_new_by_id = $order_new; + sort($order_new_by_id, SORT_NUMERIC); + + $query = ' +SELECT id + FROM '.CATEGORIES_TABLE.' + WHERE id_uppercat '.(empty($category['id_uppercat']) ? "IS NULL" : "= ".$category['id_uppercat']).' + ORDER BY `id` ASC +;'; + + $cat_asc = query2array($query, null, 'id'); + + if(strcmp(implode(',',$cat_asc), implode(',',$order_new_by_id)) !==0) + { + return new PwgError(WS_ERR_INVALID_PARAM, 'you need to provide all sub-category ids for a given category'); + } + } + else + { + $params['category_id'] = implode($params['category_id']); + + $query = ' +SELECT id + FROM '.CATEGORIES_TABLE.' + WHERE id_uppercat '.(empty($category['id_uppercat']) ? "IS NULL" : "= ".$category['id_uppercat']).' + AND id != '.$params['category_id'].' + ORDER BY `rank` ASC +;'; + + $order_old = query2array($query, null, 'id'); + $order_new = array(); + $was_inserted = false; + $i = 1; + foreach ($order_old as $category_id) + { + if($i == $params['rank']) + { + $order_new[] = $params['category_id']; + $was_inserted = true; + } + $order_new[] = $category_id; + ++$i; + } + + if (!$was_inserted) + { + $order_new[] = $params['category_id']; + } + } + // include function to set the global rank + include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); + save_categories_order($order_new); +} + /** * API method * Sets details of a category diff --git a/ws.php b/ws.php index 3c79803e3..d7e69ce4b 100644 --- a/ws.php +++ b/ws.php @@ -737,7 +737,26 @@ function ws_addDefaultMethods( $arr ) $ws_functions_root . 'pwg.categories.php', array('admin_only'=>true, 'post_only'=>true) ); - + + $service->addMethod( + 'pwg.categories.setRank', + 'ws_categories_setRank', + array( + 'category_id' => array('type'=>WS_TYPE_ID, + 'flags'=>WS_PARAM_FORCE_ARRAY), + 'rank' => array('type'=>WS_TYPE_INT|WS_TYPE_POSITIVE|WS_TYPE_NOTNULL, + 'flags'=>WS_PARAM_OPTIONAL), + ), + 'Changes the rank of an album +

If you provide a list for category_id: + .', + $ws_functions_root . 'pwg.categories.php', + array('admin_only'=>true, 'post_only'=>true) + ); + $service->addMethod( 'pwg.plugins.getList', 'ws_plugins_getList',