Test Failed
Branch master (7b1793)
by Tymoteusz
15:35
created

AbstractPlugin::pi_linkTP_keepPIvars()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 3
nop 5
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
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\Utility\ArrayUtility;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Core\Utility\MathUtility;
28
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
29
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
30
31
/**
32
 * Base class for frontend plugins
33
 * Most modern frontend plugins are extension classes of this one.
34
 * This class contains functions which assists these plugins in creating lists, searching, displaying menus, page-browsing (next/previous/1/2/3) and handling links.
35
 * 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...)
36
 */
37
class AbstractPlugin
38
{
39
    /**
40
     * The backReference to the mother cObj object set at call time
41
     *
42
     * @var ContentObjectRenderer
43
     */
44
    public $cObj;
45
46
    /**
47
     * Should be same as classname of the plugin, used for CSS classes, variables
48
     *
49
     * @var string
50
     */
51
    public $prefixId;
52
53
    /**
54
     * Path to the plugin class script relative to extension directory, eg. 'pi1/class.tx_newfaq_pi1.php'
55
     *
56
     * @var string
57
     */
58
    public $scriptRelPath;
59
60
    /**
61
     * Extension key.
62
     *
63
     * @var string
64
     */
65
    public $extKey;
66
67
    /**
68
     * This is the incoming array by name $this->prefixId merged between POST and GET, POST taking precedence.
69
     * Eg. if the class name is 'tx_myext'
70
     * then the content of this array will be whatever comes into &tx_myext[...]=...
71
     *
72
     * @var array
73
     */
74
    public $piVars = [
75
        'pointer' => '',
76
        // Used as a pointer for lists
77
        'mode' => '',
78
        // List mode
79
        'sword' => '',
80
        // Search word
81
        'sort' => ''
82
    ];
83
84
    /**
85
     * Local pointer variabe array.
86
     * Holds pointer information for the MVC like approach Kasper
87
     * initially proposed
88
     *
89
     * @var array
90
     */
91
    public $internal = ['res_count' => 0, 'results_at_a_time' => 20, 'maxPages' => 10, 'currentRow' => [], 'currentTable' => ''];
92
93
    /**
94
     * Local Language content
95
     *
96
     * @var array
97
     */
98
    public $LOCAL_LANG = [];
99
100
    /**
101
     * Contains those LL keys, which have been set to (empty) in TypoScript.
102
     * This is necessary, as we cannot distinguish between a nonexisting
103
     * translation and a label that has been cleared by TS.
104
     * In both cases ['key'][0]['target'] is "".
105
     *
106
     * @var array
107
     */
108
    protected $LOCAL_LANG_UNSET = [];
109
110
    /**
111
     * Flag that tells if the locallang file has been fetch (or tried to
112
     * be fetched) already.
113
     *
114
     * @var bool
115
     */
116
    public $LOCAL_LANG_loaded = false;
117
118
    /**
119
     * Pointer to the language to use.
120
     *
121
     * @var string
122
     */
123
    public $LLkey = 'default';
124
125
    /**
126
     * Pointer to alternative fall-back language to use.
127
     *
128
     * @var string
129
     */
130
    public $altLLkey = '';
131
132
    /**
133
     * You can set this during development to some value that makes it
134
     * easy for you to spot all labels that ARe delivered by the getLL function.
135
     *
136
     * @var string
137
     */
138
    public $LLtestPrefix = '';
139
140
    /**
141
     * Save as LLtestPrefix, but additional prefix for the alternative value
142
     * in getLL() function calls
143
     *
144
     * @var string
145
     */
146
    public $LLtestPrefixAlt = '';
147
148
    /**
149
     * @var string
150
     */
151
    public $pi_isOnlyFields = 'mode,pointer';
152
153
    /**
154
     * @var int
155
     */
156
    public $pi_alwaysPrev = 0;
157
158
    /**
159
     * @var int
160
     */
161
    public $pi_lowerThan = 5;
162
163
    /**
164
     * @var string
165
     */
166
    public $pi_moreParams = '';
167
168
    /**
169
     * @var string
170
     */
171
    public $pi_listFields = '*';
172
173
    /**
174
     * @var array
175
     */
176
    public $pi_autoCacheFields = [];
177
178
    /**
179
     * @var bool
180
     */
181
    public $pi_autoCacheEn = false;
182
183
    /**
184
     * If set, then links are
185
     * 1) not using cHash and
186
     * 2) not allowing pages to be cached. (Set this for all USER_INT plugins!)
187
     *
188
     * @var bool
189
     */
190
    public $pi_USER_INT_obj = false;
191
192
    /**
193
     * If set, then caching is disabled if piVars are incoming while
194
     * no cHash was set (Set this for all USER plugins!)
195
     *
196
     * @var bool
197
     */
198
    public $pi_checkCHash = false;
199
200
    /**
201
     * Should normally be set in the main function with the TypoScript content passed to the method.
202
     *
203
     * $conf[LOCAL_LANG][_key_] is reserved for Local Language overrides.
204
     * $conf[userFunc] reserved for setting up the USER / USER_INT object. See TSref
205
     *
206
     * @var array
207
     */
208
    public $conf = [];
209
210
    /**
211
     * internal, don't mess with...
212
     *
213
     * @var ContentObjectRenderer
214
     */
215
    public $pi_EPtemp_cObj;
216
217
    /**
218
     * @var int
219
     */
220
    public $pi_tmpPageId = 0;
221
222
    /**
223
     * Property for accessing TypoScriptFrontendController centrally
224
     *
225
     * @var TypoScriptFrontendController
226
     */
227
    protected $frontendController;
228
229
    /**
230
     * @var MarkerBasedTemplateService
231
     */
232
    protected $templateService;
233
234
    /**
235
     * Class Constructor (true constructor)
236
     * Initializes $this->piVars if $this->prefixId is set to any value
237
     * Will also set $this->LLkey based on the config.language setting.
238
     *
239
     * @param null $_ unused,
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $_ is correct as it would always require null to be passed?
Loading history...
240
     * @param TypoScriptFrontendController $frontendController
241
     */
242
    public function __construct($_ = null, TypoScriptFrontendController $frontendController = null)
243
    {
244
        $this->frontendController = $frontendController ?: $GLOBALS['TSFE'];
245
        $this->templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
246
        // Setting piVars:
247
        if ($this->prefixId) {
248
            $this->piVars = GeneralUtility::_GPmerged($this->prefixId);
249
            // cHash mode check
250
            // IMPORTANT FOR CACHED PLUGINS (USER cObject): As soon as you generate cached plugin output which
251
            // depends on parameters (eg. seeing the details of a news item) you MUST check if a cHash value is set.
252
            // Background: The function call will check if a cHash parameter was sent with the URL because only if
253
            // it was the page may be cached. If no cHash was found the function will simply disable caching to
254
            // avoid unpredictable caching behaviour. In any case your plugin can generate the expected output and
255
            // the only risk is that the content may not be cached. A missing cHash value is considered a mistake
256
            // in the URL resulting from either URL manipulation, "realurl" "grayzones" etc. The problem is rare
257
            // (more frequent with "realurl") but when it occurs it is very puzzling!
258
            if ($this->pi_checkCHash && !empty($this->piVars)) {
259
                $this->frontendController->reqCHash();
260
            }
261
        }
262
        if (!empty($this->frontendController->config['config']['language'])) {
263
            $this->LLkey = $this->frontendController->config['config']['language'];
264
            if (empty($this->frontendController->config['config']['language_alt'])) {
265
                /** @var $locales Locales */
266
                $locales = GeneralUtility::makeInstance(Locales::class);
267
                if (in_array($this->LLkey, $locales->getLocales())) {
268
                    $this->altLLkey = '';
269
                    foreach ($locales->getLocaleDependencies($this->LLkey) as $language) {
270
                        $this->altLLkey .= $language . ',';
271
                    }
272
                    $this->altLLkey = rtrim($this->altLLkey, ',');
273
                }
274
            } else {
275
                $this->altLLkey = $this->frontendController->config['config']['language_alt'];
276
            }
277
        }
278
    }
279
280
    /**
281
     * Recursively looks for stdWrap and executes it
282
     *
283
     * @param array $conf Current section of configuration to work on
284
     * @param int $level Current level being processed (currently just for tracking; no limit enforced)
285
     * @return array Current section of configuration after stdWrap applied
286
     */
287
    protected function applyStdWrapRecursive(array $conf, $level = 0)
288
    {
289
        foreach ($conf as $key => $confNextLevel) {
290
            if (strpos($key, '.') !== false) {
291
                $key = substr($key, 0, -1);
292
293
                // descend into all non-stdWrap-subelements first
294
                foreach ($confNextLevel as $subKey => $subConfNextLevel) {
295
                    if (is_array($subConfNextLevel) && strpos($subKey, '.') !== false && $subKey !== 'stdWrap.') {
296
                        $conf[$key . '.'] = $this->applyStdWrapRecursive($confNextLevel, $level + 1);
0 ignored issues
show
Bug introduced by
Are you sure $key of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

296
                        $conf[/** @scrutinizer ignore-type */ $key . '.'] = $this->applyStdWrapRecursive($confNextLevel, $level + 1);
Loading history...
297
                    }
298
                }
299
300
                // now for stdWrap
301 View Code Duplication
                foreach ($confNextLevel as $subKey => $subConfNextLevel) {
302
                    if (is_array($subConfNextLevel) && $subKey === 'stdWrap.') {
303
                        $conf[$key] = $this->cObj->stdWrap($conf[$key], $conf[$key . '.']['stdWrap.']);
304
                        unset($conf[$key . '.']['stdWrap.']);
305
                        if (empty($conf[$key . '.'])) {
306
                            unset($conf[$key . '.']);
307
                        }
308
                    }
309
                }
310
            }
311
        }
312
        return $conf;
313
    }
314
315
    /**
316
     * If internal TypoScript property "_DEFAULT_PI_VARS." is set then it will merge the current $this->piVars array onto these default values.
317
     */
318
    public function pi_setPiVarDefaults()
319
    {
320
        if (isset($this->conf['_DEFAULT_PI_VARS.']) && is_array($this->conf['_DEFAULT_PI_VARS.'])) {
321
            $this->conf['_DEFAULT_PI_VARS.'] = $this->applyStdWrapRecursive($this->conf['_DEFAULT_PI_VARS.']);
322
            $tmp = $this->conf['_DEFAULT_PI_VARS.'];
323
            ArrayUtility::mergeRecursiveWithOverrule($tmp, is_array($this->piVars) ? $this->piVars : []);
324
            $this->piVars = $tmp;
325
        }
326
    }
327
328
    /***************************
329
     *
330
     * Link functions
331
     *
332
     **************************/
333
    /**
334
     * Get URL to some page.
335
     * Returns the URL to page $id with $target and an array of additional url-parameters, $urlParameters
336
     * Simple example: $this->pi_getPageLink(123) to get the URL for page-id 123.
337
     *
338
     * The function basically calls $this->cObj->getTypoLink_URL()
339
     *
340
     * @param int $id Page id
341
     * @param string $target Target value to use. Affects the &type-value of the URL, defaults to current.
342
     * @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.
343
     * @return string The resulting URL
344
     * @see pi_linkToPage()
345
     * @see ContentObjectRenderer->getTypoLink()
346
     */
347
    public function pi_getPageLink($id, $target = '', $urlParameters = [])
348
    {
349
        return $this->cObj->getTypoLink_URL($id, $urlParameters, $target);
350
    }
351
352
    /**
353
     * Link a string to some page.
354
     * Like pi_getPageLink() but takes a string as first parameter which will in turn be wrapped with the URL including target attribute
355
     * Simple example: $this->pi_linkToPage('My link', 123) to get something like <a href="index.php?id=123&type=1">My link</a>
356
     *
357
     * @param string $str The content string to wrap in <a> tags
358
     * @param int $id Page id
359
     * @param string $target Target value to use. Affects the &type-value of the URL, defaults to current.
360
     * @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.
361
     * @return string The input string wrapped in <a> tags with the URL and target set.
362
     * @see pi_getPageLink(), ContentObjectRenderer::getTypoLink()
363
     */
364
    public function pi_linkToPage($str, $id, $target = '', $urlParameters = [])
365
    {
366
        return $this->cObj->getTypoLink($str, $id, $urlParameters, $target);
367
    }
368
369
    /**
370
     * Link string to the current page.
371
     * Returns the $str wrapped in <a>-tags with a link to the CURRENT page, but with $urlParameters set as extra parameters for the page.
372
     *
373
     * @param string $str The content string to wrap in <a> tags
374
     * @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.
375
     * @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.
376
     * @param int $altPageId Alternative page ID for the link. (By default this function links to the SAME page!)
377
     * @return string The input string wrapped in <a> tags
378
     * @see pi_linkTP_keepPIvars(), ContentObjectRenderer::typoLink()
379
     */
380
    public function pi_linkTP($str, $urlParameters = [], $cache = false, $altPageId = 0)
381
    {
382
        $conf = [];
383
        $conf['useCacheHash'] = $this->pi_USER_INT_obj ? 0 : $cache;
384
        $conf['no_cache'] = $this->pi_USER_INT_obj ? 0 : !$cache;
385
        $conf['parameter'] = $altPageId ? $altPageId : ($this->pi_tmpPageId ? $this->pi_tmpPageId : $this->frontendController->id);
386
        $conf['additionalParams'] = $this->conf['parent.']['addParams'] . GeneralUtility::implodeArrayForUrl('', $urlParameters, '', true) . $this->pi_moreParams;
387
        return $this->cObj->typoLink($str, $conf);
388
    }
389
390
    /**
391
     * Link a string to the current page while keeping currently set values in piVars.
392
     * Like pi_linkTP, but $urlParameters is by default set to $this->piVars with $overrulePIvars overlaid.
393
     * 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.
394
     *
395
     * @param string $str The content string to wrap in <a> tags
396
     * @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)
397
     * @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.
398
     * @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[]
399
     * @param int $altPageId Alternative page ID for the link. (By default this function links to the SAME page!)
400
     * @return string The input string wrapped in <a> tags
401
     * @see pi_linkTP()
402
     */
403
    public function pi_linkTP_keepPIvars($str, $overrulePIvars = [], $cache = false, $clearAnyway = false, $altPageId = 0)
404
    {
405
        if (is_array($this->piVars) && is_array($overrulePIvars) && !$clearAnyway) {
406
            $piVars = $this->piVars;
407
            unset($piVars['DATA']);
408
            ArrayUtility::mergeRecursiveWithOverrule($piVars, $overrulePIvars);
409
            $overrulePIvars = $piVars;
410
            if ($this->pi_autoCacheEn) {
411
                $cache = $this->pi_autoCache($overrulePIvars);
412
            }
413
        }
414
        return $this->pi_linkTP($str, [$this->prefixId => $overrulePIvars], $cache, $altPageId);
415
    }
416
417
    /**
418
     * Get URL to the current page while keeping currently set values in piVars.
419
     * Same as pi_linkTP_keepPIvars but returns only the URL from the link.
420
     *
421
     * @param array $overrulePIvars See pi_linkTP_keepPIvars
422
     * @param bool $cache See pi_linkTP_keepPIvars
423
     * @param bool $clearAnyway See pi_linkTP_keepPIvars
424
     * @param int $altPageId See pi_linkTP_keepPIvars
425
     * @return string The URL ($this->cObj->lastTypoLinkUrl)
426
     * @see pi_linkTP_keepPIvars()
427
     */
428
    public function pi_linkTP_keepPIvars_url($overrulePIvars = [], $cache = false, $clearAnyway = false, $altPageId = 0)
429
    {
430
        $this->pi_linkTP_keepPIvars('|', $overrulePIvars, $cache, $clearAnyway, $altPageId);
431
        return $this->cObj->lastTypoLinkUrl;
432
    }
433
434
    /**
435
     * Wraps the $str in a link to a single display of the record (using piVars[showUid])
436
     * Uses pi_linkTP for the linking
437
     *
438
     * @param string $str The content string to wrap in <a> tags
439
     * @param int $uid UID of the record for which to display details (basically this will become the value of [showUid]
440
     * @param bool $cache See pi_linkTP_keepPIvars
441
     * @param array $mergeArr Array of values to override in the current piVars. Same as $overrulePIvars in pi_linkTP_keepPIvars
442
     * @param bool $urlOnly If TRUE, only the URL is returned, not a full link
443
     * @param int $altPageId Alternative page ID for the link. (By default this function links to the SAME page!)
444
     * @return string The input string wrapped in <a> tags
445
     * @see pi_linkTP(), pi_linkTP_keepPIvars()
446
     */
447
    public function pi_list_linkSingle($str, $uid, $cache = false, $mergeArr = [], $urlOnly = false, $altPageId = 0)
448
    {
449
        if ($this->prefixId) {
450
            if ($cache) {
451
                $overrulePIvars = $uid ? ['showUid' => $uid] : [];
452
                $overrulePIvars = array_merge($overrulePIvars, (array)$mergeArr);
453
                $str = $this->pi_linkTP($str, [$this->prefixId => $overrulePIvars], $cache, $altPageId);
454
            } else {
455
                $overrulePIvars = ['showUid' => $uid ?: ''];
456
                $overrulePIvars = array_merge($overrulePIvars, (array)$mergeArr);
457
                $str = $this->pi_linkTP_keepPIvars($str, $overrulePIvars, $cache, 0, $altPageId);
458
            }
459
            // If urlOnly flag, return only URL as it has recently be generated.
460
            if ($urlOnly) {
461
                $str = $this->cObj->lastTypoLinkUrl;
462
            }
463
        }
464
        return $str;
465
    }
466
467
    /**
468
     * 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
469
     *
470
     * @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.
471
     * @param string $winName Window name for the pop-up window
472
     * @param string $winParams Window parameters, see the default list for inspiration
473
     * @return string The processed input string, modified IF a <a> tag was found
474
     */
475
    public function pi_openAtagHrefInJSwindow($str, $winName = '', $winParams = 'width=670,height=500,status=0,menubar=0,scrollbars=1,resizable=1')
476
    {
477
        if (preg_match('/(.*)(<a[^>]*>)(.*)/i', $str, $match)) {
478
            $aTagContent = GeneralUtility::get_tag_attributes($match[2]);
479
            $onClick = 'vHWin=window.open('
480
                . GeneralUtility::quoteJSvalue($this->frontendController->baseUrlWrap($aTagContent['href'])) . ','
481
                . GeneralUtility::quoteJSvalue($winName ?: md5($aTagContent['href'])) . ','
482
                . GeneralUtility::quoteJSvalue($winParams) . ');vHWin.focus();return false;';
483
            $match[2] = '<a href="#" onclick="' . htmlspecialchars($onClick) . '">';
484
            $str = $match[1] . $match[2] . $match[3];
485
        }
486
        return $str;
487
    }
488
489
    /***************************
490
     *
491
     * Functions for listing, browsing, searching etc.
492
     *
493
     **************************/
494
    /**
495
     * 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.
496
     * 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)
497
     * 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.
498
     * Using $this->internal['dontLinkActivePage'] as switch if the active (current) page should be displayed as pure text or as a link to itself
499
     * 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.
500
     * 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.
501
     * 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...
502
     * 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.
503
     *
504
     * The third parameter is an array with several wraps for the parts of the pagebrowser. The following elements will be recognized:
505
     * disabledLinkWrap, inactiveLinkWrap, activeLinkWrap, browseLinksWrap, showResultsWrap, showResultsNumbersWrap, browseBoxWrap.
506
     *
507
     * If $wrapArr['showResultsNumbersWrap'] is set, the formatting string is expected to hold template markers (###FROM###, ###TO###, ###OUT_OF###, ###FROM_TO###, ###CURRENT_PAGE###, ###TOTAL_PAGES###)
508
     * otherwise the formatting string is expected to hold sprintf-markers (%s) for from, to, outof (in that sequence)
509
     *
510
     * @param int $showResultCount Determines how the results of the page browser will be shown. See description below
511
     * @param string $tableParams Attributes for the table tag which is wrapped around the table cells containing the browse links
512
     * @param array $wrapArr Array with elements to overwrite the default $wrapper-array.
513
     * @param string $pointerName varname for the pointer.
514
     * @param bool $hscText Enable htmlspecialchars() on language labels
515
     * @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)
516
     * @return string Output HTML-Table, wrapped in <div>-tags with a class attribute (if $wrapArr is not passed,
517
     */
518
    public function pi_list_browseresults($showResultCount = 1, $tableParams = '', $wrapArr = [], $pointerName = 'pointer', $hscText = true, $forceOutput = false)
519
    {
520
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::class]['pi_list_browseresults'] ?? [] as $classRef) {
521
            $hookObj = GeneralUtility::makeInstance($classRef);
522
            if (method_exists($hookObj, 'pi_list_browseresults')) {
523
                $pageBrowser = $hookObj->pi_list_browseresults($showResultCount, $tableParams, $wrapArr, $pointerName, $hscText, $forceOutput, $this);
524
                if (is_string($pageBrowser) && trim($pageBrowser) !== '') {
525
                    return $pageBrowser;
526
                }
527
            }
528
        }
