-
Notifications
You must be signed in to change notification settings - Fork 1
/
snippet.filedir.php
185 lines (172 loc) · 6.96 KB
/
snippet.filedir.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* FileDir
* Copyright 2012 by Artdevue.com <[email protected]>
* Arguings at a forum http://modx.im/blog/addons/78.html
* @package filedir
*/
/**
* FileDir
* You can display any type of files to specified directory. Snippet for MODx Revolution 2.0 and above.
*
* @package filedir
* It is easy to display any type of files from a directory, filtering on the output type.
* You can create a photo gallery or video, file archives.
*
* Options:
* &dir - path to the file folder by default - 'assets /'
* &tpl - inner Tpl chunk, by default - 'tplFileDir'
* Available placeholders:
* [[+file]] - path to the file
* [[+fname]] - the name of the file
* [[+fdclass]] — class name depends on the file extension
* [[+size]] - the file size
* [[+fidx]] - the number of order
* [[+date]] - creation date file
* [[+meta1]],[[+meta2]]...[[+metaN]] - description of file (if &metafile is exist and filled)
* &tplOut - outer Tpl chunk, by default - 'tplFileDirOut'
* Available placeholders:
* [[+res_filedir]] - Required, do not delete!
* [[+ftotal]] - The total number of files
* &limit - maximum number of output files
* &fcache - to enable the cache (false/true), the default - 'false'
* &cachetime - the time of caching, default '0 ', to clear the cache
* &filetip - types of output files (comma), by default - 'jpg,png,gif'
* &sort - Enable or disable sorting (true or false, default - false)
* &sortDir - sort direction (DESC or ASC, default - ASC)
* &sortBy - name to sort (possibly: fname,file,fsize,date)
* &class — prefix for the style in the template tpl, by default - 'fd', ie if the file
* format is jpg, then placeholder [[+ fdclass]] in the template tpl bulet
* output - fd_jpg. If we are in the carts snippets Specify & class = ``,
* then displays the placeholder in the class extension - jpg.
* &metafile - filename contains metadata for each file in folder.
* One line = metadata for one file in folder.
* Use "|" to separate [[+meta1]],[[+meta2]] ... [[metaN]]
* Default file name is "FileDirMeta.txt"
* Example:
* File1Param1|File1Param1|File1Param1
* File2Param1|File2Param1|File2Param1
*
* Example snippet call:
* [[!filedir? &dir = `assets/images/[[*id]]/`]]
* [[!filedir? &dir = `assets/images/61/` &fcache =`true`]]
*
* An example of a template for the gallery
* <div>
* <a href="[[+file]]" title="[[+fname]]">
* <img src="[[+file:rezimgcrop=`r-150x,c-150x75`]]" alt="[[+fname]]">
* </a>
* </div>
*
* An example of a template for the file archive
* <div>
* File: [[+fname]]
* Saze: [[+size]]
* <a href="[[+file]]">Download</ a>
* </div>
*/
//default settings
$dir = $modx->getOption('dir', $scriptProperties, 'assets/');
$tpl = $modx->getOption('tpl', $scriptProperties, 'tplFileDir');
$tplOut = $modx->getOption('tplOut', $scriptProperties, 'tplFileDirOut');
$fcache = $modx->getOption('fcache', $scriptProperties, null);
$cachetime = $modx->getOption('cachetime', $scriptProperties, 0);
$start = $modx->getOption('start', $scriptProperties, 0);
$limit = $modx->getOption('limit', $scriptProperties, 0);
$offset = $modx->getOption('offset', $scriptProperties, 0);
$class = $modx->getOption('class', $scriptProperties, 'fd');
$filetip = isset($filetip) ? explode(',', $filetip) : array('jpg', 'png', 'gif');
$sort = $modx->getOption('sort', $scriptProperties, false);
$sortDir = $modx->getOption('sortDir', $scriptProperties, 'ASC'); // possibly: DESC or ASC
$sortBy = $modx->getOption('sortBy', $scriptProperties, 'fname'); // possibly: fname,file,fsize,date
$formatDate = $modx->getOption('formatDate', $scriptProperties, 'd/m/Y H:i');
$metafile = $modx->getOption('metafile', $scriptProperties, 'FileDirMeta.txt');
$inCache = true;
if (!array_search($sortBy, array('fname', 'file', 'fsize', 'date')))
$sortBy = 'fname';
$base_path = $modx->getOption('base_path');
if ($modx->getCacheManager() && isset($fcache)) {
$inCache = true;
$keyc = md5('filedir::dir:' . implode('', $scriptProperties));
$out = $modx->cacheManager->get($keyc);
if (isset($out)) {
$modx->setPlaceholder('total', $out['total']);
return $out['out'];
} else {
$inCache = false;
}
}
// check for the presence of slash
if ($dir{0} == '/')
$dir = substr($dir, 1);
if (substr($dir, -1) != '/')
$dir .= '/';
// verify the existence of a directory
if (!is_dir($dir)) {
$modx->log(modX::LOG_LEVEL_ERROR, "[FilDir] the folder $dir does not exist!");
return;
}
// verify the existence of a metafile
if (file_exists($dir . $metafile)) {
$metadata = file($dir . $metafile);
}
$sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
$diri = new DirectoryIterator($base_path . $dir);
$x = 1;
$output = array();
$sortArray = array();
foreach ($diri as $file) {
if ($file->isFile() and in_array($file->getExtension(), $filetip) and ($file->getSize() > 0) and $file->getFilename() != $metafile) {
switch ($sortBy) {
case 'fname':
$sortArray[$x] = $file->getBasename('.' . $file->getExtension());
break;
case 'file':
$sortArray[$x] = $dir . $file->getFilename();
break;
case 'fsize':
$sortArray[$x] = $file->getSize();
break;
case 'date':
$sortArray[$x] = $file->getCTime();
break;
}
if (($x - 1 < $limit + $start + $offset && $x > $offset + $start) || ($offset == $start && $offset == $limit)) {
$itemArray = array(
'fname' => $file->getBasename('.' . $file->getExtension()),
'file' => $dir . $file->getFilename(),
'fsize' => $file->getSize(),
'size' => round(($file->getSize()) / pow(1024, ($i = floor(log(($file->getSize()), 1024)))), $i > 1 ? 2 : 0) . $sizes[$i],
'fdclass' => trim(strlen($class)) > 0 ? $class . '_' . $file->getExtension() : $file->getExtension(),
'date' => date($formatDate, $file->getCTime()),
);
$imgArray[$x] = $itemArray;
}
$x++;
}
}
if ($sort == true)
array_multisort($sortArray, ($sortDir === 'ASC' ? SORT_ASC : SORT_DESC), SORT_STRING, $imgArray);
// Create [[+fidx]] and [[+meta1]],[[+meta2]]...[[+metaN]] placeholders after sorting
$x = 1;
foreach ($imgArray as $img) {
$img['fidx'] = $x;
if (isset($metadata[$x - 1])) {
$metaArray = explode('|', $metadata[$x - 1]);
foreach ($metaArray as $key => $value) {
$img["meta" . ($key + 1)] = $value;
}
}
$output[$x] = $modx->getChunk($tpl, $img);
$x++;
}
$output['out'] = $modx->getChunk($tplOut, array(
'res_filedir' => implode('', $output),
'ftotal' => $x - 1
));
$output['total'] = $x - 1;
if (!$inCache && isset($fcache)) {
$modx->cacheManager->set($keyc, $output, $cachetime);
}
$modx->setPlaceholder('total', $x - 1);
return $output['out'];