From 231d36613c0ebdca9a2929737e2b99d6a672f42c Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sat, 23 Jul 2022 06:43:02 -0400 Subject: [PATCH] issue #1615 include favorite status in image list API calls --- include/functions_url.inc.php | 24 ++++++++++++++++++++++++ include/ws_functions/pwg.categories.php | 2 ++ include/ws_functions/pwg.images.php | 2 ++ include/ws_functions/pwg.tags.php | 2 ++ 4 files changed, 30 insertions(+) diff --git a/include/functions_url.inc.php b/include/functions_url.inc.php index 68306c337..a32521be4 100644 --- a/include/functions_url.inc.php +++ b/include/functions_url.inc.php @@ -880,4 +880,28 @@ function url_is_remote($url) 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'); +} + ?> diff --git a/include/ws_functions/pwg.categories.php b/include/ws_functions/pwg.categories.php index 8e63616d2..f98c2eb34 100644 --- a/include/ws_functions/pwg.categories.php +++ b/include/ws_functions/pwg.categories.php @@ -77,6 +77,7 @@ SELECT id, name, permalink, image_order $order_by = $cats[ $params['cat_id'][0] ]['image_order']; } $order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY '.$order_by; + $favorite_ids = get_user_favorites(); $query = ' 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)) { $image = array(); + $image['is_favorite'] = isset($favorite_ids[ $row['id'] ]); foreach (array('id', 'width', 'height', 'hit') as $k) { if (isset($row[$k])) diff --git a/include/ws_functions/pwg.images.php b/include/ws_functions/pwg.images.php index 390413c4b..be5dc7677 100644 --- a/include/ws_functions/pwg.images.php +++ b/include/ws_functions/pwg.images.php @@ -646,10 +646,12 @@ SELECT * ;'; $result = pwg_query($query); $image_ids = array_flip($image_ids); + $favorite_ids = get_user_favorites(); while ($row = pwg_db_fetch_assoc($result)) { $image = array(); + $image['is_favorite'] = isset($favorite_ids[ $row['id'] ]); foreach (array('id', 'width', 'height', 'hit') as $k) { if (isset($row[$k])) diff --git a/include/ws_functions/pwg.tags.php b/include/ws_functions/pwg.tags.php index a3244ea19..fe0e726a3 100644 --- a/include/ws_functions/pwg.tags.php +++ b/include/ws_functions/pwg.tags.php @@ -134,6 +134,7 @@ SELECT image_id, GROUP_CONCAT(tag_id) AS tag_ids if (!empty($image_ids)) { $rank_of = array_flip($image_ids); + $favorite_ids = get_user_favorites(); $query = ' SELECT * @@ -146,6 +147,7 @@ SELECT * { $image = array(); $image['rank'] = $rank_of[ $row['id'] ]; + $image['is_favorite'] = isset($favorite_ids[ $row['id'] ]); foreach (array('id', 'width', 'height', 'hit') as $k) {