529
        // example $wrapArr-array how it could be traversed from an extension
530
        /* $wrapArr = array(
531
        'browseBoxWrap' => '<div class="browseBoxWrap">|</div>',
532
        'showResultsWrap' => '<div class="showResultsWrap">|</div>',
533
        'browseLinksWrap' => '<div class="browseLinksWrap">|</div>',
534
        'showResultsNumbersWrap' => '<span class="showResultsNumbersWrap">|</span>',
535
        'disabledLinkWrap' => '<span class="disabledLinkWrap">|</span>',
536
        'inactiveLinkWrap' => '<span class="inactiveLinkWrap">|</span>',
537
        'activeLinkWrap' => '<span class="activeLinkWrap">|</span>'
538
        );*/
539
        // Initializing variables:
540
        $pointer = (int)$this->piVars[$pointerName];
541
        $count = (int)$this->internal['res_count'];
542
        $results_at_a_time = MathUtility::forceIntegerInRange($this->internal['results_at_a_time'], 1, 1000);
543
        $totalPages = ceil($count / $results_at_a_time);
544
        $maxPages = MathUtility::forceIntegerInRange($this->internal['maxPages'], 1, 100);
545
        $pi_isOnlyFields = $this->pi_isOnlyFields($this->pi_isOnlyFields);
