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