TYPO3ContentElements/Classes/ViewHelpers/SkillGroupingViewHelper.php

100 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2023-07-07 09:28:13 +02:00
<?php
declare(strict_types=1);
namespace SkillDisplay\SkilldisplayContent\ViewHelpers;
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Closure;
2023-07-07 09:43:28 +02:00
use SkillDisplay\PHPToolKit\Entity\Skill;
use SkillDisplay\PHPToolKit\Entity\SkillSet;
2023-07-07 09:28:13 +02:00
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* Usage:
*
* <sd:skillGrouping skillSet="{skillSet}" as="domainGroup">
* {domainGroup.title}<br />
* <f:for each="{domainGroup.skills}" as="skill">
* {skill.name}<br />
* </f:for>
* </sd:skillGrouping>
2023-07-07 09:28:13 +02:00
*/
class SkillGroupingViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
protected $escapeOutput = false;
public function initializeArguments(): void
2023-07-07 09:28:13 +02:00
{
$this->registerArgument('skills', 'array', 'An array of skills to group', false, []);
2023-07-07 12:08:28 +02:00
$this->registerArgument('skillSet', SkillSet::class, 'The skills of this skill set will be grouped');
2023-07-07 09:28:13 +02:00
$this->registerArgument('as', 'string', 'The name of the iteration variable', true);
}
public static function renderStatic(
2023-07-07 12:16:23 +02:00
array $arguments,
Closure $renderChildrenClosure,
2023-07-07 09:28:13 +02:00
RenderingContextInterface $renderingContext
2023-07-07 12:16:23 +02:00
) {
2023-07-07 09:43:28 +02:00
/** @var Skill[] $skills */
2023-07-07 09:28:13 +02:00
$skills = $arguments['skills'];
2023-07-07 12:08:28 +02:00
/** @var SkillSet|null $skillSet */
2023-07-07 09:28:13 +02:00
$skillSet = $arguments['skillSet'];
$as = $arguments['as'];
$groups = self::group($skillSet ? $skillSet->getSkills() : $skills);
2023-07-07 09:28:13 +02:00
$templateVariableContainer = $renderingContext->getVariableProvider();
$output = '';
2023-07-07 09:43:28 +02:00
foreach ($groups as $group) {
2023-07-07 09:28:13 +02:00
$templateVariableContainer->add($as, $group);
$output .= $renderChildrenClosure();
$templateVariableContainer->remove($as);
}
return $output;
}
2023-07-07 09:43:28 +02:00
/**
* @param Skill[] $skills
* @return array
*/
2023-07-07 09:28:13 +02:00
protected static function group(array $skills): array
{
$result = [];
foreach ($skills as $skill) {
$skillAsArray = $skill->toArray();
$domainTag = !empty($skillAsArray['domainTag']) ? $skillAsArray['domainTag'] : ['uid' => 0, 'title' => ''];
if (!isset($result[$domainTag['uid']])) {
$result[$domainTag['uid']] = [
'uid' => $domainTag['uid'],
'title' => $domainTag['title'],
'skills' => [],
];
}
$result[$domainTag['uid']]['skills'][] = $skill;
}
2023-07-07 09:28:13 +02:00
return $result;
}
}