[FOLLOWUP][TASK] Make TYPO3 Core PSR-2 standard compliant
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / RecordList / AbstractRecordList.php
1 <?php
2 namespace TYPO3\CMS\Backend\RecordList;
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\Backend\Configuration\TranslationConfigurationProvider;
18 use TYPO3\CMS\Backend\Routing\Router;
19 use TYPO3\CMS\Backend\Routing\UriBuilder;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Database\DatabaseConnection;
22 use TYPO3\CMS\Core\Imaging\Icon;
23 use TYPO3\CMS\Core\Imaging\IconFactory;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Lang\LanguageService;
26
27 /**
28 * Library with a single function addElement that returns table
29 * rows based on some input.
30 *
31 * Base for class listing of database records and files in the
32 * modules Web>List and File>Filelist
33 * @see typo3/db_list.php
34 * @see typo3/sysext/filelist/mod1/index.php
35 */
36 abstract class AbstractRecordList
37 {
38 /**
39 * @var int
40 */
41 protected $id = 0;
42
43 /**
44 * default Max items shown
45 *
46 * @var int
47 */
48 public $iLimit = 10;
49
50 /**
51 * OBSOLETE - NOT USED ANYMORE. leftMargin
52 *
53 * @var int
54 */
55 public $leftMargin = 0;
56
57 /**
58 * @var int
59 */
60 public $showIcon = 1;
61
62 /**
63 * @var int
64 */
65 public $no_noWrap = 0;
66
67 /**
68 * If set this is <td> CSS-classname for odd columns in addElement. Used with db_layout / pages section
69 *
70 * @var string
71 */
72 public $oddColumnsCssClass = '';
73
74 /**
75 * Decides the columns shown. Filled with values that refers to the keys of the data-array. $this->fieldArray[0] is the title column.
76 *
77 * @var array
78 */
79 public $fieldArray = array();
80
81 /**
82 * Keys are fieldnames and values are td-parameters to add in addElement(), please use $addElement_tdCSSClass for CSS-classes;
83 *
84 * @var array
85 */
86 public $addElement_tdParams = array();
87
88 /**
89 * Keys are fieldnames and values are td-css-classes to add in addElement();
90 *
91 * @var array
92 */
93 public $addElement_tdCssClass = array();
94
95 /**
96 * Not used in this class - but maybe extension classes...
97 * Max length of strings
98 *
99 * @var int
100 */
101 public $fixedL = 30;
102
103 /**
104 * Script URL
105 *
106 * @var string
107 */
108 public $thisScript = '';
109
110 /**
111 * Set to zero, if you don't want a left-margin with addElement function
112 *
113 * @var int
114 */
115 public $setLMargin = 1;
116
117 /**
118 * Counter increased for each element. Used to index elements for the JavaScript-code that transfers to the clipboard
119 *
120 * @var int
121 */
122 public $counter = 0;
123
124 /**
125 * This could be set to the total number of items. Used by the fwd_rew_navigation...
126 *
127 * @var string
128 */
129 public $totalItems = '';
130
131 /**
132 * Internal (used in this class.)
133 *
134 * @var int
135 */
136 public $firstElementNumber = 0;
137
138 /**
139 * @var int
140 */
141 public $eCounter = 0;
142
143 /**
144 * String with accumulated HTML content
145 *
146 * @var string
147 */
148 public $HTMLcode = '';
149
150 /**
151 * Contains page translation languages
152 *
153 * @var array
154 */
155 public $pageOverlays = array();
156
157 /**
158 * Contains sys language icons and titles
159 *
160 * @var array
161 */
162 public $languageIconTitles = array();
163
164 /**
165 * @var TranslationConfigurationProvider
166 */
167 public $translateTools;
168
169 /**
170 * Constructor
171 */
172 public function __construct()
173 {
174 if (isset($GLOBALS['BE_USER']->uc['titleLen']) && $GLOBALS['BE_USER']->uc['titleLen'] > 0) {
175 $this->fixedL = $GLOBALS['BE_USER']->uc['titleLen'];
176 }
177 $this->getTranslateTools();
178 $this->determineScriptUrl();
179 }
180
181 /**
182 * Sets the script url depending on being a module or script request
183 */
184 protected function determineScriptUrl()
185 {
186 if ($routePath = GeneralUtility::_GP('route')) {
187 $router = GeneralUtility::makeInstance(Router::class);
188 $route = $router->match($routePath);
189 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
190 $this->thisScript = (string)$uriBuilder->buildUriFromRoute($route->getOption('_identifier'));
191 } elseif ($moduleName = GeneralUtility::_GP('M')) {
192 $this->thisScript = BackendUtility::getModuleUrl($moduleName);
193 } else {
194 $this->thisScript = GeneralUtility::getIndpEnv('SCRIPT_NAME');
195 }
196 }
197
198 /**
199 * @return string
200 */
201 protected function getThisScript()
202 {
203 return strpos($this->thisScript, '?') === false ? $this->thisScript . '?' : $this->thisScript . '&';
204 }
205
206 /**
207 * Returns a table-row with the content from the fields in the input data array.
208 * OBS: $this->fieldArray MUST be set! (represents the list of fields to display)
209 *
210 * @param int $h Is an integer >=0 and denotes how tall an element is. Set to '0' makes a halv line, -1 = full line, set to 1 makes a 'join' and above makes 'line'
211 * @param string $icon Is the <img>+<a> of the record. If not supplied the first 'join'-icon will be a 'line' instead
212 * @param array $data Is the dataarray, record with the fields. Notice: These fields are (currently) NOT htmlspecialchar'ed before being wrapped in <td>-tags
213 * @param string $rowParams Is insert in the <tr>-tags. Must carry a ' ' as first character
214 * @param string $_ OBSOLETE - NOT USED ANYMORE. $lMargin is the leftMargin (int)
215 * @param string $_2 OBSOLETE - NOT USED ANYMORE. Is the HTML <img>-tag for an alternative 'gfx/ol/line.gif'-icon (used in the top)
216 * @param string $colType Defines the tag being used for the columns. Default is td.
217 * @return string HTML content for the table row
218 */
219 public function addElement($h, $icon, $data, $rowParams = '', $_ = '', $_2 = '', $colType = 'td')
220 {
221 $colType = ($colType === 'th') ? 'th' : 'td';
222 $noWrap = $this->no_noWrap ? '' : ' nowrap="nowrap"';
223 // Start up:
224 $parent = isset($data['parent']) ? (int)$data['parent'] : 0;
225 $out = '
226 <!-- Element, begin: -->
227 <tr ' . $rowParams . ' data-uid="' . (int)$data['uid'] . '" data-l10nparent="' . $parent . '">';
228 // Show icon and lines
229 if ($this->showIcon) {
230 $out .= '
231 <' . $colType . ' nowrap="nowrap" class="col-icon">';
232 if (!$h) {
233 $out .= '&nbsp;';
234 } else {
235 for ($a = 0; $a < $h; $a++) {
236 if (!$a) {
237 if ($icon) {
238 $out .= $icon;
239 }
240 } else {
241 }
242 }
243 }
244 $out .= '</' . $colType . '>
245 ';
246 }
247 // Init rendering.
248 $colsp = '';
249 $lastKey = '';
250 $c = 0;
251 $ccount = 0;
252 // __label is used as the label key to circumvent problems with uid used as label (see #67756)
253 // as it was introduced later on, check if it really exists before using it
254 $fields = $this->fieldArray;
255 if ($colType === 'td' && array_key_exists('__label', $data)) {
256 $fields[0] = '__label';
257 }
258 // Traverse field array which contains the data to present:
259 foreach ($fields as $vKey) {
260 if (isset($data[$vKey])) {
261 if ($lastKey) {
262 $cssClass = $this->addElement_tdCssClass[$lastKey];
263 if ($this->oddColumnsCssClass && $ccount % 2 == 0) {
264 $cssClass = implode(' ', array($this->addElement_tdCssClass[$lastKey], $this->oddColumnsCssClass));
265 }
266 $out .= '
267 <' . $colType . $noWrap . ' class="' . $cssClass . '"' . $colsp . $this->addElement_tdParams[$lastKey] . '>' . $data[$lastKey] . '</' . $colType . '>';
268 }
269 $lastKey = $vKey;
270 $c = 1;
271 $ccount++;
272 } else {
273 if (!$lastKey) {
274 $lastKey = $vKey;
275 }
276 $c++;
277 }
278 if ($c > 1) {
279 $colsp = ' colspan="' . $c . '"';
280 } else {
281 $colsp = '';
282 }
283 }
284 if ($lastKey) {
285 $cssClass = $this->addElement_tdCssClass[$lastKey];
286 if ($this->oddColumnsCssClass) {
287 $cssClass = implode(' ', array($this->addElement_tdCssClass[$lastKey], $this->oddColumnsCssClass));
288 }
289 $out .= '
290 <' . $colType . $noWrap . ' class="' . $cssClass . '"' . $colsp . $this->addElement_tdParams[$lastKey] . '>' . $data[$lastKey] . '</' . $colType . '>';
291 }
292 // End row
293 $out .= '
294 </tr>';
295 // Return row.
296 return $out;
297 }
298
299 /**
300 * Dummy function, used to write the top of a table listing.
301 *
302 * @return void
303 */
304 public function writeTop()
305 {
306 }
307
308 /**
309 * Creates a forward/reverse button based on the status of ->eCounter, ->firstElementNumber, ->iLimit
310 *
311 * @param string $table Table name
312 * @return array array([boolean], [HTML]) where [boolean] is 1 for reverse element, [HTML] is the table-row code for the element
313 */
314 public function fwd_rwd_nav($table = '')
315 {
316 $code = '';
317 if ($this->eCounter >= $this->firstElementNumber && $this->eCounter < $this->firstElementNumber + $this->iLimit) {
318 if ($this->firstElementNumber && $this->eCounter == $this->firstElementNumber) {
319 // Reverse
320 $theData = array();
321 $titleCol = $this->fieldArray[0];
322 $theData[$titleCol] = $this->fwd_rwd_HTML('fwd', $this->eCounter, $table);
323 $code = $this->addElement(1, '', $theData, 'class="fwd_rwd_nav"');
324 }
325 return array(1, $code);
326 } else {
327 if ($this->eCounter == $this->firstElementNumber + $this->iLimit) {
328 // Forward
329 $theData = array();
330 $titleCol = $this->fieldArray[0];
331 $theData[$titleCol] = $this->fwd_rwd_HTML('rwd', $this->eCounter, $table);
332 $code = $this->addElement(1, '', $theData, 'class="fwd_rwd_nav"');
333 }
334 return array(0, $code);
335 }
336 }
337
338 /**
339 * Creates the button with link to either forward or reverse
340 *
341 * @param string $type Type: "fwd" or "rwd
342 * @param int $pointer Pointer
343 * @param string $table Table name
344 * @return string
345 * @access private
346 */
347 public function fwd_rwd_HTML($type, $pointer, $table = '')
348 {
349 $content = '';
350 $tParam = $table ? '&table=' . rawurlencode($table) : '';
351 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
352 switch ($type) {
353 case 'fwd':
354 $href = $this->listURL() . '&pointer=' . ($pointer - $this->iLimit) . $tParam;
355 $content = '<a href="' . htmlspecialchars($href) . '">' . $iconFactory->getIcon('actions-move-up', Icon::SIZE_SMALL)->render() . '</a> <i>[1 - ' . $pointer . ']</i>';
356 break;
357 case 'rwd':
358 $href = $this->listURL() . '&pointer=' . $pointer . $tParam;
359 $content = '<a href="' . htmlspecialchars($href) . '">' . $iconFactory->getIcon('actions-move-down', Icon::SIZE_SMALL)->render() . '</a> <i>[' . ($pointer + 1) . ' - ' . $this->totalItems . ']</i>';
360 break;
361 }
362 return $content;
363 }
364
365 /**
366 * Creates the URL to this script, including all relevant GPvars
367 *
368 * @param string $altId Alternative id value. Enter blank string for the current id ($this->id)
369 * @return string URL
370 */
371 public function listURL($altId = '')
372 {
373 return $this->getThisScript() . 'id=' . ($altId !== '' ? $altId : $this->id);
374 }
375
376 /**
377 * Returning JavaScript for ClipBoard functionality.
378 *
379 * @return string
380 */
381 public function CBfunctions()
382 {
383 return '
384 // checkOffCB()
385 function checkOffCB(listOfCBnames, link) { //
386 var checkBoxes, flag, i;
387 var checkBoxes = listOfCBnames.split(",");
388 if (link.rel === "") {
389 link.rel = "allChecked";
390 flag = true;
391 } else {
392 link.rel = "";
393 flag = false;
394 }
395 for (i = 0; i < checkBoxes.length; i++) {
396 setcbValue(checkBoxes[i], flag);
397 }
398 }
399 // cbValue()
400 function cbValue(CBname) { //
401 var CBfullName = "CBC["+CBname+"]";
402 return (document.dblistForm[CBfullName] && document.dblistForm[CBfullName].checked ? 1 : 0);
403 }
404 // setcbValue()
405 function setcbValue(CBname,flag) { //
406 CBfullName = "CBC["+CBname+"]";
407 if(document.dblistForm[CBfullName]) {
408 document.dblistForm[CBfullName].checked = flag ? "on" : 0;
409 }
410 }
411
412 ';
413 }
414
415 /**
416 * Initializes page languages and icons
417 *
418 * @return void
419 */
420 public function initializeLanguages()
421 {
422 // Look up page overlays:
423 $this->pageOverlays = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'pages_language_overlay', 'pid=' . (int)$this->id . BackendUtility::deleteClause('pages_language_overlay') . BackendUtility::versioningPlaceholderClause('pages_language_overlay'), '', '', '', 'sys_language_uid');
424 $this->languageIconTitles = $this->getTranslateTools()->getSystemLanguages($this->id);
425 }
426
427 /**
428 * Return the icon for the language
429 *
430 * @param int $sys_language_uid Sys language uid
431 * @param bool $addAsAdditionalText If set to true, only the flag is returned
432 * @return string Language icon
433 */
434 public function languageFlag($sys_language_uid, $addAsAdditionalText = true)
435 {
436 $out = '';
437 $title = htmlspecialchars($this->languageIconTitles[$sys_language_uid]['title']);
438 if ($this->languageIconTitles[$sys_language_uid]['flagIcon']) {
439 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
440 $out .= '<span title="' . $title . '">' . $iconFactory->getIcon($this->languageIconTitles[$sys_language_uid]['flagIcon'], Icon::SIZE_SMALL)->render() . '</span>';
441 if (!$addAsAdditionalText) {
442 return $out;
443 }
444 $out .= '&nbsp;';
445 }
446 $out .= $title;
447 return $out;
448 }
449
450 /**
451 * Gets an instance of TranslationConfigurationProvider
452 *
453 * @return TranslationConfigurationProvider
454 */
455 protected function getTranslateTools()
456 {
457 if (!isset($this->translateTools)) {
458 $this->translateTools = GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
459 }
460 return $this->translateTools;
461 }
462
463 /**
464 * Generates HTML code for a Reference tooltip out of
465 * sys_refindex records you hand over
466 *
467 * @param int $references number of records from sys_refindex table
468 * @param string $launchViewParameter JavaScript String, which will be passed as parameters to top.launchView
469 * @return string
470 */
471 protected function generateReferenceToolTip($references, $launchViewParameter = '')
472 {
473 if (!$references) {
474 $htmlCode = '-';
475 } else {
476 $htmlCode = '<a href="#"';
477 if ($launchViewParameter !== '') {
478 $htmlCode .= ' onclick="' . htmlspecialchars(('top.launchView(' . $launchViewParameter . '); return false;')) . '"';
479 }
480 $htmlCode .= ' title="' . htmlspecialchars($this->getLanguageService()->sl('LLL:EXT:backend/Resources/Private/Language/locallang.xlf:show_references') . ' (' . $references . ')') . '">';
481 $htmlCode .= $references;
482 $htmlCode .= '</a>';
483 }
484 return $htmlCode;
485 }
486
487 /**
488 * Returns the language service
489 * @return LanguageService
490 */
491 protected function getLanguageService()
492 {
493 return $GLOBALS['LANG'];
494 }
495
496 /**
497 * Returns the database connection
498 *
499 * @return DatabaseConnection
500 */
501 protected function getDatabaseConnection()
502 {
503 return $GLOBALS['TYPO3_DB'];
504 }
505 }