|
2 | 2 |
|
3 | 3 | use FileMan\FileMan; |
4 | 4 | use FileMan\Model\File; |
| 5 | +use MODX\Revolution\modX; |
| 6 | +use MODX\Revolution\Sources\modMediaSource; |
5 | 7 |
|
6 | 8 | /** @var array $scriptProperties */ |
| 9 | +/** @var modX $modx */ |
7 | 10 | /** @var FileMan $fileMan */ |
8 | 11 | $fileMan = $modx->services->get('FileMan'); |
9 | 12 |
|
10 | | -// Get script options |
| 13 | +// Fenom tpl |
11 | 14 | $tpl = $modx->getOption('tpl', $scriptProperties, 'tpl.FileMan.Files'); |
| 15 | +// MODX tpls |
| 16 | +$tplWrapper = $modx->getOption('tplWrapper', $scriptProperties, ''); |
| 17 | +$tplGroup = $modx->getOption('tplGroup', $scriptProperties, 'tpl.FileMan.Group'); |
| 18 | +$tplRow = $modx->getOption('tplRow', $scriptProperties, 'tpl.FileMan.Row'); |
| 19 | +$wrapIfEmpty = $modx->getOption('wrapIfEmpty', $scriptProperties, false); |
| 20 | + |
| 21 | +$usePdoTools = $fileMan->pdoToolsAvailable() |
| 22 | + ? $modx->getOption('fileman_pdotools', $scriptProperties, true) |
| 23 | + : false; |
12 | 24 |
|
13 | 25 | $sortby = $modx->getOption('sortBy', $scriptProperties, 'sort_order'); |
14 | 26 | $sortdir = $modx->getOption('sortDir', $scriptProperties, 'ASC'); |
|
20 | 32 |
|
21 | 33 | $ids = $modx->getOption('ids', $scriptProperties, ''); |
22 | 34 | $resource = $modx->getOption('resource', $scriptProperties, 0); |
23 | | -$showGroups = $modx->getOption('showGroups', $scriptProperties, 1); |
| 35 | +$showGroups = $modx->getOption('showGroups', $scriptProperties, true); |
24 | 36 | $makeUrl = $modx->getOption('makeUrl', $scriptProperties, true); |
25 | 37 | $private = $modx->getOption('private', $scriptProperties, false); |
26 | 38 | $includeTimeStamp = $modx->getOption('includeTimeStamp', $scriptProperties, false); |
27 | 39 |
|
| 40 | +$outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, "\n"); |
28 | 41 |
|
29 | | -// |
30 | 42 | $mediaSource = $modx->getOption('fileman_mediasource', null, 1); |
31 | | -$ms = $modx->getObject('sources.modMediaSource', array('id' => $mediaSource)); |
32 | | -$ms->initialize(); |
33 | | -$public_url = $ms->getBaseUrl(); |
| 43 | +/** @var modMediaSource $mediaSource */ |
| 44 | +$mediaSource = $modx->getObject(modMediaSource::class, array('id' => $mediaSource)); |
| 45 | +$mediaSource->initialize(); |
| 46 | +$public_url = $mediaSource->getBaseUrl(); |
34 | 47 | $private_url = $modx->getOption('fileman_assets_url', null, $modx->getOption('assets_url')) . 'components/fileman/'; |
35 | 48 | $private_url .= 'download.php?fid='; |
36 | 49 |
|
|
45 | 58 | // ids |
46 | 59 | $ids = explode(',', $ids); |
47 | 60 | $ids = array_filter(array_map('trim', $ids)); |
48 | | -if(!empty($ids)) { |
| 61 | +if (!empty($ids)) { |
49 | 62 | $ids = array_map('intval', $ids); |
50 | 63 | $c->where(['id:IN' => $ids]); |
51 | 64 | } |
|
63 | 76 | $items = $modx->getIterator(File::class, $c); |
64 | 77 |
|
65 | 78 | $outputData = []; |
| 79 | +$groupOutputData = []; |
| 80 | +$curGroup = ''; |
| 81 | +$itemsCount = iterator_count($items); |
| 82 | +$index = 0; |
66 | 83 |
|
67 | 84 | /** @var File $item */ |
68 | 85 | foreach ($items as $item) { |
69 | | - $item->source = $ms; |
70 | | - |
| 86 | + $item->source = $mediaSource; |
71 | 87 | $itemArr = $item->toArray(); |
72 | 88 |
|
73 | 89 | if ($makeUrl) { |
74 | 90 | if ($itemArr['private'] || $private) { |
75 | 91 | $itemArr['url'] = $private_url . $itemArr['fid']; |
76 | | - } |
77 | | - else { |
| 92 | + } else { |
78 | 93 | $itemArr['url'] = $public_url . $itemArr['path'] . $itemArr['internal_name']; |
79 | 94 | } |
80 | 95 | } |
81 | 96 |
|
| 97 | + if (!$showGroups) { |
| 98 | + $itemArr['group'] = false; |
| 99 | + } |
| 100 | + |
82 | 101 | if ($includeTimeStamp) { |
83 | 102 | $itemArr['timestamp'] = filectime($item->getFullPath()); |
84 | 103 | } |
85 | 104 |
|
86 | | - $outputData[] = $itemArr; |
| 105 | + if ($usePdoTools) { |
| 106 | + $outputData[] = $itemArr; |
| 107 | + } else { |
| 108 | + // Checking if we need to start a new group... |
| 109 | + if ($curGroup != $itemArr['group']) { |
| 110 | + if (count($groupOutputData) > 0) { |
| 111 | + $outputData[] = $fileMan->getChunk($tplGroup, [ |
| 112 | + 'group' => $curGroup, |
| 113 | + 'output' => implode($outputSeparator, $groupOutputData) |
| 114 | + ]); |
| 115 | + $groupOutputData = []; |
| 116 | + } |
| 117 | + $curGroup = $itemArr['group']; |
| 118 | + } |
| 119 | + $groupOutputData[] = $fileMan->getChunk($tplRow, $itemArr); |
| 120 | + |
| 121 | + $index++; |
| 122 | + |
| 123 | + // ..or is this the last iteration |
| 124 | + if ($index === $itemsCount) { |
| 125 | + $outputData[] = $fileMan->getChunk($tplGroup, [ |
| 126 | + 'group' => $curGroup, |
| 127 | + 'output' => implode($outputSeparator, $groupOutputData) |
| 128 | + ]); |
| 129 | + } |
| 130 | + } |
87 | 131 | } |
88 | 132 |
|
89 | 133 | // Output |
90 | | -$output = $fileMan->getChunk($tpl, ['files' => $outputData]); |
| 134 | +if ($usePdoTools) { |
| 135 | + $output = $fileMan->getChunk($tpl, ['files' => $outputData]); |
| 136 | +} else { |
| 137 | + $output = implode($outputSeparator, $outputData); |
| 138 | + if (!empty($tplWrapper) && $wrapIfEmpty) { |
| 139 | + $output = $fileMan->getChunk($tplWrapper, ['output' => $output]); |
| 140 | + } |
| 141 | +} |
91 | 142 |
|
| 143 | +// If using a toPlaceholder, output nothing and set output to specified placeholder |
92 | 144 | if (!empty($toPlaceholder)) { |
93 | | - // If using a placeholder, output nothing and set output to specified placeholder |
94 | 145 | $modx->setPlaceholder($toPlaceholder, $output); |
95 | 146 | return ''; |
96 | 147 | } |
|
0 commit comments