c8e272f150d9f5be18941bcaee902e2426ca67aa
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Tree / View / FolderTreeView.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tree\View;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Localization\LanguageService;
21 use TYPO3\CMS\Core\Messaging\FlashMessage;
22 use TYPO3\CMS\Core\Messaging\FlashMessageService;
23 use TYPO3\CMS\Core\Resource\Folder;
24 use TYPO3\CMS\Core\Resource\FolderInterface;
25 use TYPO3\CMS\Core\Resource\InaccessibleFolder;
26 use TYPO3\CMS\Core\Resource\ResourceStorage;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * Generate a folder tree,
31 * specially made for browsing folders in the File module
32 */
33 class FolderTreeView extends AbstractTreeView
34 {
35 /**
36 * The users' file Storages
37 *
38 * @var ResourceStorage[]
39 */
40 protected $storages = null;
41
42 /**
43 * @var array
44 */
45 protected $storageHashNumbers;
46
47 /**
48 * Indicates, whether the AJAX call was successful,
49 * i.e. the requested page has been found
50 *
51 * @var bool
52 */
53 protected $ajaxStatus = false;
54
55 /**
56 * @var array
57 */
58 protected $scope;
59
60 /**
61 * @var IconFactory
62 */
63 protected $iconFactory;
64
65 /**
66 * If file-drag mode is set, temp and recycler folders are filtered out.
67 * @var bool
68 */
69 public $ext_noTempRecyclerDirs = false;
70
71 /**
72 * override to not use a title attribute
73 * @var string
74 */
75 public $titleAttrib = '';
76
77 /**
78 * override to use this treeName
79 * does not need to be set in __construct()
80 * @var string
81 */
82 public $treeName = 'folder';
83
84 /**
85 * override to use this domIdPrefix
86 * @var string
87 */
88 public $domIdPrefix = 'folder';
89
90 /**
91 * Constructor function of the class
92 */
93 public function __construct()
94 {
95 parent::__construct();
96 $this->init();
97 $this->storages = $this->BE_USER->getFileStorages();
98 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
99 }
100
101 /**
102 * Generate the plus/minus icon for the browsable tree.
103 *
104 * @param Folder $folderObject Entry folder object
105 * @param int $subFolderCounter The current entry number
106 * @param int $totalSubFolders The total number of entries. If equal to $a, a "bottom" element is returned.
107 * @param int $nextCount The number of sub-elements to the current element.
108 * @param bool $isExpanded The element was expanded to render subelements if this flag is set.
109 *
110 * @return string Image tag with the plus/minus icon.
111 * @internal
112 * @see \TYPO3\CMS\Backend\Tree\View\PageTreeView::PMicon()
113 */
114 public function PMicon($folderObject, $subFolderCounter, $totalSubFolders, $nextCount, $isExpanded)
115 {
116 $icon = '';
117 if ($nextCount) {
118 $cmd = $this->generateExpandCollapseParameter($this->bank, !$isExpanded, $folderObject);
119 $icon = $this->PMiconATagWrap($icon, $cmd, !$isExpanded);
120 }
121 return $icon;
122 }
123
124 /**
125 * Wrap the plus/minus icon in a link
126 *
127 * @param string $icon HTML string to wrap, probably an image tag.
128 * @param string $cmd Command for 'PM' get var
129 * @param bool $isExpand Whether to be expanded
130 * @return string Link-wrapped input string
131 * @internal
132 */
133 public function PMiconATagWrap($icon, $cmd, $isExpand = true)
134 {
135 if (empty($this->scope)) {
136 $this->scope = [
137 'class' => get_class($this),
138 'script' => $this->thisScript,
139 'ext_noTempRecyclerDirs' => $this->ext_noTempRecyclerDirs
140 ];
141 }
142
143 if ($this->thisScript) {
144 // Activates dynamic AJAX based tree
145 $scopeData = serialize($this->scope);
146 $scopeHash = GeneralUtility::hmac($scopeData);
147 $js = htmlspecialchars('Tree.load(' . GeneralUtility::quoteJSvalue($cmd) . ', ' . (int)$isExpand . ', this, ' . GeneralUtility::quoteJSvalue($scopeData) . ', ' . GeneralUtility::quoteJSvalue($scopeHash) . ');');
148 return '<a class="list-tree-control' . (!$isExpand ? ' list-tree-control-open' : ' list-tree-control-closed') . '" onclick="' . $js . '"><i class="fa"></i></a>';
149 } else {
150 return $icon;
151 }
152 }
153
154 /**
155 * @param string $cmd
156 * @param bool $isOpen
157 * @return string
158 */
159 protected function renderPMIconAndLink($cmd, $isOpen)
160 {
161 $link = $this->thisScript ? ' href="' . htmlspecialchars($this->getThisScript() . 'PM=' . $cmd) . '"' : '';
162 return '<a class="list-tree-control list-tree-control-' . ($isOpen ? 'open' : 'closed') . '"' . $link . '><i class="fa"></i></a>';
163 }
164
165 /**
166 * Wrapping the folder icon
167 *
168 * @param string $icon The image tag for the icon
169 * @param Folder $folderObject The row for the current element
170 *
171 * @return string The processed icon input value.
172 * @internal
173 */
174 public function wrapIcon($icon, $folderObject)
175 {
176 // Add title attribute to input icon tag
177 $theFolderIcon = '';
178 // Wrap icon in click-menu link.
179 if (!$this->ext_IconMode) {
180 // Check storage access to wrap with click menu
181 if (!$folderObject instanceof InaccessibleFolder) {
182 $tableName = $this->getTableNameForClickMenu($folderObject);
183 $theFolderIcon = BackendUtility::wrapClickMenuOnIcon($icon, $tableName, $folderObject->getCombinedIdentifier(), 'tree');
184 }
185 } elseif ($this->ext_IconMode === 'titlelink') {
186 $aOnClick = 'return jumpTo(' . GeneralUtility::quoteJSvalue($this->getJumpToParam($folderObject)) . ',this,' . GeneralUtility::quoteJSvalue($this->domIdPrefix . $this->getId($folderObject)) . ',' . $this->bank . ');';
187 $theFolderIcon = '<a href="#" onclick="' . htmlspecialchars($aOnClick) . '">' . $icon . '</a>';
188 }
189 return $theFolderIcon;
190 }
191
192 /**
193 * Wrapping $title in a-tags.
194 *
195 * @param string $title Title string
196 * @param Folder $folderObject the folder record
197 * @param int $bank Bank pointer (which mount point number)
198 *
199 * @return string
200 * @internal
201 */
202 public function wrapTitle($title, $folderObject, $bank = 0)
203 {
204 // Check storage access to wrap with click menu
205 if ($folderObject instanceof InaccessibleFolder) {
206 return $title;
207 }
208 $aOnClick = 'return jumpTo(' . GeneralUtility::quoteJSvalue($this->getJumpToParam($folderObject)) . ', this, ' . GeneralUtility::quoteJSvalue($this->domIdPrefix . $this->getId($folderObject)) . ', ' . $bank . ');';
209 $tableName = $this->getTableNameForClickMenu($folderObject);
210 $clickMenuParts = BackendUtility::wrapClickMenuOnIcon('', $tableName, $folderObject->getCombinedIdentifier(), 'tree', '', '', true);
211
212 return '<a href="#" title="' . htmlspecialchars(strip_tags($title)) . '" onclick="' . htmlspecialchars($aOnClick) . '" ' . GeneralUtility::implodeAttributes($clickMenuParts) . '>' . $title . '</a>';
213 }
214
215 /**
216 * Returns the id from the record - for folders, this is an md5 hash.
217 *
218 * @param Folder $folderObject The folder object
219 *
220 * @return int The "uid" field value.
221 */
222 public function getId($folderObject)
223 {
224 return GeneralUtility::md5int($folderObject->getCombinedIdentifier());
225 }
226
227 /**
228 * Returns jump-url parameter value.
229 *
230 * @param Folder $folderObject The folder object
231 *
232 * @return string The jump-url parameter.
233 */
234 public function getJumpToParam($folderObject)
235 {
236 return rawurlencode($folderObject->getCombinedIdentifier());
237 }
238
239 /**
240 * Returns the title for the input record. If blank, a "no title" labele (localized) will be returned.
241 * '_title' is used for setting an alternative title for folders.
242 *
243 * @param array $row The input row array (where the key "_title" is used for the title)
244 * @param int $titleLen Title length (30)
245 * @return string The title
246 */
247 public function getTitleStr($row, $titleLen = 30)
248 {
249 return $row['_title'] ?: parent::getTitleStr($row, $titleLen);
250 }
251
252 /**
253 * Returns the value for the image "title" attribute
254 *
255 * @param Folder $folderObject The folder to be used
256 *
257 * @return string The attribute value (is htmlspecialchared() already)
258 */
259 public function getTitleAttrib($folderObject)
260 {
261 return htmlspecialchars($folderObject->getName());
262 }
263
264 /**
265 * Will create and return the HTML code for a browsable tree of folders.
266 * Is based on the mounts found in the internal array ->MOUNTS (set in the constructor)
267 *
268 * @return string HTML code for the browsable tree
269 */
270 public function getBrowsableTree()
271 {
272 // Get stored tree structure AND updating it if needed according to incoming PM GET var.
273 $this->initializePositionSaving();
274 // Init done:
275 $treeItems = [];
276 // Traverse mounts:
277 foreach ($this->storages as $storageObject) {
278 $this->getBrowseableTreeForStorage($storageObject);
279 // Add tree:
280 $treeItems = array_merge($treeItems, $this->tree);
281 }
282 return $this->printTree($treeItems);
283 }
284
285 /**
286 * Get a tree for one storage
287 *
288 * @param ResourceStorage $storageObject
289 */
290 public function getBrowseableTreeForStorage(ResourceStorage $storageObject)
291 {
292 // If there are filemounts, show each, otherwise just the rootlevel folder
293 $fileMounts = $storageObject->getFileMounts();
294 $rootLevelFolders = [];
295 if (!empty($fileMounts)) {
296 foreach ($fileMounts as $fileMountInfo) {
297 $rootLevelFolders[] = [
298 'folder' => $fileMountInfo['folder'],
299 'name' => $fileMountInfo['title']
300 ];
301 }
302 } elseif ($this->BE_USER->isAdmin()) {
303 $rootLevelFolders[] = [
304 'folder' => $storageObject->getRootLevelFolder(),
305 'name' => $storageObject->getName()
306 ];
307 }
308 // Clean the tree
309 $this->reset();
310 // Go through all "root level folders" of this tree (can be the rootlevel folder or any file mount points)
311 foreach ($rootLevelFolders as $rootLevelFolderInfo) {
312 /** @var $rootLevelFolder Folder */
313 $rootLevelFolder = $rootLevelFolderInfo['folder'];
314 $rootLevelFolderName = $rootLevelFolderInfo['name'];
315 $folderHashSpecUID = GeneralUtility::md5int($rootLevelFolder->getCombinedIdentifier());
316 $this->specUIDmap[$folderHashSpecUID] = $rootLevelFolder->getCombinedIdentifier();
317 // Hash key
318 $storageHashNumber = $this->getShortHashNumberForStorage($storageObject, $rootLevelFolder);
319 // Set first:
320 $this->bank = $storageHashNumber;
321 $isOpen = $this->stored[$storageHashNumber][$folderHashSpecUID] || $this->expandFirst;
322 // Set PM icon:
323 $cmd = $this->generateExpandCollapseParameter($this->bank, !$isOpen, $rootLevelFolder);
324 // Only show and link icon if storage is browseable
325 if (!$storageObject->isBrowsable() || $this->getNumberOfSubfolders($rootLevelFolder) === 0) {
326 $firstHtml = '';
327 } else {
328 $firstHtml = $this->renderPMIconAndLink($cmd, $isOpen);
329 }
330 // Mark a storage which is not online, as offline
331 // maybe someday there will be a special icon for this
332 if ($storageObject->isOnline() === false) {
333 $rootLevelFolderName .= ' (' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_file.xlf:sys_file_storage.isOffline') . ')';
334 }
335 // Preparing rootRec for the mount
336 $icon = $this->iconFactory->getIconForResource($rootLevelFolder, Icon::SIZE_SMALL, null, ['mount-root' => true]);
337 $firstHtml .= $this->wrapIcon($icon, $rootLevelFolder);
338 $row = [
339 'uid' => $folderHashSpecUID,
340 'title' => $rootLevelFolderName,
341 'path' => $rootLevelFolder->getCombinedIdentifier(),
342 'folder' => $rootLevelFolder
343 ];
344 // Add the storage root to ->tree
345 $this->tree[] = [
346 'HTML' => $firstHtml,
347 'row' => $row,
348 'bank' => $this->bank,
349 // hasSub is TRUE when the root of the storage is expanded
350 'hasSub' => $isOpen && $storageObject->isBrowsable(),
351 'invertedDepth' => 1000,
352 ];
353 // If the mount is expanded, go down:
354 if ($isOpen && $storageObject->isBrowsable()) {
355 // Set depth:
356 $this->getFolderTree($rootLevelFolder, 999);
357 }
358 }
359 }
360
361 /**
362 * Fetches the data for the tree
363 *
364 * @param Folder $folderObject the folderobject
365 * @param int $depth Max depth (recursivity limit)
366 * @param string $type HTML-code prefix for recursive calls.
367 *
368 * @return int The count of items on the level
369 * @see getBrowsableTree()
370 */
371 public function getFolderTree(Folder $folderObject, $depth = 999, $type = '')
372 {
373 $depth = (int)$depth;
374
375 // This generates the directory tree
376 /* array of \TYPO3\CMS\Core\Resource\Folder */
377 if ($folderObject instanceof InaccessibleFolder) {
378 $subFolders = [];
379 } else {
380 $subFolders = $folderObject->getSubfolders();
381 $subFolders = \TYPO3\CMS\Core\Resource\Utility\ListUtility::resolveSpecialFolderNames($subFolders);
382 uksort($subFolders, 'strnatcasecmp');
383 }
384
385 $totalSubFolders = count($subFolders);
386 $HTML = '';
387 $subFolderCounter = 0;
388 $treeKey = '';
389 /** @var Folder $subFolder */
390 foreach ($subFolders as $subFolderName => $subFolder) {
391 $subFolderCounter++;
392 // Reserve space.
393 $this->tree[] = [];
394 // Get the key for this space
395 end($this->tree);
396 $isLocked = $subFolder instanceof InaccessibleFolder;
397 $treeKey = key($this->tree);
398 $specUID = GeneralUtility::md5int($subFolder->getCombinedIdentifier());
399 $this->specUIDmap[$specUID] = $subFolder->getCombinedIdentifier();
400 $row = [
401 'uid' => $specUID,
402 'path' => $subFolder->getCombinedIdentifier(),
403 'title' => $subFolderName,
404 'folder' => $subFolder
405 ];
406 // Make a recursive call to the next level
407 if (!$isLocked && $depth > 1 && $this->expandNext($specUID)) {
408 $nextCount = $this->getFolderTree($subFolder, $depth - 1, $type);
409 // Set "did expand" flag
410 $isOpen = 1;
411 } else {
412 $nextCount = $isLocked ? 0 : $this->getNumberOfSubfolders($subFolder);
413 // Clear "did expand" flag
414 $isOpen = 0;
415 }
416 // Set HTML-icons, if any:
417 if ($this->makeHTML) {
418 $HTML = $this->PMicon($subFolder, $subFolderCounter, $totalSubFolders, $nextCount, $isOpen);
419 $type = '';
420
421 $role = $subFolder->getRole();
422 if ($role !== FolderInterface::ROLE_DEFAULT) {
423 $row['_title'] = '<strong>' . $subFolderName . '</strong>';
424 }
425 $icon = '<span title="' . htmlspecialchars($subFolderName) . '">'
426 . $this->iconFactory->getIconForResource($subFolder, Icon::SIZE_SMALL, null, ['folder-open' => (bool)$isOpen])
427 . '</span>';
428 $HTML .= $this->wrapIcon($icon, $subFolder);
429 }
430 // Finally, add the row/HTML content to the ->tree array in the reserved key.
431 $this->tree[$treeKey] = [
432 'row' => $row,
433 'HTML' => $HTML,
434 'hasSub' => $nextCount && $this->expandNext($specUID),
435 'isFirst' => $subFolderCounter == 1,
436 'isLast' => false,
437 'invertedDepth' => $depth,
438 'bank' => $this->bank
439 ];
440 }
441 if ($subFolderCounter > 0) {
442 $this->tree[$treeKey]['isLast'] = true;
443 }
444 return $totalSubFolders;
445 }
446
447 /**
448 * Compiles the HTML code for displaying the structure found inside the ->tree array
449 *
450 * @param array|string $treeItems "tree-array" - if blank string, the internal ->tree array is used.
451 * @return string The HTML code for the tree
452 */
453 public function printTree($treeItems = '')
454 {
455 $doExpand = false;
456 $doCollapse = false;
457 $ajaxOutput = '';
458 $titleLength = (int)$this->BE_USER->uc['titleLen'];
459 if (!is_array($treeItems)) {
460 $treeItems = $this->tree;
461 }
462
463 if (empty($treeItems)) {
464 $message = GeneralUtility::makeInstance(
465 FlashMessage::class,
466 $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang.xlf:foldertreeview.noFolders.message'),
467 $this->getLanguageService()->sL('LLL:EXT:backend/Resources/Private/Language/locallang.xlf:foldertreeview.noFolders.title'),
468 FlashMessage::INFO
469 );
470 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
471 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
472 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
473 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
474 $defaultFlashMessageQueue->enqueue($message);
475 return $defaultFlashMessageQueue->renderFlashMessages();
476 }
477
478 $expandedFolderHash = '';
479 $invertedDepthOfAjaxRequestedItem = 0;
480 $out = '<ul class="list-tree list-tree-root">';
481 // Evaluate AJAX request
482 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
483 list(, $expandCollapseCommand, $expandedFolderHash, ) = $this->evaluateExpandCollapseParameter();
484 if ($expandCollapseCommand == 1) {
485 $doExpand = true;
486 } else {
487 $doCollapse = true;
488 }
489 }
490 // We need to count the opened <ul>'s every time we dig into another level,
491 // so we know how many we have to close when all children are done rendering
492 $closeDepth = [];
493 foreach ($treeItems as $treeItem) {
494 /** @var $folderObject Folder */
495 $folderObject = $treeItem['row']['folder'];
496 $classAttr = $treeItem['row']['_CSSCLASS'];
497 $folderIdentifier = $folderObject->getCombinedIdentifier();
498 // this is set if the AJAX request has just opened this folder (via the PM command)
499 $isExpandedFolderIdentifier = $expandedFolderHash == GeneralUtility::md5int($folderIdentifier);
500 $idAttr = htmlspecialchars($this->domIdPrefix . $this->getId($folderObject) . '_' . $treeItem['bank']);
501 $itemHTML = '';
502 // If this item is the start of a new level,
503 // then a new level <ul> is needed, but not in ajax mode
504 if ($treeItem['isFirst'] && !$doCollapse && !($doExpand && $isExpandedFolderIdentifier)) {
505 $itemHTML = '<ul class="list-tree">';
506 }
507 // Add CSS classes to the list item
508 if ($treeItem['hasSub']) {
509 $classAttr .= ' list-tree-control-open';
510 }
511 $itemHTML .= '
512 <li id="' . $idAttr . '" ' . ($classAttr ? ' class="' . trim($classAttr) . '"' : '') . '><span class="list-tree-group">' . $treeItem['HTML'] . $this->wrapTitle($this->getTitleStr($treeItem['row'], $titleLength), $folderObject, $treeItem['bank']) . '</span>';
513 if (!$treeItem['hasSub']) {
514 $itemHTML .= '</li>';
515 }
516 // We have to remember if this is the last one
517 // on level X so the last child on level X+1 closes the <ul>-tag
518 if ($treeItem['isLast'] && !($doExpand && $isExpandedFolderIdentifier)) {
519 $closeDepth[$treeItem['invertedDepth']] = 1;
520 }
521 // If this is the last one and does not have subitems, we need to close
522 // the tree as long as the upper levels have last items too
523 if ($treeItem['isLast'] && !$treeItem['hasSub'] && !$doCollapse && !($doExpand && $isExpandedFolderIdentifier)) {
524 for ($i = $treeItem['invertedDepth']; $closeDepth[$i] == 1; $i++) {
525 $closeDepth[$i] = 0;
526 $itemHTML .= '</ul></li>';
527 }
528 }
529 // Ajax request: collapse
530 if ($doCollapse && $isExpandedFolderIdentifier) {
531 $this->ajaxStatus = true;
532 return $itemHTML;
533 }
534 // Ajax request: expand
535 if ($doExpand && $isExpandedFolderIdentifier) {
536 $ajaxOutput .= $itemHTML;
537 $invertedDepthOfAjaxRequestedItem = $treeItem['invertedDepth'];
538 } elseif ($invertedDepthOfAjaxRequestedItem) {
539 if ($treeItem['invertedDepth'] && ($treeItem['invertedDepth'] < $invertedDepthOfAjaxRequestedItem)) {
540 $ajaxOutput .= $itemHTML;
541 } else {
542 $this->ajaxStatus = true;
543 return $ajaxOutput;
544 }
545 }
546 $out .= $itemHTML;
547 }
548 // If this is an AJAX request, output directly
549 if ($ajaxOutput) {
550 $this->ajaxStatus = true;
551 return $ajaxOutput;
552 }
553 // Finally close the first ul
554 $out .= '</ul>';
555 return $out;
556 }
557
558 /**
559 * Returns table name for click menu
560 *
561 * @param Folder $folderObject
562 * @return string
563 */
564 protected function getTableNameForClickMenu(Folder $folderObject)
565 {
566 if (strpos($folderObject->getRole(), FolderInterface::ROLE_MOUNT) !== false) {
567 $tableName = 'sys_filemounts';
568 } elseif ($folderObject->getIdentifier() === $folderObject->getStorage()->getRootLevelFolder()->getIdentifier()) {
569 $tableName = 'sys_file_storage';
570 } else {
571 $tableName = 'sys_file';
572 }
573 return $tableName;
574 }
575
576 /**
577 * Counts the number of directories in a file path.
578 *
579 * @param Folder $folderObject File path.
580 *
581 * @return int
582 */
583 public function getNumberOfSubfolders(Folder $folderObject)
584 {
585 $subFolders = $folderObject->getSubfolders();
586 return count($subFolders);
587 }
588
589 /**
590 * Get stored tree structure AND updating it if needed according to incoming PM GET var.
591 *
592 * @access private
593 */
594 public function initializePositionSaving()
595 {
596 // Get stored tree structure:
597 $this->stored = unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
598 $this->getShortHashNumberForStorage();
599 // PM action:
600 // (If an plus/minus icon has been clicked,
601 // the PM GET var is sent and we must update the stored positions in the tree):
602 // 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
603 list($storageHashNumber, $doExpand, $numericFolderHash, $treeName) = $this->evaluateExpandCollapseParameter();
604 if ($treeName && $treeName == $this->treeName) {
605 if (in_array($storageHashNumber, $this->storageHashNumbers)) {
606 if ($doExpand == 1) {
607 // Set
608 $this->stored[$storageHashNumber][$numericFolderHash] = 1;
609 } else {
610 // Clear
611 unset($this->stored[$storageHashNumber][$numericFolderHash]);
612 }
613 $this->savePosition();
614 }
615 }
616 }
617
618 /**
619 * Helper method to map md5-hash to shorter number
620 *
621 * @param ResourceStorage $storageObject
622 * @param Folder $startingPointFolder
623 *
624 * @return int
625 */
626 protected function getShortHashNumberForStorage(ResourceStorage $storageObject = null, Folder $startingPointFolder = null)
627 {
628 if (!$this->storageHashNumbers) {
629 $this->storageHashNumbers = [];
630 foreach ($this->storages as $storageUid => $storage) {
631 $fileMounts = $storage->getFileMounts();
632 if (!empty($fileMounts)) {
633 foreach ($fileMounts as $fileMount) {
634 $nkey = hexdec(substr(GeneralUtility::md5int($fileMount['folder']->getCombinedIdentifier()), 0, 4));
635 $this->storageHashNumbers[$storageUid . $fileMount['folder']->getCombinedIdentifier()] = $nkey;
636 }
637 } else {
638 $folder = $storage->getRootLevelFolder();
639 $nkey = hexdec(substr(GeneralUtility::md5int($folder->getCombinedIdentifier()), 0, 4));
640 $this->storageHashNumbers[$storageUid . $folder->getCombinedIdentifier()] = $nkey;
641 }
642 }
643 }
644 if ($storageObject) {
645 if ($startingPointFolder) {
646 return $this->storageHashNumbers[$storageObject->getUid() . $startingPointFolder->getCombinedIdentifier()];
647 } else {
648 return $this->storageHashNumbers[$storageObject->getUid()];
649 }
650 } else {
651 return null;
652 }
653 }
654
655 /**
656 * Gets the values from the Expand/Collapse Parameter (&PM)
657 * previously known as "PM" (plus/minus)
658 * PM action:
659 * (If an plus/minus icon has been clicked,
660 * the PM GET var is sent and we must update the stored positions in the tree):
661 * 0: mount key, 1: set/clear boolean, 2: item ID (cannot contain "_"), 3: treeName
662 *
663 * @param string $PM The "plus/minus" command
664 * @return array
665 */
666 protected function evaluateExpandCollapseParameter($PM = null)
667 {
668 if ($PM === null) {
669 $PM = GeneralUtility::_GP('PM');
670 // IE takes anchor as parameter
671 if (($PMpos = strpos($PM, '#')) !== false) {
672 $PM = substr($PM, 0, $PMpos);
673 }
674 }
675 // Take the first three parameters
676 list($mountKey, $doExpand, $folderIdentifier) = explode('_', $PM, 3);
677 // In case the folder identifier contains "_", we just need to get the fourth/last parameter
678 list($folderIdentifier, $treeName) = GeneralUtility::revExplode('_', $folderIdentifier, 2);
679 return [
680 $mountKey,
681 $doExpand,
682 $folderIdentifier,
683 $treeName
684 ];
685 }
686
687 /**
688 * Generates the "PM" string to sent to expand/collapse items
689 *
690 * @param string $mountKey The mount key / storage UID
691 * @param bool $doExpand Whether to expand/collapse
692 * @param Folder $folderObject The folder object
693 * @param string $treeName The name of the tree
694 *
695 * @return string
696 */
697 protected function generateExpandCollapseParameter($mountKey = null, $doExpand = false, Folder $folderObject = null, $treeName = null)
698 {
699 $parts = [
700 $mountKey !== null ? $mountKey : $this->bank,
701 $doExpand == 1 ? 1 : 0,
702 $folderObject !== null ? GeneralUtility::md5int($folderObject->getCombinedIdentifier()) : '',
703 $treeName !== null ? $treeName : $this->treeName
704 ];
705 return implode('_', $parts);
706 }
707
708 /**
709 * Gets the AJAX status.
710 *
711 * @return bool
712 */
713 public function getAjaxStatus()
714 {
715 return $this->ajaxStatus;
716 }
717
718 /**
719 * @return LanguageService
720 */
721 protected function getLanguageService()
722 {
723 return $GLOBALS['LANG'];
724 }
725 }