546
        if (!$forceOutput && $count <= $results_at_a_time) {
547
            return '';
548
        }
549
        // $showResultCount determines how the results of the pagerowser will be shown.
550
        // If set to 0: only the result-browser will be shown
551
        //	 		 1: (default) the text "Displaying results..." and the result-browser will be shown.
552
        //	 		 2: only the text "Displaying results..." will be shown
553
        $showResultCount = (int)$showResultCount;
554
        // If this is set, two links named "<< First" and "LAST >>" will be shown and point to the very first or last page.
555
        $showFirstLast = !empty($this->internal['showFirstLast']);
556
        // If this has a value the "previous" button is always visible (will be forced if "showFirstLast" is set)
557
        $alwaysPrev = $showFirstLast ? 1 : $this->pi_alwaysPrev;
558
        if (isset($this->internal['pagefloat'])) {
559
            if (strtoupper($this->internal['pagefloat']) === 'CENTER') {
560
                $pagefloat = ceil(($maxPages - 1) / 2);
561
            } else {
562
                // pagefloat set as integer. 0 = left, value >= $this->internal['maxPages'] = right
563
                $pagefloat = MathUtility::forceIntegerInRange($this->internal['pagefloat'], -1, $maxPages - 1);
564
            }
565
        } else {
566
            // pagefloat disabled
567
            $pagefloat = -1;
568
        }
