ability to set album order with web API

* add method pwg.categories.setRank in Piwigo web API
This commit is contained in:
Teatek
2019-02-13 15:00:22 +01:00
committed by Pierrick Le Gall
parent 325a30b4b5
commit 61b7974a8d
4 changed files with 145 additions and 42 deletions
+1 -41
View File
@@ -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
+42
View File
@@ -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.
+82
View File
@@ -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
+20 -1
View File
@@ -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
<br><br>If you provide a list for category_id:
<ul>
<li>rank becomes useless, only the order of the image_id list matters</li>
<li>you are supposed to provide the list of all categories_ids belonging to the album.
</ul>.',
$ws_functions_root . 'pwg.categories.php',
array('admin_only'=>true, 'post_only'=>true)
);
$service->addMethod(
'pwg.plugins.getList',
'ws_plugins_getList',