[BUGFIX] Initialize language key in AbstractPlugin
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Plugin / AbstractPlugin.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Plugin;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Doctrine\DBAL\Driver\Statement;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Database\Connection;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Database\Query\QueryHelper;
22 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
23 use TYPO3\CMS\Core\Localization\Locales;
24 use TYPO3\CMS\Core\Localization\LocalizationFactory;
25 use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
26 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
27 use TYPO3\CMS\Core\Utility\ArrayUtility;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Core\Utility\HttpUtility;
30 use TYPO3\CMS\Core\Utility\MathUtility;
31 use TYPO3\CMS\Core\Utility\PathUtility;
32 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
33 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
34
35 /**
36 * Base class for frontend plugins
37 * Most legacy frontend plugins are extension classes of this one.
38 * This class contains functions which assists these plugins in creating lists, searching, displaying menus, page-browsing (next/previous/1/2/3) and handling links.
39 * Functions are all prefixed "pi_" which is reserved for this class. Those functions can of course be overridden in the extension classes (that is the point...)
40 */
41 class AbstractPlugin
42 {
43 /**
44 * The backReference to the mother cObj object set at call time
45 *
46 * @var ContentObjectRenderer
47 */
48 public $cObj;
49
50 /**
51 * Should be same as classname of the plugin, used for CSS classes, variables
52 *
53 * @var string
54 */
55 public $prefixId;
56
57 /**
58 * Path to the plugin class script relative to extension directory, eg. 'pi1/class.tx_newfaq_pi1.php'
59 *
60 * @var string
61 */
62 public $scriptRelPath;
63
64 /**
65 * Extension key.
66 *
67 * @var string
68 */
69 public $extKey;
70
71 /**
72 * This is the incoming array by name $this->prefixId merged between POST and GET, POST taking precedence.
73 * Eg. if the class name is 'tx_myext'
74 * then the content of this array will be whatever comes into &tx_myext[...]=...
75 *
76 * @var array
77 */
78 public $piVars = [
79 'pointer' => '',
80 // Used as a pointer for lists
81 'mode' => '',
82 // List mode
83 'sword' => '',
84 // Search word
85 'sort' => ''
86 ];
87
88 /**
89 * Local pointer variabe array.
90 * Holds pointer information for the MVC like approach Kasper
91 * initially proposed
92 *
93 * @var array
94 */
95 public $internal = ['res_count' => 0, 'results_at_a_time' => 20, 'maxPages' => 10, 'currentRow' => [], 'currentTable' => ''];
96
97 /**
98 * Local Language content
99 *
100 * @var array
101 */
102 public $LOCAL_LANG = [];
103
104 /**
105 * Contains those LL keys, which have been set to (empty) in TypoScript.
106 * This is necessary, as we cannot distinguish between a nonexisting
107 * translation and a label that has been cleared by TS.
108 * In both cases ['key'][0]['target'] is "".
109 *
110 * @var array
111 */
112 protected $LOCAL_LANG_UNSET = [];
113
114 /**
115 * Flag that tells if the locallang file has been fetch (or tried to
116 * be fetched) already.
117 *
118 * @var bool
119 */
120 public $LOCAL_LANG_loaded = false;
121
122 /**
123 * Pointer to the language to use.
124 *
125 * @var string
126 */
127 public $LLkey = 'default';
128
129 /**
130 * Pointer to alternative fall-back language to use.
131 *
132 * @var string
133 */
134 public $altLLkey = '';
135
136 /**
137 * You can set this during development to some value that makes it
138 * easy for you to spot all labels that ARe delivered by the getLL function.
139 *
140 * @var string
141 */
142 public $LLtestPrefix = '';
143
144 /**
145 * Save as LLtestPrefix, but additional prefix for the alternative value
146 * in getLL() function calls
147 *
148 * @var string
149 */
150 public $LLtestPrefixAlt = '';
151
152 /**
153 * @var string
154 */
155 public $pi_isOnlyFields = 'mode,pointer';
156
157 /**
158 * @var int
159 */
160 public $pi_alwaysPrev = 0;
161
162 /**
163 * @var int
164 */
165 public $pi_lowerThan = 5;
166
167 /**
168 * @var string
169 */
170 public $pi_moreParams = '';
171
172 /**
173 * @var string
174 */
175 public $pi_listFields = '*';
176
177 /**
178 * @var array
179 */
180 public $pi_autoCacheFields = [];
181
182 /**
183 * @var bool
184 */
185 public $pi_autoCacheEn = false;
186
187 /**
188 * If set, then links are
189 * 1) not using cHash and
190 * 2) not allowing pages to be cached. (Set this for all USER_INT plugins!)
191 *
192 * @var bool
193 */
194 public $pi_USER_INT_obj = false;
195
196 /**
197 * If set, then caching is disabled if piVars are incoming while
198 * no cHash was set (Set this for all USER plugins!)
199 *
200 * @var bool
201 */
202 public $pi_checkCHash = false;
203
204 /**
205 * Should normally be set in the main function with the TypoScript content passed to the method.
206 *
207 * $conf[LOCAL_LANG][_key_] is reserved for Local Language overrides.
208 * $conf[userFunc] reserved for setting up the USER / USER_INT object. See TSref
209 *
210 * @var array
211 */
212 public $conf = [];
213
214 /**
215 * internal, don't mess with...
216 *
217 * @var ContentObjectRenderer
218 */
219 public $pi_EPtemp_cObj;
220
221 /**
222 * @var int
223 */
224 public $pi_tmpPageId = 0;
225
226 /**
227 * Property for accessing TypoScriptFrontendController centrally
228 *
229 * @var TypoScriptFrontendController
230 */
231 protected $frontendController;
232
233 /**
234 * @var MarkerBasedTemplateService
235 */
236 protected $templateService;
237
238 /**
239 * Class Constructor (true constructor)
240 * Initializes $this->piVars if $this->prefixId is set to any value
241 * Will also set $this->LLkey based on the config.language setting.
242 *
243 * @param null $_ unused,
244 * @param TypoScriptFrontendController $frontendController
245 */
246 public function __construct($_ = null, TypoScriptFrontendController $frontendController = null)
247 {
248 $this->frontendController = $frontendController ?: $GLOBALS['TSFE'];
249 $this->templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
250 // Setting piVars:
251 if ($this->prefixId) {
252 $this->piVars = GeneralUtility::_GPmerged($this->prefixId);
253 // cHash mode check
254 // IMPORTANT FOR CACHED PLUGINS (USER cObject): As soon as you generate cached plugin output which
255 // depends on parameters (eg. seeing the details of a news item) you MUST check if a cHash value is set.
256 // Background: The function call will check if a cHash parameter was sent with the URL because only if
257 // it was the page may be cached. If no cHash was found the function will simply disable caching to
258 // avoid unpredictable caching behaviour. In any case your plugin can generate the expected output and
259 // the only risk is that the content may not be cached. A missing cHash value is considered a mistake
260 // in the URL resulting from either URL manipulation, "realurl" "grayzones" etc. The problem is rare
261 // (more frequent with "realurl") but when it occurs it is very puzzling!
262 if ($this->pi_checkCHash && !empty($this->piVars)) {
263 $this->frontendController->reqCHash();
264 }
265 }
266 $siteLanguage = $this->getCurrentSiteLanguage();
267 if ($siteLanguage) {
268 $this->LLkey = $siteLanguage->getTypo3Language();
269 } elseif (!empty($this->frontendController->config['config']['language'])) {
270 $this->LLkey = $this->frontendController->config['config']['language'];
271 }
272
273 if (empty($this->frontendController->config['config']['language_alt'])) {
274 /** @var Locales $locales */
275 $locales = GeneralUtility::makeInstance(Locales::class);
276 if (in_array($this->LLkey, $locales->getLocales())) {
277 $this->altLLkey = '';
278 foreach ($locales->getLocaleDependencies($this->LLkey) as $language) {
279 $this->altLLkey .= $language . ',';
280 }
281 $this->altLLkey = rtrim($this->altLLkey, ',');
282 }
283 } else {
284 $this->altLLkey = $this->frontendController->config['config']['language_alt'];
285 }
286 }
287
288 /**
289 * Recursively looks for stdWrap and executes it
290 *
291 * @param array $conf Current section of configuration to work on
292 * @param int $level Current level being processed (currently just for tracking; no limit enforced)
293 * @return array Current section of configuration after stdWrap applied
294 */
295 protected function applyStdWrapRecursive(array $conf, $level = 0)
296 {
297 foreach ($conf as $key => $confNextLevel) {
298 if (strpos($key, '.') !== false) {
299 $key = substr($key, 0, -1);
300
301 // descend into all non-stdWrap-subelements first
302 foreach ($confNextLevel as $subKey => $subConfNextLevel) {
303 if (is_array($subConfNextLevel) && strpos($subKey, '.') !== false && $subKey !== 'stdWrap.') {
304 $conf[$key . '.'] = $this->applyStdWrapRecursive($confNextLevel, $level + 1);
305 }
306 }
307
308 // now for stdWrap
309 foreach ($confNextLevel as $subKey => $subConfNextLevel) {
310 if (is_array($subConfNextLevel) && $subKey === 'stdWrap.') {
311 $conf[$key] = $this->cObj->stdWrap($conf[$key] ?? '', $conf[$key . '.']['stdWrap.'] ?? []);
312 unset($conf[$key . '.']['stdWrap.']);
313 if (empty($conf[$key . '.'])) {
314 unset($conf[$key . '.']);
315 }
316 }
317 }
318 }
319 }
320 return $conf;
321 }
322
323 /**
324 * If internal TypoScript property "_DEFAULT_PI_VARS." is set then it will merge the current $this->piVars array onto these default values.
325 */
326 public function pi_setPiVarDefaults()
327 {
328 if (isset($this->conf['_DEFAULT_PI_VARS.']) && is_array($this->conf['_DEFAULT_PI_VARS.'])) {
329 $this->conf['_DEFAULT_PI_VARS.'] = $this->applyStdWrapRecursive($this->conf['_DEFAULT_PI_VARS.']);
330 $tmp = $this->conf['_DEFAULT_PI_VARS.'];
331 ArrayUtility::mergeRecursiveWithOverrule($tmp, is_array($this->piVars) ? $this->piVars : []);
332 $this->piVars = $tmp;
333 }
334 }
335
336 /***************************
337 *
338 * Link functions
339 *
340 **************************/
341 /**
342 * Get URL to some page.
343 * Returns the URL to page $id with $target and an array of additional url-parameters, $urlParameters
344 * Simple example: $this->pi_getPageLink(123) to get the URL for page-id 123.
345 *
346 * The function basically calls $this->cObj->getTypoLink_URL()
347 *
348 * @param int $id Page id
349 * @param string $target Target value to use. Affects the &type-value of the URL, defaults to current.
350 * @param array|string $urlParameters As an array key/value pairs represent URL parameters to set. Values NOT URL-encoded yet, keys should be URL-encoded if needed. As a string the parameter is expected to be URL-encoded already.
351 * @return string The resulting URL
352 * @see pi_linkToPage()
353 * @see ContentObjectRenderer->getTypoLink()
354 */
355 public function pi_getPageLink($id, $target = '', $urlParameters = [])
356 {
357 return $this->cObj->getTypoLink_URL($id, $urlParameters, $target);
358 }
359
360 /**
361 * Link a string to some page.
362 * Like pi_getPageLink() but takes a string as first parameter which will in turn be wrapped with the URL including target attribute
363 * Simple example: $this->pi_linkToPage('My link', 123) to get something like <a href="index.php?id=123&type=1">My link</a>
364 *
365 * @param string $str The content string to wrap in <a> tags
366 * @param int $id Page id
367 * @param string $target Target value to use. Affects the &type-value of the URL, defaults to current.
368 * @param array|string $urlParameters As an array key/value pairs represent URL parameters to set. Values NOT URL-encoded yet, keys should be URL-encoded if needed. As a string the parameter is expected to be URL-encoded already.
369 * @return string The input string wrapped in <a> tags with the URL and target set.
370 * @see pi_getPageLink(), ContentObjectRenderer::getTypoLink()
371 */
372 public function pi_linkToPage($str, $id, $target = '', $urlParameters = [])
373 {
374 return $this->cObj->getTypoLink($str, $id, $urlParameters, $target);
375 }
376
377 /**
378 * Link string to the current page.
379 * Returns the $str wrapped in <a>-tags with a link to the CURRENT page, but with $urlParameters set as extra parameters for the page.
380 *
381 * @param string $str The content string to wrap in <a> tags
382 * @param array $urlParameters Array with URL parameters as key/value pairs. They will be "imploded" and added to the list of parameters defined in the plugins TypoScript property "parent.addParams" plus $this->pi_moreParams.
383 * @param bool $cache If $cache is set (0/1), the page is asked to be cached by a &cHash value (unless the current plugin using this class is a USER_INT). Otherwise the no_cache-parameter will be a part of the link.
384 * @param int $altPageId Alternative page ID for the link. (By default this function links to the SAME page!)
385 * @return string The input string wrapped in <a> tags
386 * @see pi_linkTP_keepPIvars(), ContentObjectRenderer::typoLink()
387 */
388 public function pi_linkTP($str, $urlParameters = [], $cache = false, $altPageId = 0)
389 {
390 $conf = [];
391 $conf['useCacheHash'] = $this->pi_USER_INT_obj ? 0 : $cache;
392 $conf['no_cache'] = $this->pi_USER_INT_obj ? 0 : !$cache;
393 $conf['parameter'] = $altPageId ? $altPageId : ($this->pi_tmpPageId ? $this->pi_tmpPageId : $this->frontendController->id);
394 $conf['additionalParams'] = $this->conf['parent.']['addParams'] . HttpUtility::buildQueryString($urlParameters, '&', true) . $this->pi_moreParams;
395 return $this->cObj->typoLink($str, $conf);
396 }
397
398 /**
399 * Link a string to the current page while keeping currently set values in piVars.
400 * Like pi_linkTP, but $urlParameters is by default set to $this->piVars with $overrulePIvars overlaid.
401 * This means any current entries from this->piVars are passed on (except the key "DATA" which will be unset before!) and entries in $overrulePIvars will OVERRULE the current in the link.
402 *
403 * @param string $str The content string to wrap in <a> tags
404 * @param array $overrulePIvars Array of values to override in the current piVars. Contrary to pi_linkTP the keys in this array must correspond to the real piVars array and therefore NOT be prefixed with the $this->prefixId string. Further, if a value is a blank string it means the piVar key will not be a part of the link (unset)
405 * @param bool $cache If $cache is set, the page is asked to be cached by a &cHash value (unless the current plugin using this class is a USER_INT). Otherwise the no_cache-parameter will be a part of the link.
406 * @param bool $clearAnyway If set, then the current values of piVars will NOT be preserved anyways... Practical if you want an easy way to set piVars without having to worry about the prefix, "tx_xxxxx[]
407 * @param int $altPageId Alternative page ID for the link. (By default this function links to the SAME page!)
408 * @return string The input string wrapped in <a> tags
409 * @see pi_linkTP()
410 */
411 public function pi_linkTP_keepPIvars($str, $overrulePIvars = [], $cache = false, $clearAnyway = false, $altPageId = 0)
412 {
413 if (is_array($this->piVars) && is_array($overrulePIvars) && !$clearAnyway) {
414 $piVars = $this->piVars;
415 unset($piVars['DATA']);
416 ArrayUtility::mergeRecursiveWithOverrule($piVars, $overrulePIvars);
417 $overrulePIvars = $piVars;
418 if ($this->pi_autoCacheEn) {
419 $cache = $this->pi_autoCache($overrulePIvars);
420 }
421 }
422 return $this->pi_linkTP($str, [$this->prefixId => $overrulePIvars], $cache, $altPageId);
423 }
424
425 /**
426 * Get URL to the current page while keeping currently set values in piVars.
427 * Same as pi_linkTP_keepPIvars but returns only the URL from the link.
428 *
429 * @param array $overrulePIvars See pi_linkTP_keepPIvars
430 * @param bool $cache See pi_linkTP_keepPIvars
431 * @param bool $clearAnyway See pi_linkTP_keepPIvars
432 * @param int $altPageId See pi_linkTP_keepPIvars
433 * @return string The URL ($this->cObj->lastTypoLinkUrl)
434 * @see pi_linkTP_keepPIvars()
435 */
436 public function pi_linkTP_keepPIvars_url($overrulePIvars = [], $cache = false, $clearAnyway = false, $altPageId = 0)
437 {
438 $this->pi_linkTP_keepPIvars('|', $overrulePIvars, $cache, $clearAnyway, $altPageId);
439 return $this->cObj->lastTypoLinkUrl;
440 }
441
442 /**
443 * Wraps the $str in a link to a single display of the record (using piVars[showUid])
444 * Uses pi_linkTP for the linking
445 *
446 * @param string $str The content string to wrap in <a> tags
447 * @param int $uid UID of the record for which to display details (basically this will become the value of [showUid]
448 * @param bool $cache See pi_linkTP_keepPIvars
449 * @param array $mergeArr Array of values to override in the current piVars. Same as $overrulePIvars in pi_linkTP_keepPIvars
450 * @param bool $urlOnly If TRUE, only the URL is returned, not a full link
451 * @param int $altPageId Alternative page ID for the link. (By default this function links to the SAME page!)
452 * @return string The input string wrapped in <a> tags
453 * @see pi_linkTP(), pi_linkTP_keepPIvars()
454 */
455 public function pi_list_linkSingle($str, $uid, $cache = false, $mergeArr = [], $urlOnly = false, $altPageId = 0)
456 {
457 if ($this->prefixId) {
458 if ($cache) {
459 $overrulePIvars = $uid ? ['showUid' => $uid] : [];
460 $overrulePIvars = array_merge($overrulePIvars, (array)$mergeArr);
461 $str = $this->pi_linkTP($str, [$this->prefixId => $overrulePIvars], $cache, $altPageId);
462 } else {
463 $overrulePIvars = ['showUid' => $uid ?: ''];
464 $overrulePIvars = array_merge($overrulePIvars, (array)$mergeArr);
465 $str = $this->pi_linkTP_keepPIvars($str, $overrulePIvars, $cache, 0, $altPageId);
466 }
467 // If urlOnly flag, return only URL as it has recently be generated.
468 if ($urlOnly) {
469 $str = $this->cObj->lastTypoLinkUrl;
470 }
471 }
472 return $str;
473 }
474
475 /**
476 * Will change the href value from <a> in the input string and turn it into an onclick event that will open a new window with the URL
477 *
478 * @param string $str The string to process. This should be a string already wrapped/including a <a> tag which will be modified to contain an onclick handler. Only the attributes "href" and "onclick" will be left.
479 * @param string $winName Window name for the pop-up window
480 * @param string $winParams Window parameters, see the default list for inspiration
481 * @return string The processed input string, modified IF a <a> tag was found
482 */
483 public function pi_openAtagHrefInJSwindow($str, $winName = '', $winParams = 'width=670,height=500,status=0,menubar=0,scrollbars=1,resizable=1')
484 {
485 if (preg_match('/(.*)(<a[^>]*>)(.*)/i', $str, $match)) {
486 $aTagContent = GeneralUtility::get_tag_attributes($match[2]);
487 $onClick = 'vHWin=window.open('
488 . GeneralUtility::quoteJSvalue($this->frontendController->baseUrlWrap($aTagContent['href'])) . ','
489 . GeneralUtility::quoteJSvalue($winName ?: md5($aTagContent['href'])) . ','
490 . GeneralUtility::quoteJSvalue($winParams) . ');vHWin.focus();return false;';
491 $match[2] = '<a href="#" onclick="' . htmlspecialchars($onClick) . '">';
492 $str = $match[1] . $match[2] . $match[3];
493 }
494 return $str;
495 }
496
497 /***************************
498 *
499 * Functions for listing, browsing, searching etc.
500 *
501 **************************/
502 /**
503 * Returns a results browser. This means a bar of page numbers plus a "previous" and "next" link. For each entry in the bar the piVars "pointer" will be pointing to the "result page" to show.
504 * Using $this->piVars['pointer'] as pointer to the page to display. Can be overwritten with another string ($pointerName) to make it possible to have more than one pagebrowser on a page)
505 * Using $this->internal['res_count'], $this->internal['results_at_a_time'] and $this->internal['maxPages'] for count number, how many results to show and the max number of pages to include in the browse bar.
506 * Using $this->internal['dontLinkActivePage'] as switch if the active (current) page should be displayed as pure text or as a link to itself
507 * Using $this->internal['showFirstLast'] as switch if the two links named "<< First" and "LAST >>" will be shown and point to the first or last page.
508 * Using $this->internal['pagefloat']: this defines were the current page is shown in the list of pages in the Pagebrowser. If this var is an integer it will be interpreted as position in the list of pages. If its value is the keyword "center" the current page will be shown in the middle of the pagelist.
509 * Using $this->internal['showRange']: this var switches the display of the pagelinks from pagenumbers to ranges f.e.: 1-5 6-10 11-15... instead of 1 2 3...
510 * Using $this->pi_isOnlyFields: this holds a comma-separated list of fieldnames which - if they are among the GETvars - will not disable caching for the page with pagebrowser.
511 *
512 * The third parameter is an array with several wraps for the parts of the pagebrowser. The following elements will be recognized:
513 * disabledLinkWrap, inactiveLinkWrap, activeLinkWrap, browseLinksWrap, showResultsWrap, showResultsNumbersWrap, browseBoxWrap.
514 *
515 * If $wrapArr['showResultsNumbersWrap'] is set, the formatting string is expected to hold template markers (###FROM###, ###TO###, ###OUT_OF###, ###FROM_TO###, ###CURRENT_PAGE###, ###TOTAL_PAGES###)
516 * otherwise the formatting string is expected to hold sprintf-markers (%s) for from, to, outof (in that sequence)
517 *
518 * @param int $showResultCount Determines how the results of the page browser will be shown. See description below
519 * @param string $tableParams Attributes for the table tag which is wrapped around the table cells containing the browse links
520 * @param array $wrapArr Array with elements to overwrite the default $wrapper-array.
521 * @param string $pointerName varname for the pointer.
522 * @param bool $hscText Enable htmlspecialchars() on language labels
523 * @param bool $forceOutput Forces the output of the page browser if you set this option to "TRUE" (otherwise it's only drawn if enough entries are available)
524 * @return string Output HTML-Table, wrapped in <div>-tags with a class attribute (if $wrapArr is not passed,
525 */
526 public function pi_list_browseresults($showResultCount = 1, $tableParams = '', $wrapArr = [], $pointerName = 'pointer', $hscText = true, $forceOutput = false)
527 {
528 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::class]['pi_list_browseresults'] ?? [] as $classRef) {
529 $hookObj = GeneralUtility::makeInstance($classRef);
530 if (method_exists($hookObj, 'pi_list_browseresults')) {
531 $pageBrowser = $hookObj->pi_list_browseresults($showResultCount, $tableParams, $wrapArr, $pointerName, $hscText, $forceOutput, $this);
532 if (is_string($pageBrowser) && trim($pageBrowser) !== '') {
533 return $pageBrowser;
534 }
535 }
536 }
537 // example $wrapArr-array how it could be traversed from an extension
538 /* $wrapArr = array(
539 'browseBoxWrap' => '<div class="browseBoxWrap">|</div>',
540 'showResultsWrap' => '<div class="showResultsWrap">|</div>',
541 'browseLinksWrap' => '<div class="browseLinksWrap">|</div>',
542 'showResultsNumbersWrap' => '<span class="showResultsNumbersWrap">|</span>',
543 'disabledLinkWrap' => '<span class="disabledLinkWrap">|</span>',
544 'inactiveLinkWrap' => '<span class="inactiveLinkWrap">|</span>',
545 'activeLinkWrap' => '<span class="activeLinkWrap">|</span>'
546 );*/
547 // Initializing variables:
548 $pointer = (int)$this->piVars[$pointerName];
549 $count = (int)$this->internal['res_count'];
550 $results_at_a_time = MathUtility::forceIntegerInRange($this->internal['results_at_a_time'], 1, 1000);
551 $totalPages = ceil($count / $results_at_a_time);
552 $maxPages = MathUtility::forceIntegerInRange($this->internal['maxPages'], 1, 100);
553 $pi_isOnlyFields = $this->pi_isOnlyFields($this->pi_isOnlyFields);
554 if (!$forceOutput && $count <= $results_at_a_time) {
555 return '';
556 }
557 // $showResultCount determines how the results of the pagerowser will be shown.
558 // If set to 0: only the result-browser will be shown
559 // 1: (default) the text "Displaying results..." and the result-browser will be shown.
560 // 2: only the text "Displaying results..." will be shown
561 $showResultCount = (int)$showResultCount;
562 // If this is set, two links named "<< First" and "LAST >>" will be shown and point to the very first or last page.
563 $showFirstLast = !empty($this->internal['showFirstLast']);
564 // If this has a value the "previous" button is always visible (will be forced if "showFirstLast" is set)
565 $alwaysPrev = $showFirstLast ? 1 : $this->pi_alwaysPrev;
566 if (isset($this->internal['pagefloat'])) {
567 if (strtoupper($this->internal['pagefloat']) === 'CENTER') {
568 $pagefloat = ceil(($maxPages - 1) / 2);
569 } else {
570 // pagefloat set as integer. 0 = left, value >= $this->internal['maxPages'] = right
571 $pagefloat = MathUtility::forceIntegerInRange($this->internal['pagefloat'], -1, $maxPages - 1);
572 }
573 } else {
574 // pagefloat disabled
575 $pagefloat = -1;
576 }
577 // Default values for "traditional" wrapping with a table. Can be overwritten by vars from $wrapArr
578 $wrapper['disabledLinkWrap'] = '<td class="nowrap"><p>|</p></td>';
579 $wrapper['inactiveLinkWrap'] = '<td class="nowrap"><p>|</p></td>';
580 $wrapper['activeLinkWrap'] = '<td' . $this->pi_classParam('browsebox-SCell') . ' class="nowrap"><p>|</p></td>';
581 $wrapper['browseLinksWrap'] = rtrim('<table ' . $tableParams) . '><tr>|</tr></table>';
582 $wrapper['showResultsWrap'] = '<p>|</p>';
583 $wrapper['browseBoxWrap'] = '
584 <!--
585 List browsing box:
586 -->
587 <div ' . $this->pi_classParam('browsebox') . '>
588 |
589 </div>';
590 // Now overwrite all entries in $wrapper which are also in $wrapArr
591 $wrapper = array_merge($wrapper, $wrapArr);
592 // Show pagebrowser
593 if ($showResultCount != 2) {
594 if ($pagefloat > -1) {
595 $lastPage = min($totalPages, max($pointer + 1 + $pagefloat, $maxPages));
596 $firstPage = max(0, $lastPage - $maxPages);
597 } else {
598 $firstPage = 0;
599 $lastPage = MathUtility::forceIntegerInRange($totalPages, 1, $maxPages);
600 }
601 $links = [];
602 // Make browse-table/links:
603 // Link to first page
604 if ($showFirstLast) {
605 if ($pointer > 0) {
606 $label = $this->pi_getLL('pi_list_browseresults_first', '<< First');
607 $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($hscText ? htmlspecialchars($label) : $label, [$pointerName => null], $pi_isOnlyFields), $wrapper['inactiveLinkWrap']);
608 } else {
609 $label = $this->pi_getLL('pi_list_browseresults_first', '<< First');
610 $links[] = $this->cObj->wrap($hscText ? htmlspecialchars($label) : $label, $wrapper['disabledLinkWrap']);
611 }
612 }
613 // Link to previous page
614 if ($alwaysPrev >= 0) {
615 if ($pointer > 0) {
616 $label = $this->pi_getLL('pi_list_browseresults_prev', '< Previous');
617 $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($hscText ? htmlspecialchars($label) : $label, [$pointerName => ($pointer - 1) ?: ''], $pi_isOnlyFields), $wrapper['inactiveLinkWrap']);
618 } elseif ($alwaysPrev) {
619 $label = $this->pi_getLL('pi_list_browseresults_prev', '< Previous');
620 $links[] = $this->cObj->wrap($hscText ? htmlspecialchars($label) : $label, $wrapper['disabledLinkWrap']);
621 }
622 }
623 // Links to pages
624 for ($a = $firstPage; $a < $lastPage; $a++) {
625 if ($this->internal['showRange']) {
626 $pageText = ($a * $results_at_a_time + 1) . '-' . min($count, ($a + 1) * $results_at_a_time);
627 } else {
628 $label = $this->pi_getLL('pi_list_browseresults_page', 'Page');
629 $pageText = trim(($hscText ? htmlspecialchars($label) : $label) . ' ' . ($a + 1));
630 }
631 // Current page
632 if ($pointer == $a) {
633 if ($this->internal['dontLinkActivePage']) {
634 $links[] = $this->cObj->wrap($pageText, $wrapper['activeLinkWrap']);
635 } else {
636 $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($pageText, [$pointerName => $a ?: ''], $pi_isOnlyFields), $wrapper['activeLinkWrap']);
637 }
638 } else {
639 $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($pageText, [$pointerName => $a ?: ''], $pi_isOnlyFields), $wrapper['inactiveLinkWrap']);
640 }
641 }
642 if ($pointer < $totalPages - 1 || $showFirstLast) {
643 // Link to next page
644 if ($pointer >= $totalPages - 1) {
645 $label = $this->pi_getLL('pi_list_browseresults_next', 'Next >');
646 $links[] = $this->cObj->wrap($hscText ? htmlspecialchars($label) : $label, $wrapper['disabledLinkWrap']);
647 } else {
648 $label = $this->pi_getLL('pi_list_browseresults_next', 'Next >');
649 $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($hscText ? htmlspecialchars($label) : $label, [$pointerName => $pointer + 1], $pi_isOnlyFields), $wrapper['inactiveLinkWrap']);
650 }
651 }
652 // Link to last page
653 if ($showFirstLast) {
654 if ($pointer < $totalPages - 1) {
655 $label = $this->pi_getLL('pi_list_browseresults_last', 'Last >>');
656 $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($hscText ? htmlspecialchars($label) : $label, [$pointerName => $totalPages - 1], $pi_isOnlyFields), $wrapper['inactiveLinkWrap']);
657 } else {
658 $label = $this->pi_getLL('pi_list_browseresults_last', 'Last >>');
659 $links[] = $this->cObj->wrap($hscText ? htmlspecialchars($label) : $label, $wrapper['disabledLinkWrap']);
660 }
661 }
662 $theLinks = $this->cObj->wrap(implode(LF, $links), $wrapper['browseLinksWrap']);
663 } else {
664 $theLinks = '';
665 }
666 $pR1 = $pointer * $results_at_a_time + 1;
667 $pR2 = $pointer * $results_at_a_time + $results_at_a_time;
668 if ($showResultCount) {
669 if ($wrapper['showResultsNumbersWrap']) {
670 // This will render the resultcount in a more flexible way using markers (new in TYPO3 3.8.0).
671 // The formatting string is expected to hold template markers (see function header). Example: 'Displaying results ###FROM### to ###TO### out of ###OUT_OF###'
672 $markerArray['###FROM###'] = $this->cObj->wrap($this->internal['res_count'] > 0 ? $pR1 : 0, $wrapper['showResultsNumbersWrap']);
673 $markerArray['###TO###'] = $this->cObj->wrap(min($this->internal['res_count'], $pR2), $wrapper['showResultsNumbersWrap']);
674 $markerArray['###OUT_OF###'] = $this->cObj->wrap($this->internal['res_count'], $wrapper['showResultsNumbersWrap']);
675 $markerArray['###FROM_TO###'] = $this->cObj->wrap(($this->internal['res_count'] > 0 ? $pR1 : 0) . ' ' . $this->pi_getLL('pi_list_browseresults_to', 'to') . ' ' . min($this->internal['res_count'], $pR2), $wrapper['showResultsNumbersWrap']);
676 $markerArray['###CURRENT_PAGE###'] = $this->cObj->wrap($pointer + 1, $wrapper['showResultsNumbersWrap']);
677 $markerArray['###TOTAL_PAGES###'] = $this->cObj->wrap($totalPages, $wrapper['showResultsNumbersWrap']);
678 // Substitute markers
679 $resultCountMsg = $this->templateService->substituteMarkerArray($this->pi_getLL('pi_list_browseresults_displays', 'Displaying results ###FROM### to ###TO### out of ###OUT_OF###'), $markerArray);
680 } else {
681 // Render the resultcount in the "traditional" way using sprintf
682 $resultCountMsg = sprintf(str_replace('###SPAN_BEGIN###', '<span' . $this->pi_classParam('browsebox-strong') . '>', $this->pi_getLL('pi_list_browseresults_displays', 'Displaying results ###SPAN_BEGIN###%s to %s</span> out of ###SPAN_BEGIN###%s</span>')), $count > 0 ? $pR1 : 0, min($count, $pR2), $count);
683 }
684 $resultCountMsg = $this->cObj->wrap($resultCountMsg, $wrapper['showResultsWrap']);
685 } else {
686 $resultCountMsg = '';
687 }
688 $sTables = $this->cObj->wrap($resultCountMsg . $theLinks, $wrapper['browseBoxWrap']);
689 return $sTables;
690 }
691
692 /**
693 * Returns a mode selector; a little menu in a table normally put in the top of the page/list.
694 *
695 * @param array $items Key/Value pairs for the menu; keys are the piVars[mode] values and the "values" are the labels for them.
696 * @param string $tableParams Attributes for the table tag which is wrapped around the table cells containing the menu
697 * @return string Output HTML, wrapped in <div>-tags with a class attribute
698 */
699 public function pi_list_modeSelector($items = [], $tableParams = '')
700 {
701 $cells = [];
702 foreach ($items as $k => $v) {
703 $cells[] = '
704 <td' . ($this->piVars['mode'] == $k ? $this->pi_classParam('modeSelector-SCell') : '') . '><p>' . $this->pi_linkTP_keepPIvars(htmlspecialchars($v), ['mode' => $k], $this->pi_isOnlyFields($this->pi_isOnlyFields)) . '</p></td>';
705 }
706 $sTables = '
707
708 <!--
709 Mode selector (menu for list):
710 -->
711 <div' . $this->pi_classParam('modeSelector') . '>
712 <' . rtrim('table ' . $tableParams) . '>
713 <tr>
714 ' . implode('', $cells) . '
715 </tr>
716 </table>
717 </div>';
718 return $sTables;
719 }
720
721 /**
722 * Returns the list of items based on the input SQL result pointer
723 * For each result row the internal var, $this->internal['currentRow'], is set with the row returned.
724 * $this->pi_list_header() makes the header row for the list
725 * $this->pi_list_row() is used for rendering each row
726 * Notice that these two functions are typically ALWAYS defined in the extension class of the plugin since they are directly concerned with the specific layout for that plugins purpose.
727 *
728 * @param Statement $statement Result pointer to a SQL result which can be traversed.
729 * @param string $tableParams Attributes for the table tag which is wrapped around the table rows containing the list
730 * @return string Output HTML, wrapped in <div>-tags with a class attribute
731 * @see pi_list_row(), pi_list_header()
732 */
733 public function pi_list_makelist($statement, $tableParams = '')
734 {
735 // Make list table header:
736 $tRows = [];
737 $this->internal['currentRow'] = '';
738 $tRows[] = $this->pi_list_header();
739 // Make list table rows
740 $c = 0;
741 while ($this->internal['currentRow'] = $statement->fetch()) {
742 $tRows[] = $this->pi_list_row($c);
743 $c++;
744 }
745 $out = '
746
747 <!--
748 Record list:
749 -->
750 <div' . $this->pi_classParam('listrow') . '>
751 <' . rtrim('table ' . $tableParams) . '>
752 ' . implode('', $tRows) . '
753 </table>
754 </div>';
755 return $out;
756 }
757
758 /**
759 * Returns a list row. Get data from $this->internal['currentRow'];
760 * (Dummy)
761 * Notice: This function should ALWAYS be defined in the extension class of the plugin since it is directly concerned with the specific layout of the listing for your plugins purpose.
762 *
763 * @param int $c Row counting. Starts at 0 (zero). Used for alternating class values in the output rows.
764 * @return string HTML output, a table row with a class attribute set (alternative based on odd/even rows)
765 */
766 public function pi_list_row($c)
767 {
768 // Dummy
769 return '<tr' . ($c % 2 ? $this->pi_classParam('listrow-odd') : '') . '><td><p>[dummy row]</p></td></tr>';
770 }
771
772 /**
773 * Returns a list header row.
774 * (Dummy)
775 * Notice: This function should ALWAYS be defined in the extension class of the plugin since it is directly concerned with the specific layout of the listing for your plugins purpose.
776 *
777 * @return string HTML output, a table row with a class attribute set
778 */
779 public function pi_list_header()
780 {
781 return '<tr' . $this->pi_classParam('listrow-header') . '><td><p>[dummy header row]</p></td></tr>';
782 }
783
784 /***************************
785 *
786 * Stylesheet, CSS
787 *
788 **************************/
789 /**
790 * Returns a class-name prefixed with $this->prefixId and with all underscores substituted to dashes (-)
791 *
792 * @param string $class The class name (or the END of it since it will be prefixed by $this->prefixId.'-')
793 * @return string The combined class name (with the correct prefix)
794 */
795 public function pi_getClassName($class)
796 {
797 return str_replace('_', '-', $this->prefixId) . ($this->prefixId ? '-' : '') . $class;
798 }
799
800 /**
801 * Returns the class-attribute with the correctly prefixed classname
802 * Using pi_getClassName()
803 *
804 * @param string $class The class name(s) (suffix) - separate multiple classes with commas
805 * @param string $addClasses Additional class names which should not be prefixed - separate multiple classes with commas
806 * @return string A "class" attribute with value and a single space char before it.
807 * @see pi_getClassName()
808 */
809 public function pi_classParam($class, $addClasses = '')
810 {
811 $output = '';
812 $classNames = GeneralUtility::trimExplode(',', $class);
813 foreach ($classNames as $className) {
814 $output .= ' ' . $this->pi_getClassName($className);
815 }
816 $additionalClassNames = GeneralUtility::trimExplode(',', $addClasses);
817 foreach ($additionalClassNames as $additionalClassName) {
818 $output .= ' ' . $additionalClassName;
819 }
820 return ' class="' . trim($output) . '"';
821 }
822
823 /**
824 * Wraps the input string in a <div> tag with the class attribute set to the prefixId.
825 * All content returned from your plugins should be returned through this function so all content from your plugin is encapsulated in a <div>-tag nicely identifying the content of your plugin.
826 *
827 * @param string $str HTML content to wrap in the div-tags with the "main class" of the plugin
828 * @return string HTML content wrapped, ready to return to the parent object.
829 */
830 public function pi_wrapInBaseClass($str)
831 {
832 $content = '<div class="' . str_replace('_', '-', $this->prefixId) . '">
833 ' . $str . '
834 </div>
835 ';
836 if (!$this->frontendController->config['config']['disablePrefixComment']) {
837 $content = '
838
839
840 <!--
841
842 BEGIN: Content of extension "' . $this->extKey . '", plugin "' . $this->prefixId . '"
843
844 -->
845 ' . $content . '
846 <!-- END: Content of extension "' . $this->extKey . '", plugin "' . $this->prefixId . '" -->
847
848 ';
849 }
850 return $content;
851 }
852
853 /***************************
854 *
855 * Frontend editing: Edit panel, edit icons
856 *
857 **************************/
858 /**
859 * Returns the Backend User edit panel for the $row from $tablename
860 *
861 * @param array $row Record array.
862 * @param string $tablename Table name
863 * @param string $label A label to show with the panel.
864 * @param array $conf TypoScript parameters to pass along to the EDITPANEL content Object that gets rendered. The property "allow" WILL get overridden/set though.
865 * @return string Returns FALSE/blank if no BE User login and of course if the panel is not shown for other reasons. Otherwise the HTML for the panel (a table).
866 * @see ContentObjectRenderer::EDITPANEL()
867 */
868 public function pi_getEditPanel($row = [], $tablename = '', $label = '', $conf = [])
869 {
870 $panel = '';
871 if (!$row || !$tablename) {
872 $row = $this->internal['currentRow'];
873 $tablename = $this->internal['currentTable'];
874 }
875 if ($this->frontendController->isBackendUserLoggedIn()) {
876 // Create local cObj if not set:
877 if (!is_object($this->pi_EPtemp_cObj)) {
878 $this->pi_EPtemp_cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
879 $this->pi_EPtemp_cObj->setParent($this->cObj->data, $this->cObj->currentRecord);
880 }
881 // Initialize the cObj object with current row
882 $this->pi_EPtemp_cObj->start($row, $tablename);
883 // Setting TypoScript values in the $conf array. See documentation in TSref for the EDITPANEL cObject.
884 $conf['allow'] = 'edit,new,delete,move,hide';
885 $panel = $this->pi_EPtemp_cObj->cObjGetSingle('EDITPANEL', $conf, 'editpanel');
886 }
887 if ($panel) {
888 if ($label) {
889 return '<!-- BEGIN: EDIT PANEL --><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td valign="top">' . $label . '</td><td valign="top" align="right">' . $panel . '</td></tr></table><!-- END: EDIT PANEL -->';
890 }
891 return '<!-- BEGIN: EDIT PANEL -->' . $panel . '<!-- END: EDIT PANEL -->';
892 }
893 return $label;
894 }
895
896 /**
897 * Adds edit-icons to the input content.
898 * ContentObjectRenderer::editIcons used for rendering
899 *
900 * @param string $content HTML content to add icons to. The icons will be put right after the last content part in the string (that means before the ending series of HTML tags)
901 * @param string $fields The list of fields to edit when the icon is clicked.
902 * @param string $title Title for the edit icon.
903 * @param array $row Table record row
904 * @param string $tablename Table name
905 * @param array $oConf Conf array
906 * @return string The processed content
907 * @see ContentObjectRenderer::editIcons()
908 */
909 public function pi_getEditIcon($content, $fields, $title = '', $row = [], $tablename = '', $oConf = [])
910 {
911 if ($this->frontendController->isBackendUserLoggedIn()) {
912 if (!$row || !$tablename) {
913 $row = $this->internal['currentRow'];
914 $tablename = $this->internal['currentTable'];
915 }
916 $conf = array_merge([
917 'beforeLastTag' => 1,
918 'iconTitle' => $title
919 ], $oConf);
920 $content = $this->cObj->editIcons($content, $tablename . ':' . $fields, $conf, $tablename . ':' . $row['uid'], $row, '&viewUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI')));
921 }
922 return $content;
923 }
924
925 /***************************
926 *
927 * Localization, locallang functions
928 *
929 **************************/
930 /**
931 * Returns the localized label of the LOCAL_LANG key, $key
932 * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
933 *
934 * @param string $key The key from the LOCAL_LANG array for which to return the value.
935 * @param string $alternativeLabel Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
936 * @return string The value from LOCAL_LANG.
937 */
938 public function pi_getLL($key, $alternativeLabel = '')
939 {
940 $word = null;
941 if (!empty($this->LOCAL_LANG[$this->LLkey][$key][0]['target'])
942 || isset($this->LOCAL_LANG_UNSET[$this->LLkey][$key])
943 ) {
944 $word = $this->LOCAL_LANG[$this->LLkey][$key][0]['target'];
945 } elseif ($this->altLLkey) {
946 $alternativeLanguageKeys = GeneralUtility::trimExplode(',', $this->altLLkey, true);
947 $alternativeLanguageKeys = array_reverse($alternativeLanguageKeys);
948 foreach ($alternativeLanguageKeys as $languageKey) {
949 if (!empty($this->LOCAL_LANG[$languageKey][$key][0]['target'])
950 || isset($this->LOCAL_LANG_UNSET[$languageKey][$key])
951 ) {
952 // Alternative language translation for key exists
953 $word = $this->LOCAL_LANG[$languageKey][$key][0]['target'];
954 break;
955 }
956 }
957 }
958 if ($word === null) {
959 if (!empty($this->LOCAL_LANG['default'][$key][0]['target'])
960 || isset($this->LOCAL_LANG_UNSET['default'][$key])
961 ) {
962 // Get default translation (without charset conversion, english)
963 $word = $this->LOCAL_LANG['default'][$key][0]['target'];
964 } else {
965 // Return alternative string or empty
966 $word = isset($this->LLtestPrefixAlt) ? $this->LLtestPrefixAlt . $alternativeLabel : $alternativeLabel;
967 }
968 }
969 return isset($this->LLtestPrefix) ? $this->LLtestPrefix . $word : $word;
970 }
971
972 /**
973 * Loads local-language values from the file passed as a parameter or
974 * by looking for a "locallang" file in the
975 * plugin class directory ($this->scriptRelPath).
976 * Also locallang values set in the TypoScript property "_LOCAL_LANG" are
977 * merged onto the values found in the "locallang" file.
978 * Supported file extensions xlf, xml
979 *
980 * @param string $languageFilePath path to the plugin language file in format EXT:....
981 */
982 public function pi_loadLL($languageFilePath = '')
983 {
984 if ($this->LOCAL_LANG_loaded) {
985 return;
986 }
987
988 if ($languageFilePath === '' && $this->scriptRelPath) {
989 $languageFilePath = 'EXT:' . $this->extKey . '/' . PathUtility::dirname($this->scriptRelPath) . '/locallang.xlf';
990 }
991 if ($languageFilePath !== '') {
992 /** @var LocalizationFactory $languageFactory */
993 $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
994 $this->LOCAL_LANG = $languageFactory->getParsedData($languageFilePath, $this->LLkey);
995 $alternativeLanguageKeys = GeneralUtility::trimExplode(',', $this->altLLkey, true);
996 foreach ($alternativeLanguageKeys as $languageKey) {
997 $tempLL = $languageFactory->getParsedData($languageFilePath, $languageKey);
998 if ($this->LLkey !== 'default' && isset($tempLL[$languageKey])) {
999 $this->LOCAL_LANG[$languageKey] = $tempLL[$languageKey];
1000 }
1001 }
1002 // Overlaying labels from TypoScript (including fictitious language keys for non-system languages!):
1003 if (isset($this->conf['_LOCAL_LANG.'])) {
1004 // Clear the "unset memory"
1005 $this->LOCAL_LANG_UNSET = [];
1006 foreach ($this->conf['_LOCAL_LANG.'] as $languageKey => $languageArray) {
1007 // Remove the dot after the language key
1008 $languageKey = substr($languageKey, 0, -1);
1009 // Don't process label if the language is not loaded
1010 if (is_array($languageArray) && isset($this->LOCAL_LANG[$languageKey])) {
1011 foreach ($languageArray as $labelKey => $labelValue) {
1012 if (!is_array($labelValue)) {
1013 $this->LOCAL_LANG[$languageKey][$labelKey][0]['target'] = $labelValue;
1014 if ($labelValue === '') {
1015 $this->LOCAL_LANG_UNSET[$languageKey][$labelKey] = '';
1016 }
1017 }
1018 }
1019 }
1020 }
1021 }
1022 }
1023 $this->LOCAL_LANG_loaded = true;
1024 }
1025
1026 /***************************
1027 *
1028 * Database, queries
1029 *
1030 **************************/
1031 /**
1032 * Executes a standard SELECT query for listing of records based on standard input vars from the 'browser' ($this->internal['results_at_a_time'] and $this->piVars['pointer']) and 'searchbox' ($this->piVars['sword'] and $this->internal['searchFieldList'])
1033 * Set $count to 1 if you wish to get a count(*) query for selecting the number of results.
1034 * Notice that the query will use $this->conf['pidList'] and $this->conf['recursive'] to generate a PID list within which to search for records.
1035 *
1036 * @param string $table The table name to make the query for.
1037 * @param bool $count If set, you will get a "count(*)" query back instead of field selecting
1038 * @param string $addWhere Additional WHERE clauses (should be starting with " AND ....")
1039 * @param mixed $mm_cat If an array, then it must contain the keys "table", "mmtable" and (optionally) "catUidList" defining a table to make a MM-relation to in the query (based on fields uid_local and uid_foreign). If not array, the query will be a plain query looking up data in only one table.
1040 * @param string $groupBy If set, this is added as a " GROUP BY ...." part of the query.
1041 * @param string $orderBy If set, this is added as a " ORDER BY ...." part of the query. The default is that an ORDER BY clause is made based on $this->internal['orderBy'] and $this->internal['descFlag'] where the orderBy field must be found in $this->internal['orderByList']
1042 * @param string $query If set, this is taken as the first part of the query instead of what is created internally. Basically this should be a query starting with "FROM [table] WHERE ... AND ...". The $addWhere clauses and all the other stuff is still added. Only the tables and PID selecting clauses are bypassed. May be deprecated in the future!
1043 * @return Statement
1044 */
1045 public function pi_exec_query($table, $count = false, $addWhere = '', $mm_cat = '', $groupBy = '', $orderBy = '', $query = '')
1046 {
1047 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1048 $queryBuilder->from($table);
1049
1050 // Begin Query:
1051 if (!$query) {
1052 // This adds WHERE-clauses that ensures deleted, hidden, starttime/endtime/access records are NOT
1053 // selected, if they should not! Almost ALWAYS add this to your queries!
1054 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1055
1056 // Fetches the list of PIDs to select from.
1057 // TypoScript property .pidList is a comma list of pids. If blank, current page id is used.
1058 // TypoScript property .recursive is an int+ which determines how many levels down from the pids in the pid-list subpages should be included in the select.
1059 $pidList = GeneralUtility::intExplode(',', $this->pi_getPidList($this->conf['pidList'], $this->conf['recursive']), true);
1060 if (is_array($mm_cat)) {
1061 $queryBuilder->from($mm_cat['table'])
1062 ->from($mm_cat['mmtable'])
1063 ->where(
1064 $queryBuilder->expr()->eq($table . '.uid', $queryBuilder->quoteIdentifier($mm_cat['mmtable'] . '.uid_local')),
1065 $queryBuilder->expr()->eq($mm_cat['table'] . '.uid', $queryBuilder->quoteIdentifier($mm_cat['mmtable'] . '.uid_foreign')),
1066 $queryBuilder->expr()->in(
1067 $table . '.pid',
1068 $queryBuilder->createNamedParameter($pidList, Connection::PARAM_INT_ARRAY)
1069 )
1070 );
1071 if (strcmp($mm_cat['catUidList'], '')) {
1072 $queryBuilder->andWhere(
1073 $queryBuilder->expr()->in(
1074 $mm_cat['table'] . '.uid',
1075 $queryBuilder->createNamedParameter(
1076 GeneralUtility::intExplode(',', $mm_cat['catUidList'], true),
1077 Connection::PARAM_INT_ARRAY
1078 )
1079 )
1080 );
1081 }
1082 } else {
1083 $queryBuilder->where(
1084 $queryBuilder->expr()->in(
1085 'pid',
1086 $queryBuilder->createNamedParameter($pidList, Connection::PARAM_INT_ARRAY)
1087 )
1088 );
1089 }
1090 } else {
1091 // Restrictions need to be handled by the $query parameter!
1092 $queryBuilder->getRestrictions()->removeAll();
1093
1094 // Split the "FROM ... WHERE" string so we get the WHERE part and TABLE names separated...:
1095 list($tableListFragment, $whereFragment) = preg_split('/WHERE/i', trim($query), 2);
1096 foreach (QueryHelper::parseTableList($tableListFragment) as $tableNameAndAlias) {
1097 list($tableName, $tableAlias) = $tableNameAndAlias;
1098 $queryBuilder->from($tableName, $tableAlias);
1099 }
1100 $queryBuilder->where(QueryHelper::stripLogicalOperatorPrefix($whereFragment));
1101 }
1102
1103 // Add '$addWhere'
1104 if ($addWhere) {
1105 $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($addWhere));
1106 }
1107 // Search word:
1108 if ($this->piVars['sword'] && $this->internal['searchFieldList']) {
1109 $queryBuilder->andWhere(
1110 QueryHelper::stripLogicalOperatorPrefix(
1111 $this->cObj->searchWhere($this->piVars['sword'], $this->internal['searchFieldList'], $table)
1112 )
1113 );
1114 }
1115
1116 if ($count) {
1117 $queryBuilder->count('*');
1118 } else {
1119 // Add 'SELECT'
1120 $fields = $this->pi_prependFieldsWithTable($table, $this->pi_listFields);
1121 $queryBuilder->select(...GeneralUtility::trimExplode(',', $fields, true));
1122
1123 // Order by data:
1124 if (!$orderBy && $this->internal['orderBy']) {
1125 if (GeneralUtility::inList($this->internal['orderByList'], $this->internal['orderBy'])) {
1126 $sorting = $this->internal['descFlag'] ? ' DESC' : 'ASC';
1127 $queryBuilder->orderBy($table . '.' . $this->internal['orderBy'], $sorting);
1128 }
1129 } elseif ($orderBy) {
1130 foreach (QueryHelper::parseOrderBy($orderBy) as $fieldNameAndSorting) {
1131 list($fieldName, $sorting) = $fieldNameAndSorting;
1132 $queryBuilder->addOrderBy($fieldName, $sorting);
1133 }
1134 }
1135
1136 // Limit data:
1137 $pointer = (int)$this->piVars['pointer'];
1138 $results_at_a_time = MathUtility::forceIntegerInRange($this->internal['results_at_a_time'], 1, 1000);
1139 $queryBuilder->setFirstResult($pointer * $results_at_a_time)
1140 ->setMaxResults($results_at_a_time);
1141
1142 // Grouping
1143 if (!empty($groupBy)) {
1144 $queryBuilder->groupBy(...QueryHelper::parseGroupBy($groupBy));
1145 }
1146 }
1147
1148 return $queryBuilder->execute();
1149 }
1150
1151 /**
1152 * Returns the row $uid from $table
1153 * (Simply calling $this->frontendEngine->sys_page->checkRecord())
1154 *
1155 * @param string $table The table name
1156 * @param int $uid The uid of the record from the table
1157 * @param bool $checkPage If $checkPage is set, it's required that the page on which the record resides is accessible
1158 * @return array If record is found, an array. Otherwise FALSE.
1159 */
1160 public function pi_getRecord($table, $uid, $checkPage = false)
1161 {
1162 return $this->frontendController->sys_page->checkRecord($table, $uid, $checkPage);
1163 }
1164
1165 /**
1166 * Returns a commalist of page ids for a query (eg. 'WHERE pid IN (...)')
1167 *
1168 * @param string $pid_list A comma list of page ids (if empty current page is used)
1169 * @param int $recursive An integer >=0 telling how deep to dig for pids under each entry in $pid_list
1170 * @return string List of PID values (comma separated)
1171 */
1172 public function pi_getPidList($pid_list, $recursive = 0)
1173 {
1174 if (!strcmp($pid_list, '')) {
1175 $pid_list = $this->frontendController->id;
1176 }
1177 $recursive = MathUtility::forceIntegerInRange($recursive, 0);
1178 $pid_list_arr = array_unique(GeneralUtility::trimExplode(',', $pid_list, true));
1179 $pid_list = [];
1180 foreach ($pid_list_arr as $val) {
1181 $val = MathUtility::forceIntegerInRange($val, 0);
1182 if ($val) {
1183 $_list = $this->cObj->getTreeList(-1 * $val, $recursive);
1184 if ($_list) {
1185 $pid_list[] = $_list;
1186 }
1187 }
1188 }
1189 return implode(',', $pid_list);
1190 }
1191
1192 /**
1193 * Having a comma list of fields ($fieldList) this is prepended with the $table.'.' name
1194 *
1195 * @param string $table Table name to prepend
1196 * @param string $fieldList List of fields where each element will be prepended with the table name given.
1197 * @return string List of fields processed.
1198 */
1199 public function pi_prependFieldsWithTable($table, $fieldList)
1200 {
1201 $list = GeneralUtility::trimExplode(',', $fieldList, true);
1202 $return = [];
1203 foreach ($list as $listItem) {
1204 $return[] = $table . '.' . $listItem;
1205 }
1206 return implode(',', $return);
1207 }
1208
1209 /**
1210 * Will select all records from the "category table", $table, and return them in an array.
1211 *
1212 * @param string $table The name of the category table to select from.
1213 * @param int $pid The page from where to select the category records.
1214 * @param string $whereClause Optional additional WHERE clauses put in the end of the query. DO NOT PUT IN GROUP BY, ORDER BY or LIMIT!
1215 * @param string $groupBy Optional GROUP BY field(s), if none, supply blank string.
1216 * @param string $orderBy Optional ORDER BY field(s), if none, supply blank string.
1217 * @param string $limit Optional LIMIT value ([begin,]max), if none, supply blank string.
1218 * @return array The array with the category records in.
1219 */
1220 public function pi_getCategoryTableContents($table, $pid, $whereClause = '', $groupBy = '', $orderBy = '', $limit = '')
1221 {
1222 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1223 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1224 $queryBuilder->select('*')
1225 ->from($table)
1226 ->where(
1227 $queryBuilder->expr()->eq(
1228 'pid',
1229 $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
1230 ),
1231 QueryHelper::stripLogicalOperatorPrefix($whereClause)
1232 );
1233
1234 if (!empty($orderBy)) {
1235 foreach (QueryHelper::parseOrderBy($orderBy) as $fieldNameAndSorting) {
1236 list($fieldName, $sorting) = $fieldNameAndSorting;
1237 $queryBuilder->addOrderBy($fieldName, $sorting);
1238 }
1239 }
1240
1241 if (!empty($groupBy)) {
1242 $queryBuilder->groupBy(...QueryHelper::parseGroupBy($groupBy));
1243 }
1244
1245 if (!empty($limit)) {
1246 $limitValues = GeneralUtility::intExplode(',', $limit, true);
1247 if (count($limitValues) === 1) {
1248 $queryBuilder->setMaxResults($limitValues[0]);
1249 } else {
1250 $queryBuilder->setFirstResult($limitValues[0])
1251 ->setMaxResults($limitValues[1]);
1252 }
1253 }
1254
1255 $result = $queryBuilder->execute();
1256 $outArr = [];
1257 while ($row = $result->fetch()) {
1258 $outArr[$row['uid']] = $row;
1259 }
1260 return $outArr;
1261 }
1262
1263 /***************************
1264 *
1265 * Various
1266 *
1267 **************************/
1268 /**
1269 * Returns TRUE if the piVars array has ONLY those fields entered that is set in the $fList (commalist) AND if none of those fields value is greater than $lowerThan field if they are integers.
1270 * Notice that this function will only work as long as values are integers.
1271 *
1272 * @param string $fList List of fields (keys from piVars) to evaluate on
1273 * @param int $lowerThan Limit for the values.
1274 * @return bool|null Returns TRUE (1) if conditions are met.
1275 */
1276 public function pi_isOnlyFields($fList, $lowerThan = -1)
1277 {
1278 $lowerThan = $lowerThan == -1 ? $this->pi_lowerThan : $lowerThan;
1279 $fList = GeneralUtility::trimExplode(',', $fList, true);
1280 $tempPiVars = $this->piVars;
1281 foreach ($fList as $k) {
1282 if (!MathUtility::canBeInterpretedAsInteger($tempPiVars[$k]) || $tempPiVars[$k] < $lowerThan) {
1283 unset($tempPiVars[$k]);
1284 }
1285 }
1286 if (empty($tempPiVars)) {
1287 //@TODO: How do we deal with this? return TRUE would be the right thing to do here but that might be breaking
1288 return 1;
1289 }
1290 return null;
1291 }
1292
1293 /**
1294 * Returns TRUE if the array $inArray contains only values allowed to be cached based on the configuration in $this->pi_autoCacheFields
1295 * Used by ->pi_linkTP_keepPIvars
1296 * This is an advanced form of evaluation of whether a URL should be cached or not.
1297 *
1298 * @param array $inArray An array with piVars values to evaluate
1299 * @return bool|null Returns TRUE (1) if conditions are met.
1300 * @see pi_linkTP_keepPIvars()
1301 */
1302 public function pi_autoCache($inArray)
1303 {
1304 if (is_array($inArray)) {
1305 foreach ($inArray as $fN => $fV) {
1306 if (!strcmp($inArray[$fN], '')) {
1307 unset($inArray[$fN]);
1308 } elseif (is_array($this->pi_autoCacheFields[$fN])) {
1309 if (is_array($this->pi_autoCacheFields[$fN]['range']) && (int)$inArray[$fN] >= (int)$this->pi_autoCacheFields[$fN]['range'][0] && (int)$inArray[$fN] <= (int)$this->pi_autoCacheFields[$fN]['range'][1]) {
1310 unset($inArray[$fN]);
1311 }
1312 if (is_array($this->pi_autoCacheFields[$fN]['list']) && in_array($inArray[$fN], $this->pi_autoCacheFields[$fN]['list'])) {
1313 unset($inArray[$fN]);
1314 }
1315 }
1316 }
1317 }
1318 if (empty($inArray)) {
1319 //@TODO: How do we deal with this? return TRUE would be the right thing to do here but that might be breaking
1320 return 1;
1321 }
1322 return null;
1323 }
1324
1325 /**
1326 * Will process the input string with the parseFunc function from ContentObjectRenderer based on configuration
1327 * set in "lib.parseFunc_RTE" in the current TypoScript template.
1328 *
1329 * @param string $str The input text string to process
1330 * @return string The processed string
1331 * @see ContentObjectRenderer::parseFunc()
1332 */
1333 public function pi_RTEcssText($str)
1334 {
1335 $parseFunc = $this->frontendController->tmpl->setup['lib.']['parseFunc_RTE.'];
1336 if (is_array($parseFunc)) {
1337 $str = $this->cObj->parseFunc($str, $parseFunc);
1338 }
1339 return $str;
1340 }
1341
1342 /*******************************
1343 *
1344 * FlexForms related functions
1345 *
1346 *******************************/
1347 /**
1348 * Converts $this->cObj->data['pi_flexform'] from XML string to flexForm array.
1349 *
1350 * @param string $field Field name to convert
1351 */
1352 public function pi_initPIflexForm($field = 'pi_flexform')
1353 {
1354 // Converting flexform data into array:
1355 if (!is_array($this->cObj->data[$field]) && $this->cObj->data[$field]) {
1356 $this->cObj->data[$field] = GeneralUtility::xml2array($this->cObj->data[$field]);
1357 if (!is_array($this->cObj->data[$field])) {
1358 $this->cObj->data[$field] = [];
1359 }
1360 }
1361 }
1362
1363 /**
1364 * Return value from somewhere inside a FlexForm structure
1365 *
1366 * @param array $T3FlexForm_array FlexForm data
1367 * @param string $fieldName Field name to extract. Can be given like "test/el/2/test/el/field_templateObject" where each part will dig a level deeper in the FlexForm data.
1368 * @param string $sheet Sheet pointer, eg. "sDEF
1369 * @param string $lang Language pointer, eg. "lDEF
1370 * @param string $value Value pointer, eg. "vDEF
1371 * @return string|null The content.
1372 */
1373 public function pi_getFFvalue($T3FlexForm_array, $fieldName, $sheet = 'sDEF', $lang = 'lDEF', $value = 'vDEF')
1374 {
1375 $sheetArray = is_array($T3FlexForm_array) ? $T3FlexForm_array['data'][$sheet][$lang] : '';
1376 if (is_array($sheetArray)) {
1377 return $this->pi_getFFvalueFromSheetArray($sheetArray, explode('/', $fieldName), $value);
1378 }
1379 return null;
1380 }
1381
1382 /**
1383 * Returns part of $sheetArray pointed to by the keys in $fieldNameArray
1384 *
1385 * @param array $sheetArray Multidimensiona array, typically FlexForm contents
1386 * @param array $fieldNameArr Array where each value points to a key in the FlexForms content - the input array will have the value returned pointed to by these keys. All integer keys will not take their integer counterparts, but rather traverse the current position in the array an return element number X (whether this is right behavior is not settled yet...)
1387 * @param string $value Value for outermost key, typ. "vDEF" depending on language.
1388 * @return mixed The value, typ. string.
1389 * @internal
1390 * @see pi_getFFvalue()
1391 */
1392 public function pi_getFFvalueFromSheetArray($sheetArray, $fieldNameArr, $value)
1393 {
1394 $tempArr = $sheetArray;
1395 foreach ($fieldNameArr as $k => $v) {
1396 if (MathUtility::canBeInterpretedAsInteger($v)) {
1397 if (is_array($tempArr)) {
1398 $c = 0;
1399 foreach ($tempArr as $values) {
1400 if ($c == $v) {
1401 $tempArr = $values;
1402 break;
1403 }
1404 $c++;
1405 }
1406 }
1407 } else {
1408 $tempArr = $tempArr[$v];
1409 }
1410 }
1411 return $tempArr[$value];
1412 }
1413
1414 /**
1415 * Returns the currently configured "site language" if a site is configured (= resolved) in the current request.
1416 *
1417 * @internal
1418 */
1419 protected function getCurrentSiteLanguage(): ?SiteLanguage
1420 {
1421 $request = $GLOBALS['TYPO3_REQUEST'] ?? null;
1422 return $request
1423 && $request instanceof ServerRequestInterface
1424 && $request->getAttribute('language') instanceof SiteLanguage
1425 ? $request->getAttribute('language')
1426 : null;
1427 }
1428 }