569
        // Default values for "traditional" wrapping with a table. Can be overwritten by vars from $wrapArr
570
        $wrapper['disabledLinkWrap'] = '<td class="nowrap"><p>|</p></td>';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$wrapper was never initialized. Although not strictly required by PHP, it is generally a good practice to add $wrapper = array(); before regardless.
Loading history...
571
        $wrapper['inactiveLinkWrap'] = '<td class="nowrap"><p>|</p></td>';
572
        $wrapper['activeLinkWrap'] = '<td' . $this->pi_classParam('browsebox-SCell') . ' class="nowrap"><p>|</p></td>';
573
        $wrapper['browseLinksWrap'] = rtrim('<table ' . $tableParams) . '><tr>|</tr></table>';
574
        $wrapper['showResultsWrap'] = '<p>|</p>';
575
        $wrapper['browseBoxWrap'] = '
576
		<!--
577
			List browsing box:
578
		-->
579
		<div ' . $this->pi_classParam('browsebox') . '>
580
			|
581
		</div>';
582
        // Now overwrite all entries in $wrapper which are also in $wrapArr
583
        $wrapper = array_merge($wrapper, $wrapArr);
584
        // Show pagebrowser
585
        if ($showResultCount != 2) {
586
            if ($pagefloat > -1) {
587
                $lastPage = min($totalPages, max($pointer + 1 + $pagefloat, $maxPages));
588
                $firstPage = max(0, $lastPage - $maxPages);
589
            } else {
590
                $firstPage = 0;
591
                $lastPage = MathUtility::forceIntegerInRange($totalPages, 1, $maxPages);
0 ignored issues
show
Bug introduced by
$totalPages of type double is incompatible with the type integer expected by parameter $theInt of TYPO3\CMS\Core\Utility\M...::forceIntegerInRange(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

591
                $lastPage = MathUtility::forceIntegerInRange(/** @scrutinizer ignore-type */ $totalPages, 1, $maxPages);
Loading history...
592
            }
593
            $links = [];
594
            // Make browse-table/links:
595
            // Link to first page
596
            if ($showFirstLast) {
597
                if ($pointer > 0) {
598
                    $label = $this->pi_getLL('pi_list_browseresults_first', '<< First');
599
                    $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($hscText ? htmlspecialchars($label) : $label, [$pointerName => null], $pi_isOnlyFields), $wrapper['inactiveLinkWrap']);
600
                } else {
601
                    $label = $this->pi_getLL('pi_list_browseresults_first', '<< First');
602
                    $links[] = $this->cObj->wrap($hscText ? htmlspecialchars($label) : $label, $wrapper['disabledLinkWrap']);
603
                }
604
            }
605
            // Link to previous page
606
            if ($alwaysPrev >= 0) {
607
                if ($pointer > 0) {
608
                    $label = $this->pi_getLL('pi_list_browseresults_prev', '< Previous');
609
                    $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($hscText ? htmlspecialchars($label) : $label, [$pointerName => ($pointer - 1) ?: ''], $pi_isOnlyFields), $wrapper['inactiveLinkWrap']);
610
                } elseif ($alwaysPrev) {
611
                    $label = $this->pi_getLL('pi_list_browseresults_prev', '< Previous');
612
                    $links[] = $this->cObj->wrap($hscText ? htmlspecialchars($label) : $label, $wrapper['disabledLinkWrap']);
613
                }
614
            }
615
            // Links to pages
616
            for ($a = $firstPage; $a < $lastPage; $a++) {
617
                if ($this->internal['showRange']) {
618
                    $pageText = ($a * $results_at_a_time + 1) . '-' . min($count, ($a + 1) * $results_at_a_time);
619
                } else {
620
                    $label = $this->pi_getLL('pi_list_browseresults_page', 'Page');
621
                    $pageText = trim(($hscText ? htmlspecialchars($label) : $label) . ' ' . ($a + 1));
622
                }
623
                // Current page
624
                if ($pointer == $a) {
625
                    if ($this->internal['dontLinkActivePage']) {
626
                        $links[] = $this->cObj->wrap($pageText, $wrapper['activeLinkWrap']);
627 View Code Duplication
                    } else {
628
                        $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($pageText, [$pointerName => $a ?: ''], $pi_isOnlyFields), $wrapper['activeLinkWrap']);
629
                    }
630 View Code Duplication
                } else {
631
                    $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($pageText, [$pointerName => $a ?: ''], $pi_isOnlyFields), $wrapper['inactiveLinkWrap']);
632
                }
633
            }
634 View Code Duplication
            if ($pointer < $totalPages - 1 || $showFirstLast) {
635
                // Link to next page
636
                if ($pointer >= $totalPages - 1) {
637
                    $label = $this->pi_getLL('pi_list_browseresults_next', 'Next >');
638
                    $links[] = $this->cObj->wrap($hscText ? htmlspecialchars($label) : $label, $wrapper['disabledLinkWrap']);
639
                } else {
640
                    $label = $this->pi_getLL('pi_list_browseresults_next', 'Next >');
641
                    $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($hscText ? htmlspecialchars($label) : $label, [$pointerName => $pointer + 1], $pi_isOnlyFields), $wrapper['inactiveLinkWrap']);
642
                }
643
            }
644
            // Link to last page
645 View Code Duplication
            if ($showFirstLast) {
646
                if ($pointer < $totalPages - 1) {
647
                    $label = $this->pi_getLL('pi_list_browseresults_last', 'Last >>');
648
                    $links[] = $this->cObj->wrap($this->pi_linkTP_keepPIvars($hscText ? htmlspecialchars($label) : $label, [$pointerName => $totalPages - 1], $pi_isOnlyFields), $wrapper['inactiveLinkWrap']);
649
                } else {
650
                    $label = $this->pi_getLL('pi_list_browseresults_last', 'Last >>');
651
                    $links[] = $this->cObj->wrap($hscText ? htmlspecialchars($label) : $label, $wrapper['disabledLinkWrap']);
652
                }
653
            }
654
            $theLinks = $this->cObj->wrap(implode(LF, $links), $wrapper['browseLinksWrap']);
655
        } else {
656
            $theLinks = '';
657
        }
658
        $pR1 = $pointer * $results_at_a_time + 1;
659
        $pR2 = $pointer * $results_at_a_time + $results_at_a_time;
660
        if ($showResultCount) {
661
            if ($wrapper['showResultsNumbersWrap']) {
662
                // This will render the resultcount in a more flexible way using markers (new in TYPO3 3.8.0).
663
                // The formatting string is expected to hold template markers (see function header). Example: 'Displaying results ###FROM### to ###TO### out of ###OUT_OF###'
664
                $markerArray['###FROM###'] = $this->cObj->wrap($this->internal['res_count'] > 0 ? $pR1 : 0, $wrapper['showResultsNumbersWrap']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$markerArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $markerArray = array(); before regardless.
Loading history...
665
                $markerArray['###TO###'] = $this->cObj->wrap(min($this->internal['res_count'], $pR2), $wrapper['showResultsNumbersWrap']);
666
                $markerArray['###OUT_OF###'] = $this->cObj->wrap($this->internal['res_count'], $wrapper['showResultsNumbersWrap']);
667
                $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']);
668
                $markerArray['###CURRENT_PAGE###'] = $this->cObj->wrap($pointer + 1, $wrapper['showResultsNumbersWrap']);
669
                $markerArray['###TOTAL_PAGES###'] = $this->cObj->wrap($totalPages, $wrapper['showResultsNumbersWrap']);
670
                // Substitute markers
671
                $resultCountMsg = $this->templateService->substituteMarkerArray($this->pi_getLL('pi_list_browseresults_displays', 'Displaying results ###FROM### to ###TO### out of ###OUT_OF###'), $markerArray);
672
            } else {
673
                // Render the resultcount in the "traditional" way using sprintf
674
                $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);
675
            }
676
            $resultCountMsg = $this->cObj->wrap($resultCountMsg, $wrapper['showResultsWrap']);
677
        } else {
678
            $resultCountMsg = '';
679
        }
