Clipboard.php 42.7 KB
Newer Older
1
2
3
<?php
namespace TYPO3\CMS\Backend\Clipboard;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
Nicole Cordes's avatar
Nicole Cordes committed
16

17
use TYPO3\CMS\Backend\Routing\UriBuilder;
Nicole Cordes's avatar
Nicole Cordes committed
18
use TYPO3\CMS\Backend\Utility\BackendUtility;
19
use TYPO3\CMS\Core\Database\ConnectionPool;
20
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
21
22
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
Nicole Cordes's avatar
Nicole Cordes committed
23
use TYPO3\CMS\Core\Resource\ResourceFactory;
24
use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
Nicole Cordes's avatar
Nicole Cordes committed
25
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
Nicole Cordes's avatar
Nicole Cordes committed
27
use TYPO3\CMS\Core\Utility\MathUtility;
28
use TYPO3\CMS\Fluid\View\StandaloneView;
Nicole Cordes's avatar
Nicole Cordes committed
29

30
31
32
/**
 * TYPO3 clipboard for records and files
 */
33
34
35
36
37
38
class Clipboard
{
    /**
     * @var int
     */
    public $numberTabs = 3;
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    /**
     * Clipboard data kept here
     *
     * Keys:
     * 'normal'
     * 'tab_[x]' where x is >=1 and denotes the pad-number
     * 'mode'	:	'copy' means copy-mode, default = moving ('cut')
     * 'el'	:	Array of elements:
     * DB: keys = '[tablename]|[uid]'	eg. 'tt_content:123'
     * DB: values = 1 (basically insignificant)
     * FILE: keys = '_FILE|[shortmd5 of path]'	eg. '_FILE|9ebc7e5c74'
     * FILE: values = The full filepath, eg. '/www/htdocs/typo3/32/dummy/fileadmin/sem1_3_examples/alternative_index.php'
     * or 'C:/www/htdocs/typo3/32/dummy/fileadmin/sem1_3_examples/alternative_index.php'
     *
     * 'current' pointer to current tab (among the above...)
     *
     * The virtual tablename '_FILE' will always indicate files/folders. When checking for elements from eg. 'all tables'
     * (by using an empty string) '_FILE' entries are excluded (so in effect only DB elements are counted)
     *
     * @var array
     */
61
    public $clipData = [];
62

63
64
65
66
    /**
     * @var int
     */
    public $changed = 0;
67

68
69
70
71
    /**
     * @var string
     */
    public $current = '';
72

73
74
75
76
    /**
     * @var int
     */
    public $lockToNormal = 0;
77

78
79
80
    /**
     * If set, clipboard is displaying files.
     *
81
     * @var bool
82
     */
83
    public $fileMode = false;
84

85
86
87
88
    /**
     * @var IconFactory
     */
    protected $iconFactory;
89

90
91
92
93
94
    /**
     * @var StandaloneView
     */
    protected $view;

95
96
    /**
     * Construct
97
98
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidExtensionNameException
     * @throws \InvalidArgumentException
99
100
101
102
     */
    public function __construct()
    {
        $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
103
        $this->view = $this->getStandaloneView();
104
    }
105

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    /*****************************************
     *
     * Initialize
     *
     ****************************************/
    /**
     * Initialize the clipboard from the be_user session
     */
    public function initializeClipboard()
    {
        // Get data
        $clipData = $this->getBackendUser()->getModuleData('clipboard', $this->getBackendUser()->getTSConfigVal('options.saveClipboard') ? '' : 'ses');
        // NumberTabs
        $clNP = $this->getBackendUser()->getTSConfigVal('options.clipboardNumberPads');
        if (MathUtility::canBeInterpretedAsInteger($clNP) && $clNP >= 0) {
            $this->numberTabs = MathUtility::forceIntegerInRange($clNP, 0, 20);
        }
        // Resets/reinstates the clipboard pads
124
        $this->clipData['normal'] = is_array($clipData['normal']) ? $clipData['normal'] : [];
125
        for ($a = 1; $a <= $this->numberTabs; $a++) {
126
            $this->clipData['tab_' . $a] = is_array($clipData['tab_' . $a]) ? $clipData['tab_' . $a] : [];
127
128
129
130
        }
        // Setting the current pad pointer ($this->current))
        $this->clipData['current'] = ($this->current = isset($this->clipData[$clipData['current']]) ? $clipData['current'] : 'normal');
    }
131

132
133
134
135
136
137
138
139
140
141
    /**
     * Call this method after initialization if you want to lock the clipboard to operate on the normal pad only.
     * Trying to switch pad through ->setCmd will not work.
     * This is used by the clickmenu since it only allows operation on single elements at a time (that is the "normal" pad)
     */
    public function lockToNormal()
    {
        $this->lockToNormal = 1;
        $this->current = 'normal';
    }
142

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    /**
     * The array $cmd may hold various keys which notes some action to take.
     * Normally perform only one action at a time.
     * In scripts like db_list.php / filelist/mod1/index.php the GET-var CB is used to control the clipboard.
     *
     * Selecting / Deselecting elements
     * Array $cmd['el'] has keys = element-ident, value = element value (see description of clipData array in header)
     * Selecting elements for 'copy' should be done by simultaneously setting setCopyMode.
     *
     * @param array $cmd Array of actions, see function description
     */
    public function setCmd($cmd)
    {
        if (is_array($cmd['el'])) {
            foreach ($cmd['el'] as $k => $v) {
158
                if ($this->current === 'normal') {
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
                    unset($this->clipData['normal']);
                }
                if ($v) {
                    $this->clipData[$this->current]['el'][$k] = $v;
                } else {
                    $this->removeElement($k);
                }
                $this->changed = 1;
            }
        }
        // Change clipboard pad (if not locked to normal)
        if ($cmd['setP']) {
            $this->setCurrentPad($cmd['setP']);
        }
        // Remove element	(value = item ident: DB; '[tablename]|[uid]'    FILE: '_FILE|[shortmd5 hash of path]'
        if ($cmd['remove']) {
            $this->removeElement($cmd['remove']);
            $this->changed = 1;
        }
        // Remove all on current pad (value = pad-ident)
        if ($cmd['removeAll']) {
180
            $this->clipData[$cmd['removeAll']] = [];
181
182
183
184
185
186
187
188
            $this->changed = 1;
        }
        // Set copy mode of the tab
        if (isset($cmd['setCopyMode'])) {
            $this->clipData[$this->current]['mode'] = $this->isElements() ? ($cmd['setCopyMode'] ? 'copy' : '') : '';
            $this->changed = 1;
        }
    }
189

190
191
192
193
194
195
196
197
198
199
200
201
    /**
     * Setting the current pad on clipboard
     *
     * @param string $padIdent Key in the array $this->clipData
     */
    public function setCurrentPad($padIdent)
    {
        // Change clipboard pad (if not locked to normal)
        if (!$this->lockToNormal && $this->current != $padIdent) {
            if (isset($this->clipData[$padIdent])) {
                $this->clipData['current'] = ($this->current = $padIdent);
            }
202
            if ($this->current !== 'normal' || !$this->isElements()) {
203
204
205
206
207
208
                $this->clipData[$this->current]['mode'] = '';
            }
            // Setting mode to default (move) if no items on it or if not 'normal'
            $this->changed = 1;
        }
    }
209

210
211
212
213
214
215
216
217
218
219
220
    /**
     * Call this after initialization and setCmd in order to save the clipboard to the user session.
     * The function will check if the internal flag ->changed has been set and if so, save the clipboard. Else not.
     */
    public function endClipboard()
    {
        if ($this->changed) {
            $this->saveClipboard();
        }
        $this->changed = 0;
    }
221

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    /**
     * Cleans up an incoming element array $CBarr (Array selecting/deselecting elements)
     *
     * @param array $CBarr Element array from outside ("key" => "selected/deselected")
     * @param string $table The 'table which is allowed'. Must be set.
     * @param bool|int $removeDeselected Can be set in order to remove entries which are marked for deselection.
     * @return array Processed input $CBarr
     */
    public function cleanUpCBC($CBarr, $table, $removeDeselected = 0)
    {
        if (is_array($CBarr)) {
            foreach ($CBarr as $k => $v) {
                $p = explode('|', $k);
                if ((string)$p[0] != (string)$table || $removeDeselected && !$v) {
                    unset($CBarr[$k]);
                }
            }
        }
        return $CBarr;
    }
242

243
244
245
246
247
248
249
250
251
    /*****************************************
     *
     * Clipboard HTML renderings
     *
     ****************************************/
    /**
     * Prints the clipboard
     *
     * @return string HTML output
252
     * @throws \BadFunctionCallException
253
254
255
     */
    public function printClipboard()
    {
256
        $languageService = $this->getLanguageService();
257
258
259
        $elementCount = count($this->elFromTable($this->fileMode ? '_FILE' : ''));
        // Copymode Selector menu
        $copymodeUrl = GeneralUtility::linkThisScript();
260

261
262
263
264
        $this->view->assign('actionCopyModeUrl', htmlspecialchars(GeneralUtility::quoteJSvalue($copymodeUrl . '&CB[setCopyMode]=')));
        $this->view->assign('actionCopyModeUrl1', htmlspecialchars(GeneralUtility::quoteJSvalue($copymodeUrl . '&CB[setCopyMode]=1')));
        $this->view->assign('currentMode', $this->currentMode());
        $this->view->assign('elementCount', $elementCount);
265

266
        if ($elementCount) {
267
            $removeAllUrl = GeneralUtility::linkThisScript(['CB' => ['removeAll' => $this->current]]);
268
            $this->view->assign('removeAllUrl', $removeAllUrl);
269
270

            // Selector menu + clear button
271
            $optionArray = [];
272
273
            // Import / Export link:
            if (ExtensionManagementUtility::isLoaded('impexp')) {
274
275
                $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
                $url = $uriBuilder->buildUriFromRoute('xMOD_tximpexp', $this->exportClipElementParameters());
276
277
                $optionArray[] = [
                    'label' => $this->clLabel('export', 'rm'),
278
                    'uri' => (string)$url
279
                ];
280
281
282
            }
            // Edit:
            if (!$this->fileMode) {
283
284
285
286
287
288
289
                $optionArray[] = [
                    'label' => $this->clLabel('edit', 'rm'),
                    'uri' => '#',
                    'additionalAttributes' => [
                        'onclick' => htmlspecialchars('window.location.href=' . GeneralUtility::quoteJSvalue($this->editUrl() . '&returnUrl=') . '+top.rawurlencode(window.location.href);'),
                    ]
                ];
290
291
292
            }

            // Delete referenced elements:
293
            $confirmationCheck = false;
294
            if ($this->getBackendUser()->jsConfirmation(JsConfirmation::DELETE)) {
295
                $confirmationCheck = true;
296
            }
297

298
            $confirmationMessage = sprintf(
299
                $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:mess.deleteClip'),
300
301
302
                $elementCount
            );
            $title = $languageService
303
                ->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.clipboard.delete_elements');
304
            $returnUrl = $this->deleteUrl(true, $this->fileMode);
305
            $btnOkText = $languageService
306
                ->sL('LLL:EXT:backend/Resources/Private/Language/locallang_alt_doc.xlf:buttons.confirm.delete_elements.yes');
307
            $btnCancelText = $languageService
308
                ->sL('LLL:EXT:backend/Resources/Private/Language/locallang_alt_doc.xlf:buttons.confirm.delete_elements.no');
309
310
311
312
313
314
315
316
317
318
319
320
321
322
            $optionArray[] = [
                'label' => htmlspecialchars($title),
                'uri' => $returnUrl,
                'additionalAttributes' => [
                    'class' => $confirmationCheck ? 't3js-modal-trigger' : '',
                ],
                'data' => [
                    'severity' => 'warning',
                    'button-close-text' => htmlspecialchars($btnCancelText),
                    'button-ok-text' => htmlspecialchars($btnOkText),
                    'content' => htmlspecialchars($confirmationMessage),
                    'title' => htmlspecialchars($title)
                ]
            ];
323
324

            // Clear clipboard
325
            $optionArray[] = [
326
                'label' => $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.clipboard.clear_clipboard', true),
327
328
329
                'uri' => $removeAllUrl . '#clip_head'
            ];
            $this->view->assign('optionArray', $optionArray);
330
        }
331

332
        // Print header and content for the NORMAL tab:
333
334
335
336
337
338
339
340
341
342
        $this->view->assign('current', $this->current);
        $tabArray = [];
        $tabArray['normal'] = [
            'id' => 'normal',
            'number' => 0,
            'url' => GeneralUtility::linkThisScript(['CB' => ['setP' => 'normal']]),
            'description' => 'normal-description',
            'label' => 'labels.normal',
            'padding' => $this->padTitle('normal')
        ];
343
        if ($this->current === 'normal') {
344
            $tabArray['normal']['content'] = $this->getContentFromTab('normal');
345
346
347
        }
        // Print header and content for the NUMERIC tabs:
        for ($a = 1; $a <= $this->numberTabs; $a++) {
348
349
350
351
352
353
354
355
356
357
            $tabArray['tab_' . $a] = [
                'id' => 'tab_' . $a,
                'number' => $a,
                'url' => GeneralUtility::linkThisScript(['CB' => ['setP' => 'tab_' . $a]]),
                'description' => 'cliptabs-description',
                'label' => 'labels.cliptabs-name',
                'padding' => $this->padTitle('tab_' . $a)
            ];
            if ($this->current === 'tab_' . $a) {
                $tabArray['tab_' . $a]['content'] = $this->getContentFromTab('tab_' . $a);
358
359
            }
        }
360
361
362
        $this->view->assign('clipboardHeader', BackendUtility::wrapInHelp('xMOD_csh_corebe', 'list_clipboard', $this->clLabel('clipboard', 'buttons')));
        $this->view->assign('tabArray', $tabArray);
        return $this->view->render();
363
    }
364

365
366
367
368
369
370
371
    /**
     * Print the content on a pad. Called from ->printClipboard()
     *
     * @access private
     * @param string $pad Pad reference
     * @return array Array with table rows for the clipboard.
     */
372
    public function getContentFromTab($pad)
373
    {
374
        $lines = [];
375
        if (is_array($this->clipData[$pad]['el'] ?? false)) {
376
377
378
379
            foreach ($this->clipData[$pad]['el'] as $k => $v) {
                if ($v) {
                    list($table, $uid) = explode('|', $k);
                    // Rendering files/directories on the clipboard
380
                    if ($table === '_FILE') {
381
382
                        $fileObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($v);
                        if ($fileObject) {
383
                            $thumb = [];
384
385
                            $folder = $fileObject instanceof \TYPO3\CMS\Core\Resource\Folder;
                            $size = $folder ? '' : '(' . GeneralUtility::formatSize($fileObject->getSize()) . 'bytes)';
386
387
388
389
390
391
                            if (
                                !$folder
                                && GeneralUtility::inList(
                                    $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
                                    $fileObject->getExtension()
                                )
392
393
                            ) {
                                $thumb = [
394
                                    'image' => $fileObject->process(\TYPO3\CMS\Core\Resource\ProcessedFile::CONTEXT_IMAGEPREVIEW, [])->getPublicUrl(true),
395
396
                                    'title' => htmlspecialchars($fileObject->getName())
                                ];
397
                            }
398
                            $lines[] = [
399
400
401
402
403
404
405
406
                                'icon' => '<span title="' . htmlspecialchars($fileObject->getName() . ' ' . $size) . '">' . $this->iconFactory->getIconForResource(
                                    $fileObject,
                                    Icon::SIZE_SMALL
                                )->render() . '</span>',
                                'title' => $this->linkItemText(htmlspecialchars(GeneralUtility::fixed_lgd_cs(
                                    $fileObject->getName(),
                                    $this->getBackendUser()->uc['titleLen']
                                )), $fileObject->getName()),
407
                                'thumb' => $thumb,
408
                                'infoLink' => htmlspecialchars('top.TYPO3.InfoWindow.showItem(' . GeneralUtility::quoteJSvalue($table) . ', ' . GeneralUtility::quoteJSvalue($v) . '); return false;'),
409
410
                                'removeLink' => $this->removeUrl('_FILE', GeneralUtility::shortMD5($v))
                            ];
411
412
413
414
415
416
417
418
419
                        } else {
                            // If the file did not exist (or is illegal) then it is removed from the clipboard immediately:
                            unset($this->clipData[$pad]['el'][$k]);
                            $this->changed = 1;
                        }
                    } else {
                        // Rendering records:
                        $rec = BackendUtility::getRecordWSOL($table, $uid);
                        if (is_array($rec)) {
420
                            $lines[] = [
421
422
423
424
425
426
427
428
429
                                'icon' => $this->linkItemText($this->iconFactory->getIconForRecord(
                                    $table,
                                    $rec,
                                    Icon::SIZE_SMALL
                                )->render(), $rec, $table),
                                'title' => $this->linkItemText(htmlspecialchars(GeneralUtility::fixed_lgd_cs(BackendUtility::getRecordTitle(
                                    $table,
                                    $rec
                                ), $this->getBackendUser()->uc['titleLen'])), $rec, $table),
430
                                'infoLink' => htmlspecialchars('top.TYPO3.InfoWindow.showItem(' . GeneralUtility::quoteJSvalue($table) . ', \'' . (int)$uid . '\'); return false;'),
431
432
433
                                'removeLink' => $this->removeUrl($table, $uid)
                            ];

434
                            $localizationData = $this->getLocalizations($table, $rec);
435
436
                            if (!empty($localizationData)) {
                                $lines = array_merge($lines, $localizationData);
437
438
439
440
441
442
443
444
445
446
447
448
                            }
                        } else {
                            unset($this->clipData[$pad]['el'][$k]);
                            $this->changed = 1;
                        }
                    }
                }
            }
        }
        $this->endClipboard();
        return $lines;
    }
449

450
451
452
453
454
455
456
457
458
459
460
461
    /**
     * Returns true if the clipboard contains elements
     *
     * @return bool
     */
    public function hasElements()
    {
        foreach ($this->clipData as $data) {
            if (isset($data['el']) && is_array($data['el']) && !empty($data['el'])) {
                return true;
            }
        }
462

463
464
        return false;
    }
465

466
467
468
469
470
    /**
     * Gets all localizations of the current record.
     *
     * @param string $table The table
     * @param array $parentRec The current record
471
     * @return array HTML table rows
472
     */
473
    public function getLocalizations($table, $parentRec)
474
    {
475
        $lines = [];
476
        $tcaCtrl = $GLOBALS['TCA'][$table]['ctrl'];
477
        if (BackendUtility::isTableLocalizable($table)) {
478
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
479
480
481
482
            $queryBuilder->getRestrictions()
                ->removeAll()
                ->add(GeneralUtility::makeInstance(DeletedRestriction::class));

483
484
485
            $queryBuilder
                ->select('*')
                ->from($table)
486
                ->where(
487
488
489
490
491
492
493
                    $queryBuilder->expr()->eq(
                        $tcaCtrl['transOrigPointerField'],
                        $queryBuilder->createNamedParameter($parentRec['uid'], \PDO::PARAM_INT)
                    ),
                    $queryBuilder->expr()->neq(
                        $tcaCtrl['languageField'],
                        $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
494
495
496
497
                    ),
                    $queryBuilder->expr()->gt(
                        'pid',
                        $queryBuilder->createNamedParameter(-1, \PDO::PARAM_INT)
498
                    )
499
500
                );

501
            if (isset($tcaCtrl['versioningWS']) && $tcaCtrl['versioningWS']) {
502
503
504
505
506
507
508
                $queryBuilder
                    ->andWhere(
                        $queryBuilder->expr()->eq(
                            't3ver_wsid',
                            $queryBuilder->createNamedParameter($parentRec['t3ver_wsid'], \PDO::PARAM_INT)
                        )
                    );
509
            }
510
            $rows = $queryBuilder->execute()->fetchAll();
511
512
            if (is_array($rows)) {
                foreach ($rows as $rec) {
513
514
515
516
                    $lines[] = [
                        'icon' => $this->iconFactory->getIconForRecord($table, $rec, Icon::SIZE_SMALL)->render(),
                        'title' => htmlspecialchars(GeneralUtility::fixed_lgd_cs(BackendUtility::getRecordTitle($table, $rec), $this->getBackendUser()->uc['titleLen']))
                    ];
517
518
519
                }
            }
        }
520
        return $lines;
521
    }
522

523
    /**
524
     * Warps title with number of elements if any.
525
526
     *
     * @param string  $pad Pad reference
527
     * @return string padding
528
     */
529
    public function padTitle($pad)
530
531
532
    {
        $el = count($this->elFromTable($this->fileMode ? '_FILE' : '', $pad));
        if ($el) {
533
            return ' (' . ($pad === 'normal' ? ($this->clipData['normal']['mode'] === 'copy' ? $this->clLabel('copy', 'cm') : $this->clLabel('cut', 'cm')) : htmlspecialchars($el)) . ')';
534
        }
535
        return '';
536
    }
537

538
539
540
541
542
543
544
545
546
547
    /**
     * Wraps the title of the items listed in link-tags. The items will link to the page/folder where they originate from
     *
     * @param string $str Title of element - must be htmlspecialchar'ed on beforehand.
     * @param mixed $rec If array, a record is expected. If string, its a path
     * @param string $table Table name
     * @return string
     */
    public function linkItemText($str, $rec, $table = '')
    {
548
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
549
550
551
552
        if (is_array($rec) && $table) {
            if ($this->fileMode) {
                $str = '<span class="text-muted">' . $str . '</span>';
            } else {
553
                $str = '<a href="' . htmlspecialchars((string)$uriBuilder->buildUriFromRoute('web_list', ['id' => $rec['pid']])) . '">' . $str . '</a>';
554
555
556
557
            }
        } elseif (file_exists($rec)) {
            if (!$this->fileMode) {
                $str = '<span class="text-muted">' . $str . '</span>';
558
559
            } elseif (ExtensionManagementUtility::isLoaded('filelist')) {
                $str = '<a href="' . htmlspecialchars((string)$uriBuilder->buildUriFromRoute('file_list', ['id' => dirname($rec)])) . '">' . $str . '</a>';
560
561
562
563
            }
        }
        return $str;
    }
564

565
566
567
568
569
570
571
572
573
574
    /**
     * Returns the select-url for database elements
     *
     * @param string $table Table name
     * @param int $uid Uid of record
     * @param bool|int $copy If set, copymode will be enabled
     * @param bool|int $deselect If set, the link will deselect, otherwise select.
     * @param array $baseArray The base array of GET vars to be sent in addition. Notice that current GET vars WILL automatically be included.
     * @return string URL linking to the current script but with the CB array set to select the element with table/uid
     */
575
    public function selUrlDB($table, $uid, $copy = 0, $deselect = 0, $baseArray = [])
576
    {
577
        $CB = ['el' => [rawurlencode($table . '|' . $uid) => $deselect ? 0 : 1]];
578
579
580
581
582
583
        if ($copy) {
            $CB['setCopyMode'] = 1;
        }
        $baseArray['CB'] = $CB;
        return GeneralUtility::linkThisScript($baseArray);
    }
584

585
586
587
588
589
590
591
592
593
    /**
     * Returns the select-url for files
     *
     * @param string $path Filepath
     * @param bool|int $copy If set, copymode will be enabled
     * @param bool|int $deselect If set, the link will deselect, otherwise select.
     * @param array $baseArray The base array of GET vars to be sent in addition. Notice that current GET vars WILL automatically be included.
     * @return string URL linking to the current script but with the CB array set to select the path
     */
594
    public function selUrlFile($path, $copy = 0, $deselect = 0, $baseArray = [])
595
    {
596
        $CB = ['el' => [rawurlencode('_FILE|' . GeneralUtility::shortMD5($path)) => $deselect ? '' : $path]];
597
598
599
600
601
602
        if ($copy) {
            $CB['setCopyMode'] = 1;
        }
        $baseArray['CB'] = $CB;
        return GeneralUtility::linkThisScript($baseArray);
    }
603

604
605
606
607
608
609
610
611
    /**
     * pasteUrl of the element (database and file)
     * For the meaning of $table and $uid, please read from ->makePasteCmdArray!!!
     * The URL will point to tce_file or tce_db depending in $table
     *
     * @param string $table Tablename (_FILE for files)
     * @param mixed $uid "destination": can be positive or negative indicating how the paste is done (paste into / paste after)
     * @param bool $setRedirect If set, then the redirect URL will point back to the current script, but with CB reset.
612
     * @param array|null $update Additional key/value pairs which should get set in the moved/copied record (via DataHandler)
613
614
615
616
617
618
619
620
621
     * @return string
     */
    public function pasteUrl($table, $uid, $setRedirect = true, array $update = null)
    {
        $urlParameters = [
            'CB[paste]' => $table . '|' . $uid,
            'CB[pad]' => $this->current
        ];
        if ($setRedirect) {
622
            $urlParameters['redirect'] = GeneralUtility::linkThisScript(['CB' => '']);
623
624
625
626
        }
        if (is_array($update)) {
            $urlParameters['CB[update]'] = $update;
        }
627
628
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
        return (string)$uriBuilder->buildUriFromRoute($table === '_FILE' ? 'tce_file' : 'tce_db', $urlParameters);
629
    }
630

631
632
633
    /**
     * deleteUrl for current pad
     *
634
635
     * @param bool $setRedirect If set, then the redirect URL will point back to the current script, but with CB reset.
     * @param bool $file If set, then the URL will link to the tce_file.php script in the typo3/ dir.
636
637
     * @return string
     */
638
    public function deleteUrl($setRedirect = true, $file = false)
639
640
641
642
643
644
    {
        $urlParameters = [
            'CB[delete]' => 1,
            'CB[pad]' => $this->current
        ];
        if ($setRedirect) {
645
            $urlParameters['redirect'] = GeneralUtility::linkThisScript(['CB' => '']);
646
        }
647
648
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
        return (string)$uriBuilder->buildUriFromRoute($file ? 'tce_file' : 'tce_db', $urlParameters);
649
    }
650

651
652
653
654
655
656
657
658
659
    /**
     * editUrl of all current elements
     * ONLY database
     * Links to FormEngine
     *
     * @return string The URL to FormEngine with parameters.
     */
    public function editUrl()
    {
660
        $parameters = [];
661
662
663
664
665
666
        // All records
        $elements = $this->elFromTable('');
        foreach ($elements as $tP => $value) {
            list($table, $uid) = explode('|', $tP);
            $parameters['edit[' . $table . '][' . $uid . ']'] = 'edit';
        }
667
668
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
        return (string)$uriBuilder->buildUriFromRoute('record_edit', $parameters);
669
    }
670

671
672
673
674
675
676
677
678
679
680
    /**
     * Returns the remove-url (file and db)
     * for file $table='_FILE' and $uid = shortmd5 hash of path
     *
     * @param string $table Tablename
     * @param string $uid Uid integer/shortmd5 hash
     * @return string URL
     */
    public function removeUrl($table, $uid)
    {
681
        return GeneralUtility::linkThisScript(['CB' => ['remove' => $table . '|' . $uid]]);
682
    }
683

684
685
686
687
688
689
    /**
     * Returns confirm JavaScript message
     *
     * @param string $table Table name
     * @param mixed $rec For records its an array, for files its a string (path)
     * @param string $type Type-code
690
691
692
693
694
     * @param array $clElements Array of selected elements
     * @param string $columnLabel Name of the content column
     * @return string the text for a confirm message
     */
    public function confirmMsgText($table, $rec, $type, $clElements, $columnLabel = '')
695
696
    {
        if ($this->getBackendUser()->jsConfirmation(JsConfirmation::COPY_MOVE_PASTE)) {
697
            $labelKey = 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:mess.' . ($this->currentMode() === 'copy' ? 'copy' : 'move') . ($this->current === 'normal' ? '' : 'cb') . '_' . $type;
Christian Kuhn's avatar
Christian Kuhn committed
698
            $msg = $this->getLanguageService()->sL($labelKey . ($columnLabel ? '_colPos' : ''));
699
            if ($table === '_FILE') {
700
                $thisRecTitle = basename($rec);
701
                if ($this->current === 'normal') {
702
703
704
705
706
707
                    $selItem = reset($clElements);
                    $selRecTitle = basename($selItem);
                } else {
                    $selRecTitle = count($clElements);
                }
            } else {
708
709
                $thisRecTitle = $table === 'pages' && !is_array($rec) ? $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] : BackendUtility::getRecordTitle($table, $rec);
                if ($this->current === 'normal') {
710
711
712
713
714
715
716
717
718
719
720
721
722
                    $selItem = $this->getSelectedRecord();
                    $selRecTitle = $selItem['_RECORD_TITLE'];
                } else {
                    $selRecTitle = count($clElements);
                }
            }
            // @TODO
            // This can get removed as soon as the "_colPos" label is translated
            // into all available locallang languages.
            if (!$msg && $columnLabel) {
                $thisRecTitle .= ' | ' . $columnLabel;
                $msg = $this->getLanguageService()->sL($labelKey);
            }
723

724
            // Message
725
726
727
728
729
730
            $conf = sprintf(
                $msg,
                GeneralUtility::fixed_lgd_cs($selRecTitle, 30),
                GeneralUtility::fixed_lgd_cs($thisRecTitle, 30),
                GeneralUtility::fixed_lgd_cs($columnLabel, 30)
            );
731
732
733
734
735
        } else {
            $conf = '';
        }
        return $conf;
    }
