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