forked from terminal42/contao-pageimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageSlideshow.php
183 lines (146 loc) · 5.04 KB
/
PageSlideshow.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
<?php
/**
* pageimage Extension for Contao Open Source CMS
*
* @copyright Copyright (c) 2009-2014, terminal42 gmbh, 2018 Comolo GmbH
* @author terminal42 gmbh <info@terminal42.ch>, Hendrik Obermayer <github.com/henobi>
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL
* @link http://github.com/comolo/contao-pageimage
*/
class PageSlideshow extends Frontend
{
/**
* Images
* @var array
*/
protected static $arrImages;
/**
* Get one image by offset
* @param \PageModel
* @param int
* @param bool
* @return array|null
*/
public static function getOne(\PageModel $objPage, $intIndex=0, $blnInherit=true)
{
$arrImages = static::findForPage($objPage, $blnInherit);
if ($arrImages === null) {
return null;
}
// Random image
if ($intIndex < 0) {
$intIndex = rand(0, count($arrImages) - 1);
}
if (!isset($arrImages[$intIndex])) {
return null;
}
return $arrImages[$intIndex];
}
/**
* Get multiple images by offset and length
* @param int
* @param int|null
* @param bool
* @return array
*/
public static function getMultiple(\PageModel $objPage, $blnInherit=true)
{
$arrImages = static::findForPage($objPage, $blnInherit);
if (null === $arrImages) {
return [];
}
return $arrImages;
}
/**
* Find images for given page
* @param \PageModel
* @return array
*/
protected static function findForPage(\PageModel $objPage, $blnInherit=true)
{
if (!isset(static::$arrImages[$objPage->id])) {
static::$arrImages[$objPage->id] = false;
$arrImages = static::parsePage($objPage);
if (!empty($arrImages)) {
static::$arrImages[$objPage->id] = array(
'images' => $arrImages,
'inherited' => false
);
}
// Walk the trail
else {
$objPage->loadDetails();
$objTrails = \PageModel::findMultipleByIds(array_reverse($objPage->trail));
if (null !== $objTrails) {
foreach ($objTrails as $objTrail) {
$arrImages = static::parsePage($objTrail);
if (!empty($arrImages)) {
static::$arrImages[$objPage->id] = array(
'images' => $arrImages,
'inherited' => true
);
break;
}
}
}
}
}
if (static::$arrImages[$objPage->id] === false || (!$blnInherit && static::$arrImages[$objPage->id]['inherited'])) {
return null;
}
return static::$arrImages[$objPage->id]['images'];
}
/**
* Parse the given page and return the image information
* @param \PageModel
* @return array
*/
protected static function parsePage(\PageModel $objPage)
{
if ($objPage->pageSlideshow == '') {
return [];
}
$arrImages = array();
$objImages = \FilesModel::findMultipleByIds(deserialize($objPage->pageSlideshow, true));
if (null !== $objImages) {
while ($objImages->next()) {
$objFile = new \File($objImages->path, true);
if (!$objFile->isGdImage) {
continue;
}
$arrImage = $objImages->row();
$arrMeta = static::getMetaData($objImages->meta, $objPage->language);
// Use the file name as title if none is given
if ($arrMeta['title'] == '') {
$arrMeta['title'] = specialchars(str_replace('_', ' ', preg_replace('/^[0-9]+_/', '', $objFile->filename)));
}
$arrImage['imageUrl'] = $arrMeta['link'];
$arrImages[] = $arrImage;
}
$arrOrder = deserialize($objPage->pageSlideshowOrder);
if (!empty($arrOrder) && is_array($arrOrder))
{
// Remove all values
$arrOrder = array_map(function(){}, array_flip($arrOrder));
// Move the matching elements to their position in $arrOrder
foreach ($arrImages as $k=>$v)
{
if (array_key_exists($v['uuid'], $arrOrder))
{
$arrOrder[$v['uuid']] = $v;
unset($arrImages[$k]);
}
}
// Append the left-over images at the end
if (!empty($arrImages))
{
$arrOrder = array_merge($arrOrder, array_values($arrImages));
}
// Remove empty (unreplaced) entries
$arrImages = array_values(array_filter($arrOrder));
unset($arrOrder);
}
}
return $arrImages;
}
}