<?php

namespace Opentalent\OtTemplating\ViewHelpers\EventsPage;

use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

/**
 * Retrieves and returns the Uri to the events page of the current site
 * The events page is the first page found with the template 'OpenTalent.OtTemplating->events'
 * Return an empty string if no such page is found
 *
 * Call it in fluid templates with:
 *
 *     {namespace ot=Opentalent\OtTemplating\ViewHelpers}
 *
 *     {ot:eventsPage.getUri()}
 *
 * @package Opentalent\OtTemplating\ViewHelpers
 */
class GetUriViewHelper extends OtAbstractViewHelper
{
    /**
     *  -- This method is expected by Fluid --
     * Renders the content as html
     *
     * @param array $arguments
     * @param \Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     * @return string|null
     */
    public static function renderStatic(
        array $arguments,
        \Closure $renderChildrenClosure,
        RenderingContextInterface $renderingContext
    ) {
        $pageUid = GetIdViewHelper::renderStatic($arguments, $renderChildrenClosure, $renderingContext);

        if ($pageUid == null) {
            return '';
        }

        $uriBuilder = $renderingContext->getControllerContext()->getUriBuilder();
        return $uriBuilder->setTargetPageUid($pageUid)->build();
    }
}
