API call to fetch user favorites (#582)

* new api call to get user favorites
This commit is contained in:
Dave Anderson
2019-07-15 10:04:52 -04:00
committed by Pierrick Le Gall
parent ad6e96b82c
commit d406a12d45
2 changed files with 126 additions and 0 deletions
+104
View File
@@ -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()
)
);
}
?>