From bbb43a2c2537491433cdacd9440652a1534e72a7 Mon Sep 17 00:00:00 2001 From: plegall Date: Tue, 14 Jun 2011 13:53:40 +0000 Subject: bug 2345 fixed: ability to update the rank of a photo for an existing category. I haven't modified pwg.images.setInfo, I've just added a new method pwg.images.setRank which does this very specific job. git-svn-id: http://piwigo.org/svn/branches/2.2@11371 68402e56-0260-453c-a942-63ccdbb3a9ee --- include/ws_functions.inc.php | 112 +++++++++++++++++++++++++++++++++++++++++++ ws.php | 11 +++++ 2 files changed, 123 insertions(+) diff --git a/include/ws_functions.inc.php b/include/ws_functions.inc.php index da7877f71..e8767f45d 100644 --- a/include/ws_functions.inc.php +++ b/include/ws_functions.inc.php @@ -1016,6 +1016,118 @@ UPDATE '.IMAGES_TABLE.' return $affected_rows; } +function ws_images_setRank($params, &$service) +{ + if (!is_admin()) + { + return new PwgError(401, 'Access denied'); + } + + if (!$service->isPost()) + { + return new PwgError(405, "This method requires HTTP POST"); + } + + // is the image_id valid? + $params['image_id'] = (int)$params['image_id']; + if ($params['image_id'] <= 0) + { + return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); + } + + // is the category valid? + $params['category_id'] = (int)$params['category_id']; + if ($params['category_id'] <= 0) + { + return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); + } + + // is the rank valid? + $params['rank'] = (int)$params['rank']; + if ($params['rank'] <= 0) + { + return new PwgError(WS_ERR_INVALID_PARAM, "Invalid rank"); + } + + // does the image really exist? + $query=' +SELECT + * + FROM '.IMAGES_TABLE.' + WHERE id = '.$params['image_id'].' +;'; + + $image_row = pwg_db_fetch_assoc(pwg_query($query)); + if ($image_row == null) + { + return new PwgError(404, "image_id not found"); + } + + // is the image associated to this category? + $query = ' +SELECT + image_id, + category_id, + rank + FROM '.IMAGE_CATEGORY_TABLE.' + WHERE image_id = '.$params['image_id'].' + AND category_id = '.$params['category_id'].' +;'; + $category_row = pwg_db_fetch_assoc(pwg_query($query)); + if ($category_row == null) + { + return new PwgError(404, "This image is not associated to this category"); + } + + // what is the current higher rank for this category? + $query = ' +SELECT + MAX(rank) AS max_rank + FROM '.IMAGE_CATEGORY_TABLE.' + WHERE category_id = '.$params['category_id'].' +;'; + $result = pwg_query($query); + $row = pwg_db_fetch_assoc($result); + + if (is_numeric($row['max_rank'])) + { + if ($params['rank'] > $row['max_rank']) + { + $params['rank'] = $row['max_rank'] + 1; + } + } + else + { + $params['rank'] = 1; + } + + // update rank for all other photos in the same category + $query = ' +UPDATE '.IMAGE_CATEGORY_TABLE.' + SET rank = rank + 1 + WHERE category_id = '.$params['category_id'].' + AND rank IS NOT NULL + AND rank >= '.$params['rank'].' +;'; + pwg_query($query); + + // set the new rank for the photo + $query = ' +UPDATE '.IMAGE_CATEGORY_TABLE.' + SET rank = '.$params['rank'].' + WHERE image_id = '.$params['image_id'].' + AND category_id = '.$params['category_id'].' +;'; + pwg_query($query); + + // return data for client + return array( + 'image_id' => $params['image_id'], + 'category_id' => $params['category_id'], + 'rank' => $params['rank'], + ); +} + function ws_images_add_chunk($params, &$service) { global $conf; diff --git a/ws.php b/ws.php index f3c63b940..e8bbb2590 100644 --- a/ws.php +++ b/ws.php @@ -143,6 +143,17 @@ function ws_addDefaultMethods( $arr ) 'sets the privacy levels for the images (POST method only)' ); + $service->addMethod( + 'pwg.images.setRank', + 'ws_images_setRank', + array( + 'image_id' => array(), + 'category_id' => array(), + 'rank' => array(), + ), + 'sets the rank of a photo for a given album (POST method only, for admins)' + ); + $service->addMethod('pwg.session.getStatus', 'ws_session_getStatus', null, '' ); $service->addMethod('pwg.session.login', 'ws_session_login', array('username', 'password'), -- cgit v1.2.3