[CLEANUP] Ensure variables initalized and fix code smell
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Database / QueryGenerator.php
1 <?php
2 namespace TYPO3\CMS\Core\Database;
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\Module\BaseScriptClass;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20 use TYPO3\CMS\Core\Database\Query\QueryHelper;
21 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\StringUtility;
24 use TYPO3\CMS\Lang\LanguageService;
25
26 /**
27 * Class for generating front end for building queries
28 */
29 class QueryGenerator
30 {
31 /**
32 * @var array
33 */
34 public $lang = [
35 'OR' => 'or',
36 'AND' => 'and',
37 'comparison' => [
38 // Type = text offset = 0
39 '0_' => 'contains',
40 '1_' => 'does not contain',
41 '2_' => 'starts with',
42 '3_' => 'does not start with',
43 '4_' => 'ends with',
44 '5_' => 'does not end with',
45 '6_' => 'equals',
46 '7_' => 'does not equal',
47 // Type = number , offset = 32
48 '32_' => 'equals',
49 '33_' => 'does not equal',
50 '34_' => 'is greater than',
51 '35_' => 'is less than',
52 '36_' => 'is between',
53 '37_' => 'is not between',
54 '38_' => 'is in list',
55 '39_' => 'is not in list',
56 '40_' => 'binary AND equals',
57 '41_' => 'binary AND does not equal',
58 '42_' => 'binary OR equals',
59 '43_' => 'binary OR does not equal',
60 // Type = multiple, relation, files , offset = 64
61 '64_' => 'equals',
62 '65_' => 'does not equal',
63 '66_' => 'contains',
64 '67_' => 'does not contain',
65 '68_' => 'is in list',
66 '69_' => 'is not in list',
67 '70_' => 'binary AND equals',
68 '71_' => 'binary AND does not equal',
69 '72_' => 'binary OR equals',
70 '73_' => 'binary OR does not equal',
71 // Type = date,time offset = 96
72 '96_' => 'equals',
73 '97_' => 'does not equal',
74 '98_' => 'is greater than',
75 '99_' => 'is less than',
76 '100_' => 'is between',
77 '101_' => 'is not between',
78 '102_' => 'binary AND equals',
79 '103_' => 'binary AND does not equal',
80 '104_' => 'binary OR equals',
81 '105_' => 'binary OR does not equal',
82 // Type = boolean, offset = 128
83 '128_' => 'is True',
84 '129_' => 'is False',
85 // Type = binary , offset = 160
86 '160_' => 'equals',
87 '161_' => 'does not equal',
88 '162_' => 'contains',
89 '163_' => 'does not contain'
90 ]
91 ];
92
93 /**
94 * @var array
95 */
96 public $compSQL = [
97 // Type = text offset = 0
98 '0' => '#FIELD# LIKE \'%#VALUE#%\'',
99 '1' => '#FIELD# NOT LIKE \'%#VALUE#%\'',
100 '2' => '#FIELD# LIKE \'#VALUE#%\'',
101 '3' => '#FIELD# NOT LIKE \'#VALUE#%\'',
102 '4' => '#FIELD# LIKE \'%#VALUE#\'',
103 '5' => '#FIELD# NOT LIKE \'%#VALUE#\'',
104 '6' => '#FIELD# = \'#VALUE#\'',
105 '7' => '#FIELD# != \'#VALUE#\'',
106 // Type = number, offset = 32
107 '32' => '#FIELD# = \'#VALUE#\'',
108 '33' => '#FIELD# != \'#VALUE#\'',
109 '34' => '#FIELD# > #VALUE#',
110 '35' => '#FIELD# < #VALUE#',
111 '36' => '#FIELD# >= #VALUE# AND #FIELD# <= #VALUE1#',
112 '37' => 'NOT (#FIELD# >= #VALUE# AND #FIELD# <= #VALUE1#)',
113 '38' => '#FIELD# IN (#VALUE#)',
114 '39' => '#FIELD# NOT IN (#VALUE#)',
115 '40' => '(#FIELD# & #VALUE#)=#VALUE#',
116 '41' => '(#FIELD# & #VALUE#)!=#VALUE#',
117 '42' => '(#FIELD# | #VALUE#)=#VALUE#',
118 '43' => '(#FIELD# | #VALUE#)!=#VALUE#',
119 // Type = multiple, relation, files , offset = 64
120 '64' => '#FIELD# = \'#VALUE#\'',
121 '65' => '#FIELD# != \'#VALUE#\'',
122 '66' => '#FIELD# LIKE \'%#VALUE#%\' AND #FIELD# LIKE \'%#VALUE1#%\'',
123 '67' => '(#FIELD# NOT LIKE \'%#VALUE#%\' OR #FIELD# NOT LIKE \'%#VALUE1#%\')',
124 '68' => '#FIELD# IN (#VALUE#)',
125 '69' => '#FIELD# NOT IN (#VALUE#)',
126 '70' => '(#FIELD# & #VALUE#)=#VALUE#',
127 '71' => '(#FIELD# & #VALUE#)!=#VALUE#',
128 '72' => '(#FIELD# | #VALUE#)=#VALUE#',
129 '73' => '(#FIELD# | #VALUE#)!=#VALUE#',
130 // Type = date, offset = 32
131 '96' => '#FIELD# = \'#VALUE#\'',
132 '97' => '#FIELD# != \'#VALUE#\'',
133 '98' => '#FIELD# > #VALUE#',
134 '99' => '#FIELD# < #VALUE#',
135 '100' => '#FIELD# >= #VALUE# AND #FIELD# <= #VALUE1#',
136 '101' => 'NOT (#FIELD# >= #VALUE# AND #FIELD# <= #VALUE1#)',
137 '102' => '(#FIELD# & #VALUE#)=#VALUE#',
138 '103' => '(#FIELD# & #VALUE#)!=#VALUE#',
139 '104' => '(#FIELD# | #VALUE#)=#VALUE#',
140 '105' => '(#FIELD# | #VALUE#)!=#VALUE#',
141 // Type = boolean, offset = 128
142 '128' => '#FIELD# = \'1\'',
143 '129' => '#FIELD# != \'1\'',
144 // Type = binary = 160
145 '160' => '#FIELD# = \'#VALUE#\'',
146 '161' => '#FIELD# != \'#VALUE#\'',
147 '162' => '(#FIELD# & #VALUE#)=#VALUE#',
148 '163' => '(#FIELD# & #VALUE#)=0'
149 ];
150
151 /**
152 * @var array
153 */
154 public $comp_offsets = [
155 'text' => 0,
156 'number' => 1,
157 'multiple' => 2,
158 'relation' => 2,
159 'files' => 2,
160 'date' => 3,
161 'time' => 3,
162 'boolean' => 4,
163 'binary' => 5
164 ];
165
166 /**
167 * @var string
168 */
169 public $noWrap = ' nowrap';
170
171 /**
172 * Form data name prefix
173 *
174 * @var string
175 */
176 public $name;
177
178 /**
179 * Table for the query
180 *
181 * @var string
182 */
183 public $table;
184
185 /**
186 * @var array
187 */
188 public $tableArray;
189
190 /**
191 * Field list
192 *
193 * @var string
194 */
195 public $fieldList;
196
197 /**
198 * Array of the fields possible
199 *
200 * @var array
201 */
202 public $fields = [];
203
204 /**
205 * @var array
206 */
207 public $extFieldLists = [];
208
209 /**
210 * The query config
211 *
212 * @var array
213 */
214 public $queryConfig = [];
215
216 /**
217 * @var bool
218 */
219 public $enablePrefix = false;
220
221 /**
222 * @var bool
223 */
224 public $enableQueryParts = false;
225
226 /**
227 * @var string
228 */
229 protected $formName = '';
230
231 /**
232 * @var int
233 */
234 protected $limitBegin;
235
236 /**
237 * @var int
238 */
239 protected $limitLength;
240
241 /**
242 * @var string
243 */
244 protected $fieldName;
245
246 /**
247 * Make a list of fields for current table
248 *
249 * @return string Separated list of fields
250 */
251 public function makeFieldList()
252 {
253 $fieldListArr = [];
254 if (is_array($GLOBALS['TCA'][$this->table])) {
255 $fieldListArr = array_keys($GLOBALS['TCA'][$this->table]['columns']);
256 $fieldListArr[] = 'uid';
257 $fieldListArr[] = 'pid';
258 $fieldListArr[] = 'deleted';
259 if ($GLOBALS['TCA'][$this->table]['ctrl']['tstamp']) {
260 $fieldListArr[] = $GLOBALS['TCA'][$this->table]['ctrl']['tstamp'];
261 }
262 if ($GLOBALS['TCA'][$this->table]['ctrl']['crdate']) {
263 $fieldListArr[] = $GLOBALS['TCA'][$this->table]['ctrl']['crdate'];
264 }
265 if ($GLOBALS['TCA'][$this->table]['ctrl']['cruser_id']) {
266 $fieldListArr[] = $GLOBALS['TCA'][$this->table]['ctrl']['cruser_id'];
267 }
268 if ($GLOBALS['TCA'][$this->table]['ctrl']['sortby']) {
269 $fieldListArr[] = $GLOBALS['TCA'][$this->table]['ctrl']['sortby'];
270 }
271 }
272 return implode(',', $fieldListArr);
273 }
274
275 /**
276 * Init function
277 *
278 * @param string $name The name
279 * @param string $table The table name
280 * @param string $fieldList The field list
281 * @return void
282 */
283 public function init($name, $table, $fieldList = '')
284 {
285 // Analysing the fields in the table.
286 if (is_array($GLOBALS['TCA'][$table])) {
287 $this->name = $name;
288 $this->table = $table;
289 $this->fieldList = $fieldList ? $fieldList : $this->makeFieldList();
290 $fieldArr = GeneralUtility::trimExplode(',', $this->fieldList, true);
291 foreach ($fieldArr as $fieldName) {
292 $fC = $GLOBALS['TCA'][$this->table]['columns'][$fieldName];
293 $this->fields[$fieldName] = $fC['config'];
294 $this->fields[$fieldName]['exclude'] = $fC['exclude'];
295 if (is_array($fC) && $fC['label']) {
296 $this->fields[$fieldName]['label'] = rtrim(trim($this->getLanguageService()->sL($fC['label'])), ':');
297 switch ($this->fields[$fieldName]['type']) {
298 case 'input':
299 if (preg_match('/int|year/i', $this->fields[$fieldName]['eval'])) {
300 $this->fields[$fieldName]['type'] = 'number';
301 } elseif (preg_match('/time/i', $this->fields[$fieldName]['eval'])) {
302 $this->fields[$fieldName]['type'] = 'time';
303 } elseif (preg_match('/date/i', $this->fields[$fieldName]['eval'])) {
304 $this->fields[$fieldName]['type'] = 'date';
305 } else {
306 $this->fields[$fieldName]['type'] = 'text';
307 }
308 break;
309 case 'check':
310 if (!$this->fields[$fieldName]['items'] || count($this->fields[$fieldName]['items']) <= 1) {
311 $this->fields[$fieldName]['type'] = 'boolean';
312 } else {
313 $this->fields[$fieldName]['type'] = 'binary';
314 }
315 break;
316 case 'radio':
317 $this->fields[$fieldName]['type'] = 'multiple';
318 break;
319 case 'select':
320 $this->fields[$fieldName]['type'] = 'multiple';
321 if ($this->fields[$fieldName]['foreign_table']) {
322 $this->fields[$fieldName]['type'] = 'relation';
323 }
324 if ($this->fields[$fieldName]['special']) {
325 $this->fields[$fieldName]['type'] = 'text';
326 }
327 break;
328 case 'group':
329 $this->fields[$fieldName]['type'] = 'files';
330 if ($this->fields[$fieldName]['internal_type'] === 'db') {
331 $this->fields[$fieldName]['type'] = 'relation';
332 }
333 break;
334 case 'user':
335 case 'flex':
336 case 'passthrough':
337 case 'none':
338 case 'text':
339 default:
340 $this->fields[$fieldName]['type'] = 'text';
341 }
342 } else {
343 $this->fields[$fieldName]['label'] = '[FIELD: ' . $fieldName . ']';
344 switch ($fieldName) {
345 case 'pid':
346 $this->fields[$fieldName]['type'] = 'relation';
347 $this->fields[$fieldName]['allowed'] = 'pages';
348 break;
349 case 'cruser_id':
350 $this->fields[$fieldName]['type'] = 'relation';
351 $this->fields[$fieldName]['allowed'] = 'be_users';
352 break;
353 case 'tstamp':
354 case 'crdate':
355 $this->fields[$fieldName]['type'] = 'time';
356 break;
357 case 'deleted':
358 $this->fields[$fieldName]['type'] = 'boolean';
359 break;
360 default:
361 $this->fields[$fieldName]['type'] = 'number';
362 }
363 }
364 }
365 }
366 /* // EXAMPLE:
367 $this->queryConfig = array(
368 array(
369 'operator' => 'AND',
370 'type' => 'FIELD_spaceBefore',
371 ),
372 array(
373 'operator' => 'AND',
374 'type' => 'FIELD_records',
375 'negate' => 1,
376 'inputValue' => 'foo foo'
377 ),
378 array(
379 'type' => 'newlevel',
380 'nl' => array(
381 array(
382 'operator' => 'AND',
383 'type' => 'FIELD_spaceBefore',
384 'negate' => 1,
385 'inputValue' => 'foo foo'
386 ),
387 array(
388 'operator' => 'AND',
389 'type' => 'FIELD_records',
390 'negate' => 1,
391 'inputValue' => 'foo foo'
392 )
393 )
394 ),
395 array(
396 'operator' => 'OR',
397 'type' => 'FIELD_maillist',
398 )
399 );
400 */
401 $this->initUserDef();
402 }
403
404 /**
405 * Set and clean up external lists
406 *
407 * @param string $name The name
408 * @param string $list The list
409 * @param string $force
410 * @return void
411 */
412 public function setAndCleanUpExternalLists($name, $list, $force = '')
413 {
414 $fields = array_unique(GeneralUtility::trimExplode(',', $list . ',' . $force, true));
415 $reList = [];
416 foreach ($fields as $fieldName) {
417 if ($this->fields[$fieldName]) {
418 $reList[] = $fieldName;
419 }
420 }
421 $this->extFieldLists[$name] = implode(',', $reList);
422 }
423
424 /**
425 * Process data
426 *
427 * @param string $qC Query config
428 * @return void
429 */
430 public function procesData($qC = '')
431 {
432 $this->queryConfig = $qC;
433 $POST = GeneralUtility::_POST();
434 // If delete...
435 if ($POST['qG_del']) {
436 // Initialize array to work on, save special parameters
437 $ssArr = $this->getSubscript($POST['qG_del']);
438 $workArr = &$this->queryConfig;
439 $ssArrSize = count($ssArr) - 1;
440 $i = 0;
441 for (; $i < $ssArrSize; $i++) {
442 $workArr = &$workArr[$ssArr[$i]];
443 }
444 // Delete the entry and move the other entries
445 unset($workArr[$ssArr[$i]]);
446 $workArrSize = count($workArr);
447 for ($j = $ssArr[$i]; $j < $workArrSize; $j++) {
448 $workArr[$j] = $workArr[$j + 1];
449 unset($workArr[$j + 1]);
450 }
451 }
452 // If insert...
453 if ($POST['qG_ins']) {
454 // Initialize array to work on, save special parameters
455 $ssArr = $this->getSubscript($POST['qG_ins']);
456 $workArr = &$this->queryConfig;
457 $ssArrSize = count($ssArr) - 1;
458 $i = 0;
459 for (; $i < $ssArrSize; $i++) {
460 $workArr = &$workArr[$ssArr[$i]];
461 }
462 // Move all entries above position where new entry is to be inserted
463 $workArrSize = count($workArr);
464 for ($j = $workArrSize; $j > $ssArr[$i]; $j--) {
465 $workArr[$j] = $workArr[$j - 1];
466 }
467 // Clear new entry position
468 unset($workArr[$ssArr[$i] + 1]);
469 $workArr[$ssArr[$i] + 1]['type'] = 'FIELD_';
470 }
471 // If move up...
472 if ($POST['qG_up']) {
473 // Initialize array to work on
474 $ssArr = $this->getSubscript($POST['qG_up']);
475 $workArr = &$this->queryConfig;
476 $ssArrSize = count($ssArr) - 1;
477 $i = 0;
478 for (; $i < $ssArrSize; $i++) {
479 $workArr = &$workArr[$ssArr[$i]];
480 }
481 // Swap entries
482 $qG_tmp = $workArr[$ssArr[$i]];
483 $workArr[$ssArr[$i]] = $workArr[$ssArr[$i] - 1];
484 $workArr[$ssArr[$i] - 1] = $qG_tmp;
485 }
486 // If new level...
487 if ($POST['qG_nl']) {
488 // Initialize array to work on
489 $ssArr = $this->getSubscript($POST['qG_nl']);
490 $workArr = &$this->queryConfig;
491 $ssArraySize = count($ssArr) - 1;
492 $i = 0;
493 for (; $i < $ssArraySize; $i++) {
494 $workArr = &$workArr[$ssArr[$i]];
495 }
496 // Do stuff:
497 $tempEl = $workArr[$ssArr[$i]];
498 if (is_array($tempEl)) {
499 if ($tempEl['type'] !== 'newlevel') {
500 $workArr[$ssArr[$i]] = [
501 'type' => 'newlevel',
502 'operator' => $tempEl['operator'],
503 'nl' => [$tempEl]
504 ];
505 }
506 }
507 }
508 // If collapse level...
509 if ($POST['qG_remnl']) {
510 // Initialize array to work on
511 $ssArr = $this->getSubscript($POST['qG_remnl']);
512 $workArr = &$this->queryConfig;
513 $ssArrSize = count($ssArr) - 1;
514 $i = 0;
515 for (; $i < $ssArrSize; $i++) {
516 $workArr = &$workArr[$ssArr[$i]];
517 }
518 // Do stuff:
519 $tempEl = $workArr[$ssArr[$i]];
520 if (is_array($tempEl)) {
521 if ($tempEl['type'] === 'newlevel') {
522 $a1 = array_slice($workArr, 0, $ssArr[$i]);
523 $a2 = array_slice($workArr, $ssArr[$i]);
524 array_shift($a2);
525 $a3 = $tempEl['nl'];
526 $a3[0]['operator'] = $tempEl['operator'];
527 $workArr = array_merge($a1, $a3, $a2);
528 }
529 }
530 }
531 }
532
533 /**
534 * Clean up query config
535 *
536 * @param array $queryConfig Query config
537 * @return array
538 */
539 public function cleanUpQueryConfig($queryConfig)
540 {
541 // Since we don't traverse the array using numeric keys in the upcoming while-loop make sure it's fresh and clean before displaying
542 if (is_array($queryConfig)) {
543 ksort($queryConfig);
544 } else {
545 // queryConfig should never be empty!
546 if (!$queryConfig[0] || !$queryConfig[0]['type']) {
547 $queryConfig[0] = ['type' => 'FIELD_'];
548 }
549 }
550 // Traverse:
551 foreach ($queryConfig as $key => $conf) {
552 $fieldName = '';
553 if (substr($conf['type'], 0, 6) === 'FIELD_') {
554 $fieldName = substr($conf['type'], 6);
555 $fieldType = $this->fields[$fieldName]['type'];
556 } elseif ($conf['type'] === 'newlevel') {
557 $fieldType = $conf['type'];
558 } else {
559 $fieldType = 'ignore';
560 }
561 switch ($fieldType) {
562 case 'newlevel':
563 if (!$queryConfig[$key]['nl']) {
564 $queryConfig[$key]['nl'][0]['type'] = 'FIELD_';
565 }
566 $queryConfig[$key]['nl'] = $this->cleanUpQueryConfig($queryConfig[$key]['nl']);
567 break;
568 case 'userdef':
569 $queryConfig[$key] = $this->userDefCleanUp($queryConfig[$key]);
570 break;
571 case 'ignore':
572 default:
573 $verifiedName = $this->verifyType($fieldName);
574 $queryConfig[$key]['type'] = 'FIELD_' . $this->verifyType($verifiedName);
575 if ($conf['comparison'] >> 5 != $this->comp_offsets[$fieldType]) {
576 $conf['comparison'] = $this->comp_offsets[$fieldType] << 5;
577 }
578 $queryConfig[$key]['comparison'] = $this->verifyComparison($conf['comparison'], $conf['negate'] ? 1 : 0);
579 $queryConfig[$key]['inputValue'] = $this->cleanInputVal($queryConfig[$key]);
580 $queryConfig[$key]['inputValue1'] = $this->cleanInputVal($queryConfig[$key], 1);
581 }
582 }
583 return $queryConfig;
584 }
585
586 /**
587 * Get form elements
588 *
589 * @param int $subLevel
590 * @param string $queryConfig
591 * @param string $parent
592 * @return array
593 */
594 public function getFormElements($subLevel = 0, $queryConfig = '', $parent = '')
595 {
596 $codeArr = [];
597 if (!is_array($queryConfig)) {
598 $queryConfig = $this->queryConfig;
599 }
600 $c = 0;
601 $arrCount = 0;
602 $loopCount = 0;
603 foreach ($queryConfig as $key => $conf) {
604 $fieldName = '';
605 $subscript = $parent . '[' . $key . ']';
606 $lineHTML = [];
607 $lineHTML[] = $this->mkOperatorSelect($this->name . $subscript, $conf['operator'], $c, $conf['type'] !== 'FIELD_');
608 if (substr($conf['type'], 0, 6) === 'FIELD_') {
609 $fieldName = substr($conf['type'], 6);
610 $this->fieldName = $fieldName;
611 $fieldType = $this->fields[$fieldName]['type'];
612 if ($conf['comparison'] >> 5 != $this->comp_offsets[$fieldType]) {
613 $conf['comparison'] = $this->comp_offsets[$fieldType] << 5;
614 }
615 //nasty nasty...
616 //make sure queryConfig contains _actual_ comparevalue.
617 //mkCompSelect don't care, but getQuery does.
618 $queryConfig[$key]['comparison'] += isset($conf['negate']) - $conf['comparison'] % 2;
619 } elseif ($conf['type'] === 'newlevel') {
620 $fieldType = $conf['type'];
621 } else {
622 $fieldType = 'ignore';
623 }
624 $fieldPrefix = htmlspecialchars($this->name . $subscript);
625 switch ($fieldType) {
626 case 'ignore':
627 break;
628 case 'newlevel':
629 if (!$queryConfig[$key]['nl']) {
630 $queryConfig[$key]['nl'][0]['type'] = 'FIELD_';
631 }
632 $lineHTML[] = '<input type="hidden" name="' . $fieldPrefix . '[type]" value="newlevel">';
633 $codeArr[$arrCount]['sub'] = $this->getFormElements($subLevel + 1, $queryConfig[$key]['nl'], $subscript . '[nl]');
634 break;
635 case 'userdef':
636 $lineHTML[] = $this->userDef($fieldPrefix, $conf, $fieldName, $fieldType);
637 break;
638 case 'date':
639 $lineHTML[] = '<div class="form-inline">';
640 $lineHTML[] = $this->makeComparisonSelector($subscript, $fieldName, $conf);
641 if ($conf['comparison'] === 100 || $conf['comparison'] === 101) {
642 // between
643 $lineHTML[] = $this->getDateTimePickerField($fieldPrefix . '[inputValue]', $conf['inputValue'], 'date');
644 $lineHTML[] = $this->getDateTimePickerField($fieldPrefix . '[inputValue1]', $conf['inputValue1'], 'date');
645 } else {
646 $lineHTML[] = $this->getDateTimePickerField($fieldPrefix . '[inputValue]', $conf['inputValue'], 'date');
647 }
648 $lineHTML[] = '</div>';
649 break;
650 case 'time':
651 $lineHTML[] = '<div class="form-inline">';
652 $lineHTML[] = $this->makeComparisonSelector($subscript, $fieldName, $conf);
653 if ($conf['comparison'] === 100 || $conf['comparison'] === 101) {
654 // between:
655 $lineHTML[] = $this->getDateTimePickerField($fieldPrefix . '[inputValue]', $conf['inputValue'], 'datetime');
656 $lineHTML[] = $this->getDateTimePickerField($fieldPrefix . '[inputValue1]', $conf['inputValue1'], 'datetime');
657 } else {
658 $lineHTML[] = $this->getDateTimePickerField($fieldPrefix . '[inputValue]', $conf['inputValue'], 'datetime');
659 }
660 $lineHTML[] = '</div>';
661 break;
662 case 'multiple':
663 case 'binary':
664 case 'relation':
665 $lineHTML[] = '<div class="form-inline">';
666 $lineHTML[] = $this->makeComparisonSelector($subscript, $fieldName, $conf);
667 if ($conf['comparison'] === 68 || $conf['comparison'] === 69 || $conf['comparison'] === 162 || $conf['comparison'] === 163) {
668 $lineHTML[] = '<select class="form-control" name="' . $fieldPrefix . '[inputValue]' . '[]" multiple="multiple">';
669 } elseif ($conf['comparison'] === 66 || $conf['comparison'] === 67) {
670 if (is_array($conf['inputValue'])) {
671 $conf['inputValue'] = implode(',', $conf['inputValue']);
672 }
673 $lineHTML[] = '<input class="form-control t3js-clearable" type="text" value="' . htmlspecialchars($conf['inputValue']) . '" name="' . $fieldPrefix . '[inputValue]' . '">';
674 } else {
675 $lineHTML[] = '<select class="form-control t3js-submit-change" name="' . $fieldPrefix . '[inputValue]' . '">';
676 }
677 if ($conf['comparison'] != 66 && $conf['comparison'] != 67) {
678 $lineHTML[] = $this->makeOptionList($fieldName, $conf, $this->table);
679 $lineHTML[] = '</select>';
680 }
681 $lineHTML[] = '</div>';
682 break;
683 case 'files':
684 $lineHTML[] = '<div class="form-inline">';
685 $lineHTML[] = $this->makeComparisonSelector($subscript, $fieldName, $conf);
686 if ($conf['comparison'] === 68 || $conf['comparison'] === 69) {
687 $lineHTML[] = '<select class="form-control" name="' . $fieldPrefix . '[inputValue]' . '[]" multiple="multiple">';
688 } else {
689 $lineHTML[] = '<select class="form-control t3js-submit-change" name="' . $fieldPrefix . '[inputValue]' . '">';
690 }
691 $lineHTML[] = '<option value=""></option>' . $this->makeOptionList($fieldName, $conf, $this->table);
692 $lineHTML[] = '</select>';
693 if ($conf['comparison'] === 66 || $conf['comparison'] === 67) {
694 $lineHTML[] = ' + <input class="form-control t3js-clearable" type="text" value="' . htmlspecialchars($conf['inputValue1']) . '" name="' . $fieldPrefix . '[inputValue1]' . '">';
695 }
696 $lineHTML[] = '</div>';
697 break;
698 case 'boolean':
699 $lineHTML[] = '<div class="form-inline">';
700 $lineHTML[] = $this->makeComparisonSelector($subscript, $fieldName, $conf);
701 $lineHTML[] = '<input type="hidden" value="1" name="' . $fieldPrefix . '[inputValue]' . '">';
702 $lineHTML[] = '</div>';
703 break;
704 default:
705 $lineHTML[] = '<div class="form-inline">';
706 $lineHTML[] = $this->makeComparisonSelector($subscript, $fieldName, $conf);
707 if ($conf['comparison'] === 37 || $conf['comparison'] === 36) {
708 // between:
709 $lineHTML[] = '<input class="form-control t3js-clearable" type="text" value="' . htmlspecialchars($conf['inputValue']) . '" name="' . $fieldPrefix . '[inputValue]' . '">';
710 $lineHTML[] = '<input class="form-control t3js-clearable" type="text" value="' . htmlspecialchars($conf['inputValue1']) . '" name="' . $fieldPrefix . '[inputValue1]' . '">';
711 } else {
712 $lineHTML[] = '<input class="form-control t3js-clearable" type="text" value="' . htmlspecialchars($conf['inputValue']) . '" name="' . $fieldPrefix . '[inputValue]' . '">';
713 }
714 $lineHTML[] = '</div>';
715 }
716 if ($fieldType !== 'ignore') {
717 $lineHTML[] = '<div class="btn-group action-button-group">';
718 $lineHTML[] = $this->updateIcon();
719 if ($loopCount) {
720 $lineHTML[] = '<button class="btn btn-default" title="Remove condition" name="qG_del' . htmlspecialchars($subscript) . '"><i class="fa fa-trash fa-fw"></i></button>';
721 }
722 $lineHTML[] = '<button class="btn btn-default" title="Add condition" name="qG_ins' . htmlspecialchars($subscript) . '"><i class="fa fa-plus fa-fw"></i></button>';
723 if ($c != 0) {
724 $lineHTML[] = '<button class="btn btn-default" title="Move up" name="qG_up' . htmlspecialchars($subscript) . '"><i class="fa fa-chevron-up fa-fw"></i></button>';
725 }
726 if ($c != 0 && $fieldType !== 'newlevel') {
727 $lineHTML[] = '<button class="btn btn-default" title="New level" name="qG_nl' . htmlspecialchars($subscript) . '"><i class="fa fa-chevron-right fa-fw"></i></button>';
728 }
729 if ($fieldType === 'newlevel') {
730 $lineHTML[] = '<button class="btn btn-default" title="Collapse new level" name="qG_remnl' . htmlspecialchars($subscript) . '"><i class="fa fa-chevron-left fa-fw"></i></button>';
731 }
732 $lineHTML[] = '</div>';
733 $codeArr[$arrCount]['html'] = implode(LF, $lineHTML);
734 $codeArr[$arrCount]['query'] = $this->getQuerySingle($conf, $c > 0 ? 0 : 1);
735 $arrCount++;
736 $c++;
737 }
738 $loopCount = 1;
739 }
740 $this->queryConfig = $queryConfig;
741 return $codeArr;
742 }
743
744 /**
745 * @param string $subscript
746 * @param string $fieldName
747 * @param array $conf
748 *
749 * @return string
750 */
751 protected function makeComparisonSelector($subscript, $fieldName, $conf)
752 {
753 $fieldPrefix = $this->name . $subscript;
754 $lineHTML = [];
755 $lineHTML[] = $this->mkTypeSelect($fieldPrefix . '[type]', $fieldName);
756 $lineHTML[] = ' <div class="input-group">';
757 $lineHTML[] = $this->mkCompSelect($fieldPrefix . '[comparison]', $conf['comparison'], $conf['negate'] ? 1 : 0);
758 $lineHTML[] = ' <div class="input-group-addon">';
759 $lineHTML[] = ' <input type="checkbox" class="checkbox t3js-submit-click"' . ($conf['negate'] ? ' checked' : '') . ' name="' . htmlspecialchars($fieldPrefix) . '[negate]' . '">';
760 $lineHTML[] = ' </div>';
761 $lineHTML[] = ' </div>';
762 return implode(LF, $lineHTML);
763 }
764
765 /**
766 * Make option list
767 *
768 * @param string $fieldName
769 * @param array $conf
770 * @param string $table
771 * @return string
772 */
773 public function makeOptionList($fieldName, $conf, $table)
774 {
775 $out = [];
776 $fieldSetup = $this->fields[$fieldName];
777 $languageService = $this->getLanguageService();
778 if ($fieldSetup['type'] === 'files') {
779 if ($conf['comparison'] === 66 || $conf['comparison'] === 67) {
780 $fileExtArray = explode(',', $fieldSetup['allowed']);
781 natcasesort($fileExtArray);
782 foreach ($fileExtArray as $fileExt) {
783 if (GeneralUtility::inList($conf['inputValue'], $fileExt)) {
784 $out[] = '<option value="' . htmlspecialchars($fileExt) . '" selected>.' . htmlspecialchars($fileExt) . '</option>';
785 } else {
786 $out[] = '<option value="' . htmlspecialchars($fileExt) . '">.' . htmlspecialchars($fileExt) . '</option>';
787 }
788 }
789 }
790 $d = dir(PATH_site . $fieldSetup['uploadfolder']);
791 while (false !== ($entry = $d->read())) {
792 if ($entry === '.' || $entry === '..') {
793 continue;
794 }
795 $fileArray[] = $entry;
796 }
797 $d->close();
798 natcasesort($fileArray);
799 foreach ($fileArray as $fileName) {
800 if (GeneralUtility::inList($conf['inputValue'], $fileName)) {
801 $out[] = '<option value="' . htmlspecialchars($fileName) . '" selected>' . htmlspecialchars($fileName) . '</option>';
802 } else {
803 $out[] = '<option value="' . htmlspecialchars($fileName) . '">' . htmlspecialchars($fileName) . '</option>';
804 }
805 }
806 }
807 if ($fieldSetup['type'] === 'multiple') {
808 foreach ($fieldSetup['items'] as $key => $val) {
809 if (substr($val[0], 0, 4) === 'LLL:') {
810 $value = $languageService->sL($val[0]);
811 } else {
812 $value = $val[0];
813 }
814 if (GeneralUtility::inList($conf['inputValue'], $val[1])) {
815 $out[] = '<option value="' . htmlspecialchars($val[1]) . '" selected>' . htmlspecialchars($value) . '</option>';
816 } else {
817 $out[] = '<option value="' . htmlspecialchars($val[1]) . '">' . htmlspecialchars($value) . '</option>';
818 }
819 }
820 }
821 if ($fieldSetup['type'] === 'binary') {
822 foreach ($fieldSetup['items'] as $key => $val) {
823 if (substr($val[0], 0, 4) === 'LLL:') {
824 $value = $languageService->sL($val[0]);
825 } else {
826 $value = $val[0];
827 }
828 if (GeneralUtility::inList($conf['inputValue'], pow(2, $key))) {
829 $out[] = '<option value="' . pow(2, $key) . '" selected>' . htmlspecialchars($value) . '</option>';
830 } else {
831 $out[] = '<option value="' . pow(2, $key) . '">' . htmlspecialchars($value) . '</option>';
832 }
833 }
834 }
835 if ($fieldSetup['type'] === 'relation') {
836 $useTablePrefix = 0;
837 $dontPrefixFirstTable = 0;
838 if ($fieldSetup['items']) {
839 foreach ($fieldSetup['items'] as $key => $val) {
840 if (substr($val[0], 0, 4) === 'LLL:') {
841 $value = $languageService->sL($val[0]);
842 } else {
843 $value = $val[0];
844 }
845 if (GeneralUtility::inList($conf['inputValue'], $val[1])) {
846 $out[] = '<option value="' . htmlspecialchars($val[1]) . '" selected>' . htmlspecialchars($value) . '</option>';
847 } else {
848 $out[] = '<option value="' . htmlspecialchars($val[1]) . '">' . htmlspecialchars($value) . '</option>';
849 }
850 }
851 }
852 if (stristr($fieldSetup['allowed'], ',')) {
853 $from_table_Arr = explode(',', $fieldSetup['allowed']);
854 $useTablePrefix = 1;
855 if (!$fieldSetup['prepend_tname']) {
856 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
857 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
858 $statement = $queryBuilder->select($fieldName)
859 ->from($table)
860 ->execute();
861 while ($row = $statement->fetch()) {
862 if (stristr($row[$fieldName], ',')) {
863 $checkContent = explode(',', $row[$fieldName]);
864 foreach ($checkContent as $singleValue) {
865 if (!stristr($singleValue, '_')) {
866 $dontPrefixFirstTable = 1;
867 }
868 }
869 } else {
870 $singleValue = $row[$fieldName];
871 if ($singleValue !== '' && !stristr($singleValue, '_')) {
872 $dontPrefixFirstTable = 1;
873 }
874 }
875 }
876 }
877 } else {
878 $from_table_Arr[0] = $fieldSetup['allowed'];
879 }
880 if ($fieldSetup['prepend_tname']) {
881 $useTablePrefix = 1;
882 }
883 if ($fieldSetup['foreign_table']) {
884 $from_table_Arr[0] = $fieldSetup['foreign_table'];
885 }
886 $counter = 0;
887 $tablePrefix = '';
888 $backendUserAuthentication = $this->getBackendUserAuthentication();
889 $module = $this->getModule();
890 $outArray = [];
891 $labelFieldSelect = [];
892 foreach ($from_table_Arr as $from_table) {
893 $useSelectLabels = false;
894 $useAltSelectLabels = false;
895 if ($useTablePrefix && !$dontPrefixFirstTable && $counter != 1 || $counter === 1) {
896 $tablePrefix = $from_table . '_';
897 }
898 $counter = 1;
899 if (is_array($GLOBALS['TCA'][$from_table])) {
900 $labelField = $GLOBALS['TCA'][$from_table]['ctrl']['label'];
901 $altLabelField = $GLOBALS['TCA'][$from_table]['ctrl']['label_alt'];
902 if ($GLOBALS['TCA'][$from_table]['columns'][$labelField]['config']['items']) {
903 foreach ($GLOBALS['TCA'][$from_table]['columns'][$labelField]['config']['items'] as $labelArray) {
904 if (substr($labelArray[0], 0, 4) === 'LLL:') {
905 $labelFieldSelect[$labelArray[1]] = $languageService->sL($labelArray[0]);
906 } else {
907 $labelFieldSelect[$labelArray[1]] = $labelArray[0];
908 }
909 }
910 $useSelectLabels = true;
911 }
912 $altLabelFieldSelect = [];
913 if ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items']) {
914 foreach ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items'] as $altLabelArray) {
915 if (substr($altLabelArray[0], 0, 4) === 'LLL:') {
916 $altLabelFieldSelect[$altLabelArray[1]] = $languageService->sL($altLabelArray[0]);
917 } else {
918 $altLabelFieldSelect[$altLabelArray[1]] = $altLabelArray[0];
919 }
920 }
921 $useAltSelectLabels = true;
922 }
923
924 if (!$this->tableArray[$from_table]) {
925 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($from_table);
926 if ($module->MOD_SETTINGS['show_deleted']) {
927 $queryBuilder->getRestrictions()->removeAll();
928 } else {
929 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
930 }
931 $selectFields = ['uid', $labelField];
932 if ($altLabelField) {
933 $selectFields[] = $altLabelField;
934 }
935 $queryBuilder->select(...$selectFields)
936 ->from($from_table)
937 ->orderBy('uid');
938 if (!$backendUserAuthentication->isAdmin() && $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts']) {
939 $webMounts = $backendUserAuthentication->returnWebmounts();
940 $perms_clause = $backendUserAuthentication->getPagePermsClause(1);
941 $webMountPageTree = '';
942 $webMountPageTreePrefix = '';
943 foreach ($webMounts as $webMount) {
944 if ($webMountPageTree) {
945 $webMountPageTreePrefix = ',';
946 }
947 $webMountPageTree .= $webMountPageTreePrefix
948 . $this->getTreeList($webMount, 999, ($begin = 0), $perms_clause);
949 }
950 if ($from_table === 'pages') {
951 $queryBuilder->where(
952 QueryHelper::stripLogicalOperatorPrefix($perms_clause),
953 $queryBuilder->expr()->in(
954 'uid',
955 $queryBuilder->createNamedParameter(
956 GeneralUtility::intExplode(',', $webMountPageTree),
957 Connection::PARAM_INT_ARRAY
958 )
959 )
960 );
961 } else {
962 $queryBuilder->where(
963 $queryBuilder->expr()->in(
964 'pid',
965 $queryBuilder->createNamedParameter(
966 GeneralUtility::intExplode(',', $webMountPageTree),
967 Connection::PARAM_INT_ARRAY
968 )
969 )
970 );
971 }
972 }
973 $statement = $queryBuilder->execute();
974 $this->tableArray[$from_table] = [];
975 while ($row = $statement->fetch()) {
976 $this->tableArray[$from_table][] = $row;
977 }
978 }
979
980 foreach ($this->tableArray[$from_table] as $key => $val) {
981 if ($useSelectLabels) {
982 $outArray[$tablePrefix . $val['uid']] = htmlspecialchars($labelFieldSelect[$val[$labelField]]);
983 } elseif ($val[$labelField]) {
984 $outArray[$tablePrefix . $val['uid']] = htmlspecialchars($val[$labelField]);
985 } elseif ($useAltSelectLabels) {
986 $outArray[$tablePrefix . $val['uid']] = htmlspecialchars($altLabelFieldSelect[$val[$altLabelField]]);
987 } else {
988 $outArray[$tablePrefix . $val['uid']] = htmlspecialchars($val[$altLabelField]);
989 }
990 }
991 if ($module->MOD_SETTINGS['options_sortlabel'] && is_array($outArray)) {
992 natcasesort($outArray);
993 }
994 }
995 }
996 foreach ($outArray as $key2 => $val2) {
997 if (GeneralUtility::inList($conf['inputValue'], $key2)) {
998 $out[] = '<option value="' . htmlspecialchars($key2) . '" selected>[' . htmlspecialchars($key2) . '] ' . htmlspecialchars($val2) . '</option>';
999 } else {
1000 $out[] = '<option value="' . htmlspecialchars($key2) . '">[' . htmlspecialchars($key2) . '] ' . htmlspecialchars($val2) . '</option>';
1001 }
1002 }
1003 }
1004 return implode(LF, $out);
1005 }
1006
1007 /**
1008 * Print code array
1009 *
1010 * @param array $codeArr
1011 * @param int $recursionLevel
1012 * @return string
1013 */
1014 public function printCodeArray($codeArr, $recursionLevel = 0)
1015 {
1016 $indent = 'row-group';
1017 if ($recursionLevel) {
1018 $indent = 'row-group indent indent-' . (int)$recursionLevel;
1019 }
1020 $out = [];
1021 foreach ($codeArr as $k => $v) {
1022 $out[] = '<div class="' . $indent . '">';
1023 $out[] = $v['html'];
1024
1025 if ($this->enableQueryParts) {
1026 $out[] = '<pre>';
1027 $out[] = htmlspecialchars($v['query']);
1028 $out[] = '</pre>';
1029 }
1030 if (is_array($v['sub'])) {
1031 $out[] = '<div class="' . $indent . '">';
1032 $out[] = $this->printCodeArray($v['sub'], ($recursionLevel + 1));
1033 $out[] = '</div>';
1034 }
1035
1036 $out[] = '</div>';
1037 }
1038 return implode(LF, $out);
1039 }
1040
1041 /**
1042 * Make operator select
1043 *
1044 * @param string $name
1045 * @param string $op
1046 * @param bool $draw
1047 * @param bool $submit
1048 * @return string
1049 */
1050 public function mkOperatorSelect($name, $op, $draw, $submit)
1051 {
1052 $out = [];
1053 if ($draw) {
1054 $out[] = '<select class="form-control from-control-operator' . ($submit ? ' t3js-submit-change' : '') . '" name="' . htmlspecialchars($name) . '[operator]">';
1055 $out[] = ' <option value="AND"' . (!$op || $op === 'AND' ? ' selected' : '') . '>' . htmlspecialchars($this->lang['AND']) . '</option>';
1056 $out[] = ' <option value="OR"' . ($op === 'OR' ? ' selected' : '') . '>' . htmlspecialchars($this->lang['OR']) . '</option>';
1057 $out[] = '</select>';
1058 } else {
1059 $out[] = '<input type="hidden" value="' . htmlspecialchars($op) . '" name="' . htmlspecialchars($name) . '[operator]">';
1060 }
1061 return implode(LF, $out);
1062 }
1063
1064 /**
1065 * Make type select
1066 *
1067 * @param string $name
1068 * @param string $fieldName
1069 * @param string $prepend
1070 * @return string
1071 */
1072 public function mkTypeSelect($name, $fieldName, $prepend = 'FIELD_')
1073 {
1074 $out = [];
1075 $out[] = '<select class="form-control t3js-submit-change" name="' . htmlspecialchars($name) . '">';
1076 $out[] = '<option value=""></option>';
1077 foreach ($this->fields as $key => $value) {
1078 if (!$value['exclude'] || $this->getBackendUserAuthentication()->check('non_exclude_fields', $this->table . ':' . $key)) {
1079 $label = $this->fields[$key]['label'];
1080 $out[] = '<option value="' . htmlspecialchars($prepend . $key) . '"' . ($key === $fieldName ? ' selected' : '') . '>' . htmlspecialchars($label) . '</option>';
1081 }
1082 }
1083 $out[] = '</select>';
1084 return implode(LF, $out);
1085 }
1086
1087 /**
1088 * Verify type
1089 *
1090 * @param string $fieldName
1091 * @return string
1092 */
1093 public function verifyType($fieldName)
1094 {
1095 $first = '';
1096 foreach ($this->fields as $key => $value) {
1097 if (!$first) {
1098 $first = $key;
1099 }
1100 if ($key === $fieldName) {
1101 return $key;
1102 }
1103 }
1104 return $first;
1105 }
1106
1107 /**
1108 * Verify comparison
1109 *
1110 * @param string $comparison
1111 * @param int $neg
1112 * @return int
1113 */
1114 public function verifyComparison($comparison, $neg)
1115 {
1116 $compOffSet = $comparison >> 5;
1117 $first = -1;
1118 for ($i = 32 * $compOffSet + $neg; $i < 32 * ($compOffSet + 1); $i += 2) {
1119 if ($first === -1) {
1120 $first = $i;
1121 }
1122 if ($i >> 1 === $comparison >> 1) {
1123 return $i;
1124 }
1125 }
1126 return $first;
1127 }
1128
1129 /**
1130 * Make field to input select
1131 *
1132 * @param string $name
1133 * @param string $fieldName
1134 * @return string
1135 */
1136 public function mkFieldToInputSelect($name, $fieldName)
1137 {
1138 $out = [];
1139 $out[] = '<div class="input-group">';
1140 $out[] = ' <div class="input-group-addon">';
1141 $out[] = ' <span class="input-group-btn">';
1142 $out[] = $this->updateIcon();
1143 $out[] = ' </span>';
1144 $out[] = ' </div>';
1145 $out[] = ' <input type="text" class="form-control t3js-clearable" value="' . htmlspecialchars($fieldName) . '" name="' . htmlspecialchars($name) . '">';
1146 $out[] = '</div>';
1147
1148 $out[] = '<select class="form-control t3js-addfield" name="_fieldListDummy" size="5" data-field="' . htmlspecialchars($name) . '">';
1149 foreach ($this->fields as $key => $value) {
1150 if (!$value['exclude'] || $this->getBackendUserAuthentication()->check('non_exclude_fields', $this->table . ':' . $key)) {
1151 $label = $this->fields[$key]['label'];
1152 $out[] = '<option value="' . htmlspecialchars($key) . '"' . ($key === $fieldName ? ' selected' : '') . '>' . htmlspecialchars($label) . '</option>';
1153 }
1154 }
1155 $out[] = '</select>';
1156 return implode(LF, $out);
1157 }
1158
1159 /**
1160 * Make table select
1161 *
1162 * @param string $name
1163 * @param string $cur
1164 * @return string
1165 */
1166 public function mkTableSelect($name, $cur)
1167 {
1168 $out = [];
1169 $out[] = '<select class="form-control t3js-submit-change" name="' . $name . '">';
1170 $out[] = '<option value=""></option>';
1171 foreach ($GLOBALS['TCA'] as $tN => $value) {
1172 if ($this->getBackendUserAuthentication()->check('tables_select', $tN)) {
1173 $out[] = '<option value="' . htmlspecialchars($tN) . '"' . ($tN === $cur ? ' selected' : '') . '>' . htmlspecialchars($this->getLanguageService()->sL($GLOBALS['TCA'][$tN]['ctrl']['title'])) . '</option>';
1174 }
1175 }
1176 $out[] = '</select>';
1177 return implode(LF, $out);
1178 }
1179
1180 /**
1181 * Make comparison select
1182 *
1183 * @param string $name
1184 * @param string $comparison
1185 * @param int $neg
1186 * @return string
1187 */
1188 public function mkCompSelect($name, $comparison, $neg)
1189 {
1190 $compOffSet = $comparison >> 5;
1191 $out = [];
1192 $out[] = '<select class="form-control t3js-submit-change" name="' . $name . '">';
1193 for ($i = 32 * $compOffSet + $neg; $i < 32 * ($compOffSet + 1); $i += 2) {
1194 if ($this->lang['comparison'][$i . '_']) {
1195 $out[] = '<option value="' . $i . '"' . ($i >> 1 === $comparison >> 1 ? ' selected' : '') . '>' . htmlspecialchars($this->lang['comparison'][$i . '_']) . '</option>';
1196 }
1197 }
1198 $out[] = '</select>';
1199 return implode(LF, $out);
1200 }
1201
1202 /**
1203 * Get subscript
1204 *
1205 * @param array $arr
1206 * @return array
1207 */
1208 public function getSubscript($arr)
1209 {
1210 $retArr = [];
1211 while (is_array($arr)) {
1212 reset($arr);
1213 list($key, ) = each($arr);
1214 $retArr[] = $key;
1215 $arr = $arr[$key];
1216 }
1217 return $retArr;
1218 }
1219
1220 /**
1221 * Init user definition
1222 *
1223 * @return void
1224 */
1225 public function initUserDef()
1226 {
1227 }
1228
1229 /**
1230 * User definition
1231 *
1232 * @param string $fieldPrefix
1233 * @param array $conf
1234 * @param string $fieldName
1235 * @param string $fieldType
1236 *
1237 * @return string
1238 */
1239 public function userDef($fieldPrefix, $conf, $fieldName, $fieldType)
1240 {
1241 return '';
1242 }
1243
1244 /**
1245 * User definition clean up
1246 *
1247 * @param array $queryConfig
1248 * @return array
1249 */
1250 public function userDefCleanUp($queryConfig)
1251 {
1252 return $queryConfig;
1253 }
1254
1255 /**
1256 * Get query
1257 *
1258 * @param array $queryConfig
1259 * @param string $pad
1260 * @return string
1261 */
1262 public function getQuery($queryConfig, $pad = '')
1263 {
1264 $qs = '';
1265 // Since we don't traverse the array using numeric keys in the upcoming whileloop make sure it's fresh and clean
1266 ksort($queryConfig);
1267 $first = 1;
1268 foreach ($queryConfig as $key => $conf) {
1269 switch ($conf['type']) {
1270 case 'newlevel':
1271 $qs .= LF . $pad . trim($conf['operator']) . ' (' . $this->getQuery($queryConfig[$key]['nl'], ($pad . ' ')) . LF . $pad . ')';
1272 break;
1273 case 'userdef':
1274 $qs .= LF . $pad . $this->getUserDefQuery($conf, $first);
1275 break;
1276 default:
1277 $qs .= LF . $pad . $this->getQuerySingle($conf, $first);
1278 }
1279 $first = 0;
1280 }
1281 return $qs;
1282 }
1283
1284 /**
1285 * Get single query
1286 *
1287 * @param array $conf
1288 * @param bool $first
1289 * @return string
1290 */
1291 public function getQuerySingle($conf, $first)
1292 {
1293 $qs = '';
1294 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->table);
1295 $prefix = $this->enablePrefix ? $this->table . '.' : '';
1296 if (!$first) {
1297 // Is it OK to insert the AND operator if none is set?
1298 $qs .= trim($conf['operator'] ?: 'AND') . ' ';
1299 }
1300 $qsTmp = str_replace('#FIELD#', $prefix . trim(substr($conf['type'], 6)), $this->compSQL[$conf['comparison']]);
1301 $inputVal = $this->cleanInputVal($conf);
1302 if ($conf['comparison'] === 68 || $conf['comparison'] === 69) {
1303 $inputVal = explode(',', $inputVal);
1304 foreach ($inputVal as $key => $fileName) {
1305 $inputVal[$key] = '\'' . $fileName . '\'';
1306 }
1307 $inputVal = implode(',', $inputVal);
1308 $qsTmp = str_replace('#VALUE#', $inputVal, $qsTmp);
1309 } elseif ($conf['comparison'] === 162 || $conf['comparison'] === 163) {
1310 $inputValArray = explode(',', $inputVal);
1311 $inputVal = 0;
1312 foreach ($inputValArray as $fileName) {
1313 $inputVal += (int)$fileName;
1314 }
1315 $qsTmp = str_replace('#VALUE#', $inputVal, $qsTmp);
1316 } else {
1317 if (is_array($inputVal)) {
1318 $inputVal = $inputVal[0];
1319 }
1320 $qsTmp = str_replace('#VALUE#', trim($queryBuilder->quote($inputVal), '\''), $qsTmp);
1321 }
1322 if ($conf['comparison'] === 37 || $conf['comparison'] === 36 || $conf['comparison'] === 66 || $conf['comparison'] === 67 || $conf['comparison'] === 100 || $conf['comparison'] === 101) {
1323 // between:
1324 $inputVal = $this->cleanInputVal($conf, '1');
1325 $qsTmp = str_replace('#VALUE1#', trim($queryBuilder->quote($inputVal), '\''), $qsTmp);
1326 }
1327 $qs .= trim($qsTmp);
1328 return $qs;
1329 }
1330
1331 /**
1332 * Clear input value
1333 *
1334 * @param array $conf
1335 * @param string $suffix
1336 * @return string
1337 */
1338 public function cleanInputVal($conf, $suffix = '')
1339 {
1340 if ($conf['comparison'] >> 5 === 0 || ($conf['comparison'] === 32 || $conf['comparison'] === 33 || $conf['comparison'] === 64 || $conf['comparison'] === 65 || $conf['comparison'] === 66 || $conf['comparison'] === 67 || $conf['comparison'] === 96 || $conf['comparison'] === 97)) {
1341 $inputVal = $conf['inputValue' . $suffix];
1342 } elseif ($conf['comparison'] === 39 || $conf['comparison'] === 38) {
1343 // in list:
1344 $inputVal = implode(',', GeneralUtility::intExplode(',', $conf['inputValue' . $suffix]));
1345 } elseif ($conf['comparison'] === 68 || $conf['comparison'] === 69 || $conf['comparison'] === 162 || $conf['comparison'] === 163) {
1346 // in list:
1347 if (is_array($conf['inputValue' . $suffix])) {
1348 $inputVal = implode(',', $conf['inputValue' . $suffix]);
1349 } elseif ($conf['inputValue' . $suffix]) {
1350 $inputVal = $conf['inputValue' . $suffix];
1351 } else {
1352 $inputVal = 0;
1353 }
1354 } else {
1355 $inputVal = (float)$conf['inputValue' . $suffix];
1356 }
1357 return $inputVal;
1358 }
1359
1360 /**
1361 * Get user definition query
1362 *
1363 * @param array $qcArr
1364 * @param bool $first
1365 * @return void
1366 */
1367 public function getUserDefQuery($qcArr, $first)
1368 {
1369 }
1370
1371 /**
1372 * Update icon
1373 *
1374 * @return string
1375 */
1376 public function updateIcon()
1377 {
1378 return '<button class="btn btn-default" title="Update" name="just_update"><i class="fa fa-refresh fa-fw"></i></button>';
1379 }
1380
1381 /**
1382 * Get label column
1383 *
1384 * @return string
1385 */
1386 public function getLabelCol()
1387 {
1388 return $GLOBALS['TCA'][$this->table]['ctrl']['label'];
1389 }
1390
1391 /**
1392 * Make selector table
1393 *
1394 * @param array $modSettings
1395 * @param string $enableList
1396 * @return string
1397 */
1398 public function makeSelectorTable($modSettings, $enableList = 'table,fields,query,group,order,limit')
1399 {
1400 $out = [];
1401 $enableArr = explode(',', $enableList);
1402 $backendUserAuthentication = $this->getBackendUserAuthentication();
1403 // Make output
1404 if (in_array('table', $enableArr) && !$backendUserAuthentication->userTS['mod.']['dbint.']['disableSelectATable']) {
1405 $out[] = '<div class="form-group">';
1406 $out[] = ' <label for="SET[queryTable]">Select a table:</label>';
1407 $out[] = $this->mkTableSelect('SET[queryTable]', $this->table);
1408 $out[] = '</div>';
1409 }
1410 if ($this->table) {
1411 // Init fields:
1412 $this->setAndCleanUpExternalLists('queryFields', $modSettings['queryFields'], 'uid,' . $this->getLabelCol());
1413 $this->setAndCleanUpExternalLists('queryGroup', $modSettings['queryGroup']);
1414 $this->setAndCleanUpExternalLists('queryOrder', $modSettings['queryOrder'] . ',' . $modSettings['queryOrder2']);
1415 // Limit:
1416 $this->extFieldLists['queryLimit'] = $modSettings['queryLimit'];
1417 if (!$this->extFieldLists['queryLimit']) {
1418 $this->extFieldLists['queryLimit'] = 100;
1419 }
1420 $parts = GeneralUtility::intExplode(',', $this->extFieldLists['queryLimit']);
1421 if ($parts[1]) {
1422 $this->limitBegin = $parts[0];
1423 $this->limitLength = $parts[1];
1424 } else {
1425 $this->limitLength = $this->extFieldLists['queryLimit'];
1426 }
1427 $this->extFieldLists['queryLimit'] = implode(',', array_slice($parts, 0, 2));
1428 // Insert Descending parts
1429 if ($this->extFieldLists['queryOrder']) {
1430 $descParts = explode(',', $modSettings['queryOrderDesc'] . ',' . $modSettings['queryOrder2Desc']);
1431 $orderParts = explode(',', $this->extFieldLists['queryOrder']);
1432 $reList = [];
1433 foreach ($orderParts as $kk => $vv) {
1434 $reList[] = $vv . ($descParts[$kk] ? ' DESC' : '');
1435 }
1436 $this->extFieldLists['queryOrder_SQL'] = implode(',', $reList);
1437 }
1438 // Query Generator:
1439 $this->procesData($modSettings['queryConfig'] ? unserialize($modSettings['queryConfig']) : '');
1440 $this->queryConfig = $this->cleanUpQueryConfig($this->queryConfig);
1441 $this->enableQueryParts = (bool)$modSettings['search_query_smallparts'];
1442 $codeArr = $this->getFormElements();
1443 $queryCode = $this->printCodeArray($codeArr);
1444 if (in_array('fields', $enableArr) && !$backendUserAuthentication->userTS['mod.']['dbint.']['disableSelectFields']) {
1445 $out[] = '<div class="form-group form-group-with-button-addon">';
1446 $out[] = ' <label for="SET[queryFields]">Select fields:</label>';
1447 $out[] = $this->mkFieldToInputSelect('SET[queryFields]', $this->extFieldLists['queryFields']);
1448 $out[] = '</div>';
1449 }
1450 if (in_array('query', $enableArr) && !$backendUserAuthentication->userTS['mod.']['dbint.']['disableMakeQuery']) {
1451 $out[] = '<div class="form-group">';
1452 $out[] = ' <label>Make Query:</label>';
1453 $out[] = $queryCode;
1454 $out[] = '</div>';
1455 }
1456 if (in_array('group', $enableArr) && !$backendUserAuthentication->userTS['mod.']['dbint.']['disableGroupBy']) {
1457 $out[] = '<div class="form-group form-inline">';
1458 $out[] = ' <label for="SET[queryGroup]">Group By:</label>';
1459 $out[] = $this->mkTypeSelect('SET[queryGroup]', $this->extFieldLists['queryGroup'], '');
1460 $out[] = '</div>';
1461 }
1462 if (in_array('order', $enableArr) && !$backendUserAuthentication->userTS['mod.']['dbint.']['disableOrderBy']) {
1463 $module = $this->getModule();
1464 $orderByArr = explode(',', $this->extFieldLists['queryOrder']);
1465 $orderBy = [];
1466 $orderBy[] = $this->mkTypeSelect('SET[queryOrder]', $orderByArr[0], '');
1467 $orderBy[] = '<div class="checkbox">';
1468 $orderBy[] = ' <label for="checkQueryOrderDesc">';
1469 $orderBy[] = BackendUtility::getFuncCheck($module->id, 'SET[queryOrderDesc]', $modSettings['queryOrderDesc'], '', '', 'id="checkQueryOrderDesc"') . ' Descending';
1470 $orderBy[] = ' </label>';
1471 $orderBy[] = '</div>';
1472
1473 if ($orderByArr[0]) {
1474 $orderBy[] = $this->mkTypeSelect('SET[queryOrder2]', $orderByArr[1], '');
1475 $orderBy[] = '<div class="checkbox">';
1476 $orderBy[] = ' <label for="checkQueryOrder2Desc">';
1477 $orderBy[] = BackendUtility::getFuncCheck($module->id, 'SET[queryOrder2Desc]', $modSettings['queryOrder2Desc'], '', '', 'id="checkQueryOrder2Desc"') . ' Descending';
1478 $orderBy[] = ' </label>';
1479 $orderBy[] = '</div>';
1480 }
1481 $out[] = '<div class="form-group form-inline">';
1482 $out[] = ' <label>Order By:</label>';
1483 $out[] = implode(LF, $orderBy);
1484 $out[] = '</div>';
1485 }
1486 if (in_array('limit', $enableArr) && !$backendUserAuthentication->userTS['mod.']['dbint.']['disableLimit']) {
1487 $limit = [];
1488 $limit[] = '<div class="input-group">';
1489 $limit[] = ' <div class="input-group-addon">';
1490 $limit[] = ' <span class="input-group-btn">';
1491 $limit[] = $this->updateIcon();
1492 $limit[] = ' </span>';
1493 $limit[] = ' </div>';
1494 $limit[] = ' <input type="text" class="form-control" value="' . htmlspecialchars($this->extFieldLists['queryLimit']) . '" name="SET[queryLimit]" id="queryLimit">';
1495 $limit[] = '</div>';
1496
1497 $prevLimit = $this->limitBegin - $this->limitLength < 0 ? 0 : $this->limitBegin - $this->limitLength;
1498 $prevButton = '';
1499 $nextButton = '';
1500
1501 if ($this->limitBegin) {
1502 $prevButton = '<input type="button" class="btn btn-default" value="previous ' . htmlspecialchars($this->limitLength) . '" data-value="' . htmlspecialchars($prevLimit . ',' . $this->limitLength) . '">';
1503 }
1504 if (!$this->limitLength) {
1505 $this->limitLength = 100;
1506 }
1507
1508 $nextLimit = $this->limitBegin + $this->limitLength;
1509 if ($nextLimit < 0) {
1510 $nextLimit = 0;
1511 }
1512 if ($nextLimit) {
1513 $nextButton = '<input type="button" class="btn btn-default" value="next ' . htmlspecialchars($this->limitLength) . '" data-value="' . htmlspecialchars($nextLimit . ',' . $this->limitLength) . '">';
1514 }
1515
1516 $out[] = '<div class="form-group form-group-with-button-addon">';
1517 $out[] = ' <label>Limit:</label>';
1518 $out[] = ' <div class="form-inline">';
1519 $out[] = implode(LF, $limit);
1520 $out[] = ' <div class="input-group">';
1521 $out[] = ' <div class="btn-group t3js-limit-submit">';
1522 $out[] = $prevButton;
1523 $out[] = $nextButton;
1524 $out[] = ' </div>';
1525 $out[] = ' <div class="btn-group t3js-limit-submit">';
1526 $out[] = ' <input type="button" class="btn btn-default" data-value="10" value="10">';
1527 $out[] = ' <input type="button" class="btn btn-default" data-value="20" value="20">';
1528 $out[] = ' <input type="button" class="btn btn-default" data-value="50" value="50">';
1529 $out[] = ' <input type="button" class="btn btn-default" data-value="100" value="100">';
1530 $out[] = ' </div>';
1531 $out[] = ' </div>';
1532 $out[] = ' </div>';
1533 $out[] = '</div>';
1534 }
1535 }
1536 return implode(LF, $out);
1537 }
1538
1539 /**
1540 * Get tree list
1541 *
1542 * @param int $id
1543 * @param int $depth
1544 * @param int $begin
1545 * @param string $permClause
1546 * @return string
1547 */
1548 public function getTreeList($id, $depth, $begin = 0, $permClause)
1549 {
1550 $depth = (int)$depth;
1551 $begin = (int)$begin;
1552 $id = (int)$id;
1553 if ($id < 0) {
1554 $id = abs($id);
1555 }
1556 if ($begin === 0) {
1557 $theList = $id;
1558 } else {
1559 $theList = '';
1560 }
1561 if ($id && $depth > 0) {
1562 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
1563 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
1564 $statement = $queryBuilder->select('uid')
1565 ->from('pages')
1566 ->where(
1567 $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)),
1568 QueryHelper::stripLogicalOperatorPrefix($permClause)
1569 )
1570 ->execute();
1571 while ($row = $statement->fetch()) {
1572 if ($begin <= 0) {
1573 $theList .= ',' . $row['uid'];
1574 }
1575 if ($depth > 1) {
1576 $theList .= $this->getTreeList($row['uid'], $depth - 1, $begin - 1, $permClause);
1577 }
1578 }
1579 }
1580 return $theList;
1581 }
1582
1583 /**
1584 * Get select query
1585 *
1586 * @param string $qString
1587 * @return bool|\mysqli_result|object
1588 */
1589 public function getSelectQuery($qString = '')
1590 {
1591 $backendUserAuthentication = $this->getBackendUserAuthentication();
1592 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
1593 if ($this->getModule()->MOD_SETTINGS['show_deleted']) {
1594 $queryBuilder->getRestrictions()->removeAll();
1595 } else {
1596 $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
1597 }
1598 $fieldList = GeneralUtility::trimExplode(',', $this->extFieldLists['queryFields']
1599 . ',pid'
1600 . ($GLOBALS['TCA'][$this->table]['ctrl']['delete'] ? ',' . $GLOBALS['TCA'][$this->table]['ctrl']['delete'] : '')
1601 );
1602 $queryBuilder->select(...$fieldList)
1603 ->from($this->table);
1604
1605 if ($this->extFieldLists['queryGroup']) {
1606 $queryBuilder->groupBy(...QueryHelper::parseGroupBy($this->extFieldLists['queryGroup']));
1607 }
1608 if ($this->extFieldLists['queryOrder']) {
1609 foreach (QueryHelper::parseOrderBy($this->extFieldLists['queryOrder_SQL']) as $orderPair) {
1610 list($fieldName, $order) = $orderPair;
1611 $queryBuilder->addOrderBy($fieldName, $order);
1612 }
1613 }
1614 if ($this->extFieldLists['queryLimit']) {
1615 $queryBuilder->setMaxResults((int)$this->extFieldLists['queryLimit']);
1616 }
1617
1618 if (!$backendUserAuthentication->isAdmin() && $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts']) {
1619 $webMounts = $backendUserAuthentication->returnWebmounts();
1620 $perms_clause = $backendUserAuthentication->getPagePermsClause(1);
1621 $webMountPageTree = '';
1622 $webMountPageTreePrefix = '';
1623 foreach ($webMounts as $webMount) {
1624 if ($webMountPageTree) {
1625 $webMountPageTreePrefix = ',';
1626 }
1627 $webMountPageTree .= $webMountPageTreePrefix
1628 . $this->getTreeList($webMount, 999, ($begin = 0), $perms_clause);
1629 }
1630 if ($this->table === 'pages') {
1631 $queryBuilder->where(
1632 QueryHelper::stripLogicalOperatorPrefix($perms_clause),
1633 $queryBuilder->expr()->in(
1634 'uid',
1635 $queryBuilder->createNamedParameter(
1636 GeneralUtility::intExplode(',', $webMountPageTree),
1637 Connection::PARAM_INT_ARRAY
1638 )
1639 )
1640 );
1641 } else {
1642 $queryBuilder->where(
1643 $queryBuilder->expr()->in(
1644 'pid',
1645 $queryBuilder->createNamedParameter(
1646 GeneralUtility::intExplode(',', $webMountPageTree),
1647 Connection::PARAM_INT_ARRAY
1648 )
1649 )
1650 );
1651 }
1652 }
1653 if (!$qString) {
1654 $qString = $this->getQuery($this->queryConfig);
1655 }
1656 $queryBuilder->andWhere(QueryHelper::stripLogicalOperatorPrefix($qString));
1657
1658 return $queryBuilder->getSQL();
1659 }
1660
1661 /**
1662 * @param string $name the field name
1663 * @param int $timestamp the unix timestamp
1664 * @param string $type [datetime, date, time, timesec, year]
1665 *
1666 * @return string
1667 */
1668 protected function getDateTimePickerField($name, $timestamp, $type)
1669 {
1670 $dateFormat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? '%H:%M %m-%d-%Y' : '%H:%M %d-%m-%Y';
1671 $value = ($timestamp > 0 ? strftime($dateFormat, $timestamp) : '');
1672 $id = StringUtility::getUniqueId('dt_');
1673 $html = [];
1674 $html[] = '<div class="input-group" id="' . $id . '-wrapper">';
1675 $html[] = ' <input data-formengine-input-name="' . htmlspecialchars($name) . '" value="' . $value . '" class="form-control t3js-datetimepicker t3js-clearable" data-date-type="' . htmlspecialchars($type) . '" data-date-offset="0" type="text" id="' . $id . '">';
1676 $html[] = ' <input name="' . htmlspecialchars($name) . '" value="' . (int)$timestamp . '" type="hidden">';
1677 $html[] = ' <span class="input-group-btn">';
1678 $html[] = ' <label class="btn btn-default" for="' . $id . '">';
1679 $html[] = ' <span class="fa fa-calendar"></span>';
1680 $html[] = ' </label>';
1681 $html[] = ' </span>';
1682 $html[] = '</div>';
1683 return implode(LF, $html);
1684 }
1685
1686 /**
1687 * Sets the current name of the input form.
1688 *
1689 * @param string $formName The name of the form.
1690 * @return void
1691 */
1692 public function setFormName($formName)
1693 {
1694 $this->formName = trim($formName);
1695 }
1696
1697 /**
1698 * @return BackendUserAuthentication
1699 */
1700 protected function getBackendUserAuthentication()
1701 {
1702 return $GLOBALS['BE_USER'];
1703 }
1704
1705 /**
1706 * @return BaseScriptClass
1707 */
1708 protected function getModule()
1709 {
1710 return $GLOBALS['SOBE'];
1711 }
1712
1713 /**
1714 * @return LanguageService
1715 */
1716 protected function getLanguageService()
1717 {
1718 return $GLOBALS['LANG'];
1719 }
1720 }