fixes #349, API set ranks of all album photos at once

on pwg.images.setRank, ability to provide a list of image_id (and no rank).
This way you can set the order of all photos of a given album, at once.
This commit is contained in:
plegall 2016-01-28 16:03:39 +01:00
parent 417a6b77b2
commit bc7316639f
4 changed files with 72 additions and 37 deletions

View file

@ -45,38 +45,6 @@ if (!isset($_GET['cat_id']) or !is_numeric($_GET['cat_id']))
$page['category_id'] = $_GET['cat_id'];
// +-----------------------------------------------------------------------+
// | functions |
// +-----------------------------------------------------------------------+
/**
* save the rank depending on given images order
*
* The list of ordered images id is supposed to be in the same parent
* category
*
* @param array categories
* @return void
*/
function save_images_order($category_id, $images)
{
$current_rank = 0;
$datas = array();
foreach ($images as $id)
{
$datas[] = array(
'category_id' => $category_id,
'image_id' => $id,
'rank' => ++$current_rank,
);
}
$fields = array(
'primary' => array('image_id', 'category_id'),
'update' => array('rank')
);
mass_updates(IMAGE_CATEGORY_TABLE, $fields, $datas);
}
// +-----------------------------------------------------------------------+
// | global mode form submission |
// +-----------------------------------------------------------------------+

View file

@ -2848,4 +2848,33 @@ SELECT
;';
return query2array($query, null, 'id');
}
}
/**
* save the rank depending on given images order
*
* The list of ordered images id is supposed to be in the same parent
* category
*
* @param int category_id
* @param int[] images
* @return void
*/
function save_images_order($category_id, $images)
{
$current_rank = 0;
$datas = array();
foreach ($images as $id)
{
$datas[] = array(
'category_id' => $category_id,
'image_id' => $id,
'rank' => ++$current_rank,
);
}
$fields = array(
'primary' => array('image_id', 'category_id'),
'update' => array('rank')
);
mass_updates(IMAGE_CATEGORY_TABLE, $fields, $datas);
}

View file

@ -740,7 +740,40 @@ UPDATE '. IMAGES_TABLE .'
* @option int rank
*/
function ws_images_setRank($params, $service)
{
{
if (count($params['image_id']) > 1)
{
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
save_images_order(
$params['category_id'],
$params['image_id']
);
$query = '
SELECT
image_id
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE category_id = '.$params['category_id'].'
ORDER BY rank ASC
;';
$image_ids = query2array($query, null, 'image_id');
// return data for client
return array(
'image_id' => $image_ids,
'category_id' => $params['category_id'],
);
}
// turns image_id into a simple int instead of array
$params['image_id'] = array_shift($params['image_id']);
if (empty($params['rank']))
{
return new PwgError(WS_ERR_MISSING_PARAM, 'rank is missing');
}
// does the image really exist?
$query = '
SELECT COUNT(*)

11
ws.php
View file

@ -293,11 +293,16 @@ function ws_addDefaultMethods( $arr )
'pwg.images.setRank',
'ws_images_setRank',
array(
'image_id' => array('type'=>WS_TYPE_ID),
'image_id' => array('type'=>WS_TYPE_ID,'flags'=>WS_PARAM_FORCE_ARRAY),
'category_id' => array('type'=>WS_TYPE_ID),
'rank' => array('type'=>WS_TYPE_INT|WS_TYPE_POSITIVE|WS_TYPE_NOTNULL)
'rank' => array('type'=>WS_TYPE_INT|WS_TYPE_POSITIVE|WS_TYPE_NOTNULL, 'default'=>null)
),
'Sets the rank of a photo for a given album.',
'Sets the rank of a photo for a given album.
<br><br>If you provide a list for image_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 image_ids belonging to the album.
</ul>',
$ws_functions_root . 'pwg.images.php',
array('admin_only'=>true, 'post_only'=>true)
);