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