680
        $sTables = $this->cObj->wrap($resultCountMsg . $theLinks, $wrapper['browseBoxWrap']);
681
        return $sTables;
682
    }
683
684
    /**
685
     * Returns a mode selector; a little menu in a table normally put in the top of the page/list.
686
     *
687
     * @param array $items Key/Value pairs for the menu; keys are the piVars[mode] values and the "values" are the labels for them.
688
     * @param string $tableParams Attributes for the table tag which is wrapped around the table cells containing the menu
689
     * @return string Output HTML, wrapped in <div>-tags with a class attribute
690
     */
691
    public function pi_list_modeSelector($items = [], $tableParams = '')
692
    {
693
        $cells = [];
694
        foreach ($items as $k => $v) {
695
            $cells[] = '
696
					<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>';
697
        }
698
        $sTables = '
699
700
		<!--
701
			Mode selector (menu for list):
702
		-->
703
		<div' . $this->pi_classParam('modeSelector') . '>
704
			<' . rtrim('table ' . $tableParams) . '>
705
				<tr>
706
					' . implode('', $cells) . '
707
				</tr>
708
			</table>
709
		</div>';
710
        return $sTables;
711
    }
712
713
    /**
714
     * Returns the list of items based on the input SQL result pointer
715
     * For each result row the internal var, $this->internal['currentRow'], is set with the row returned.
716
     * $this->pi_list_header() makes the header row for the list
717
     * $this->pi_list_row() is used for rendering each row
718
     * 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.
719
     *
720
     * @param Statement $statement Result pointer to a SQL result which can be traversed.
721
     * @param string $tableParams Attributes for the table tag which is wrapped around the table rows containing the list
722
     * @return string Output HTML, wrapped in <div>-tags with a class attribute
723
     * @see pi_list_row(), pi_list_header()
724
     */
725
    public function pi_list_makelist($statement, $tableParams = '')
726
    {
727
        // Make list table header:
728
        $tRows = [];
729
        $this->internal['currentRow'] = '';
730
        $tRows[] = $this->pi_list_header();
731
        // Make list table rows
732
        $c = 0;
733
        while ($this->internal['currentRow'] = $statement->fetch()) {
734
            $tRows[] = $this->pi_list_row($c);
735
            $c++;
736
        }
737
        $out = '
738
739
		<!--
740
			Record list:
741
		-->
742
		<div' . $this->pi_classParam('listrow') . '>
743
			<' . rtrim('table ' . $tableParams) . '>
744
				' . implode('', $tRows) . '
745
			</table>
746
		</div>';
747
        return $out;
748
    }
749
750
    /**
751
     * Returns a list row. Get data from $this->internal['currentRow'];
752
     * (Dummy)
753
     * 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.
754
     *
755
     * @param int $c Row counting. Starts at 0 (zero). Used for alternating class values in the output rows.
756
     * @return string HTML output, a table row with a class attribute set (alternative based on odd/even rows)
757
     */
758
    public function pi_list_row($c)
759
    {
760
        // Dummy
761
        return '<tr' . ($c % 2 ? $this->pi_classParam('listrow-odd') : '') . '><td><p>[dummy row]</p></td></tr>';
762
    }
763
764
    /**
765
     * Returns a list header row.
766
     * (Dummy)
767
     * 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.
768
     *
769
     * @return string HTML output, a table row with a class attribute set
770
     */
771
    public function pi_list_header()
772
    {
773
        return '<tr' . $this->pi_classParam('listrow-header') . '><td><p>[dummy header row]</p></td></tr>';
774
    }
775
776
    /***************************
777
     *
778
     * Stylesheet, CSS
779
     *
780
     **************************/
781
    /**
782
     * Returns a class-name prefixed with $this->prefixId and with all underscores substituted to dashes (-)
783
     *
784
     * @param string $class The class name (or the END of it since it will be prefixed by $this->prefixId.'-')
785
     * @return string The combined class name (with the correct prefix)
786
     */
787
    public function pi_getClassName($class)
788
    {
789
        return str_replace('_', '-', $this->prefixId) . ($this->prefixId ? '-' : '') . $class;
790
    }
791
792
    /**
793
     * Returns the class-attribute with the correctly prefixed classname
794
     * Using pi_getClassName()
795
     *
796
     * @param string $class The class name(s) (suffix) - separate multiple classes with commas
797
     * @param string $addClasses Additional class names which should not be prefixed - separate multiple classes with commas
798
     * @return string A "class" attribute with value and a single space char before it.
799
     * @see pi_getClassName()
800
     */
801
    public function pi_classParam($class, $addClasses = '')
802
    {
803
        $output = '';
804
        $classNames = GeneralUtility::trimExplode(',', $class);
805
        foreach ($classNames as $className) {
806
            $output .= ' ' . $this->pi_getClassName($className);
807
        }
808
        $additionalClassNames = GeneralUtility::trimExplode(',', $addClasses);
809
        foreach ($additionalClassNames as $additionalClassName) {
810
            $output .= ' ' . $additionalClassName;
811
        }
812
        return ' class="' . trim($output) . '"';
813
    }
814
815
    /**
816
     * Wraps the input string in a <div> tag with the class attribute set to the prefixId.
817
     * 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.
818
     *
819
     * @param string $str HTML content to wrap in the div-tags with the "main class" of the plugin
820
     * @return string HTML content wrapped, ready to return to the parent object.
821
     */
822
    public function pi_wrapInBaseClass($str)
823
    {
824
        $content = '<div class="' . str_replace('_', '-', $this->prefixId) . '">
825
		' . $str . '
826
	</div>
827
	';
828
        if (!$this->frontendController->config['config']['disablePrefixComment']) {
829
            $content = '
830
831
832
	<!--
833
834
		BEGIN: Content of extension "' . $this->extKey . '", plugin "' . $this->prefixId . '"
835
836
	-->
837
	' . $content . '
838
	<!-- END: Content of extension "' . $this->extKey . '", plugin "' . $this->prefixId . '" -->
839
840
	';
841
        }
842
        return $content;
843
    }
844
845
    /***************************
846
     *
847
     * Frontend editing: Edit panel, edit icons
848
     *
849
     **************************/
850
    /**
851
     * Returns the Backend User edit panel for the $row from $tablename
852
     *
853
     * @param array $row Record array.
854
     * @param string $tablename Table name
855
     * @param string $label A label to show with the panel.
856
     * @param array $conf TypoScript parameters to pass along to the EDITPANEL content Object that gets rendered. The property "allow" WILL get overridden/set though.
857
     * @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).
858
     * @see ContentObjectRenderer::EDITPANEL()
859
     */
860
    public function pi_getEditPanel($row = [], $tablename = '', $label = '', $conf = [])
861
    {
862
        $panel = '';
863 View Code Duplication
        if (!$row || !$tablename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
864
            $row = $this->internal['currentRow'];
865
            $tablename = $this->internal['currentTable'];
866
        }
867
        if ($this->frontendController->beUserLogin) {
868
            // Create local cObj if not set:
869
            if (!is_object($this->pi_EPtemp_cObj)) {
870
                $this->pi_EPtemp_cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
871
                $this->pi_EPtemp_cObj->setParent($this->cObj->data, $this->cObj->currentRecord);
872
            }
873
            // Initialize the cObj object with current row
874
            $this->pi_EPtemp_cObj->start($row, $tablename);
875
            // Setting TypoScript values in the $conf array. See documentation in TSref for the EDITPANEL cObject.
876
            $conf['allow'] = 'edit,new,delete,move,hide';
877
            $panel = $this->pi_EPtemp_cObj->cObjGetSingle('EDITPANEL', $conf, 'editpanel');
878
        }
879
        if ($panel) {
880
            if ($label) {
881
                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 -->';
882
            }
883
            return '<!-- BEGIN: EDIT PANEL -->' . $panel . '<!-- END: EDIT PANEL -->';
884
        }
885
        return $label;
886
    }
