[FEATURE] Introduce AJAX functionality for list module
[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 $rowParams Is insert in the <tr>-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 * @param string $colType Defines the tag being used for the columns. Default is td.
204 * @return string HTML content for the table row
205 */
206 public function addElement($h, $icon, $data, $rowParams = '', $lMargin = '', $altLine = '', $colType = 'td') {
207 $colType = ($colType === 'th') ? 'th' : 'td';
208 $noWrap = $this->no_noWrap ? '' : ' nowrap="nowrap"';
209 // Start up:
210 $out = '
211 <!-- Element, begin: -->
212 <tr ' . $rowParams . ' data-uid="' . (int)$data['uid'] . '">';
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 // Traverse field array which contains the data to present:
239 foreach ($this->fieldArray as $vKey) {
240 if (isset($data[$vKey])) {
241 if ($lastKey) {
242 $cssClass = $this->addElement_tdCssClass[$lastKey];
243 if ($this->oddColumnsCssClass && $ccount % 2 == 0) {
244 $cssClass = implode(' ', array($this->addElement_tdCssClass[$lastKey], $this->oddColumnsCssClass));
245 }
246 $out .= '
247 <' . $colType . $noWrap . ' class="' . $cssClass . '"' . $colsp . $this->addElement_tdParams[$lastKey] . '>' . $data[$lastKey] . '</' . $colType .'>';
248 }
249 $lastKey = $vKey;
250 $c = 1;
251 $ccount++;
252 } else {
253 if (!$lastKey) {
254 $lastKey = $vKey;
255 }
256 $c++;
257 }
258 if ($c > 1) {
259 $colsp = ' colspan="' . $c . '"';
260 } else {
261 $colsp = '';
262 }
263 }
264 if ($lastKey) {
265 $cssClass = $this->addElement_tdCssClass[$lastKey];
266 if ($this->oddColumnsCssClass) {
267 $cssClass = implode(' ', array($this->addElement_tdCssClass[$lastKey], $this->oddColumnsCssClass));
268 }
269 $out .= '
270 <' . $colType . $noWrap . ' class="' . $cssClass . '"' . $colsp . $this->addElement_tdParams[$lastKey] . '>' . $data[$lastKey] . '</' . $colType . '>';
271 }
272 // End row
273 $out .= '
274 </tr>';
275 // Return row.
276 return $out;
277 }
278
279 /**
280 * Dummy function, used to write the top of a table listing.
281 *
282 * @return void
283 */
284 public function writeTop() {
285
286 }
287
288 /**
289 * Creates a forward/reverse button based on the status of ->eCounter, ->firstElementNumber, ->iLimit
290 *
291 * @param string $table Table name
292 * @return array array([boolean], [HTML]) where [boolean] is 1 for reverse element, [HTML] is the table-row code for the element
293 */
294 public function fwd_rwd_nav($table = '') {
295 $code = '';
296 if ($this->eCounter >= $this->firstElementNumber && $this->eCounter < $this->firstElementNumber + $this->iLimit) {
297 if ($this->firstElementNumber && $this->eCounter == $this->firstElementNumber) {
298 // Reverse
299 $theData = array();
300 $titleCol = $this->fieldArray[0];
301 $theData[$titleCol] = $this->fwd_rwd_HTML('fwd', $this->eCounter, $table);
302 $code = $this->addElement(1, '', $theData, 'class="fwd_rwd_nav"');
303 }
304 return array(1, $code);
305 } else {
306 if ($this->eCounter == $this->firstElementNumber + $this->iLimit) {
307 // Forward
308 $theData = array();
309 $titleCol = $this->fieldArray[0];
310 $theData[$titleCol] = $this->fwd_rwd_HTML('rwd', $this->eCounter, $table);
311 $code = $this->addElement(1, '', $theData, 'class="fwd_rwd_nav"');
312 }
313 return array(0, $code);
314 }
315 }
316
317 /**
318 * Creates the button with link to either forward or reverse
319 *
320 * @param string $type Type: "fwd" or "rwd
321 * @param int $pointer Pointer
322 * @param string $table Table name
323 * @return string
324 * @access private
325 */
326 public function fwd_rwd_HTML($type, $pointer, $table = '') {
327 $content = '';
328 $tParam = $table ? '&table=' . rawurlencode($table) : '';
329 switch ($type) {
330 case 'fwd':
331 $href = $this->listURL() . '&pointer=' . ($pointer - $this->iLimit) . $tParam;
332 $content = '<a href="' . htmlspecialchars($href) . '">' . IconUtility::getSpriteIcon('actions-move-up') . '</a> <i>[1 - ' . $pointer . ']</i>';
333 break;
334 case 'rwd':
335 $href = $this->listURL() . '&pointer=' . $pointer . $tParam;
336 $content = '<a href="' . htmlspecialchars($href) . '">' . IconUtility::getSpriteIcon('actions-move-down') . '</a> <i>[' . ($pointer + 1) . ' - ' . $this->totalItems . ']</i>';
337 break;
338 }
339 return $content;
340 }
341
342 /**
343 * Creates the URL to this script, including all relevant GPvars
344 *
345 * @param string $altId Alternative id value. Enter blank string for the current id ($this->id)
346 * @return string URL
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 */
357 public function CBfunctions() {
358 return '
359 // checkOffCB()
360 function checkOffCB(listOfCBnames, link) { //
361 var checkBoxes, flag, i;
362 var checkBoxes = listOfCBnames.split(",");
363 if (link.rel === "") {
364 link.rel = "allChecked";
365 flag = true;
366 } else {
367 link.rel = "";
368 flag = false;
369 }
370 for (i = 0; i < checkBoxes.length; i++) {
371 setcbValue(checkBoxes[i], flag);
372 }
373 }
374 // cbValue()
375 function cbValue(CBname) { //
376 var CBfullName = "CBC["+CBname+"]";
377 return (document.dblistForm[CBfullName] && document.dblistForm[CBfullName].checked ? 1 : 0);
378 }
379 // setcbValue()
380 function setcbValue(CBname,flag) { //
381 CBfullName = "CBC["+CBname+"]";
382 if(document.dblistForm[CBfullName]) {
383 document.dblistForm[CBfullName].checked = flag ? "on" : 0;
384 }
385 }
386
387 ';
388 }
389
390 /**
391 * Initializes page languages and icons
392 *
393 * @return void
394 */
395 public function initializeLanguages() {
396 // Look up page overlays:
397 $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');
398 $this->languageIconTitles = $this->getTranslateTools()->getSystemLanguages($this->id, $this->backPath);
399 }
400
401 /**
402 * Return the icon for the language
403 *
404 * @param int $sys_language_uid Sys language uid
405 * @param bool $addAsAdditionalText If set to true, only the flag is returned
406 * @return string Language icon
407 */
408 public function languageFlag($sys_language_uid, $addAsAdditionalText = TRUE) {
409 $out = '';
410 $title = htmlspecialchars($this->languageIconTitles[$sys_language_uid]['title']);
411 if ($this->languageIconTitles[$sys_language_uid]['flagIcon']) {
412 $out .= IconUtility::getSpriteIcon($this->languageIconTitles[$sys_language_uid]['flagIcon'], array('title' => $title));
413 if (!$addAsAdditionalText) {
414 return $out;
415 }
416 $out .= '&nbsp;';
417 }
418 $out .= $title;
419 return $out;
420 }
421
422 /**
423 * Gets an instance of TranslationConfigurationProvider
424 *
425 * @return \TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider
426 */
427 protected function getTranslateTools() {
428 if (!isset($this->translateTools)) {
429 $this->translateTools = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider::class);
430 }
431 return $this->translateTools;
432 }
433
434 /**
435 * Generates HTML code for a Reference tooltip out of
436 * sys_refindex records you hand over
437 *
438 * @param array $references array of records from sys_refindex table
439 * @param string $launchViewParameter JavaScript String, which will be passed as parameters to top.launchView
440 * @return string
441 */
442 protected function generateReferenceToolTip(array $references, $launchViewParameter = '') {
443 $result = array();
444 foreach ($references as $reference) {
445 $result[] = $reference['tablename'] . ':' . $reference['recuid'] . ':' . $reference['field'];
446 if (strlen(implode(' / ', $result)) >= 100) {
447 break;
448 }
449 }
450 if (empty($result)) {
451 $htmlCode = '-';
452 } else {
453 $htmlCode = '<a href="#"';
454 if ($launchViewParameter !== '') {
455 $htmlCode .= ' onclick="' . htmlspecialchars(('top.launchView(' . $launchViewParameter . '); return false;')) . '"';
456 }
457 $htmlCode .= ' title="' . htmlspecialchars(GeneralUtility::fixed_lgd_cs(implode(' / ', $result), 100)) . '">';
458 $htmlCode .= count($references);
459 $htmlCode .= '</a>';
460 }
461 return $htmlCode;
462 }
463
464 }