Compare commits

..

26 Commits

Author SHA1 Message Date
Aaron van Geffen 25feb31c1a EditAsset: some hardening; deduplicate redirect code 2024-01-18 13:40:17 +01:00
Aaron van Geffen 6ec5994de0 ViewPhotoAlbum: build edit menu in controller 2024-01-18 13:18:22 +01:00
Aaron van Geffen 24c2e9cdcf PhotosIndex: allow setting image as the album cover as well 2024-01-17 18:28:24 +01:00
Aaron van Geffen 0487ad16b9 Asset: remove old setKeyData method 2024-01-17 17:54:18 +01:00
Aaron van Geffen c2aae4fb6e EditAsset: replace Asset::setKeyData with Asset::save equivalent 2024-01-17 17:54:14 +01:00
Aaron van Geffen 069d56383e PhotosIndex: replace edit button with edit menu 2024-01-17 17:51:45 +01:00
Aaron van Geffen 8613054d69 Asset: introduce save method 2024-01-17 17:51:25 +01:00
Roflin cf6adbf80c Merge pull request 'Allow users to filter albums by contributors' (#48) from refactor/viewalbum into master
Reviewed-on: #48
Reviewed-by: Bart Schuurmans <bart@minnozz.com>
2024-01-20 20:11:16 +01:00
Aaron van Geffen 30bc0bb884 ViewPhotoAlbum: don't include empty $by in page links 2024-01-15 13:44:51 +01:00
Aaron van Geffen c0dd2cbd49 ViewPhotoAlbum: drop 'Show' from empty filter caption 2024-01-15 13:41:51 +01:00
Aaron van Geffen bb81f7e086 Download: remove limits on maximum execution time 2024-01-15 11:46:01 +01:00
Aaron van Geffen 4b289a5e83 Download: allow limiting by user uploaded as well 2024-01-15 11:40:33 +01:00
Aaron van Geffen ec2d702a0d ViewPhoto: simplify filter verification 2024-01-15 11:33:43 +01:00
Aaron van Geffen 52472d8b58 ViewPhotoAlbum: add 'label' key to empty filter as well 2024-01-15 11:26:17 +01:00
Aaron van Geffen 5d990501f6 ViewPhotoAlbum: move $is_person declaration to where it's used 2024-01-15 11:25:04 +01:00
Aaron van Geffen 1f53689e4b AlbumButtonBox: add visual cue to indicate a filter is active 2024-01-15 00:55:33 +01:00
Aaron van Geffen accf093935 PageIndex: rewrite getLink to be way less messy 2024-01-15 00:51:06 +01:00
Aaron van Geffen d8c3e76df6 ViewPhoto: take filter into account for prev/next links 2024-01-15 00:43:02 +01:00
Aaron van Geffen f33a7e397c Asset: combine getUrlFor{Next,Previous}InSet into one private method 2024-01-15 00:19:39 +01:00
Aaron van Geffen 9c00248a7f ViewPhotoAlbum: don't populate filter box if there are no album contributors 2024-01-14 22:17:09 +01:00
Aaron van Geffen 99b867b241 AlbumButtonBox: add way for users to select an album filter 2024-01-14 21:28:45 +01:00
Aaron van Geffen 6a25ecec23 ViewPhotoAlbum: add method to filter by id_user_uploaded 2024-01-14 21:06:54 +01:00
Aaron van Geffen 16683d2f1f Tag: add getContributorList method 2024-01-14 21:06:34 +01:00
Aaron van Geffen 7cdcf8197c ViewPhotoAlbum: use Tag::getUrl instead of fumbling with $_GET['tag'] 2024-01-14 20:40:58 +01:00
Aaron van Geffen 25b9528628 ViewPhotoAlbum: simplify tag handling in getAlbumButtons 2024-01-14 20:40:58 +01:00
Aaron van Geffen 08cdbfe7b6 ViewPhotoAlbum: move some logic into new prepareHeaderBox method 2024-01-14 20:40:58 +01:00
11 changed files with 265 additions and 111 deletions

View File

@ -21,22 +21,30 @@ class Download
$tag = (int)$_GET['tag'];
$album = Tag::fromId($tag);
if (isset($_GET['by']) && ($user = Member::fromSlug($_GET['by'])) !== false)
$id_user_uploaded = $user->getUserId();
else
$id_user_uploaded = null;
if (isset($_SESSION['current_export']))
throw new UserFacingException('You can only export one album at the same time. Please wait until the other download finishes, or try again later.');
// So far so good?
$this->exportAlbum($album);
$this->exportAlbum($album, $id_user_uploaded);
exit;
}
private function exportAlbum(Tag $album)
private function exportAlbum(Tag $album, $id_user_uploaded)
{
$files = [];
$album_ids = array_merge([$album->id_tag], $this->getChildAlbumIds($album->id_tag));
foreach ($album_ids as $album_id)
{
$iterator = AssetIterator::getByOptions(['id_tag' => $album_id]);
$iterator = AssetIterator::getByOptions([
'id_tag' => $album_id,
'id_user_uploaded' => $id_user_uploaded,
]);
while ($asset = $iterator->next())
$files[] = join(DIRECTORY_SEPARATOR, [$asset->getSubdir(), $asset->getFilename()]);
}
@ -71,6 +79,9 @@ class Download
// STDOUT should not block.
stream_set_blocking($pipes[1], 0);
// Allow this the download to take its time...
set_time_limit(0);
header('Pragma: no-cache');
header('Content-Description: File Download');
header('Content-disposition: attachment; filename="' . $album->tag . '.tar"');

View File

@ -46,6 +46,18 @@ class ViewPhoto extends HTMLController
if (isset($tag))
$page->setTag($tag);
// Keeping tabs on a filter?
if (isset($_GET['by']))
{
// Let's first verify that the filter is valid
$user = Member::fromSlug($_GET['by']);
if (!$user)
throw new UnexpectedValueException('Invalid filter for this album or tag.');
// Alright, let's run with it then
$page->setActiveFilter($user->getSlug());
}
$this->page->adopt($page);
$this->page->setCanonicalUrl($this->photo->getPageUrl());
}