887
888
    /**
889
     * Adds edit-icons to the input content.
890
     * ContentObjectRenderer::editIcons used for rendering
891
     *
892
     * @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)
893
     * @param string $fields The list of fields to edit when the icon is clicked.
894
     * @param string $title Title for the edit icon.
895
     * @param array $row Table record row
896
     * @param string $tablename Table name
897
     * @param array $oConf Conf array
898
     * @return string The processed content
899
     * @see ContentObjectRenderer::editIcons()
900
     */
901
    public function pi_getEditIcon($content, $fields, $title = '', $row = [], $tablename = '', $oConf = [])
902
    {
903
        if ($this->frontendController->beUserLogin) {
904 View Code Duplication
            if (!$row || !$tablename) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
905
                $row = $this->internal['currentRow'];
906
                $tablename = $this->internal['currentTable'];
907
            }
908
            $conf = array_merge([
909
                'beforeLastTag' => 1,
910
                'iconTitle' => $title
911
            ], $oConf);
912
            $content = $this->cObj->editIcons($content, $tablename . ':' . $fields, $conf, $tablename . ':' . $row['uid'], $row, '&viewUrl=' . rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI')));
913
        }
914
        return $content;
915
    }
916
917
    /***************************
918
     *
919
     * Localization, locallang functions
920
     *
921
     **************************/
922
    /**
923
     * Returns the localized label of the LOCAL_LANG key, $key
924
     * Notice that for debugging purposes prefixes for the output values can be set with the internal vars ->LLtestPrefixAlt and ->LLtestPrefix
925
     *
926
     * @param string $key The key from the LOCAL_LANG array for which to return the value.
927
     * @param string $alternativeLabel Alternative string to return IF no value is found set for the key, neither for the local language nor the default.
928
     * @return string The value from LOCAL_LANG.
929
     */
930
    public function pi_getLL($key, $alternativeLabel = '')
931
    {
932
        $word = null;
933
        if (!empty($this->LOCAL_LANG[$this->LLkey][$key][0]['target'])
934
            || isset($this->LOCAL_LANG_UNSET[$this->LLkey][$key])
935
        ) {
936
            $word = $this->LOCAL_LANG[$this->LLkey][$key][0]['target'];
937
        } elseif ($this->altLLkey) {
938
            $alternativeLanguageKeys = GeneralUtility::trimExplode(',', $this->altLLkey, true);
939
            $alternativeLanguageKeys = array_reverse($alternativeLanguageKeys);
940 View Code Duplication
            foreach ($alternativeLanguageKeys as $languageKey) {
941
                if (!empty($this->LOCAL_LANG[$languageKey][$key][0]['target'])
942
                    || isset($this->LOCAL_LANG_UNSET[$languageKey][$key])
943
                ) {
944
                    // Alternative language translation for key exists
945
                    $word = $this->LOCAL_LANG[$languageKey][$key][0]['target'];
946
                    break;
947
                }
948
            }
949
        }
950
        if ($word === null) {
951
            if (!empty($this->LOCAL_LANG['default'][$key][0]['target'])
952
                || isset($this->LOCAL_LANG_UNSET['default'][$key])
953
            ) {
954
                // Get default translation (without charset conversion, english)
955
                $word = $this->LOCAL_LANG['default'][$key][0]['target'];
956
            } else {
957
                // Return alternative string or empty
958
                $word = isset($this->LLtestPrefixAlt) ? $this->LLtestPrefixAlt . $alternativeLabel : $alternativeLabel;
959
            }
960
        }
961
        return isset($this->LLtestPrefix) ? $this->LLtestPrefix . $word : $word;
962
    }
963
964
    /**
965
     * Loads local-language values from the file passed as a parameter or
966
     * by looking for a "locallang" file in the
967
     * plugin class directory ($this->scriptRelPath).
968
     * Also locallang values set in the TypoScript property "_LOCAL_LANG" are
969
     * merged onto the values found in the "locallang" file.
970
     * Supported file extensions xlf, xml
971
     *
972
     * @param string $languageFilePath path to the plugin language file in format EXT:....
973
     */
974
    public function pi_loadLL($languageFilePath = '')
975
    {
976
        if ($this->LOCAL_LANG_loaded) {
977
            return;
978
        }
979
980
        if ($languageFilePath === '' && $this->scriptRelPath) {
981
            $languageFilePath = 'EXT:' . $this->extKey . '/' . dirname($this->scriptRelPath) . '/locallang.xlf';
982
        }
983
        if ($languageFilePath !== '') {
984
            /** @var $languageFactory LocalizationFactory */
985
            $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
986
            $this->LOCAL_LANG = $languageFactory->getParsedData($languageFilePath, $this->LLkey);
987
            $alternativeLanguageKeys = GeneralUtility::trimExplode(',', $this->altLLkey, true);
988 View Code Duplication
            foreach ($alternativeLanguageKeys as $languageKey) {
989
                $tempLL = $languageFactory->getParsedData($languageFilePath, $languageKey);
990
                if ($this->LLkey !== 'default' && isset($tempLL[$languageKey])) {
991
                    $this->LOCAL_LANG[$languageKey] = $tempLL[$languageKey];
992
                }
993
            }
994
            // Overlaying labels from TypoScript (including fictitious language keys for non-system languages!):
995
            if (isset($this->conf['_LOCAL_LANG.'])) {
996
                // Clear the "unset memory"
997
                $this->LOCAL_LANG_UNSET = [];
998
                foreach ($this->conf['_LOCAL_LANG.'] as $languageKey => $languageArray) {
999
                    // Remove the dot after the language key
1000
                    $languageKey = substr($languageKey, 0, -1);
1001
                    // Don't process label if the language is not loaded
1002
                    if (is_array($languageArray) && isset($this->LOCAL_LANG[$languageKey])) {
1003 View Code Duplication
                        foreach ($languageArray as $labelKey => $labelValue) {
1004
                            if (!is_array($labelValue)) {
1005
                                $this->LOCAL_LANG[$languageKey][$labelKey][0]['target'] = $labelValue;
1006
                                if ($labelValue === '') {
1007
                                    $this->LOCAL_LANG_UNSET[$languageKey][$labelKey] = '';
1008
                                }
1009
                            }
1010
                        }
1011
                    }
1012
                }
1013
            }
1014
        }
1015
        $this->LOCAL_LANG_loaded = true;
1016
    }
1017
1018
    /***************************
1019
     *
1020
     * Database, queries
1021
     *
1022
     **************************/
1023
    /**
1024
     * 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'])
1025
     * Set $count to 1 if you wish to get a count(*) query for selecting the number of results.
1026
     * Notice that the query will use $this->conf['pidList'] and $this->conf['recursive'] to generate a PID list within which to search for records.
1027
     *
1028
     * @param string $table The table name to make the query for.
1029
     * @param bool $count If set, you will get a "count(*)" query back instead of field selecting
1030
     * @param string $addWhere Additional WHERE clauses (should be starting with " AND ....")
1031
     * @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.
1032
     * @param string $groupBy If set, this is added as a " GROUP BY ...." part of the query.
1033
     * @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']
1034
     * @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!
1035
     * @return Statement
1036
     */
1037
    public function pi_exec_query($table, $count = false, $addWhere = '', $mm_cat = '', $groupBy = '', $orderBy = '', $query = '')
