-
-
Notifications
You must be signed in to change notification settings - Fork 358
/
ItemsProcFunc.php
286 lines (254 loc) · 9.64 KB
/
ItemsProcFunc.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
namespace GeorgRinger\News\Hooks;
/**
* 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 GeorgRinger\News\Utility\TemplateLayout;
use TYPO3\CMS\Backend\Utility\BackendUtility as BackendUtilityCore;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
/**
* Userfunc to render alternative label for media elements
*/
class ItemsProcFunc
{
/** @var TemplateLayout $templateLayoutsUtility */
protected $templateLayoutsUtility;
public function __construct()
{
$this->templateLayoutsUtility = GeneralUtility::makeInstance(TemplateLayout::class);
}
/**
* Itemsproc function to extend the selection of templateLayouts in the plugin
*
* @param array &$config configuration array
*/
public function user_templateLayout(array &$config)
{
$pageId = 0;
$currentColPos = $config['flexParentDatabaseRow']['colPos'];
$pageId = $this->getPageId($config['flexParentDatabaseRow']['pid']);
if ($pageId > 0) {
$templateLayouts = $this->templateLayoutsUtility->getAvailableTemplateLayouts($pageId);
$templateLayouts = $this->reduceTemplateLayouts($templateLayouts, $currentColPos);
foreach ($templateLayouts as $layout) {
$additionalLayout = [
htmlspecialchars($this->getLanguageService()->sL($layout[0])),
$layout[1]
];
array_push($config['items'], $additionalLayout);
}
}
}
/**
* Reduce the template layouts by the ones that are not allowed in given colPos
*
* @param array $templateLayouts
* @param int $currentColPos
* @return array
*/
protected function reduceTemplateLayouts($templateLayouts, $currentColPos)
{
$currentColPos = (int)$currentColPos;
$restrictions = [];
$allLayouts = [];
foreach ($templateLayouts as $key => $layout) {
if (is_array($layout[0])) {
if (isset($layout[0]['allowedColPos']) && StringUtility::endsWith($layout[1], '.')) {
$layoutKey = substr($layout[1], 0, -1);
$restrictions[$layoutKey] = GeneralUtility::intExplode(',', $layout[0]['allowedColPos'], true);
}
} else {
$allLayouts[$key] = $layout;
}
}
if (!empty($restrictions)) {
foreach ($restrictions as $restrictedIdentifier => $restrictedColPosList) {
if (!in_array($currentColPos, $restrictedColPosList, true)) {
unset($allLayouts[$restrictedIdentifier]);
}
}
}
return $allLayouts;
}
/**
* Modifies the select box of orderBy-options as a category menu
* needs different ones then a news action
*
* @param array &$config configuration array
*/
public function user_orderBy(array &$config)
{
$row = $this->getContentElementRow($config['row']['uid']);
// check if the record has been saved once
if (is_array($row) && !empty($row['pi_flexform'])) {
$flexformConfig = GeneralUtility::xml2array($row['pi_flexform']);
// check if there is a flexform configuration
if (isset($flexformConfig['data']['sDEF']['lDEF']['switchableControllerActions']['vDEF'])) {
$selectedActionList = $flexformConfig['data']['sDEF']['lDEF']['switchableControllerActions']['vDEF'];
// check for selected action
if (GeneralUtility::isFirstPartOfStr($selectedActionList, 'Category')) {
$newItems = $GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['orderByCategory'];
} elseif (GeneralUtility::isFirstPartOfStr($selectedActionList, 'Tag')) {
$this->removeNonValidOrderFields($config, 'tx_news_domain_model_tag');
$newItems = $GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['orderByTag'];
} else {
$newItems = $GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['orderByNews'];
}
}
}
// if a override configuration is found
if (!empty($newItems)) {
// remove default configuration
$config['items'] = [];
// empty default line
array_push($config['items'], ['', '']);
$newItemArray = GeneralUtility::trimExplode(',', $newItems, true);
$languageKey = 'LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_general.orderBy.';
foreach ($newItemArray as $item) {
// label: if empty, key (=field) is used
$label = $this->getLanguageService()->sL($languageKey . $item);
if (empty($label)) {
$label = $item;
}
array_push($config['items'], [htmlspecialchars($label), $item]);
}
}
}
/**
* Remove not valid fields from ordering
*
* @param array $config tca items
* @param string $tableName table name
*/
protected function removeNonValidOrderFields(array &$config, $tableName)
{
$allowedFields = array_keys($GLOBALS['TCA'][$tableName]['columns']);
foreach ($config['items'] as $key => $item) {
if ($item[1] != '' && !in_array($item[1], $allowedFields)) {
unset($config['items'][$key]);
}
}
}
/**
* Modifies the selectbox of available actions
*
* @param array &$config
*/
public function user_switchableControllerActions(array &$config)
{
if (!empty($GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['switchableControllerActions']['list'])) {
$configuration = (int)$GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['switchableControllerActions']['list'];
switch ($configuration) {
case 1:
$this->removeActionFromList($config, 'News->list');
break;
case 2:
$this->removeActionFromList($config, 'News->list;News->detail');
break;
default:
}
}
// Add additional actions
if (isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['switchableControllerActions']['newItems'])
&& is_array($GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['switchableControllerActions']['newItems'])
) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['switchableControllerActions']['newItems'] as $key => $label) {
array_push($config['items'], [$this->getLanguageService()->sL($label), $key, '']);
}
}
}
/**
* Remove given action from switchableControllerActions
*
* @param array $config available items
* @param string $action action to be removed
*/
private function removeActionFromList(array &$config, $action)
{
foreach ($config['items'] as $key => $item) {
if ($item[1] === $action) {
unset($config['items'][$key]);
continue;
}
}
}
/**
* Generate a select box of languages to choose an overlay
*
* @return string select box
*/
public function user_categoryOverlay()
{
$html = '';
$languages = $this->getAllLanguages();
// if any language is available
if (count($languages) > 0) {
$html = '<select name="data[newsoverlay]" id="field_newsoverlay" class="form-control">
<option value="0">' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_general.xlf:LGL.default_value')) . '</option>';
foreach ($languages as $language) {
$selected = ((int)$GLOBALS['BE_USER']->uc['newsoverlay'] === (int)$language['uid']) ? ' selected="selected" ' : '';
$html .= '<option ' . $selected . 'value="' . $language['uid'] . '">' . htmlspecialchars($language['title']) . '</option>';
}
$html .= '</select>';
} else {
$html .= htmlspecialchars($this->getLanguageService()->sL(
'LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:usersettings.no-languages-available')
);
}
return $html;
}
/**
* Get all languages
*
* @return array
*/
protected function getAllLanguages()
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_language');
return $queryBuilder->select('*')
->from('sys_language')
->orderBy('sorting')
->execute()
->fetchAll();
}
/**
* Get tt_content record
*
* @param int $uid
* @return array
*/
protected function getContentElementRow($uid)
{
return BackendUtilityCore::getRecord('tt_content', $uid);
}
/**
* Get page id, if negative, then it is a "after record"
*
* @param int $pid
* @return int
*/
protected function getPageId($pid)
{
$pid = (int)$pid;
if ($pid > 0) {
return $pid;
}
$row = BackendUtilityCore::getRecord('tt_content', abs($pid), 'uid,pid');
return $row['pid'];
}
/**
* Returns LanguageService
*
* @return \TYPO3\CMS\Lang\LanguageService
*/
protected function getLanguageService()
{
return $GLOBALS['LANG'];
}
}