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