1038
    {
1039
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1040
        $queryBuilder->from($table);
1041
1042
        // Begin Query:
1043
        if (!$query) {
1044
            // This adds WHERE-clauses that ensures deleted, hidden, starttime/endtime/access records are NOT
1045
            // selected, if they should not! Almost ALWAYS add this to your queries!
1046
            $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1047
1048
            // Fetches the list of PIDs to select from.
1049
            // TypoScript property .pidList is a comma list of pids. If blank, current page id is used.
1050
            // 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.
1051
            $pidList = GeneralUtility::intExplode(',', $this->pi_getPidList($this->conf['pidList'], $this->conf['recursive']), true);
1052
            if (is_array($mm_cat)) {
1053
                $queryBuilder->from($mm_cat['table'])
1054
                    ->from($mm_cat['mmtable'])
1055
                    ->where(
1056
                        $queryBuilder->expr()->eq($table . '.uid', $queryBuilder->quoteIdentifier($mm_cat['mmtable'] . '.uid_local')),
1057
                        $queryBuilder->expr()->eq($mm_cat['table'] . '.uid', $queryBuilder->quoteIdentifier($mm_cat['mmtable'] . '.uid_foreign')),
1058
                        $queryBuilder->expr()->in(
1059
                            $table . '.pid',
1060
                            $queryBuilder->createNamedParameter($pidList, Connection::PARAM_INT_ARRAY)
1061
                        )
1062
                    );
1063
                if (strcmp($mm_cat['catUidList'], '')) {
1064
                    $queryBuilder->andWhere(
1065
                        $queryBuilder->expr()->in(
1066
                            $mm_cat['table'] . '.uid',
1067
                            $queryBuilder->createNamedParameter(
1068
                                GeneralUtility::intExplode(',', $mm_cat['catUidList'], true),
1069
                                Connection::PARAM_INT_ARRAY
1070
                            )
1071
                        )
1072
                    );
1073
                }
1074
            } else {
1075
                $queryBuilder->where(
1076
                    $queryBuilder->expr()->in(
1077
                        'pid',
1078
                        $queryBuilder->createNamedParameter($pidList, Connection::PARAM_INT_ARRAY)
1079
                    )
1080
                );
1081
            }
1082
        } else {
1083
            // Restrictions need to be handled by the $query parameter!
1084
            $queryBuilder->getRestrictions()->removeAll();
1085
1086
            // Split the "FROM ... WHERE" string so we get the WHERE part and TABLE names separated...:
1087
            list($tableListFragment, $whereFragment) = preg_split('/WHERE/i', trim($query), 2);
1088
            foreach (QueryHelper::parseTableList($tableListFragment) as $tableNameAndAlias) {
1089
                list($tableName, $tableAlias) = $tableNameAndAlias;
1090
                $queryBuilder->from($tableName, $tableAlias);
1091
            }
1092
            $queryBuilder->where(QueryHelper::stripLogicalOperatorPrefix($whereFragment));
1093
        }
1094
1095
        // Add '$addWhere'
1096
        if ($addWhere) {
1097
            $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($addWhere));
1098
        }
1099
        // Search word:
1100
        if ($this->piVars['sword'] && $this->internal['searchFieldList']) {
1101
            $queryBuilder->andWhere(
1102
                QueryHelper::stripLogicalOperatorPrefix(
1103
                    $this->cObj->searchWhere($this->piVars['sword'], $this->internal['searchFieldList'], $table)
1104
                )
1105
            );
1106
        }
1107
1108
        if ($count) {
1109
            $queryBuilder->count('*');
1110
        } else {
1111
            // Add 'SELECT'
1112
            $fields = $this->pi_prependFieldsWithTable($table, $this->pi_listFields);
1113
            $queryBuilder->select(...GeneralUtility::trimExplode(',', $fields, true));
1114
1115
            // Order by data:
1116
            if (!$orderBy && $this->internal['orderBy']) {
1117
                if (GeneralUtility::inList($this->internal['orderByList'], $this->internal['orderBy'])) {
1118
                    $sorting = $this->internal['descFlag'] ? ' DESC' : 'ASC';
1119
                    $queryBuilder->orderBy($table . '.' . $this->internal['orderBy'], $sorting);
1120
                }
1121 View Code Duplication
            } elseif ($orderBy) {
1122
                foreach (QueryHelper::parseOrderBy($orderBy) as $fieldNameAndSorting) {
1123
                    list($fieldName, $sorting) = $fieldNameAndSorting;
1124
                    $queryBuilder->addOrderBy($fieldName, $sorting);
1125
                }
1126
            }
1127
1128
            // Limit data:
1129
            $pointer = (int)$this->piVars['pointer'];
1130
            $results_at_a_time = MathUtility::forceIntegerInRange($this->internal['results_at_a_time'], 1, 1000);
1131
            $queryBuilder->setFirstResult($pointer * $results_at_a_time)
1132
                ->setMaxResults($results_at_a_time);
1133
1134
            // Grouping
1135
            if (!empty($groupBy)) {
1136
                $queryBuilder->groupBy(...QueryHelper::parseGroupBy($groupBy));
1137
            }
1138
        }
1139
1140
        return $queryBuilder->execute();
1141
    }
1142
1143
    /**
1144
     * Returns the row $uid from $table
1145
     * (Simply calling $this->frontendEngine->sys_page->checkRecord())
1146
     *
1147
     * @param string $table The table name
1148
     * @param int $uid The uid of the record from the table
1149
     * @param bool $checkPage If $checkPage is set, it's required that the page on which the record resides is accessible
1150
     * @return array If record is found, an array. Otherwise FALSE.
1151
     */
1152
    public function pi_getRecord($table, $uid, $checkPage = false)
1153
    {
1154
        return $this->frontendController->sys_page->checkRecord($table, $uid, $checkPage);
1155
    }
1156
1157
    /**
1158
     * Returns a commalist of page ids for a query (eg. 'WHERE pid IN (...)')
1159
     *
1160
     * @param string $pid_list A comma list of page ids (if empty current page is used)
1161
     * @param int $recursive An integer >=0 telling how deep to dig for pids under each entry in $pid_list
1162
     * @return string List of PID values (comma separated)
1163
     */
1164
    public function pi_getPidList($pid_list, $recursive = 0)
1165
    {
1166
        if (!strcmp($pid_list, '')) {
1167
            $pid_list = $this->frontendController->id;
1168
        }
1169
        $recursive = MathUtility::forceIntegerInRange($recursive, 0);
1170
        $pid_list_arr = array_unique(GeneralUtility::trimExplode(',', $pid_list, true));
1171
        $pid_list = [];
1172
        foreach ($pid_list_arr as $val) {
1173
            $val = MathUtility::forceIntegerInRange($val, 0);
1174
            if ($val) {
1175
                $_list = $this->cObj->getTreeList(-1 * $val, $recursive);
1176
                if ($_list) {
1177
                    $pid_list[] = $_list;
1178
                }
1179
            }
1180
        }
1181
        return implode(',', $pid_list);
1182
    }
1183
1184
    /**
1185
     * Having a comma list of fields ($fieldList) this is prepended with the $table.'.' name
1186
     *
1187
     * @param string $table Table name to prepend
1188
     * @param string $fieldList List of fields where each element will be prepended with the table name given.
1189
     * @return string List of fields processed.
1190
     */
1191
    public function pi_prependFieldsWithTable($table, $fieldList)
1192
    {
1193
        $list = GeneralUtility::trimExplode(',', $fieldList, true);
1194
        $return = [];
1195
        foreach ($list as $listItem) {
1196
            $return[] = $table . '.' . $listItem;
1197
        }
1198
        return implode(',', $return);
1199
    }
1200
1201
    /**
1202
     * Will select all records from the "category table", $table, and return them in an array.
1203
     *
1204
     * @param string $table The name of the category table to select from.
1205
     * @param int $pid The page from where to select the category records.
1206
     * @param string $whereClause Optional additional WHERE clauses put in the end of the query. DO NOT PUT IN GROUP BY, ORDER BY or LIMIT!
1207
     * @param string $groupBy Optional GROUP BY field(s), if none, supply blank string.
1208
     * @param string $orderBy Optional ORDER BY field(s), if none, supply blank string.
1209
     * @param string $limit Optional LIMIT value ([begin,]max), if none, supply blank string.
1210
     * @return array The array with the category records in.
1211
     */
