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