Skip to content

Commit

Permalink
fixes #802 pwg.categories.getImages include all related albums to eac…
Browse files Browse the repository at this point in the history
…h photo

... and not only the requested album.
  • Loading branch information
plegall committed Sep 26, 2022
1 parent 099d1f3 commit 4d81d28
Showing 1 changed file with 74 additions and 24 deletions.
98 changes: 74 additions & 24 deletions include/ws_functions/pwg.categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function ws_categories_getImages($params, &$service)
global $user, $conf;

$images = array();
$image_ids = array();

//------------------------------------------------- get the related categories
$where_clauses = array();
Expand All @@ -45,7 +46,9 @@ function ws_categories_getImages($params, &$service)
);

$query = '
SELECT id, name, permalink, image_order
SELECT
id,
image_order
FROM '. CATEGORIES_TABLE .'
WHERE '. implode("\n AND ", $where_clauses) .'
;';
Expand Down Expand Up @@ -80,7 +83,7 @@ function ws_categories_getImages($params, &$service)
$favorite_ids = get_user_favorites();

$query = '
SELECT SQL_CALC_FOUND_ROWS i.*, GROUP_CONCAT(category_id) AS cat_ids
SELECT SQL_CALC_FOUND_ROWS i.*
FROM '. IMAGES_TABLE .' i
INNER JOIN '. IMAGE_CATEGORY_TABLE .' ON i.id=image_id
WHERE '. implode("\n AND ", $where_clauses) .'
Expand All @@ -93,6 +96,8 @@ function ws_categories_getImages($params, &$service)

while ($row = pwg_db_fetch_assoc($result))
{
$image_ids[] = $row['id'];

$image = array();
$image['is_favorite'] = isset($favorite_ids[ $row['id'] ]);
foreach (array('id', 'width', 'height', 'hit') as $k)
Expand All @@ -108,34 +113,79 @@ function ws_categories_getImages($params, &$service)
}
$image = array_merge($image, ws_std_get_urls($row));

$image_cats = array();
foreach (explode(',', $row['cat_ids']) as $cat_id)
$images[] = $image;
}

// let's take care of adding the related albums to each photo
if (count($image_ids) > 0)
{
$category_ids = array();

// find the complete list (given permissions) of albums linked to photos
$query = '
SELECT
image_id,
category_id
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE image_id IN ('.implode(',', $image_ids).')
AND '.get_sql_condition_FandF(array('forbidden_categories' => 'category_id'), null, true).'
;';
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
$category_ids[] = $row['category_id'];
@$categories_of_image[ $row['image_id'] ][] = $row['category_id'];
}

if (count($category_ids) > 0)
{
$url = make_index_url(
array(
'category' => $cats[$cat_id],
)
);
$page_url = make_picture_url(
array(
'category' => $cats[$cat_id],
'image_id' => $row['id'],
'image_file' => $row['file'],
// find details (for URL generation) about each album
$query = '
SELECT
id,
name,
permalink
FROM '. CATEGORIES_TABLE .'
WHERE id IN ('. implode(',', $category_ids) .')
;';
$details_for_category = query2array($query, 'id');
}

foreach ($images as $idx => $image)
{
$image_cats = array();

// it should not be possible at this point, but let's consider a photo can be in no album
if (!isset($categories_of_image[ $image['id'] ]))
{
continue;
}

foreach ($categories_of_image[ $image['id'] ] as $cat_id)
{
$url = make_index_url(array('category' => $details_for_category[$cat_id]));

$page_url = make_picture_url(
array(
'category' => $details_for_category[$cat_id],
'image_id' => $image['id'],
'image_file' => $image['file'],
)
);
$image_cats[] = array(
'id' => (int)$cat_id,
'url' => $url,
'page_url' => $page_url,

$image_cats[] = array(
'id' => (int)$cat_id,
'url' => $url,
'page_url' => $page_url,
);
}
}

$image['categories'] = new PwgNamedArray(
$image_cats,
'category',
array('id', 'url', 'page_url')
$images[$idx]['categories'] = new PwgNamedArray(
$image_cats,
'category',
array('id', 'url', 'page_url')
);
$images[] = $image;
}
}
}

Expand Down

0 comments on commit 4d81d28

Please sign in to comment.