1212
    public function pi_getCategoryTableContents($table, $pid, $whereClause = '', $groupBy = '', $orderBy = '', $limit = '')
1213
    {
1214
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
1215
        $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
1216
        $queryBuilder->select('*')
1217
            ->from($table)
1218
            ->where(
1219
                $queryBuilder->expr()->eq(
1220
                    'pid',
1221
                    $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
1222
                ),
1223
                QueryHelper::stripLogicalOperatorPrefix($whereClause)
1224
            );
1225
1226 View Code Duplication
        if (!empty($orderBy)) {
1227
            foreach (QueryHelper::parseOrderBy($orderBy) as $fieldNameAndSorting) {
1228
                list($fieldName, $sorting) = $fieldNameAndSorting;
1229
                $queryBuilder->addOrderBy($fieldName, $sorting);
1230
            }
1231
        }
1232
1233
        if (!empty($groupBy)) {
1234
            $queryBuilder->groupBy(...QueryHelper::parseGroupBy($groupBy));
1235
        }
1236
1237
        if (!empty($limit)) {
1238
            $limitValues = GeneralUtility::intExplode(',', $limit, true);
1239
            if (count($limitValues) === 1) {
1240
                $queryBuilder->setMaxResults($limitValues[0]);
1241
            } else {
1242
                $queryBuilder->setFirstResult($limitValues[0])
1243
                    ->setMaxResults($limitValues[1]);
1244
            }
1245
        }
1246
1247
        $result = $queryBuilder->execute();
1248
        $outArr = [];
1249
        while ($row = $result->fetch()) {
1250
            $outArr[$row['uid']] = $row;
1251
        }
1252
        return $outArr;
1253
    }
1254
1255
    /***************************
1256
     *
1257
     * Various
1258
     *
1259
     **************************/
1260
    /**
1261
     * 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.
1262
     * Notice that this function will only work as long as values are integers.
1263
     *
1264
     * @param string $fList List of fields (keys from piVars) to evaluate on
1265
     * @param int $lowerThan Limit for the values.
1266
     * @return bool|null Returns TRUE (1) if conditions are met.
1267
     */
1268
    public function pi_isOnlyFields($fList, $lowerThan = -1)
1269
    {
1270
        $lowerThan = $lowerThan == -1 ? $this->pi_lowerThan : $lowerThan;
1271
        $fList = GeneralUtility::trimExplode(',', $fList, true);
1272
        $tempPiVars = $this->piVars;
1273
        foreach ($fList as $k) {
1274
            if (!MathUtility::canBeInterpretedAsInteger($tempPiVars[$k]) || $tempPiVars[$k] < $lowerThan) {
1275
                unset($tempPiVars[$k]);
1276
            }
1277
        }
1278
        if (empty($tempPiVars)) {
1279
            //@TODO: How do we deal with this? return TRUE would be the right thing to do here but that might be breaking
1280
            return 1;
1281
        }
1282
        return null;
1283
    }
1284
1285
    /**
1286
     * Returns TRUE if the array $inArray contains only values allowed to be cached based on the configuration in $this->pi_autoCacheFields
1287
     * Used by ->pi_linkTP_keepPIvars
1288
     * This is an advanced form of evaluation of whether a URL should be cached or not.
1289
     *
1290
     * @param array $inArray An array with piVars values to evaluate
1291
     * @return bool|null Returns TRUE (1) if conditions are met.
1292
     * @see pi_linkTP_keepPIvars()
1293
     */
1294
    public function pi_autoCache($inArray)
1295
    {
1296
        if (is_array($inArray)) {
1297
            foreach ($inArray as $fN => $fV) {
1298
                if (!strcmp($inArray[$fN], '')) {
1299
                    unset($inArray[$fN]);
1300
                } elseif (is_array($this->pi_autoCacheFields[$fN])) {
1301
                    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]) {
1302
                        unset($inArray[$fN]);
1303
                    }
1304
                    if (is_array($this->pi_autoCacheFields[$fN]['list']) && in_array($inArray[$fN], $this->pi_autoCacheFields[$fN]['list'])) {
1305
                        unset($inArray[$fN]);
1306
                    }
1307
                }
1308
            }
1309
        }
1310
        if (empty($inArray)) {
1311
            //@TODO: How do we deal with this? return TRUE would be the right thing to do here but that might be breaking
1312
            return 1;
1313
        }
1314
        return null;
1315
    }
1316
1317
    /**
1318
     * Will process the input string with the parseFunc function from ContentObjectRenderer based on configuration
1319
     * set in "lib.parseFunc_RTE" in the current TypoScript template.
1320
     *
1321
     * @param string $str The input text string to process
1322
     * @return string The processed string
1323
     * @see ContentObjectRenderer::parseFunc()
1324
     */
1325
    public function pi_RTEcssText($str)
1326
    {
1327
        $parseFunc = $this->frontendController->tmpl->setup['lib.']['parseFunc_RTE.'];
1328
        if (is_array($parseFunc)) {
1329
            $str = $this->cObj->parseFunc($str, $parseFunc);
1330
        }
1331
        return $str;
1332
    }
1333
1334
    /*******************************
1335
     *
1336
     * FlexForms related functions
1337
     *
1338
     *******************************/
1339
    /**
1340
     * Converts $this->cObj->data['pi_flexform'] from XML string to flexForm array.
1341
     *
1342
     * @param string $field Field name to convert
1343
     */
1344
    public function pi_initPIflexForm($field = 'pi_flexform')
1345
    {
1346
        // Converting flexform data into array:
1347
        if (!is_array($this->cObj->data[$field]) && $this->cObj->data[$field]) {
1348
            $this->cObj->data[$field] = GeneralUtility::xml2array($this->cObj->data[$field]);
1349
            if (!is_array($this->cObj->data[$field])) {
1350
                $this->cObj->data[$field] = [];
1351
            }
1352
        }
1353
    }
1354
1355
    /**
1356
     * Return value from somewhere inside a FlexForm structure
1357
     *
1358
     * @param array $T3FlexForm_array FlexForm data
1359
     * @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.
1360
     * @param string $sheet Sheet pointer, eg. "sDEF
1361
     * @param string $lang Language pointer, eg. "lDEF
1362
     * @param string $value Value pointer, eg. "vDEF
1363
     * @return string|null The content.
1364
     */
1365
    public function pi_getFFvalue($T3FlexForm_array, $fieldName, $sheet = 'sDEF', $lang = 'lDEF', $value = 'vDEF')
1366
    {
1367
        $sheetArray = is_array($T3FlexForm_array) ? $T3FlexForm_array['data'][$sheet][$lang] : '';
1368
        if (is_array($sheetArray)) {
1369
            return $this->pi_getFFvalueFromSheetArray($sheetArray, explode('/', $fieldName), $value);
1370
        }
1371
        return null;
1372
    }
1373
1374
    /**
1375
     * Returns part of $sheetArray pointed to by the keys in $fieldNameArray
1376
     *
1377
     * @param array $sheetArray Multidimensiona array, typically FlexForm contents
1378
     * @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...)
1379
     * @param string $value Value for outermost key, typ. "vDEF" depending on language.
1380
     * @return mixed The value, typ. string.
1381
     * @access private
1382
     * @see pi_getFFvalue()
1383
     */
1384
    public function pi_getFFvalueFromSheetArray($sheetArray, $fieldNameArr, $value)
1385
    {
1386
        $tempArr = $sheetArray;
1387
        foreach ($fieldNameArr as $k => $v) {
1388
            if (MathUtility::canBeInterpretedAsInteger($v)) {
1389
                if (is_array($tempArr)) {
1390
                    $c = 0;
1391
                    foreach ($tempArr as $values) {
1392
                        if ($c == $v) {
1393
                            $tempArr = $values;
1394
                            break;
1395
                        }
1396
                        $c++;
1397
                    }
1398
                }
1399
            } else {
1400
                $tempArr = $tempArr[$v];
1401
            }
1402
        }
1403
        return $tempArr[$value];
1404
    }
1405
}
1406