diff --git a/include/ws_functions/pwg.users.php b/include/ws_functions/pwg.users.php index 3881f3b65..fb9fa009a 100644 --- a/include/ws_functions/pwg.users.php +++ b/include/ws_functions/pwg.users.php @@ -632,4 +632,108 @@ SELECT )); } +/** + * API method + * Returns the favorite images of the current user + * @param mixed[] $params + */ +function ws_getFavorites($params, &$service) +{ + global $conf, $user; + if (is_a_guest()) + { + return false; + } + + $search_user = $user; + if (is_admin() && isset($params['user_id']) && is_numeric($params['user_id'])) + { + + // ensure the indicated user exists + if (get_username($params['user_id']) === false) + { + return new PwgError(WS_ERR_INVALID_PARAM, 'This user does not exist.'); + } + + $search_user = array('id' => $params['user_id']); + $search_user = array_merge( $search_user, getuserdata($search_user['id']) ); + } + + if (!empty($search_user['forbidden_categories'])) + { + $query = ' + SELECT DISTINCT f.image_id + FROM '.FAVORITES_TABLE.' AS f INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic + ON f.image_id = ic.image_id + WHERE f.user_id = '.$search_user['id'].' + AND ic.category_id NOT IN ('.$search_user['forbidden_categories'].') + ;'; + $authorizeds = query2array($query,null, 'image_id'); + $query = ' + SELECT image_id + FROM '.FAVORITES_TABLE.' + WHERE user_id = '.$search_user['id'].' + ;'; + $favorites = query2array($query,null, 'image_id'); + $to_deletes = array_diff($favorites, $authorizeds); + if (count($to_deletes) > 0) + { + $query = ' + DELETE FROM '.FAVORITES_TABLE.' + WHERE image_id IN ('.implode(',', $to_deletes).') + AND user_id = '.$search_user['id'].' + ;'; + pwg_query($query); + } + } + + $visible_images_cond = ''; + if (!empty($filter['visible_images'])) + { + $visible_images_cond = 'AND id IN ('.$filter['visible_images'].')'; + } + + $order_by = ws_std_image_sql_order($params, 'i.'); + $order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY '.$order_by; + $query = 'SELECT i.* + FROM '.FAVORITES_TABLE.' + INNER JOIN '.IMAGES_TABLE.' i ON image_id = i.id + WHERE user_id = '.$search_user['id'].' + '.$visible_images_cond.' + '.$order_by.';'; + $images = array(); + $result = pwg_query($query); + while ($row = pwg_db_fetch_assoc($result)) + { + $image = array(); + foreach (array('id', 'width', 'height', 'hit') as $k) + { + if (isset($row[$k])) + { + $image[$k] = (int)$row[$k]; + } + } + foreach (array('file', 'name', 'comment', 'date_creation', 'date_available') as $k) + { + $image[$k] = $row[$k]; + } + $images[] = array_merge($image, ws_std_get_urls($row)); + } + $count = count($images); + $images = array_slice($images, $params['per_page']*$params['page'], $params['per_page']); + return array( + 'paging' => new PwgNamedStruct( + array( + 'page' => $params['page'], + 'per_page' => $params['per_page'], + 'count' => $count + ) + ), + 'images' => new PwgNamedArray( + $images, 'image', + ws_std_get_image_xml_attributes() + ) + ); +} + ?> diff --git a/ws.php b/ws.php index 8e78c1bdf..241d78397 100644 --- a/ws.php +++ b/ws.php @@ -1076,6 +1076,28 @@ enabled_high, registration_date, registration_date_string, registration_date_sin $ws_functions_root . 'pwg.permissions.php', array('admin_only'=>true, 'post_only'=>true) ); + + $service->addMethod( + 'pwg.users.getFavorites', + 'ws_getFavorites', + array( + 'user_id' => array( + 'flags'=>WS_PARAM_OPTIONAL, + 'type'=>WS_TYPE_ID), + 'per_page' => array( + 'default'=>100, + 'maxValue'=>$conf['ws_max_images_per_page'], + 'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE), + 'page' => array( + 'default'=>0, + 'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE), + 'order' => array( + 'default'=>null, + 'info'=>'id, file, name, hit, rating_score, date_creation, date_available, random') + ), + 'Returns the favorite images of the current user. If identified as an admin, you may fetch favorites from any user_id, otherwise user_id will be ignored', + $ws_functions_root . 'pwg.users.php' + ); } ?>