[BUGFIX] Fade out record translations if main records gets deleted
[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 an 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 $parent = isset($data['parent']) ? (int)$data['parent'] : 0;
210 $out = '
211 <!-- Element, begin: -->
212 <tr ' . $rowParams . ' data-uid="' . (int)$data['uid'] . '" data-l10nparent="' . $parent . '">';
213 // Show icon and lines
214 if ($this->showIcon) {
215 $out .= '
216 <' . $colType . ' nowrap="nowrap" class="col-icon">';
217 if (!$h) {
218 $out .= '&nbsp;';
219 } else {
220 for ($a = 0; $a < $h; $a++) {
221 if (!$a) {
222 if ($icon) {
223 $out .= $icon;
224 }
225 } else {
226
227 }
228 }
229 }
230 $out .= '</' . $colType . '>
231 ';
232 }
233 // Init rendering.
234 $colsp = '';
235 $lastKey = '';
236 $c = 0;
237 $ccount = 0;
238 // __label is used as the label key to circumvent problems with uid used as label (see #67756)
239 // as it was introduced later on, check if it really exists before using it
240 $fields = $this->fieldArray;
241 if ($colType === 'td' && array_key_exists('__label', $data)) {
242 $fields[0] = '__label';
243 }
244 // Traverse field array which contains the data to present:
245 foreach ($fields as $vKey) {
246 if (isset($data[$vKey])) {
247 if ($lastKey) {
248 $cssClass = $this->addElement_tdCssClass[$lastKey];
249 if ($this->oddColumnsCssClass && $ccount % 2 == 0) {
250 $cssClass = implode(' ', array($this->addElement_tdCssClass[$lastKey], $this->oddColumnsCssClass));
251 }
252 $out .= '
253 <' . $colType . $noWrap . ' class="' . $cssClass . '"' . $colsp . $this->addElement_tdParams[$lastKey] . '>' . $data[$lastKey] . '</' . $colType .'>';
254 }
255 $lastKey = $vKey;
256 $c = 1;
257 $ccount++;
258 } else {
259 if (!$lastKey) {
260 $lastKey = $vKey;
261 }
262 $c++;
263 }
264 if ($c > 1) {
265 $colsp = ' colspan="' . $c . '"';
266 } else {
267 $colsp = '';
268 }
269 }
270 if ($lastKey) {
271 $cssClass = $this->addElement_tdCssClass[$lastKey];
272 if ($this->oddColumnsCssClass) {
273 $cssClass = implode(' ', array($this->addElement_tdCssClass[$lastKey], $this->oddColumnsCssClass));
274 }
275 $out .= '
276 <' . $colType . $noWrap . ' class="' . $cssClass . '"' . $colsp . $this->addElement_tdParams[$lastKey] . '>' . $data[$lastKey] . '</' . $colType . '>';
277 }
278 // End row
279 $out .= '
280 </tr>';
281 // Return row.
282 return $out;
283 }
284
285 /**
286 * Dummy function, used to write the top of a table listing.
287 *
288 * @return void
289 */
290 public function writeTop() {
291
292 }
293
294 /**
295 * Creates a forward/reverse button based on the status of ->eCounter, ->firstElementNumber, ->iLimit
296 *
297 * @param string $table Table name
298 * @return array array([boolean], [HTML]) where [boolean] is 1 for reverse element, [HTML] is the table-row code for the element
299 */
300 public function fwd_rwd_nav($table = '') {
301 $code = '';
302 if ($this->eCounter >= $this->firstElementNumber && $this->eCounter < $this->firstElementNumber + $this->iLimit) {
303 if ($this->firstElementNumber && $this->eCounter == $this->firstElementNumber) {
304 // Reverse
305 $theData = array();
306 $titleCol = $this->fieldArray[0];
307 $theData[$titleCol] = $this->fwd_rwd_HTML('fwd', $this->eCounter, $table);
308 $code = $this->addElement(1, '', $theData, 'class="fwd_rwd_nav"');
309 }
310 return array(1, $code);
311 } else {
312 if ($this->eCounter == $this->firstElementNumber + $this->iLimit) {
313 // Forward
314 $theData = array();
315 $titleCol = $this->fieldArray[0];
316 $theData[$titleCol] = $this->fwd_rwd_HTML('rwd', $this->eCounter, $table);
317 $code = $this->addElement(1, '', $theData, 'class="fwd_rwd_nav"');
318 }
319 return array(0, $code);
320 }
321 }
322
323 /**
324 * Creates the button with link to either forward or reverse
325 *
326 * @param string $type Type: "fwd" or "rwd
327 * @param int $pointer Pointer
328 * @param string $table Table name
329 * @return string
330 * @access private
331 */
332 public function fwd_rwd_HTML($type, $pointer, $table = '') {
333 $content = '';
334 $tParam = $table ? '&table=' . rawurlencode($table) : '';
335 switch ($type) {
336 case 'fwd':
337 $href = $this->listURL() . '&pointer=' . ($pointer - $this->iLimit) . $tParam;
338 $content = '<a href="' . htmlspecialchars($href) . '">' . IconUtility::getSpriteIcon('actions-move-up') . '</a> <i>[1 - ' . $pointer . ']</i>';
339 break;
340 case 'rwd':
341 $href = $this->listURL() . '&pointer=' . $pointer . $tParam;
342 $content = '<a href="' . htmlspecialchars($href) . '">' . IconUtility::getSpriteIcon('actions-move-down') . '</a> <i>[' . ($pointer + 1) . ' - ' . $this->totalItems . ']</i>';
343 break;
344 }
345 return $content;
346 }
347
348 /**
349 * Creates the URL to this script, including all relevant GPvars
350 *
351 * @param string $altId Alternative id value. Enter blank string for the current id ($this->id)
352 * @return string URL
353 */
354 public function listURL($altId = '') {
355 return $this->getThisScript() . 'id=' . ($altId !== '' ? $altId : $this->id);
356 }
357
358 /**
359 * Returning JavaScript for ClipBoard functionality.
360 *
361 * @return string
362 */
363 public function CBfunctions() {
364 return '
365 // checkOffCB()
366 function checkOffCB(listOfCBnames, link) { //
367 var checkBoxes, flag, i;
368 var checkBoxes = listOfCBnames.split(",");
369 if (link.rel === "") {
370 link.rel = "allChecked";
371 flag = true;
372 } else {
373 link.rel = "";
374 flag = false;
375 }
376 for (i = 0; i < checkBoxes.length; i++) {
377 setcbValue(checkBoxes[i], flag);
378 }
379 }
380 // cbValue()
381 function cbValue(CBname) { //
382 var CBfullName = "CBC["+CBname+"]";
383 return (document.dblistForm[CBfullName] && document.dblistForm[CBfullName].checked ? 1 : 0);
384 }
385 // setcbValue()
386 function setcbValue(CBname,flag) { //
387 CBfullName = "CBC["+CBname+"]";
388 if(document.dblistForm[CBfullName]) {
389 document.dblistForm[CBfullName].checked = flag ? "on" : 0;
390 }
391 }
392
393 ';
394 }
395
396 /**
397 * Initializes page languages and icons
398 *
399 * @return void
400 */
401 public function initializeLanguages() {
402 // Look up page overlays:
403 $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');
404 $this->languageIconTitles = $this->getTranslateTools()->getSystemLanguages($this->id);
405 }
406
407 /**
408 * Return the icon for the language
409 *
410 * @param int $sys_language_uid Sys language uid
411 * @param bool $addAsAdditionalText If set to true, only the flag is returned
412 * @return string Language icon
413 */
414 public function languageFlag($sys_language_uid, $addAsAdditionalText = TRUE) {
415 $out = '';
416 $title = htmlspecialchars($this->languageIconTitles[$sys_language_uid]['title']);
417 if ($this->languageIconTitles[$sys_language_uid]['flagIcon']) {
418 $out .= IconUtility::getSpriteIcon($this->languageIconTitles[$sys_language_uid]['flagIcon'], array('title' => $title));
419 if (!$addAsAdditionalText) {
420 return $out;
421 }
422 $out .= '&nbsp;';
423 }
424 $out .= $title;
425 return $out;
426 }
427
428 /**
429 * Gets an instance of TranslationConfigurationProvider
430 *
431 * @return \TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider
432 */
433 protected function getTranslateTools() {
434 if (!isset($this->translateTools)) {
435 $this->translateTools = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider::class);
436 }
437 return $this->translateTools;
438 }
439
440 /**
441 * Generates HTML code for a Reference tooltip out of
442 * sys_refindex records you hand over
443 *
444 * @param int $references number of records from sys_refindex table
445 * @param string $launchViewParameter JavaScript String, which will be passed as parameters to top.launchView
446 * @return string
447 */
448 protected function generateReferenceToolTip($references, $launchViewParameter = '') {
449 if (!$references) {
450 $htmlCode = '-';
451 } else {
452 $htmlCode = '<a href="#"';
453 if ($launchViewParameter !== '') {
454 $htmlCode .= ' onclick="' . htmlspecialchars(('top.launchView(' . $launchViewParameter . '); return false;')) . '"';
455 }
456 $htmlCode .= ' title="' . htmlspecialchars($this->getLanguageService()->sl('LLL:EXT:backend/Resources/Private/Language/locallang.xlf:show_references') . ' (' . $references . ')') . '">';
457 $htmlCode .= $references;
458 $htmlCode .= '</a>';
459 }
460 return $htmlCode;
461 }
462
463 /**
464 * Returns the language service
465 * @return LanguageService
466 */
467 protected function getLanguageService() {
468 return $GLOBALS['LANG'];
469 }
470
471 }