736

737
    /**
738
     * Clipboard label - getting from "EXT:core/Resources/Private/Language/locallang_core.xlf:"
739
740
741
742
743
744
745
     *
     * @param string $key Label Key
     * @param string $Akey Alternative key to "labels
     * @return string
     */
    public function clLabel($key, $Akey = 'labels')
    {
746
        return htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:' . $Akey . '.' . $key));
747
    }
748

749
750
751
752
753
754
755
756
757
    /**
     * Creates GET parameters for linking to the export module.
     *
     * @return array GET parameters for current clipboard content to be exported
     */
    protected function exportClipElementParameters()
    {
        // Init
        $pad = $this->current;
758
        $params = [];
759
        $params['tx_impexp']['action'] = 'export';
760
        // Traverse items:
761
        if (is_array($this->clipData[$pad]['el'] ?? false)) {
762
763
764
765
            foreach ($this->clipData[$pad]['el'] as $k => $v) {
                if ($v) {
                    list($table, $uid) = explode('|', $k);
                    // Rendering files/directories on the clipboard
766
                    if ($table === '_FILE') {
767
                        if (file_exists($v) && GeneralUtility::isAllowedAbsPath($v)) {
768
                            $params['tx_impexp'][is_dir($v) ? 'dir' : 'file'][] = $v;
769
770
771
772
773
                        }
                    } else {
                        // Rendering records:
                        $rec = BackendUtility::getRecord($table, $uid);
                        if (is_array($rec)) {
774
                            $params['tx_impexp']['record'][] = $table . ':' . $uid;
775
776
777
778
779
780
781
                        }
                    }
                }
            }
        }
        return $params;
    }
