1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 
<?php
namespace Omeka\View\Helper;

use Omeka\Api\Representation\SitePageRepresentation;
use Zend\View\Exception;
use Zend\View\Helper\AbstractHelper;

/**
 * View helper for rendering site page pagination.
 */
class SitePagePagination extends AbstractHelper
{
    /**
     * @var SitePageRepresentation
     */
    protected $page;

    /**
     * @var null|SitePageRepresentation
     */
    protected $prevPage;

    /**
     * @var null|SitePageRepresentation
     */
    protected $nextPage;

    /**
     * @var bool Whether the page is found in site navigation.
     */
    protected $pageInNav = true;

    /**
     * Return the site page pagination markup.
     *
     * Returns null if the page is not found in site navigation.
     *
     * @throws Exception\RuntimeException
     * @return string|null
     */
    public function __invoke()
    {
        if (null === $this->page) {
            throw new Exception\RuntimeException('No site page provided');
        }
        if (!$this->pageInNav) {
            return null;
        }
        return $this->getView()->partial(
            'common/site-page-pagination',
            [
                'page' => $this->page,
                'prevPage' => $this->prevPage,
                'nextPage' => $this->nextPage,
            ]
        );
    }

    /**
     * Set the current, previous, and next page.
     *
     * @param SitePageRepresentation $page
     */
    public function setPage(SitePageRepresentation $page)
    {
        $this->page = $page;
        $linkedPages = $page->site()->linkedPages();
        if (!array_key_exists($page->id(), $linkedPages)) {
            // Page not found in navigation. Don't attempt to find prev/next.
            $this->pageInNav = false;
            return;
        }
        // Iterate the linked pages, setting the previous and next pages, if any.
        while ($linkedPage = current($linkedPages)) {
            if ($page->id() === $linkedPage->id()) {
                $this->nextPage = next($linkedPages);
                break;
            }
            $this->prevPage = $linkedPage;
            next($linkedPages);
        }
    }
}