forked from terminal42/contao-pageimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageImage.php
225 lines (179 loc) · 6.41 KB
/
PageImage.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
<?php
/**
* pageimage Extension for Contao Open Source CMS
*
* @copyright Copyright (c) 2009-2014, terminal42 gmbh
* @author terminal42 gmbh <[email protected]>
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL
* @link http://github.com/terminal42/contao-pageimage
*/
class PageImage extends Frontend
{
/**
* Images
* @var array
*/
protected static $arrImages;
/**
* Replace "pageimage" inserttag
* @param string
* @return mixed
*/
public static function replaceTags($strTag)
{
$arrTag = trimsplit('::', $strTag);
switch($arrTag[0])
{
case 'pageimage':
$arrTag[0] = 'pageimage_path';
// Do NOT add a break;
case 'pageimage_alt':
case 'pageimage_title':
case 'pageimage_href':
global $objPage;
$arrImage = static::getOne($objPage, (int) $arrTag[1]);
$strKey = str_replace('pageimage_', '', $arrTag[0]);
if (null === $arrImage || !isset($arrImage[$strKey])) {
return '';
}
return $arrImage[$strKey];
}
return false;
}
/**
* 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, $intOffset=0, $intLength=null, $blnInherit=true)
{
$arrImages = static::findForPage($objPage, $blnInherit);
if (null === $arrImages || !isset($arrImages[$intOffset])) {
return array();
}
return array_slice($arrImages, $intOffset, $intLength);
}
/**
* 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->pageImage == '') {
return array();
}
$arrImages = array();
$objImages = \FilesModel::findMultipleByIds(deserialize($objPage->pageImage, 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['alt'] = $objPage->pageImageAlt;
$arrImage['imageUrl'] = $arrMeta['link'];
$arrImage['caption'] = $arrMeta['caption'];
if (null !== $objPage->getRelated('pageImageJumpTo')) {
$arrImage['hasLink'] = true;
$arrImage['title'] = ($objPage->pageImageTitle ?: ($arrMeta['title'] ?: ($objJumpTo->pageTitle ?: $objJumpTo->title)));
$arrImage['href'] = \Controller::generateFrontendUrl($objPage->getRelated('pageImageJumpTo')->row());
}
$arrImages[] = $arrImage;
}
$arrOrder = deserialize($objPage->pageImageOrder);
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;
}
}