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