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