<?php
/*****************************************************************************
 * ViewTimeline.php
 * Contains the photo timeline index controller
 *
 * Kabuki CMS (C) 2013-2015, Aaron van Geffen
 *****************************************************************************/

class ViewTimeline extends HTMLController
{
	protected $iterator;
	protected $total_count;
	protected $base_url;

	const PER_PAGE = 24;

	public function __construct($title = 'Photos - ' . SITE_TITLE)
	{
		// Ensure we're logged in at this point.
		if (!Registry::get('user')->isLoggedIn())
			throw new NotAllowedException();

		// What page are we at?
		$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;

		parent::__construct('Timeline - Page ' . $page . ' - ' . SITE_TITLE);

		// Load a photo mosaic.
		list($this->iterator, $total_count) = AssetIterator::getByOptions([
			'order' => 'date_captured',
			'direction' => 'desc',
			'limit' => self::PER_PAGE,
			'page' => $page,
		], true);

		$mosaic = $total_count > 0 ? new PhotoMosaic($this->iterator) : null;
		if (isset($mosaic))
			$this->page->adopt(new PhotosIndex($mosaic, Registry::get('user')->isAdmin()));

		// 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,
				'start' => (isset($_GET['page']) ? $_GET['page'] - 1 : 0) * self::PER_PAGE,
				'base_url' => BASEURL . '/timeline/',
				'page_slug' => 'page/%PAGE%/',
				'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 . '/timeline/');
	}

	public function __destruct()
	{
		if (isset($this->iterator))
			$this->iterator->clean();
	}
}