DigiComp.Menu/Classes/ViewHelpers/ProgressViewHelper.php

78 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2021-05-12 02:01:22 +02:00
namespace DigiComp\Menu\ViewHelpers;
/*
* This file is part of the DigiComp.Menu package.
*
* (c) digital competence
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
use Neos\FluidAdaptor\Core\ViewHelper\Exception;
/**
* Helps to get useful template variables for process menus
*/
class ProgressViewHelper extends AbstractViewHelper
{
protected $escapeOutput = false;
/**
* @throws Exception
*/
public function initializeArguments(): void
{
$this->registerArgument('for', 'string', 'path in Menu.yaml', false);
$this->registerArgument('as', 'string', 'Name in Frontend', false);
$this->registerArgument('links', 'array', 'links to show', true);
$this->registerArgument('activeStep', 'int', 'current active link index', false, 1);
$this->registerArgument('returnable', 'bool', 'can you go back to the last index', false, true);
$this->registerArgument('stepsAs', 'string', 'variable name of a single step', false, 'steps');
$this->registerArgument('backLinkAs', 'string', 'variable name of a backlink', false, 'backLink');
$this->registerArgument('linksAs', 'string', 'variable name of the links array', false, 'links');
$this->registerArgument(
'activeStepLinkAs',
'string',
'variable name of the active step link',
false,
'activeStepLink'
);
$this->registerArgument('offset', 'int', 'offset to start with', false, 1);
$this->registerArgument('linkCount', 'int', 'force this number of steps', false);
}
/**
* @return mixed
*/
public function render()
{
$links = $activeStep = $returnable = $stepsAs = $backLinkAs = $activeStepAs = $linksAs =
$activeStepLinkAs = $offset = $linkCount = null;
2023-02-07 16:44:52 +01:00
\extract($this->arguments, \EXTR_OVERWRITE);
//handling famous off by one
$activeStep -= $offset;
//make sure our array index is numeric
2023-02-07 16:44:52 +01:00
$links = \array_values($links);
if (!$linkCount) {
2023-02-07 16:44:52 +01:00
$linkCount = \count($links);
}
foreach ($links as $i => &$link) {
$link['completed'] = $activeStep > $i;
$link['returnable'] = $returnable && $i < $activeStep;
}
$this->templateVariableContainer->add($linksAs, $links);
$this->templateVariableContainer->add($stepsAs, $linkCount);
$this->templateVariableContainer->add($activeStepAs, $activeStep + $offset);
$this->templateVariableContainer->add($activeStepLinkAs, $links[$activeStep]);
if (isset($links[$activeStep - 1])) {
$this->templateVariableContainer->add($backLinkAs, $links[$activeStep - 1]);
}
return $this->renderChildren();
}
}