782

783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
    /*****************************************
     *
     * Helper functions
     *
     ****************************************/
    /**
     * Removes element on clipboard
     *
     * @param string $el Key of element in ->clipData array
     */
    public function removeElement($el)
    {
        unset($this->clipData[$this->current]['el'][$el]);
        $this->changed = 1;
    }
798

799
800
801
802
803
804
805
806
807
808
    /**
     * Saves the clipboard, no questions asked.
     * Use ->endClipboard normally (as it checks if changes has been done so saving is necessary)
     *
     * @access private
     */
    public function saveClipboard()
    {
        $this->getBackendUser()->pushModuleData('clipboard', $this->clipData);
    }
809

810
811
812
813
814
815
816
    /**
     * Returns the current mode, 'copy' or 'cut'
     *
     * @return string "copy" or "cut
     */
    public function currentMode()
    {
817
        return ($this->clipData[$this->current]['mode'] ?? '') === 'copy' ? 'copy' : 'cut';
818
    }
819

820
821
822
823
824
825
    /**
     * This traverses the elements on the current clipboard pane
     * and unsets elements which does not exist anymore or are disabled.
     */
    public function cleanCurrent()
    {
826
        if (is_array($this->clipData[$this->current]['el'] ?? false)) {
827
828
            foreach ($this->clipData[$this->current]['el'] as $k => $v) {
                list($table, $uid) = explode('|', $k);
829
                if ($table !== '_FILE') {
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
                    if (!$v || !is_array(BackendUtility::getRecord($table, $uid, 'uid'))) {
                        unset($this->clipData[$this->current]['el'][$k]);
                        $this->changed = 1;
                    }
                } else {
                    if (!$v) {
                        unset($this->clipData[$this->current]['el'][$k]);
                        $this->changed = 1;
                    } else {
                        try {
                            ResourceFactory::getInstance()->retrieveFileOrFolderObject($v);
                        } catch (\TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException $e) {
                            // The file has been deleted in the meantime, so just remove it silently
                            unset($this->clipData[$this->current]['el'][$k]);
                        }
                    }
                }
            }
        }
    }
