-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMarkupPwpswpGallery.module
275 lines (218 loc) · 7.55 KB
/
MarkupPwpswpGallery.module
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
<?php namespace ProcessWire;
/**
* MarkupPwpswpGallery
*
* Basic gallery and base module for other MarkupPwpswpGallery extension modules
* legacy plain theme
*
*/
class MarkupPwpswpGallery extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Pwpswp Gallery: Plain',
'author' => 'Steffen Henschel',
'version' => '0.0.1',
'summary' => __('Basic gallery and base module for other MarkupPwpswpGallery extension modules. Alias: "plain"'),
'permission' => [],
'autoload' => false,
'singular' => false,
'permanent' => false,
'requires' => [
'ProcessWire>=3.0.0',
'PHP>=5.6',
],
'installs' => []
);
}
/**
* alias
*
* return a short unique name for this module here
* legacy theme name
* it will be used for css classes, module identification, etc.
*
* @return string
*/
static public function alias() {
return 'plain';
}
/**
* init
*
* @return void
*/
public function init() {}
// public function setOptions() {} // maybe set some options beforehand?
/**
* getRelatedFileUrl
*
* returns url to a file of same name but with given extension
* residing just next to this .module file
*
* @param string $ext file extension
* @return string|null
*/
public function getRelatedFileUrl($ext) {
$this->rootPath = wire('config')->paths->root;
// reflection class to get correct file name (__FILE__ returns base class)
$reflector = new \ReflectionClass('\\' .$this->className(true));
$file = str_replace('.module', '.'.$ext, $reflector->getFileName());
if(is_file($file)) {
return str_replace($this->rootPath, '/', $file);
} else {
return null;
}
}
/**
* getJsUrl
*
* @return string|null
*/
public function getJsUrl() {
return $this->getRelatedFileUrl('js');
}
/**
* getCssUrl
*
* @return string|null
*/
public function getCssUrl() {
return $this->getRelatedFileUrl('css');
}
/**
* render
*
* build and render the gallery structure for this module
*
* these methods are helpful and provide direct functionality:
* use $this->renderImageElement() to render a single image unit
* use $this->wrapMarkup() to wrap your structure into
*
* @param [type] $images
* @param [type] $options
* @return string
*/
public function ___render($images, $options = []) {
$imagesOutput = '';
foreach($images as $image) {
$imageData = $this->prepareImageData($image, $options);
$imagesOutput .= $this->renderImageElement($imageData);
}
return $this->wrapMarkup($imagesOutput, $options);
}
/**
* resizeImage
*
* helper to resize an image according to
*
* @param array $resizeOptions
* @param array $mixinOptions
* @return callable
*/
protected function resizeImage($image, $resizeOptions) {
if(isset($resizeOptions['size'])) {
if(strpos($resizeOptions['size'], 'x' === false)) throw new WireException("size option must contain \"x\"", 1);
$resizeOptions['size'] = explode('x', $resizeOptions['size']);
}
$w = isset($resizeOptions['size'][0]) ? $resizeOptions['size'][0] : (isset($resizeOptions['width']) ? $resizeOptions['width'] : 0);
$h = isset($resizeOptions['size'][1]) ? $resizeOptions['size'][1] : (isset($resizeOptions['height']) ? $resizeOptions['height'] : 0);
return $image->size($w, $h, $resizeOptions);
}
/**
* prepareImageData
*
* helper to prepare all data for image template
*
* @param PageImage $image
* @param array $options
* @return array
*/
protected function prepareImageData(WireData $image, $options = []) {
$imageData = $image->getArray();
$imageData['url'] = $image->url;
$imageData['width'] = $image->width;
$imageData['height'] = $image->height;
$imageData['alt'] = $imageData['alt'] ?: $imageData['description'];
$imageData['thumbImage'] = $this->resizeImage($image, $options['imageResizerOptions']);
$imageData['loresImage'] = $this->resizeImage($image, $options['loresResizerOptions']);
return $imageData;
}
/**
* renderImageElement [markup helper]
*
* render markup for a single image
* the atom piece of the gallery
*
* expected $data:
* url - original image url!
* width - original image width
* height - original image height
* alt - image alt text
*
* loresImage - low resolution image
* thumbImage - thumbnail iamge
*
*
* @param [type] $data
* @return string
*/
protected function renderImageElement($data) {
$out = '';
$out .= '<figure class="pwpswp-gallery__item pwpswp-gallery__item--' .$this->alias() .'"';
$out .= ' itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"';
$out .= '>';
$out .= '<a class="pwpswp-gallery__link pwpswp-gallery__link--' .$this->alias() .'"';
$out .= ' itemprop="contentUrl"';
$out .= ' href="' .$data['url'] .'"';
$out .= ' data-size="' .$data['width'] .'x' .$data['height'] .'"';
$out .= ' data-lores-src="' .$data['loresImage']->url .'"';
$out .= '>';
$out .= '<img class="pwpswp-gallery__image pwpswp-gallery__image--' .$this->alias() .'"';
$out .= ' itemprop="thumbnail"';
$out .= ' src="' .$data['thumbImage']->url .'"';
$out .= ' alt="' .$data['alt'] .'"';
$out .= '/>';
$out .= '</a>';
$out .= '<figcaption class="pwpswp-gallery__caption pwpswp-gallery__caption--' .$this->alias() .'"';
$out .= ' itemprop="caption description">';
$out .= $data['description'];
$out .= '</figcaption>';
$out .= '</figure>';
return $out;
}
/**
* wrapMarkup [markup helper]
*
* put the images into a wrapping tag
* defines the gallery package
*
* @param [type] $options
* @return void
*/
protected function wrapMarkup($innerMarkup, $options = []) {
$out = '<div';
$out .= ' class="pwpswp-gallery pwpswp-gallery--' .$this->alias() .'"';
$out .= ' itemscope itemtype="http://schema.org/ImageGallery"';
$out .= isset($options['pswpOptions']) ? $this->getPswpOptionsAttribute($options['pswpOptions']) : '';
$out .= '>';
$out .= '<div class="pwpswp-gallery__inner pwpswp-gallery__inner--' .$this->alias() .'">';
$out .= $innerMarkup;
$out .= '</div></div>';
return $out;
}
/**
* getPswpOptionsAttribute [markup helper]
*
* @param object $options photoswipe options (will be json encoded)
* @return void
*/
protected function getPswpOptionsAttribute($pswpOptions) {
return isset($pswpOptions) ? ' data-pswp-options="' . htmlspecialchars(json_encode($pswpOptions), ENT_QUOTES, 'UTF-8') . '"' : '';
}
/**
* module installation / deinstallation
*
*/
public function ___install() {}
public function ___uninstall() {}
}