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