850

851
852
853
854
855
856
857
858
859
860
    /**
     * Counts the number of elements from the table $matchTable. If $matchTable is blank, all tables (except '_FILE' of course) is counted.
     *
     * @param string $matchTable Table to match/count for.
     * @param string $pad Can optionally be used to set another pad than the current.
     * @return array Array with keys from the CB.
     */
    public function elFromTable($matchTable = '', $pad = '')
    {
        $pad = $pad ? $pad : $this->current;
861
        $list = [];
862
        if (is_array($this->clipData[$pad]['el'] ?? false)) {
863
864
865
            foreach ($this->clipData[$pad]['el'] as $k => $v) {
                if ($v) {
                    list($table, $uid) = explode('|', $k);
866
                    if ($table !== '_FILE') {
867
                        if ((!$matchTable || (string)$table == (string)$matchTable) && $GLOBALS['TCA'][$table]) {
868
                            $list[$k] = $pad === 'normal' ? $v : $uid;
869
870
871
872
873
874
875
876
877
878
879
                        }
                    } else {
                        if ((string)$table == (string)$matchTable) {
                            $list[$k] = $v;
                        }
                    }
                }
            }
        }
        return $list;
    }
880

881
882
883
884
885
886
887
888
889
890
891
    /**
     * Verifies if the item $table/$uid is on the current pad.
     * If the pad is "normal", the mode value is returned if the element existed. Thus you'll know if the item was copy or cut moded...
     *
     * @param string $table Table name, (_FILE for files...)
     * @param int $uid Element uid (path for files)
     * @return string
     */
    public function isSelected($table, $uid)
    {
        $k = $table . '|' . $uid;
892
        return !empty($this->clipData[$this->current]['el'][$k]) ? ($this->current === 'normal' ? $this->currentMode() : 1) : '';
893
    }
