Project

General

Profile

Feature #92875 ยป FlexViewHelper.php

Dieter Porth, 2020-11-19 11:49

 
<?php

namespace Porth\Timer\ViewHelpers;

/***************************************************************
*
* Copyright notice
*
* (c) 2020 Dr. Dieter Porth <info@mobger.de>
*
* All rights reserved
*
* This script 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 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use Porth\Timer\Exception\TimerException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

class FlexViewHelper extends AbstractViewHelper
{

use CompileWithRenderStatic;

/**
* @var boolean
*/
protected $escapeOutput = false;
protected const DEFAULT_FLATTEN_KEYS = 'data,sDEF,lDEF,vDEF';

/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('field',
'string',
'The string with the flexform',
true
);
$this->registerArgument('as',
'string',
'The name of the array variable with the flexform-entries',
true
);
$this->registerArgument('jsonInsteadOf',
'boolean',
'If true, the string will be parsed as a JSON-String.',
false,
false);
$this->registerArgument('flattenKeys',
'boolean',
'Comma-separated list of keys, which are remove to flatten the array-structure. (Remove in the frontend not Flexform-parts)',
false,
self::DEFAULT_FLATTEN_KEYS
);


}

/**
* https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array
*
* @param $array
* @return mixed
*/
protected static function arrayFlatten(&$listFlatKeys, $array)
{

if (!is_array($array)) {
return $array;
}

if (count($array) === 1) {
$key = array_key_first($array);
$value = self::arrayFlatten($listFlatKeys, $array[$key]);
if (in_array($key, $listFlatKeys)) {
return $value;
} else {
return [$key => $value];
}
}

$return = [];
foreach ($array as $key => $value) {
if (in_array($key, $listFlatKeys)) {
$return[] = self::arrayFlatten($listFlatKeys, $value);
} else {
$return[$key] = self::arrayFlatten($listFlatKeys, $value);
}
}
return $return;
}

/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
* @throws TimerException
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$templateVariableContainer = $renderingContext->getVariableProvider();
if (!isset($arguments['field'])) {
return '';
}

if (!is_string($arguments['field'])) {
throw new TimerException(
'FlexViewHelper only supports flex-fields or JSN-String and transform them to arrays. Your argument is not a string.', 1601245879);
}
if (isset($arguments['jsonInsteadOf']) && ($arguments['jsonInsteadOf'] === true)) {
$singleElement = json_decode($arguments['field'], true);
$flagError = (($singleElement === null) ?
'The string could not be decoded as a JSON-string. ' :
'');
} else {
$stringFlatKeys = ((!empty($arguments['flattenKeys'])) ?
$arguments['flattenKeys'] :
self::DEFAULT_FLATTEN_KEYS
);
$singleElementRaw = GeneralUtility::xml2array($arguments['field']);
$flagError = (((is_string($singleElementRaw)) && (substr($singleElementRaw, 0, strlen('Line ')) === 'Line ')) ?
'The string could not decode as xml/flexform. ' :
'');
$listFlatKeys = explode(',', $stringFlatKeys);
$singleElement = self::arrayFlatten($listFlatKeys, $singleElementRaw);

}
if (!empty($flagError)) {
throw new TimerException(
'The flexViewHelper failed on the value `' . $arguments['field'] . '` ' . $flagError .
'Is your viewhelper-configuration correct? Check your datas.',
1601245979
);

}
$templateVariableContainer->add($arguments['as'], $singleElement);
$output = $renderChildrenClosure();
$templateVariableContainer->remove($arguments['as']);
return $output;
}
}
    (1-1/1)