This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
/
ContentGallery.php
386 lines (315 loc) · 8.25 KB
/
ContentGallery.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
/**
* Front end content element "gallery".
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class ContentGallery extends \ContentElement
{
/**
* Files object
* @var \Model\Collection|\FilesModel
*/
protected $objFiles;
/**
* Template
* @var string
*/
protected $strTemplate = 'ce_gallery';
/**
* Return if there are no files
*
* @return string
*/
public function generate()
{
// Use the home directory of the current user as file source
if ($this->useHomeDir && FE_USER_LOGGED_IN)
{
$this->import('FrontendUser', 'User');
if ($this->User->assignDir && $this->User->homeDir)
{
$this->multiSRC = array($this->User->homeDir);
}
}
else
{
$this->multiSRC = deserialize($this->multiSRC);
}
// Return if there are no files
if (!is_array($this->multiSRC) || empty($this->multiSRC))
{
return '';
}
// Get the file entries from the database
$this->objFiles = \FilesModel::findMultipleByUuids($this->multiSRC);
if ($this->objFiles === null)
{
if (!\Validator::isUuid($this->multiSRC[0]))
{
return '<p class="error">'.$GLOBALS['TL_LANG']['ERR']['version2format'].'</p>';
}
return '';
}
return parent::generate();
}
/**
* Generate the content element
*/
protected function compile()
{
/** @var \PageModel $objPage */
global $objPage;
$images = array();
$auxDate = array();
$objFiles = $this->objFiles;
// Get all images
while ($objFiles->next())
{
// Continue if the files has been processed or does not exist
if (isset($images[$objFiles->path]) || !file_exists(TL_ROOT . '/' . $objFiles->path))
{
continue;
}
// Single files
if ($objFiles->type == 'file')
{
$objFile = new \File($objFiles->path, true);
if (!$objFile->isImage)
{
continue;
}
$arrMeta = $this->getMetaData($objFiles->meta, $objPage->language);
if (empty($arrMeta))
{
if ($this->metaIgnore)
{
continue;
}
elseif ($objPage->rootFallbackLanguage !== null)
{
$arrMeta = $this->getMetaData($objFiles->meta, $objPage->rootFallbackLanguage);
}
}
// Use the file name as title if none is given
if ($arrMeta['title'] == '')
{
$arrMeta['title'] = specialchars($objFile->basename);
}
// Add the image
$images[$objFiles->path] = array
(
'id' => $objFiles->id,
'uuid' => $objFiles->uuid,
'name' => $objFile->basename,
'singleSRC' => $objFiles->path,
'alt' => $arrMeta['title'],
'imageUrl' => $arrMeta['link'],
'caption' => $arrMeta['caption']
);
$auxDate[] = $objFile->mtime;
}
// Folders
else
{
$objSubfiles = \FilesModel::findByPid($objFiles->uuid);
if ($objSubfiles === null)
{
continue;
}
while ($objSubfiles->next())
{
// Skip subfolders
if ($objSubfiles->type == 'folder')
{
continue;
}
$objFile = new \File($objSubfiles->path, true);
if (!$objFile->isImage)
{
continue;
}
$arrMeta = $this->getMetaData($objSubfiles->meta, $objPage->language);
if (empty($arrMeta))
{
if ($this->metaIgnore)
{
continue;
}
elseif ($objPage->rootFallbackLanguage !== null)
{
$arrMeta = $this->getMetaData($objSubfiles->meta, $objPage->rootFallbackLanguage);
}
}
// Use the file name as title if none is given
if ($arrMeta['title'] == '')
{
$arrMeta['title'] = specialchars($objFile->basename);
}
// Add the image
$images[$objSubfiles->path] = array
(
'id' => $objSubfiles->id,
'uuid' => $objSubfiles->uuid,
'name' => $objFile->basename,
'singleSRC' => $objSubfiles->path,
'alt' => $arrMeta['title'],
'imageUrl' => $arrMeta['link'],
'caption' => $arrMeta['caption']
);
$auxDate[] = $objFile->mtime;
}
}
}
// Sort array
switch ($this->sortBy)
{
default:
case 'name_asc':
uksort($images, 'basename_natcasecmp');
break;
case 'name_desc':
uksort($images, 'basename_natcasercmp');
break;
case 'date_asc':
array_multisort($images, SORT_NUMERIC, $auxDate, SORT_ASC);
break;
case 'date_desc':
array_multisort($images, SORT_NUMERIC, $auxDate, SORT_DESC);
break;
case 'meta': // Backwards compatibility
case 'custom':
if ($this->orderSRC != '')
{
$tmp = deserialize($this->orderSRC);
if (!empty($tmp) && is_array($tmp))
{
// Remove all values
$arrOrder = array_map(function () {}, array_flip($tmp));
// Move the matching elements to their position in $arrOrder
foreach ($images as $k=>$v)
{
if (array_key_exists($v['uuid'], $arrOrder))
{
$arrOrder[$v['uuid']] = $v;
unset($images[$k]);
}
}
// Append the left-over images at the end
if (!empty($images))
{
$arrOrder = array_merge($arrOrder, array_values($images));
}
// Remove empty (unreplaced) entries
$images = array_values(array_filter($arrOrder));
unset($arrOrder);
}
}
break;
case 'random':
shuffle($images);
break;
}
$images = array_values($images);
// Limit the total number of items (see #2652)
if ($this->numberOfItems > 0)
{
$images = array_slice($images, 0, $this->numberOfItems);
}
$offset = 0;
$total = count($images);
$limit = $total;
// Paginate the result of not randomly sorted (see #8033)
if ($this->perPage > 0 && $this->sortBy != 'random')
{
// Get the current page
$id = 'page_g' . $this->id;
$page = (\Input::get($id) !== null) ? \Input::get($id) : 1;
// Do not index or cache the page if the page number is outside the range
if ($page < 1 || $page > max(ceil($total/$this->perPage), 1))
{
/** @var \PageError404 $objHandler */
$objHandler = new $GLOBALS['TL_PTY']['error_404']();
$objHandler->generate($objPage->id);
}
// Set limit and offset
$offset = ($page - 1) * $this->perPage;
$limit = min($this->perPage + $offset, $total);
$objPagination = new \Pagination($total, $this->perPage, \Config::get('maxPaginationLinks'), $id);
$this->Template->pagination = $objPagination->generate("\n ");
}
$rowcount = 0;
$colwidth = floor(100/$this->perRow);
$intMaxWidth = (TL_MODE == 'BE') ? floor((640 / $this->perRow)) : floor((\Config::get('maxImageWidth') / $this->perRow));
$strLightboxId = 'lightbox[lb' . $this->id . ']';
$body = array();
// Rows
for ($i=$offset; $i<$limit; $i=($i+$this->perRow))
{
$class_tr = '';
if ($rowcount == 0)
{
$class_tr .= ' row_first';
}
if (($i + $this->perRow) >= $limit)
{
$class_tr .= ' row_last';
}
$class_eo = (($rowcount % 2) == 0) ? ' even' : ' odd';
// Columns
for ($j=0; $j<$this->perRow; $j++)
{
$class_td = '';
if ($j == 0)
{
$class_td .= ' col_first';
}
if ($j == ($this->perRow - 1))
{
$class_td .= ' col_last';
}
$objCell = new \stdClass();
$key = 'row_' . $rowcount . $class_tr . $class_eo;
// Empty cell
if (!is_array($images[($i+$j)]) || ($j+$i) >= $limit)
{
$objCell->colWidth = $colwidth . '%';
$objCell->class = 'col_'.$j . $class_td;
}
else
{
// Add size and margin
$images[($i+$j)]['size'] = $this->size;
$images[($i+$j)]['imagemargin'] = $this->imagemargin;
$images[($i+$j)]['fullsize'] = $this->fullsize;
$this->addImageToTemplate($objCell, $images[($i+$j)], $intMaxWidth, $strLightboxId);
// Add column width and class
$objCell->colWidth = $colwidth . '%';
$objCell->class = 'col_'.$j . $class_td;
}
$body[$key][$j] = $objCell;
}
++$rowcount;
}
$strTemplate = 'gallery_default';
// Use a custom template
if (TL_MODE == 'FE' && $this->galleryTpl != '')
{
$strTemplate = $this->galleryTpl;
}
/** @var \FrontendTemplate|object $objTemplate */
$objTemplate = new \FrontendTemplate($strTemplate);
$objTemplate->setData($this->arrData);
$objTemplate->body = $body;
$objTemplate->headline = $this->headline; // see #1603
$this->Template->images = $objTemplate->parse();
}
}