894

895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
    /**
     * Returns item record $table,$uid if selected on current clipboard
     * If table and uid is blank, the first element is returned.
     * Makes sense only for DB records - not files!
     *
     * @param string $table Table name
     * @param int|string $uid Element uid
     * @return array Element record with extra field _RECORD_TITLE set to the title of the record
     */
    public function getSelectedRecord($table = '', $uid = '')
    {
        if (!$table && !$uid) {
            $elArr = $this->elFromTable('');
            reset($elArr);
            list($table, $uid) = explode('|', key($elArr));
        }
        if ($this->isSelected($table, $uid)) {
            $selRec = BackendUtility::getRecordWSOL($table, $uid);
            $selRec['_RECORD_TITLE'] = BackendUtility::getRecordTitle($table, $selRec);
            return $selRec;
        }
916
        return [];
917
    }
918

919
920
921
922
923
924
925
926
927
    /**
     * Reports if the current pad has elements (does not check file/DB type OR if file/DBrecord exists or not. Only counting array)
     *
     * @return bool TRUE if elements exist.
     */
    public function isElements()
    {
        return is_array($this->clipData[$this->current]['el']) && !empty($this->clipData[$this->current]['el']);
    }
928

929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
    /*****************************************
     *
     * FOR USE IN tce_db.php:
     *
     ****************************************/
    /**
     * Applies the proper paste configuration in the $cmd array send to tce_db.php.
     * $ref is the target, see description below.
     * The current pad is pasted
     *
     * $ref: [tablename]:[paste-uid].
     * Tablename is the name of the table from which elements *on the current clipboard* is pasted with the 'pid' paste-uid.
     * No tablename means that all items on the clipboard (non-files) are pasted. This requires paste-uid to be positive though.
     * so 'tt_content:-3'	means 'paste tt_content elements on the clipboard to AFTER tt_content:3 record
     * 'tt_content:30'	means 'paste tt_content elements on the clipboard into page with id 30
     * ':30'	means 'paste ALL database elements on the clipboard into page with id 30
     * ':-30'	not valid.
     *
     * @param string $ref [tablename]:[paste-uid], see description
     * @param array $CMD Command-array
949
     * @param array|null $update If additional values should get set in the copied/moved record this will be an array containing key=>value pairs
950
951
952
953
954
955
956
957
958
959
960
961
     * @return array Modified Command-array
     */
    public function makePasteCmdArray($ref, $CMD, array $update = null)
    {
        list($pTable, $pUid) = explode('|', $ref);
        $pUid = (int)$pUid;
        // pUid must be set and if pTable is not set (that means paste ALL elements)
        // the uid MUST be positive/zero (pointing to page id)
        if ($pTable || $pUid >= 0) {
            $elements = $this->elFromTable($pTable);
            // So the order is preserved.
            $elements = array_reverse($elements);
962
            $mode = $this->currentMode() === 'copy' ? 'copy' : 'move';
963
964
965
966
            // Traverse elements and make CMD array
            foreach ($elements as $tP => $value) {
                list($table, $uid) = explode('|', $tP);
                if (!is_array($CMD[$table])) {
967
                    $CMD[$table] = [];
968
969
                }
                if (is_array($update)) {
970
                    $CMD[$table][$uid][$mode] = [
971
972
973
                        'action' => 'paste',
                        'target' => $pUid,
                        'update' => $update,
974
                    ];
975
976
977
                } else {
                    $CMD[$table][$uid][$mode] = $pUid;
                }
978
                if ($mode === 'move') {
979
980
981
982
983
984
985
                    $this->removeElement($tP);
                }
            }
            $this->endClipboard();
        }
        return $CMD;
    }
