first commit

This commit is contained in:
salam committed 2026-10-06 19:40:28 +02:00
commit e81864e683
2984 files changed
+267797

No files matched your search

@@ -0,0 +1,142 @@
<?php
declare(strict_types=1);
namespace In2code\Femanager\ViewHelpers\Pagination;
use Closure;
use In2code\Femanager\Exception\NotPaginatableException;
use TYPO3\CMS\Core\Pagination\ArrayPaginator;
use TYPO3\CMS\Core\Pagination\PaginationInterface;
use TYPO3\CMS\Core\Pagination\PaginatorInterface;
use TYPO3\CMS\Core\Pagination\SimplePagination;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Service\ExtensionService;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception as FluidViewHelperException;
/**
* PaginateViewHelper
*/
class PaginateViewHelper extends AbstractViewHelper
{
/**
* @var bool
*/
protected $escapeOutput = false;
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('objects', 'mixed', 'array or queryresult', true);
$this->registerArgument('as', 'string', 'new variable name', true);
$this->registerArgument('itemsPerPage', 'int', 'items per page', false, 10);
$this->registerArgument('name', 'string', 'unique identification - will take "as" as fallback', false, '');
}
/**
* @param array $arguments
* @param Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
* @throws NotPaginatableException
*/
public static function renderStatic(
array $arguments,
Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
): string {
if ($arguments['objects'] === null) {
return $renderChildrenClosure();
}
$templateVariableContainer = $renderingContext->getVariableProvider();
$templateVariableContainer->add($arguments['as'], [
'pagination' => self::getPagination($arguments, $renderingContext),
'paginator' => self::getPaginator($arguments, $renderingContext),
'name' => self::getName($arguments)
]);
$output = $renderChildrenClosure();
$templateVariableContainer->remove($arguments['as']);
return $output;
}
/**
* @param array $arguments
* @param RenderingContextInterface $renderingContext
* @return PaginationInterface
* @throws NotPaginatableException
*/
protected static function getPagination(
array $arguments,
RenderingContextInterface $renderingContext
): PaginationInterface {
$paginator = self::getPaginator($arguments, $renderingContext);
return GeneralUtility::makeInstance(SimplePagination::class, $paginator);
}
/**
* @param array $arguments
* @param RenderingContextInterface $renderingContext
* @return PaginatorInterface
* @throws NotPaginatableException
* @throws FluidViewHelperException
*/
protected static function getPaginator(
array $arguments,
RenderingContextInterface $renderingContext
): PaginatorInterface {
if (is_array($arguments['objects'])) {
$paginatorClass = ArrayPaginator::class;
} elseif (is_a($arguments['objects'], QueryResultInterface::class)) {
$paginatorClass = QueryResultPaginator::class;
} else {
throw new NotPaginatableException('Given object is not supported for pagination', 1634132847);
}
return GeneralUtility::makeInstance(
$paginatorClass,
$arguments['objects'],
self::getPageNumber($arguments, $renderingContext),
$arguments['itemsPerPage']
);
}
/**
* @param array $arguments
* @param RenderingContextInterface $renderingContext
* @return int
* @throws FluidViewHelperException
*/
protected static function getPageNumber(array $arguments, RenderingContextInterface $renderingContext): int
{
if (! $renderingContext instanceof RenderingContext) {
throw new FluidViewHelperException(
'Something went wrong; RenderingContext should be available in ViewHelper',
1637986936
);
}
$extensionName = $renderingContext->getRequest()->getControllerExtensionName();
$pluginName = $renderingContext->getRequest()->getPluginName();
$extensionService = GeneralUtility::makeInstance(ExtensionService::class);
$pluginNamespace = $extensionService->getPluginNamespace($extensionName, $pluginName);
$variables = GeneralUtility::_GP($pluginNamespace);
if ($variables !== null) {
if (!empty($variables[self::getName($arguments)]['currentPage'])) {
return (int)$variables[self::getName($arguments)]['currentPage'];
}
}
return 1;
}
/**
* @param array $arguments
* @return string
*/
protected static function getName(array $arguments): string
{
return $arguments['name'] ?: $arguments['as'];
}
}
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace In2code\Femanager\ViewHelpers\Pagination;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Service\ExtensionService;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception as FluidViewHelperException;
/**
* UriViewHelper
*/
class UriViewHelper extends AbstractTagBasedViewHelper
{
/**
* Initialize arguments
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('name', 'string', 'identifier important if more widgets on same page', false, 'widget');
$this->registerArgument('arguments', 'array', 'Arguments', false, []);
}
/**
* Build an uri to current action with &tx_ext_plugin[currentPage]=2
*
* @return string The rendered uri
* @throws FluidViewHelperException
*/
public function render(): string
{
if (! $this->renderingContext instanceof RenderingContext) {
throw new FluidViewHelperException(
'Something went wrong; RenderingContext should be available in ViewHelper',
1638341671
);
}
$uriBuilder = $this->renderingContext->getUriBuilder();
$extensionName = $this->renderingContext->getRequest()->getControllerExtensionName();
$pluginName = $this->renderingContext->getRequest()->getPluginName();
$extensionService = GeneralUtility::makeInstance(ExtensionService::class);
$pluginNamespace = $extensionService->getPluginNamespace($extensionName, $pluginName);
$argumentPrefix = $pluginNamespace . '[' . $this->arguments['name'] . ']';
$arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : [];
if ($this->hasArgument('action')) {
$arguments['action'] = $this->arguments['action'];
}
if ($this->hasArgument('format') && $this->arguments['format'] !== '') {
$arguments['format'] = $this->arguments['format'];
}
$uriBuilder->reset()
->setArguments([$argumentPrefix => $arguments])
->setAddQueryString(true)
->setArgumentsToBeExcludedFromQueryString([$argumentPrefix, 'cHash']);
return $uriBuilder->build();
}
}