-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgumentsActiveWithinParentViewHelper.php
131 lines (117 loc) · 4.35 KB
/
ArgumentsActiveWithinParentViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
namespace GeorgRinger\News\ViewHelpers\MultiCategoryLink;
/**
* This file is part of the "news" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* ViewHelper to get additional params including add/remove categories from list
*
* # Example: Basic Example
* # Description: Render the content of the VH as page title
* <code>
* <f:link.page title="{category.item.title}" pageUid="{settings.listPid}"
* additionalParams="{n:multiCategoryLink.arguments(
* mode:'add',
* item:category.item.uid,
* list:overwriteDemand.categories)}">link
* </f:link.page>
* </code>
* <output>
* <title>TYPO3 is awesome</title>
* </output>
*
*/
class ArgumentsActiveWithinParentViewHelper extends AbstractViewHelper implements CompilableInterface
{
use CompileWithRenderStatic;
/**
* Initialize arguments
*/
public function initializeArguments()
{
$this->registerArgument('mode', 'string', 'Mode, either "add" or "remove"', true);
$this->registerArgument('list', 'string', 'Category list', false, '');
$this->registerArgument('item', 'int', 'Category id', true);
$this->registerArgument('parent', 'int', 'Parent category id', true);
$this->registerArgument('additionalParams', 'array', 'Additional params', false, []);
parent::initializeArguments();
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
)
{
if ($arguments['mode'] !== 'add' && $arguments['mode'] !== 'remove') {
throw new Exception('Mode must be either "add" or "remove', 1522293549);
}
$allArguments = (array)$arguments['additionalParams'];
$categoryId = $arguments['item'];
$parentCategoryId = (int)$arguments['parent'];
$categoryList = $arguments['list'];
if ($arguments['mode'] === 'add') {
if (!GeneralUtility::inList($categoryList, $categoryId)) {
$categoryList .= ',' . $categoryId;
}
} else {
// remove
if (GeneralUtility::inList($categoryList, $categoryId)) {
$categoryList = GeneralUtility::rmFromList($categoryId, $categoryList);
} else {
// replace
$all = self::getIdListOfParent($parentCategoryId);
foreach ($all as $itToBeRemoved) {
$categoryList = GeneralUtility::rmFromList($itToBeRemoved, $categoryList);
}
$categoryList .= ',' . $categoryId;
}
}
if (!empty($categoryList)) {
$categoryList = trim($categoryList, ',');
$categoryArray = [
'tx_news_pi1' => [
'overwriteDemand' => [
'categories' => $categoryList
]
]
];
ArrayUtility::mergeRecursiveWithOverrule($allArguments, $categoryArray);
}
return $allArguments;
}
protected static function getIdListOfParent(int $id): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_category');
$rows = $queryBuilder
->select('*')
->from('sys_category')
->where(
$queryBuilder->expr()->eq('parent', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT))
)
->execute()
->fetchAll();
$idList = [];
foreach ($rows as $row) {
$idList[] = $row['uid'];
}
return $idList;
}
}