986

987
988
989
990
991
992
993
994
995
996
997
998
999
    /**
     * Delete record entries in CMD array
     *
     * @param array $CMD Command-array
     * @return array Modified Command-array
     */
    public function makeDeleteCmdArray($CMD)
    {
        // all records
        $elements = $this->elFromTable('');
        foreach ($elements as $tP => $value) {
            list($table, $uid) = explode('|', $tP);
            if (!is_array($CMD[$table])) {
1000
                $CMD[$table] = [];
1001
1002
1003
1004
1005
1006
1007
            }
            $CMD[$table][$uid]['delete'] = 1;
            $this->removeElement($tP);
        }
        $this->endClipboard();
        return $CMD;
    }
1008

1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
    /*****************************************
     *
     * FOR USE IN tce_file.php:
     *
     ****************************************/
    /**
     * Applies the proper paste configuration in the $file array send to tce_file.php.
     * The current pad is pasted
     *
     * @param string $ref Reference to element (splitted by "|")
     * @param array $FILE Command-array
     * @return array Modified Command-array
     */
    public function makePasteCmdArray_file($ref, $FILE)
    {
        list($pTable, $pUid) = explode('|', $ref);
        $elements = $this->elFromTable('_FILE');
1026
        $mode = $this->currentMode() === 'copy' ? 'copy' : 'move';
1027
1028
        // Traverse elements and make CMD array
        foreach ($elements as $tP => $path) {
1029
            $FILE[$mode][] = ['data' => $path, 'target' => $pUid];
1030
            if ($mode === 'move') {
1031
1032
1033
1034
1035
1036
                $this->removeElement($tP);
            }
        }
        $this->endClipboard();
        return $FILE;
    }
