Changeset 11372
- Timestamp:
- Jun 14, 2011, 3:54:05 PM (13 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/ws_functions.inc.php
r11155 r11372 1015 1015 } 1016 1016 return $affected_rows; 1017 } 1018 1019 function ws_images_setRank($params, &$service) 1020 { 1021 if (!is_admin()) 1022 { 1023 return new PwgError(401, 'Access denied'); 1024 } 1025 1026 if (!$service->isPost()) 1027 { 1028 return new PwgError(405, "This method requires HTTP POST"); 1029 } 1030 1031 // is the image_id valid? 1032 $params['image_id'] = (int)$params['image_id']; 1033 if ($params['image_id'] <= 0) 1034 { 1035 return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id"); 1036 } 1037 1038 // is the category valid? 1039 $params['category_id'] = (int)$params['category_id']; 1040 if ($params['category_id'] <= 0) 1041 { 1042 return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); 1043 } 1044 1045 // is the rank valid? 1046 $params['rank'] = (int)$params['rank']; 1047 if ($params['rank'] <= 0) 1048 { 1049 return new PwgError(WS_ERR_INVALID_PARAM, "Invalid rank"); 1050 } 1051 1052 // does the image really exist? 1053 $query=' 1054 SELECT 1055 * 1056 FROM '.IMAGES_TABLE.' 1057 WHERE id = '.$params['image_id'].' 1058 ;'; 1059 1060 $image_row = pwg_db_fetch_assoc(pwg_query($query)); 1061 if ($image_row == null) 1062 { 1063 return new PwgError(404, "image_id not found"); 1064 } 1065 1066 // is the image associated to this category? 1067 $query = ' 1068 SELECT 1069 image_id, 1070 category_id, 1071 rank 1072 FROM '.IMAGE_CATEGORY_TABLE.' 1073 WHERE image_id = '.$params['image_id'].' 1074 AND category_id = '.$params['category_id'].' 1075 ;'; 1076 $category_row = pwg_db_fetch_assoc(pwg_query($query)); 1077 if ($category_row == null) 1078 { 1079 return new PwgError(404, "This image is not associated to this category"); 1080 } 1081 1082 // what is the current higher rank for this category? 1083 $query = ' 1084 SELECT 1085 MAX(rank) AS max_rank 1086 FROM '.IMAGE_CATEGORY_TABLE.' 1087 WHERE category_id = '.$params['category_id'].' 1088 ;'; 1089 $result = pwg_query($query); 1090 $row = pwg_db_fetch_assoc($result); 1091 1092 if (is_numeric($row['max_rank'])) 1093 { 1094 if ($params['rank'] > $row['max_rank']) 1095 { 1096 $params['rank'] = $row['max_rank'] + 1; 1097 } 1098 } 1099 else 1100 { 1101 $params['rank'] = 1; 1102 } 1103 1104 // update rank for all other photos in the same category 1105 $query = ' 1106 UPDATE '.IMAGE_CATEGORY_TABLE.' 1107 SET rank = rank + 1 1108 WHERE category_id = '.$params['category_id'].' 1109 AND rank IS NOT NULL 1110 AND rank >= '.$params['rank'].' 1111 ;'; 1112 pwg_query($query); 1113 1114 // set the new rank for the photo 1115 $query = ' 1116 UPDATE '.IMAGE_CATEGORY_TABLE.' 1117 SET rank = '.$params['rank'].' 1118 WHERE image_id = '.$params['image_id'].' 1119 AND category_id = '.$params['category_id'].' 1120 ;'; 1121 pwg_query($query); 1122 1123 // return data for client 1124 return array( 1125 'image_id' => $params['image_id'], 1126 'category_id' => $params['category_id'], 1127 'rank' => $params['rank'], 1128 ); 1017 1129 } 1018 1130 -
trunk/ws.php
r11366 r11372 144 144 ); 145 145 146 $service->addMethod( 147 'pwg.images.setRank', 148 'ws_images_setRank', 149 array( 150 'image_id' => array(), 151 'category_id' => array(), 152 'rank' => array(), 153 ), 154 'sets the rank of a photo for a given album (POST method only, for admins)' 155 ); 156 146 157 $service->addMethod('pwg.session.getStatus', 'ws_session_getStatus', null, '' ); 147 158 $service->addMethod('pwg.session.login', 'ws_session_login',
Note: See TracChangeset
for help on using the changeset viewer.