2016-09-01 23:13:23 +02:00
|
|
|
<?php
|
|
|
|
/*****************************************************************************
|
|
|
|
* ViewPhotoAlbum.php
|
|
|
|
* Contains the photo album index controller
|
|
|
|
*
|
|
|
|
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class ViewPhotoAlbum extends HTMLController
|
|
|
|
{
|
|
|
|
protected $iterator;
|
|
|
|
protected $total_count;
|
|
|
|
protected $base_url;
|
|
|
|
|
|
|
|
const PER_PAGE = 24;
|
|
|
|
|
|
|
|
public function __construct($title = 'Photos - ' . SITE_TITLE)
|
|
|
|
{
|
2016-09-02 11:16:01 +02:00
|
|
|
// Ensure we're logged in at this point.
|
|
|
|
if (!Registry::get('user')->isLoggedIn())
|
|
|
|
throw new NotAllowedException();
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
// Viewing an album?
|
|
|
|
if (isset($_GET['tag']))
|
|
|
|
{
|
|
|
|
$tag = Tag::fromSlug($_GET['tag']);
|
|
|
|
$id_tag = $tag->id_tag;
|
|
|
|
$title = $tag->tag;
|
2024-01-14 20:22:23 +01:00
|
|
|
$header_box = $this->getHeaderBox($tag);
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
// View the album root.
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$id_tag = 1;
|
2024-01-14 20:24:59 +01:00
|
|
|
$tag = Tag::fromId($id_tag);
|
2016-09-01 23:13:23 +02:00
|
|
|
$title = 'Albums';
|
|
|
|
}
|
|
|
|
|
|
|
|
// What page are we at?
|
2024-01-14 21:28:45 +01:00
|
|
|
$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
|
2016-09-01 23:13:23 +02:00
|
|
|
|
2024-01-14 21:28:45 +01:00
|
|
|
parent::__construct($title . ' - Page ' . $current_page . ' - ' . SITE_TITLE);
|
2016-09-03 22:23:27 +02:00
|
|
|
if (isset($header_box))
|
|
|
|
$this->page->adopt($header_box);
|
2016-09-01 23:13:23 +02:00
|
|
|
|
2024-01-14 21:06:54 +01:00
|
|
|
// Who contributed to this album?
|
|
|
|
$contributors = $tag->getContributorList();
|
|
|
|
|
|
|
|
// Enumerate possible filters
|
2024-01-14 22:17:09 +01:00
|
|
|
$filters = [];
|
|
|
|
if (!empty($contributors))
|
2024-01-14 21:06:54 +01:00
|
|
|
{
|
2024-01-15 13:41:51 +01:00
|
|
|
$filters[''] = ['id_user' => null, 'label' => '', 'caption' => 'All photos',
|
2024-01-15 11:26:17 +01:00
|
|
|
'link' => $tag->getUrl()];
|
|
|
|
|
2024-01-14 22:17:09 +01:00
|
|
|
foreach ($contributors as $contributor)
|
|
|
|
{
|
|
|
|
$filters[$contributor['slug']] = [
|
|
|
|
'id_user' => $contributor['id_user'],
|
2024-01-15 00:55:33 +01:00
|
|
|
'label' => $contributor['first_name'],
|
2024-01-14 22:17:09 +01:00
|
|
|
'caption' => sprintf('By %s (%s photos)',
|
|
|
|
$contributor['first_name'], $contributor['num_assets']),
|
2024-01-15 13:43:54 +01:00
|
|
|
'link' => $tag->getUrl() . '?by=' . $contributor['slug'],
|
2024-01-14 22:17:09 +01:00
|
|
|
];
|
|
|
|
}
|
2024-01-14 21:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Limit to a particular uploader?
|
2024-01-14 21:28:45 +01:00
|
|
|
$active_filter = '';
|
|
|
|
$id_user_uploaded = null;
|
2024-01-14 21:06:54 +01:00
|
|
|
if (!empty($_GET['by']))
|
|
|
|
{
|
|
|
|
if (!isset($filters[$_GET['by']]))
|
|
|
|
throw new UnexpectedValueException('Invalid filter for this album or tag.');
|
|
|
|
|
|
|
|
$active_filter = $_GET['by'];
|
2024-01-14 21:28:45 +01:00
|
|
|
$id_user_uploaded = $filters[$active_filter]['id_user'];
|
|
|
|
$filters[$active_filter]['is_active'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add an interface to query and modify the album/tag
|
2024-01-15 11:40:33 +01:00
|
|
|
$buttons = $this->getAlbumButtons($tag, $active_filter);
|
2024-01-14 21:28:45 +01:00
|
|
|
$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 ($current_page === 1)
|
|
|
|
{
|
|
|
|
$albums = $this->getAlbums($id_tag);
|
|
|
|
$index = new AlbumIndex($albums);
|
|
|
|
$this->page->adopt($index);
|
2024-01-14 21:06:54 +01:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:25:04 +01:00
|
|
|
// Are we viewing a person tag?
|
|
|
|
$is_person = $tag->kind === 'Person';
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
// Load a photo mosaic for the current tag.
|
2024-01-15 11:25:04 +01:00
|
|
|
list($mosaic, $total_count) = $this->getPhotoMosaic($id_tag, $id_user_uploaded, $current_page, !$is_person);
|
2016-09-01 23:13:23 +02:00
|
|
|
if (isset($mosaic))
|
2016-09-04 16:00:39 +02:00
|
|
|
{
|
|
|
|
$index = new PhotosIndex($mosaic, Registry::get('user')->isAdmin());
|
|
|
|
$this->page->adopt($index);
|
2024-01-15 00:43:02 +01:00
|
|
|
|
|
|
|
$url_params = [];
|
|
|
|
if (isset($tag) && $tag->id_parent != 0)
|
|
|
|
$url_params['in'] = $tag->id_tag;
|
|
|
|
if (!empty($active_filter))
|
|
|
|
$url_params['by'] = $active_filter;
|
|
|
|
|
|
|
|
if (!empty($url_params))
|
|
|
|
$index->setUrlSuffix('?' . http_build_query($url_params));
|
2016-09-04 16:00:39 +02:00
|
|
|
}
|
2016-09-01 23:13:23 +02:00
|
|
|
|
|
|
|
// Make a page index as needed, while we're at it.
|
|
|
|
if ($total_count > self::PER_PAGE)
|
|
|
|
{
|
|
|
|
$index = new PageIndex([
|
|
|
|
'recordCount' => $total_count,
|
|
|
|
'items_per_page' => self::PER_PAGE,
|
2024-01-14 21:28:45 +01:00
|
|
|
'start' => ($current_page - 1) * self::PER_PAGE,
|
2024-01-14 20:38:37 +01:00
|
|
|
'base_url' => $tag->getUrl(),
|
2024-01-15 13:43:54 +01:00
|
|
|
'page_slug' => 'page/%PAGE%/' . (!empty($active_filter) ? '?by=' . $active_filter : ''),
|
2023-12-02 01:38:07 +01:00
|
|
|
'index_class' => 'pagination-lg justify-content-around justify-content-lg-center',
|
2016-09-01 23:13:23 +02:00
|
|
|
]);
|
2023-03-11 13:20:59 +01:00
|
|
|
$this->page->adopt(new PageIndexWidget($index));
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the canonical url.
|
2024-01-14 21:28:45 +01:00
|
|
|
$this->page->setCanonicalUrl($tag->getUrl() . ($current_page > 1 ? 'page/' . $current_page . '/' : ''));
|
2016-09-01 23:13:23 +02:00
|
|
|
}
|
|
|
|
|
2024-01-14 21:06:54 +01:00
|
|
|
public function getPhotoMosaic($id_tag, $id_user_uploaded, $page, $sort_linear)
|
2016-09-01 23:13:23 +02:00
|
|
|
{
|
|
|
|
// Create an iterator.
|
|
|
|
list($this->iterator, $total_count) = AssetIterator::getByOptions([
|
|
|
|
'id_tag' => $id_tag,
|
2024-01-14 21:06:54 +01:00
|
|
|
'id_user_uploaded' => $id_user_uploaded,
|
2016-09-01 23:13:23 +02:00
|
|
|
'order' => 'date_captured',
|
2016-11-24 16:17:52 +01:00
|
|
|
'direction' => $sort_linear ? 'asc' : 'desc',
|
2016-09-01 23:13:23 +02:00
|
|
|
'limit' => self::PER_PAGE,
|
|
|
|
'page' => $page,
|
|
|
|
], true);
|
|
|
|
|
|
|
|
$mosaic = $total_count > 0 ? new PhotoMosaic($this->iterator) : null;
|
|
|
|
return [$mosaic, $total_count];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getAlbums($id_tag)
|
|
|
|
{
|
|
|
|
// Fetch subalbums.
|
|
|
|
$subalbums = Tag::getAlbums($id_tag);
|
|
|
|
|
|
|
|
// What assets are we using?
|
|
|
|
$id_assets = array_map(function($album) {
|
|
|
|
return (int) $album['id_asset_thumb'];
|
|
|
|
}, $subalbums);
|
|
|
|
|
|
|
|
// Fetch assets for thumbnails.
|
|
|
|
$assets = Asset::fromIds($id_assets, 'object');
|
|
|
|
|
|
|
|
// Build album list.
|
|
|
|
$albums = [];
|
|
|
|
foreach ($subalbums as $album)
|
|
|
|
{
|
|
|
|
$albums[$album['id_tag']] = [
|
|
|
|
'id_tag' => $album['id_tag'],
|
|
|
|
'caption' => $album['tag'],
|
|
|
|
'link' => BASEURL . '/' . $album['slug'] . '/',
|
2023-03-11 21:45:03 +01:00
|
|
|
'thumbnail' => !empty($album['id_asset_thumb']) && isset($assets[$album['id_asset_thumb']])
|
|
|
|
? $assets[$album['id_asset_thumb']]->getImage() : null,
|
2016-09-01 23:13:23 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $albums;
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:40:33 +01:00
|
|
|
private function getAlbumButtons(Tag $tag, $active_filter)
|
2023-03-11 20:34:58 +01:00
|
|
|
{
|
|
|
|
$buttons = [];
|
|
|
|
$user = Registry::get('user');
|
|
|
|
|
|
|
|
if ($user->isLoggedIn())
|
|
|
|
{
|
2024-01-15 11:40:33 +01:00
|
|
|
$suffix = !empty($active_filter) ? '&by=' . $active_filter : '';
|
2023-03-11 20:34:58 +01:00
|
|
|
$buttons[] = [
|
2024-01-15 11:40:33 +01:00
|
|
|
'url' => BASEURL . '/download/?tag=' . $tag->id_tag . $suffix,
|
2023-03-11 20:34:58 +01:00
|
|
|
'caption' => 'Download album',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-01-14 20:24:59 +01:00
|
|
|
if ($tag->id_parent != 0)
|
2023-03-11 20:34:58 +01:00
|
|
|
{
|
|
|
|
if ($tag->kind === 'Album')
|
|
|
|
{
|
|
|
|
$buttons[] = [
|
2024-01-14 20:24:59 +01:00
|
|
|
'url' => BASEURL . '/uploadmedia/?tag=' . $tag->id_tag,
|
2023-03-11 20:34:58 +01:00
|
|
|
'caption' => 'Upload photos here',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user->isAdmin())
|
|
|
|
{
|
|
|
|
if ($tag->kind === 'Album')
|
|
|
|
{
|
|
|
|
$buttons[] = [
|
2024-01-14 20:24:59 +01:00
|
|
|
'url' => BASEURL . '/editalbum/?id=' . $tag->id_tag,
|
2023-03-11 20:34:58 +01:00
|
|
|
'caption' => 'Edit album',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
elseif ($tag->kind === 'Person')
|
|
|
|
{
|
|
|
|
$buttons[] = [
|
2024-01-14 20:24:59 +01:00
|
|
|
'url' => BASEURL . '/edittag/?id=' . $tag->id_tag,
|
2023-03-11 20:34:58 +01:00
|
|
|
'caption' => 'Edit tag',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user->isAdmin() && (!isset($tag) || $tag->kind === 'Album'))
|
|
|
|
{
|
|
|
|
$buttons[] = [
|
2024-01-14 20:24:59 +01:00
|
|
|
'url' => BASEURL . '/addalbum/?tag=' . $tag->id_tag,
|
2023-03-11 20:34:58 +01:00
|
|
|
'caption' => 'Create subalbum',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $buttons;
|
|
|
|
}
|
|
|
|
|
2024-01-14 20:22:23 +01:00
|
|
|
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 "' . $ptag->tag . '"';
|
|
|
|
}
|
|
|
|
elseif ($tag->kind === 'Person')
|
|
|
|
{
|
|
|
|
$back_link = BASEURL . '/people/';
|
|
|
|
$back_link_title = 'Back to "People"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$description = !empty($tag->description) ? $tag->description : '';
|
|
|
|
return new AlbumHeaderBox($tag->tag, $description, $back_link, $back_link_title);
|
|
|
|
}
|
|
|
|
|
2016-09-01 23:13:23 +02:00
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if (isset($this->iterator))
|
|
|
|
$this->iterator->clean();
|
|
|
|
}
|
|
|
|
}
|