1037

1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
    /**
     * Delete files in CMD array
     *
     * @param array $FILE Command-array
     * @return array Modified Command-array
     */
    public function makeDeleteCmdArray_file($FILE)
    {
        $elements = $this->elFromTable('_FILE');
        // Traverse elements and make CMD array
        foreach ($elements as $tP => $path) {
1049
            $FILE['delete'][] = ['data' => $path];
1050
1051
1052
1053
1054
            $this->removeElement($tP);
        }
        $this->endClipboard();
        return $FILE;
    }
1055

1056
1057
1058
    /**
     * Returns LanguageService
     *
1059
     * @return \TYPO3\CMS\Core\Localization\LanguageService
1060
1061
1062
1063
1064
     */
    protected function getLanguageService()
    {
        return $GLOBALS['LANG'];
    }
1065

1066
1067
1068
1069
1070
1071
1072
1073
1074
    /**
     * Returns the current BE user.
     *
     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
     */
    protected function getBackendUser()
    {
        return $GLOBALS['BE_USER'];
    }
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095

    /**
     * returns a new standalone view, shorthand function
     *
     * @return StandaloneView
     * @throws \InvalidArgumentException
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidExtensionNameException
     */
    protected function getStandaloneView()
    {
        /** @var StandaloneView $view */
        $view = GeneralUtility::makeInstance(StandaloneView::class);
        $view->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts')]);
        $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
        $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);

        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/Clipboard/Main.html'));

        $view->getRequest()->setControllerExtensionName('Backend');
        return $view;
    }
1096
}