View File

@ -26,59 +26,88 @@ class ViewPhotoAlbum extends HTMLController
$tag = Tag::fromSlug($_GET['tag']);
$id_tag = $tag->id_tag;
$title = $tag->tag;
$description = !empty($tag->description) ? $tag->description : '';
// Can we go up a level?
if ($tag->id_parent != 0)
{
$ptag = Tag::fromId($tag->id_parent);
$back_link = BASEURL . '/' . (!empty($ptag->slug) ? $ptag->slug . '/' : '');
$back_link_title = 'Back to &quot;' . $ptag->tag . '&quot;';
}
elseif ($tag->kind === 'Person')
{
$back_link = BASEURL . '/people/';
$back_link_title = 'Back to &quot;People&quot;';
$is_person = true;
}
$header_box = new AlbumHeaderBox($title, $description, $back_link, $back_link_title);
$header_box = $this->getHeaderBox($tag);
}
// View the album root.
else
{
$id_tag = 1;
$tag = Tag::fromId($id_tag);
$title = 'Albums';
}
// What page are we at?
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
parent::__construct($title . ' - Page ' . $page . ' - ' . SITE_TITLE);
parent::__construct($title . ' - Page ' . $current_page . ' - ' . SITE_TITLE);
if (isset($header_box))
$this->page->adopt($header_box);
// Can we do fancy things here?
$buttons = $this->getAlbumButtons($id_tag, $tag ?? null);
if (!empty($buttons))
$this->page->adopt(new AlbumButtonBox($buttons));
// Who contributed to this album?
$contributors = $tag->getContributorList();
// Enumerate possible filters
$filters = [];
if (!empty($contributors))
{
$filters[''] = ['id_user' => null, 'label' => '', 'caption' => 'All photos',
'link' => $tag->getUrl()];
foreach ($contributors as $contributor)
{
$filters[$contributor['slug']] = [
'id_user' => $contributor['id_user'],
'label' => $contributor['first_name'],
'caption' => sprintf('By %s (%s photos)',
$contributor['first_name'], $contributor['num_assets']),
'link' => $tag->getUrl() . '?by=' . $contributor['slug'],
];
}
}
// Limit to a particular uploader?
$active_filter = '';
$id_user_uploaded = null;
if (!empty($_GET['by']))
{
if (!isset($filters[$_GET['by']]))
throw new UnexpectedValueException('Invalid filter for this album or tag.');
$active_filter = $_GET['by'];
$id_user_uploaded = $filters[$active_filter]['id_user'];
$filters[$active_filter]['is_active'] = true;
}
// Add an interface to query and modify the album/tag
$buttons = $this->getAlbumButtons($tag, $active_filter);
$button_strip = new AlbumButtonBox($buttons, $filters, $active_filter);
$this->page->adopt($button_strip);
// Fetch subalbums, but only if we're on the first page.
if ($page === 1)
if ($current_page === 1)
{
$albums = $this->getAlbums($id_tag);
$index = new AlbumIndex($albums);
$this->page->adopt($index);
}
// Are we viewing a person tag?
$is_person = $tag->kind === 'Person';
// Load a photo mosaic for the current tag.
list($mosaic, $total_count) = $this->getPhotoMosaic($id_tag, $page, !isset($is_person));
list($mosaic, $total_count) = $this->getPhotoMosaic($id_tag, $id_user_uploaded, $current_page, !$is_person);
if (isset($mosaic))
{
$index = new PhotosIndex($mosaic, Registry::get('user')->isAdmin());
$this->page->adopt($index);
$url_suffix = $id_tag > 1 ? 'in=' . $id_tag : '';
$url_params = [];
if (isset($tag) && $tag->id_parent != 0)
$url_params['in'] = $tag->id_tag;
if (!empty($active_filter))
$url_params['by'] = $active_filter;
$url_suffix = http_build_query($url_params);
$index->setUrlSuffix('?' . $url_suffix);
$menu_items = $this->getEditMenuItems('&' . $url_suffix);
@ -91,24 +120,24 @@ class ViewPhotoAlbum extends HTMLController
$index = new PageIndex([
'recordCount' => $total_count,
'items_per_page' => self::PER_PAGE,
'start' => (isset($_GET['page']) ? $_GET['page'] - 1 : 0) * self::PER_PAGE,
'base_url' => BASEURL . '/' . (isset($_GET['tag']) ? $_GET['tag'] . '/' : ''),
'page_slug' => 'page/%PAGE%/',
'start' => ($current_page - 1) * self::PER_PAGE,
'base_url' => $tag->getUrl(),
'page_slug' => 'page/%PAGE%/' . (!empty($active_filter) ? '?by=' . $active_filter : ''),
'index_class' => 'pagination-lg justify-content-around justify-content-lg-center',
]);
$this->page->adopt(new PageIndexWidget($index));
}
// Set the canonical url.
$this->page->setCanonicalUrl(BASEURL . '/' . (isset($_GET['tag']) ? $_GET['tag'] . '/' : '') .
($page > 1 ? 'page/' . $page . '/' : ''));
$this->page->setCanonicalUrl($tag->getUrl() . ($current_page > 1 ? 'page/' . $current_page . '/' : ''));
}
public function getPhotoMosaic($id_tag, $page, $sort_linear)
public function getPhotoMosaic($id_tag, $id_user_uploaded, $page, $sort_linear)
{
// Create an iterator.
list($this->iterator, $total_count) = AssetIterator::getByOptions([
'id_tag' => $id_tag,
'id_user_uploaded' => $id_user_uploaded,
'order' => 'date_captured',
'direction' => $sort_linear ? 'asc' : 'desc',
'limit' => self::PER_PAGE,
@ -148,25 +177,26 @@ class ViewPhotoAlbum extends HTMLController
return $albums;
}
private function getAlbumButtons($id_tag, $tag)
private function getAlbumButtons(Tag $tag, $active_filter)
{
$buttons = [];
$user = Registry::get('user');
if ($user->isLoggedIn())
{
$suffix = !empty($active_filter) ? '&by=' . $active_filter : '';
$buttons[] = [
'url' => BASEURL . '/download/?tag=' . $id_tag,
'url' => BASEURL . '/download/?tag=' . $tag->id_tag . $suffix,
'caption' => 'Download album',
];
}
if (isset($tag))
if ($tag->id_parent != 0)
{
if ($tag->kind === 'Album')
{
$buttons[] = [
'url' => BASEURL . '/uploadmedia/?tag=' . $id_tag,
'url' => BASEURL . '/uploadmedia/?tag=' . $tag->id_tag,
'caption' => 'Upload photos here',
];
}
@ -176,14 +206,14 @@ class ViewPhotoAlbum extends HTMLController
if ($tag->kind === 'Album')
{
$buttons[] = [
'url' => BASEURL . '/editalbum/?id=' . $id_tag,
'url' => BASEURL . '/editalbum/?id=' . $tag->id_tag,
'caption' => 'Edit album',
];
}
elseif ($tag->kind === 'Person')
{
$buttons[] = [
'url' => BASEURL . '/edittag/?id=' . $id_tag,
'url' => BASEURL . '/edittag/?id=' . $tag->id_tag,
'caption' => 'Edit tag',
];
}
@ -193,7 +223,7 @@ class ViewPhotoAlbum extends HTMLController
if ($user->isAdmin() && (!isset($tag) || $tag->kind === 'Album'))
{
$buttons[] = [
'url' => BASEURL . '/addalbum/?tag=' . $id_tag,
'url' => BASEURL . '/addalbum/?tag=' . $tag->id_tag,
'caption' => 'Create subalbum',
];
}
@ -241,6 +271,25 @@ class ViewPhotoAlbum extends HTMLController
return $items;
}
private function getHeaderBox(Tag $tag)
{
// Can we go up a level?
if ($tag->id_parent != 0)
{
$ptag = Tag::fromId($tag->id_parent);
$back_link = BASEURL . '/' . (!empty($ptag->slug) ? $ptag->slug . '/' : '');
$back_link_title = 'Back to &quot;' . $ptag->tag . '&quot;';
}
elseif ($tag->kind === 'Person')
{
$back_link = BASEURL . '/people/';
$back_link_title = 'Back to &quot;People&quot;';
}
$description = !empty($tag->description) ? $tag->description : '';
return new AlbumHeaderBox($tag->tag, $description, $back_link, $back_link_title);
}
public function __destruct()
{
if (isset($this->iterator))

View File

@ -702,65 +702,79 @@ class Asset
get_object_vars($this));
}
public function getUrlForPreviousInSet(?Tag $tag)
protected function getUrlForAdjacentInSet($prevNext, ?Tag $tag, $activeFilter)
{
$row = Registry::get('db')->queryAssoc('
SELECT a.*
' . (isset($tag) ? '
FROM assets_tags AS t
INNER JOIN assets AS a ON a.id_asset = t.id_asset
WHERE t.id_tag = {int:id_tag} AND
(a.date_captured, a.id_asset) < ({datetime:date_captured}, {int:id_asset})
ORDER BY a.date_captured DESC, a.id_asset DESC'
: '
FROM assets AS a
WHERE (a.date_captured, a.id_asset) > ({datetime:date_captured}, {int:id_asset})
ORDER BY date_captured ASC, a.id_asset ASC')
. '
LIMIT 1',
[
'id_asset' => $this->id_asset,
'id_tag' => isset($tag) ? $tag->id_tag : null,
'date_captured' => $this->date_captured,
]);
$next = $prevNext === 'next';
$previous = !$next;
if ($row)
$where = [];
$params = [
'id_asset' => $this->id_asset,
'date_captured' => $this->date_captured,
];
// Direction depends on whether we're browsing a tag or timeline
if (isset($tag))
{
$obj = self::byRow($row, 'object');
return $obj->getPageUrl() . ($tag ? '?in=' . $tag->id_tag : '');
$where[] = 't.id_tag = {int:id_tag}';
$params['id_tag'] = $tag->id_tag;
$params['where_op'] = $previous ? '<' : '>';
$params['order_dir'] = $previous ? 'DESC' : 'ASC';
}
else
{
$params['where_op'] = $previous ? '>' : '<';
$params['order_dir'] = $previous ? 'ASC' : 'DESC';
}
// Take active filter into account as well
if (!empty($activeFilter) && ($user = Member::fromSlug($activeFilter)) !== false)
{
$where[] = 'id_user_uploaded = {int:id_user_uploaded}';
$params['id_user_uploaded'] = $user->getUserId();
}
// Use complete ordering when sorting the set
$where[] = '(a.date_captured, a.id_asset) {raw:where_op} ' .
'({datetime:date_captured}, {int:id_asset})';
// Stringify conditions together
$where = '(' . implode(') AND (', $where) . ')';
// Run query, leaving out tags table if not required
$row = Registry::get('db')->queryAssoc('
SELECT a.*
FROM assets AS a
' . (isset($tag) ? '
INNER JOIN assets_tags AS t ON a.id_asset = t.id_asset' : '') . '
WHERE ' . $where . '
ORDER BY a.date_captured {raw:order_dir}, a.id_asset {raw:order_dir}
LIMIT 1',
$params);
if (!$row)
return false;
$obj = self::byRow($row, 'object');
$urlParams = [];
if (isset($tag))
$urlParams['in'] = $tag->id_tag;
if (!empty($activeFilter))
$urlParams['by'] = $activeFilter;
$queryString = !empty($urlParams) ? '?' . http_build_query($urlParams) : '';
return $obj->getPageUrl() . $queryString;
}
public function getUrlForNextInSet(?Tag $tag)
public function getUrlForPreviousInSet(?Tag $tag, $activeFilter)
{
$row = Registry::get('db')->queryAssoc('
SELECT a.*
' . (isset($tag) ? '
FROM assets_tags AS t
INNER JOIN assets AS a ON a.id_asset = t.id_asset
WHERE t.id_tag = {int:id_tag} AND
(a.date_captured, a.id_asset) > ({datetime:date_captured}, {int:id_asset})
ORDER BY a.date_captured ASC, a.id_asset ASC'
: '
FROM assets AS a
WHERE (a.date_captured, a.id_asset) < ({datetime:date_captured}, {int:id_asset})
ORDER BY date_captured DESC, a.id_asset DESC')
. '
LIMIT 1',
[
'id_asset' => $this->id_asset,
'id_tag' => isset($tag) ? $tag->id_tag : null,
'date_captured' => $this->date_captured,
]);
return $this->getUrlForAdjacentInSet('previous', $tag, $activeFilter);
}
if ($row)
{
$obj = self::byRow($row, 'object');
return $obj->getPageUrl() . ($tag ? '?in=' . $tag->id_tag : '');
}
else
return false;
public function getUrlForNextInSet(?Tag $tag, $activeFilter)
{
return $this->getUrlForAdjacentInSet('next', $tag, $activeFilter);
}
}

View File

@ -118,6 +118,11 @@ class AssetIterator extends Asset
else
$where[] = 'a.mimetype = {string:mime_type}';
}
if (isset($options['id_user_uploaded']))
{
$params['id_user_uploaded'] = $options['id_user_uploaded'];
$where[] = 'id_user_uploaded = {int:id_user_uploaded}';
}
if (isset($options['id_tag']))
{
$params['id_tag'] = $options['id_tag'];

View File

@ -155,24 +155,20 @@ class PageIndex
public function getLink($start = null, $order = null, $dir = null)
{
$url = $this->base_url;
$amp = strpos($this->base_url, '?') ? '&' : '?';
$page = !is_string($start) ? ($start / $this->items_per_page) + 1 : $start;
$url = $this->base_url . str_replace('%PAGE%', $page, $this->page_slug);
if (!empty($start))
{
$page = $start !== '%d' ? ($start / $this->items_per_page) + 1 : $start;
$url .= strtr($this->page_slug, ['%PAGE%' => $page, '%AMP%' => $amp]);
$amp = '&';
}
$urlParams = [];
if (!empty($order))
{
$url .= $amp . 'order=' . $order;
$amp = '&';
}
$urlParams['order'] = $order;
if (!empty($dir))
$urlParams['dir'] = $dir;
if (!empty($urlParams))
{
$url .= $amp . 'dir=' . $dir;
$amp = '&';
$queryString = (strpos($uri, '?') !== false ? '&' : '?');
$queryString .= http_build_query($urlParams);
$url .= $queryString;
}
return $url;

View File

@ -141,6 +141,21 @@ class Tag
return $rows;
}
public function getContributorList()
{
return Registry::get('db')->queryPairs('
SELECT u.id_user, u.first_name, u.surname, u.slug, COUNT(*) AS num_assets
FROM assets_tags AS at
LEFT JOIN assets AS a ON at.id_asset = a.id_asset
LEFT JOIN users AS u ON a.id_user_uploaded = u.id_user
WHERE at.id_tag = {int:id_tag}
GROUP BY a.id_user_uploaded
ORDER BY u.first_name, u.surname',
[
'id_tag' => $this->id_tag,
]);
}
public static function getPeople($id_parent = 0, $offset = 0, $limit = 24, $return_format = 'array')
{
$rows = Registry::get('db')->queryAssocs('

View File

@ -75,6 +75,11 @@ abstract class User
return $this->ip_address;
}
public function getSlug()
{
return $this->slug;
}
/**
* Returns whether user is logged in.
*/

View File

@ -367,9 +367,11 @@ div.polaroid a {
---------------------*/
.album_button_box {
float: right;
display: flex;
margin-bottom: 3rem;
}
.album_button_box > a {
.album_button_box > a,
.album_button_box .btn {
background: var(--bs-body-bg);
box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
border-color: var( --bs-secondary-bg);
@ -377,7 +379,9 @@ div.polaroid a {
padding: 8px 10px;
margin-left: 12px;
}
.album_button_box > a:hover {
.album_button_box > a:hover,
.album_button_box .btn:hover,
.album_button_box .btn:focus {
background: var(--bs-secondary-bg);
border-color: var(--bs-tertiary-bg);
color: var(--bs-secondary-color);

View File

@ -8,11 +8,15 @@
class AlbumButtonBox extends Template
{
private $active_filter;
private $buttons;
private $filters;
public function __construct($buttons)
public function __construct(array $buttons, array $filters, $active_filter)
{
$this->active_filter = $active_filter;
$this->buttons = $buttons;
$this->filters = $filters;
}
public function html_main()
@ -24,6 +28,39 @@ class AlbumButtonBox extends Template
echo '
<a class="btn btn-light" href="', $button['url'], '">', $button['caption'], '</a>';
if (!empty($this->filters))
{
echo '
<div class="dropdown">
<button class="btn btn-light dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-filter"></i>';
if ($this->active_filter)
{
echo '
<span class="badge text-bg-danger">',
$this->filters[$this->active_filter]['label'], '</span>';
}
echo '
</button>
<ul class="dropdown-menu">';
foreach ($this->filters as $key => $filter)
{
$is_active = $key === $this->active_filter;
echo '
<li><a class="dropdown-item', $is_active ? ' active' : '',
'" href="', $filter['link'], '">',
$filter['caption'],
'</a></li>';
}
echo '
</ul>
</div>';
}
echo '
</div>';
}

View File

@ -8,6 +8,7 @@
class PhotoPage extends Template
{
private $activeFilter;
private $photo;
private $metaData;
private $tag;
@ -80,6 +81,11 @@ class PhotoPage extends Template
</a>';
}
public function setActiveFilter($filter)
{
$this->activeFilter = $filter;
}
public function setTag(Tag $tag)
{
$this->tag = $tag;
@ -87,14 +93,14 @@ class PhotoPage extends Template
private function photoNav()
{
if ($previousUrl = $this->photo->getUrlForPreviousInSet($this->tag))
if ($previousUrl = $this->photo->getUrlForPreviousInSet($this->tag, $this->activeFilter))
echo '
<a href="', $previousUrl, '#photo_frame" id="previous_photo"><i class="bi bi-arrow-left"></i></a>';
else
echo '
<span id="previous_photo"><i class="bi bi-arrow-left"></i></span>';
if ($nextUrl = $this->photo->getUrlForNextInSet($this->tag))
if ($nextUrl = $this->photo->getUrlForNextInSet($this->tag, $this->activeFilter))
echo '
<a href="', $nextUrl, '#photo_frame" id="next_photo"><i class="bi bi-arrow-right"></i></a>';
else