91a86b9e8ddbabcd290e6bf139cb2ccb12abf89a
[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\ConnectionPool;
22 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
23 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
24 use TYPO3\CMS\Core\Imaging\Icon;
25 use TYPO3\CMS\Core\Imaging\IconFactory;
26 use TYPO3\CMS\Core\Localization\LanguageService;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * Library with a single function addElement that returns table
31 * rows based on some input.
32 *
33 * Base for class listing of database records and files in the
34 * modules Web>List and File>Filelist
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 = [];
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 = [];
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 = [];
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 = [];
156
157 /**
158 * Contains sys language icons and titles
159 *
160 * @var array
161 */
162 public $languageIconTitles = [];
163
164 /**
165 * @var TranslationConfigurationProvider
166 */
167 public $translateTools;
168
169 /**
170 * @var IconFactory
171 */
172 protected $iconFactory;
173
174 /**
175 * Constructor
176 */
177 public function __construct()
178 {
179 if (isset($GLOBALS['BE_USER']->uc['titleLen']) && $GLOBALS['BE_USER']->uc['titleLen'] > 0) {
180 $this->fixedL = $GLOBALS['BE_USER']->uc['titleLen'];
181 }
182 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
183 $this->getTranslateTools();
184 $this->determineScriptUrl();
185 }
186
187 /**
188 * Sets the script url depending on being a module or script request
189 */
190 protected function determineScriptUrl()
191 {
192 if ($routePath = GeneralUtility::_GP('route')) {
193 $router = GeneralUtility::makeInstance(Router::class);
194 $route = $router->match($routePath);
195 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
196 $this->thisScript = (string)$uriBuilder->buildUriFromRoute($route->getOption('_identifier'));
197 } elseif ($moduleName = GeneralUtility::_GP('M')) {
198 $this->thisScript = BackendUtility::getModuleUrl($moduleName);
199 } else {
200 $this->thisScript = GeneralUtility::getIndpEnv('SCRIPT_NAME');
201 }
202 }
203
204 /**
205 * @return string
206 */
207 protected function getThisScript()
208 {
209 return strpos($this->thisScript, '?') === false ? $this->thisScript . '?' : $this->thisScript . '&';
210 }
211
212 /**
213 * Returns a table-row with the content from the fields in the input data array.
214 * OBS: $this->fieldArray MUST be set! (represents the list of fields to display)
215 *
216 * @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'
217 * @param string $icon Is the <img>+<a> of the record. If not supplied the first 'join'-icon will be a 'line' instead
218 * @param array $data Is the dataarray, record with the fields. Notice: These fields are (currently) NOT htmlspecialchar'ed before being wrapped in <td>-tags
219 * @param string $rowParams Is insert in the <tr>-tags. Must carry a ' ' as first character
220 * @param string $_ OBSOLETE - NOT USED ANYMORE. $lMargin is the leftMargin (int)
221 * @param string $_2 OBSOLETE - NOT USED ANYMORE. Is the HTML <img>-tag for an alternative 'gfx/ol/line.gif'-icon (used in the top)
222 * @param string $colType Defines the tag being used for the columns. Default is td.
223 * @return string HTML content for the table row
224 */
225 public function addElement($h, $icon, $data, $rowParams = '', $_ = '', $_2 = '', $colType = 'td')
226 {
227 $colType = ($colType === 'th') ? 'th' : 'td';
228 $noWrap = $this->no_noWrap ? '' : ' nowrap';
229 // Start up:
230 $l10nParent = isset($data['_l10nparent_']) ? (int)$data['_l10nparent_'] : 0;
231 $out = '
232 <!-- Element, begin: -->
233 <tr ' . $rowParams . ' data-uid="' . (int)$data['uid'] . '" data-l10nparent="' . $l10nParent . '">';
234 // Show icon and lines
235 if ($this->showIcon) {
236 $out .= '
237 <' . $colType . ' class="col-icon nowrap">';
238 if (!$h) {
239 $out .= '&nbsp;';
240 } else {
241 for ($a = 0; $a < $h; $a++) {
242 if (!$a) {
243 if ($icon) {
244 $out .= $icon;
245 }
246 }
247 }
248 }
249 $out .= '</' . $colType . '>
250 ';
251 }
252 // Init rendering.
253 $colsp = '';
254 $lastKey = '';
255 $c = 0;
256 $ccount = 0;
257 // __label is used as the label key to circumvent problems with uid used as label (see #67756)
258 // as it was introduced later on, check if it really exists before using it
259 $fields = $this->fieldArray;
260 if ($colType === 'td' && array_key_exists('__label', $data)) {
261 $fields[0] = '__label';
262 }
263 // Traverse field array which contains the data to present:
264 foreach ($fields as $vKey) {
265 if (isset($data[$vKey])) {
266 if ($lastKey) {
267 $cssClass = $this->addElement_tdCssClass[$lastKey];
268 if ($this->oddColumnsCssClass && $ccount % 2 == 0) {
269 $cssClass = implode(' ', [$this->addElement_tdCssClass[$lastKey], $this->oddColumnsCssClass]);
270 }
271 $out .= '
272 <' . $colType . ' class="' . $cssClass . $noWrap . '"' . $colsp . $this->addElement_tdParams[$lastKey] . '>' . $data[$lastKey] . '</' . $colType . '>';
273 }
274 $lastKey = $vKey;
275 $c = 1;
276 $ccount++;
277 } else {
278 if (!$lastKey) {
279 $lastKey = $vKey;
280 }
281 $c++;
282 }
283 if ($c > 1) {
284 $colsp = ' colspan="' . $c . '"';
285 } else {
286 $colsp = '';
287 }
288 }
289 if ($lastKey) {
290 $cssClass = $this->addElement_tdCssClass[$lastKey];
291 if ($this->oddColumnsCssClass) {
292 $cssClass = implode(' ', [$this->addElement_tdCssClass[$lastKey], $this->oddColumnsCssClass]);
293 }
294 $out .= '
295 <' . $colType . ' class="' . $cssClass . $noWrap . '"' . $colsp . $this->addElement_tdParams[$lastKey] . '>' . $data[$lastKey] . '</' . $colType . '>';
296 }
297 // End row
298 $out .= '
299 </tr>';
300 // Return row.
301 return $out;
302 }
303
304 /**
305 * Dummy function, used to write the top of a table listing.
306 */
307 public function writeTop()
308 {
309 }
310
311 /**
312 * Creates a forward/reverse button based on the status of ->eCounter, ->firstElementNumber, ->iLimit
313 *
314 * @param string $table Table name
315 * @return array array([boolean], [HTML]) where [boolean] is 1 for reverse element, [HTML] is the table-row code for the element
316 */
317 public function fwd_rwd_nav($table = '')
318 {
319 $code = '';
320 if ($this->eCounter >= $this->firstElementNumber && $this->eCounter < $this->firstElementNumber + $this->iLimit) {
321 if ($this->firstElementNumber && $this->eCounter == $this->firstElementNumber) {
322 // Reverse
323 $theData = [];
324 $titleCol = $this->fieldArray[0];
325 $theData[$titleCol] = $this->fwd_rwd_HTML('fwd', $this->eCounter, $table);
326 $code = $this->addElement(1, '', $theData, 'class="fwd_rwd_nav"');
327 }
328 return [1, $code];
329 } else {
330 if ($this->eCounter == $this->firstElementNumber + $this->iLimit) {
331 // Forward
332 $theData = [];
333 $titleCol = $this->fieldArray[0];
334 $theData[$titleCol] = $this->fwd_rwd_HTML('rwd', $this->eCounter, $table);
335 $code = $this->addElement(1, '', $theData, 'class="fwd_rwd_nav"');
336 }
337 return [0, $code];
338 }
339 }
340
341 /**
342 * Creates the button with link to either forward or reverse
343 *
344 * @param string $type Type: "fwd" or "rwd
345 * @param int $pointer Pointer
346 * @param string $table Table name
347 * @return string
348 * @access private
349 */
350 public function fwd_rwd_HTML($type, $pointer, $table = '')
351 {
352 $content = '';
353 $tParam = $table ? '&table=' . rawurlencode($table) : '';
354 switch ($type) {
355 case 'fwd':
356 $href = $this->listURL() . '&pointer=' . ($pointer - $this->iLimit) . $tParam;
357 $content = '<a href="' . htmlspecialchars($href) . '">' . $this->iconFactory->getIcon('actions-move-up', Icon::SIZE_SMALL)->render() . '</a> <i>[1 - ' . $pointer . ']</i>';
358 break;
359 case 'rwd':
360 $href = $this->listURL() . '&pointer=' . $pointer . $tParam;
361 $content = '<a href="' . htmlspecialchars($href) . '">' . $this->iconFactory->getIcon('actions-move-down', Icon::SIZE_SMALL)->render() . '</a> <i>[' . ($pointer + 1) . ' - ' . $this->totalItems . ']</i>';
362 break;
363 }
364 return $content;
365 }
366
367 /**
368 * Creates the URL to this script, including all relevant GPvars
369 *
370 * @param string $altId Alternative id value. Enter blank string for the current id ($this->id)
371 * @param string $table Table name to display. Enter "-1" for the current table.
372 * @param string $exclList Comma separated list of fields NOT to include ("sortField", "sortRev" or "firstElementNumber")
373 * @return string URL
374 */
375 public function listURL($altId = '', $table = '-1', $exclList = '')
376 {
377 return $this->getThisScript() . 'id=' . ($altId !== '' ? $altId : $this->id);
378 }
379
380 /**
381 * Returning JavaScript for ClipBoard functionality.
382 *
383 * @return string
384 */
385 public function CBfunctions()
386 {
387 return '
388 // checkOffCB()
389 function checkOffCB(listOfCBnames, link) { //
390 var checkBoxes, flag, i;
391 var checkBoxes = listOfCBnames.split(",");
392 if (link.rel === "") {
393 link.rel = "allChecked";
394 flag = true;
395 } else {
396 link.rel = "";
397 flag = false;
398 }
399 for (i = 0; i < checkBoxes.length; i++) {
400 setcbValue(checkBoxes[i], flag);
401 }
402 }
403 // cbValue()
404 function cbValue(CBname) { //
405 var CBfullName = "CBC["+CBname+"]";
406 return (document.dblistForm[CBfullName] && document.dblistForm[CBfullName].checked ? 1 : 0);
407 }
408 // setcbValue()
409 function setcbValue(CBname,flag) { //
410 CBfullName = "CBC["+CBname+"]";
411 if(document.dblistForm[CBfullName]) {
412 document.dblistForm[CBfullName].checked = flag ? "on" : 0;
413 }
414 }
415
416 ';
417 }
418
419 /**
420 * Initializes page languages and icons
421 */
422 public function initializeLanguages()
423 {
424 // Look up page overlays:
425 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
426 ->getQueryBuilderForTable('pages_language_overlay');
427 $queryBuilder->getRestrictions()
428 ->removeAll()
429 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
430 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
431 $result = $queryBuilder
432 ->select('*')
433 ->from('pages_language_overlay')
434 ->where(
435 $queryBuilder->expr()->andX(
436 $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)),
437 $queryBuilder->expr()->gt('sys_language_uid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
438 )
439 )
440 ->execute();
441
442 $this->pageOverlays = [];
443 while ($row = $result->fetch()) {
444 $this->pageOverlays[$row['sys_language_uid']] = $row;
445 }
446
447 $this->languageIconTitles = $this->getTranslateTools()->getSystemLanguages($this->id);
448 }
449
450 /**
451 * Return the icon for the language
452 *
453 * @param int $sys_language_uid Sys language uid
454 * @param bool $addAsAdditionalText If set to true, only the flag is returned
455 * @return string Language icon
456 */
457 public function languageFlag($sys_language_uid, $addAsAdditionalText = true)
458 {
459 $out = '';
460 $title = htmlspecialchars($this->languageIconTitles[$sys_language_uid]['title']);
461 if ($this->languageIconTitles[$sys_language_uid]['flagIcon']) {
462 $out .= '<span title="' . $title . '">' . $this->iconFactory->getIcon($this->languageIconTitles[$sys_language_uid]['flagIcon'], Icon::SIZE_SMALL)->render() . '</span>';
463 if (!$addAsAdditionalText) {
464 return $out;
465 }
466 $out .= '&nbsp;';
467 }
468 $out .= $title;
469 return $out;
470 }
471
472 /**
473 * Gets an instance of TranslationConfigurationProvider
474 *
475 * @return TranslationConfigurationProvider
476 */
477 protected function getTranslateTools()
478 {
479 if (!isset($this->translateTools)) {
480 $this->translateTools = GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
481 }
482 return $this->translateTools;
483 }
484
485 /**
486 * Generates HTML code for a Reference tooltip out of
487 * sys_refindex records you hand over
488 *
489 * @param int $references number of records from sys_refindex table
490 * @param string $launchViewParameter JavaScript String, which will be passed as parameters to top.launchView
491 * @return string
492 */
493 protected function generateReferenceToolTip($references, $launchViewParameter = '')
494 {
495 if (!$references) {
496 $htmlCode = '-';
497 } else {
498 $htmlCode = '<a href="#"';
499 if ($launchViewParameter !== '') {
500 $htmlCode .= ' onclick="' . htmlspecialchars(('top.launchView(' . $launchViewParameter . '); return false;')) . '"';
501 }
502 $htmlCode .= ' title="' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang.xlf:show_references') . ' (' . $references . ')') . '">';
503 $htmlCode .= $references;
504 $htmlCode .= '</a>';
505 }
506 return $htmlCode;
507 }
508
509 /**
510 * Returns the language service
511 * @return LanguageService
512 */
513 protected function getLanguageService()
514 {
515 return $GLOBALS['LANG'];
516 }
517 }