issue #1615 include favorite status in image list API calls

This commit is contained in:
Dave Anderson 2022-07-23 06:43:02 -04:00 committed by GitHub
parent 3239296bf3
commit 231d36613c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 0 deletions

View file

@ -880,4 +880,28 @@ function url_is_remote($url)
return false; return false;
} }
/**
* List favorite image_ids of the current user.
* @since 13
*/
function get_user_favorites()
{
global $user;
if (is_a_guest())
{
return array();
}
$query = '
SELECT
image_id,
1 as fake_value
FROM '.FAVORITES_TABLE.'
WHERE user_id = '.$user['id'].'
';
return query2array($query, 'image_id', 'fake_value');
}
?> ?>

View file

@ -77,6 +77,7 @@ SELECT id, name, permalink, image_order
$order_by = $cats[ $params['cat_id'][0] ]['image_order']; $order_by = $cats[ $params['cat_id'][0] ]['image_order'];
} }
$order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY '.$order_by; $order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY '.$order_by;
$favorite_ids = get_user_favorites();
$query = ' $query = '
SELECT SQL_CALC_FOUND_ROWS i.*, GROUP_CONCAT(category_id) AS cat_ids SELECT SQL_CALC_FOUND_ROWS i.*, GROUP_CONCAT(category_id) AS cat_ids
@ -93,6 +94,7 @@ SELECT SQL_CALC_FOUND_ROWS i.*, GROUP_CONCAT(category_id) AS cat_ids
while ($row = pwg_db_fetch_assoc($result)) while ($row = pwg_db_fetch_assoc($result))
{ {
$image = array(); $image = array();
$image['is_favorite'] = isset($favorite_ids[ $row['id'] ]);
foreach (array('id', 'width', 'height', 'hit') as $k) foreach (array('id', 'width', 'height', 'hit') as $k)
{ {
if (isset($row[$k])) if (isset($row[$k]))

View file

@ -646,10 +646,12 @@ SELECT *
;'; ;';
$result = pwg_query($query); $result = pwg_query($query);
$image_ids = array_flip($image_ids); $image_ids = array_flip($image_ids);
$favorite_ids = get_user_favorites();
while ($row = pwg_db_fetch_assoc($result)) while ($row = pwg_db_fetch_assoc($result))
{ {
$image = array(); $image = array();
$image['is_favorite'] = isset($favorite_ids[ $row['id'] ]);
foreach (array('id', 'width', 'height', 'hit') as $k) foreach (array('id', 'width', 'height', 'hit') as $k)
{ {
if (isset($row[$k])) if (isset($row[$k]))

View file

@ -134,6 +134,7 @@ SELECT image_id, GROUP_CONCAT(tag_id) AS tag_ids
if (!empty($image_ids)) if (!empty($image_ids))
{ {
$rank_of = array_flip($image_ids); $rank_of = array_flip($image_ids);
$favorite_ids = get_user_favorites();
$query = ' $query = '
SELECT * SELECT *
@ -146,6 +147,7 @@ SELECT *
{ {
$image = array(); $image = array();
$image['rank'] = $rank_of[ $row['id'] ]; $image['rank'] = $rank_of[ $row['id'] ];
$image['is_favorite'] = isset($favorite_ids[ $row['id'] ]);
foreach (array('id', 'width', 'height', 'hit') as $k) foreach (array('id', 'width', 'height', 'hit') as $k)
{ {