lib.php 36.9 KB
Newer Older
Evan Goldenberg's avatar
Evan Goldenberg committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
21
 * @subpackage blocktype-gallery
Evan Goldenberg's avatar
Evan Goldenberg committed
22
 * @author     Catalyst IT Ltd
23
 * @author     Gregor Anzelj (External Galleries, e.g. Flickr, Picasa)
Evan Goldenberg's avatar
Evan Goldenberg committed
24
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
26
 * @copyright  (C) 2011 Gregor Anzelj <gregor.anzelj@gmail.com>
Evan Goldenberg's avatar
Evan Goldenberg committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 *
 */

defined('INTERNAL') || die();

class PluginBlocktypeGallery extends PluginBlocktype {

    public static function get_title() {
        return get_string('title', 'blocktype.file/gallery');
    }

    public static function get_description() {
        return get_string('description', 'blocktype.file/gallery');
    }

    public static function get_categories() {
        return array('fileimagevideo');
    }

    public static function get_instance_javascript(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
48
        switch ($configdata['style']) {
Evan Goldenberg's avatar
Evan Goldenberg committed
49
            case 0: // thumbnails
50
            case 2: // squarethumbs
Evan Goldenberg's avatar
Evan Goldenberg committed
51
52
53
54
55
56
57
58
59
60
61
62
63
                return array();
            case 1: // slideshow
                return array('js/slideshow.js');
        }
    }

    public static function get_instance_config_javascript() {
        return array('js/configform.js');
    }

    public static function render_instance(BlockInstance $instance, $editing=false) {
        $configdata = $instance->get('configdata'); // this will make sure to unserialize it for us
        $configdata['viewid'] = $instance->get('view');
64
65
66
        $style = isset($configdata['style']) ? intval($configdata['style']) : 2;
        $copyright = null; // Needed to set Panoramio copyright later...

Richard Mansfield's avatar
Richard Mansfield committed
67
        switch ($style) {
Evan Goldenberg's avatar
Evan Goldenberg committed
68
69
            case 0: // thumbnails
                $template = 'thumbnails';
70
                $width = isset($configdata['width']) ? $configdata['width'] : 75;
Evan Goldenberg's avatar
Evan Goldenberg committed
71
72
73
                break;
            case 1: // slideshow
                $template = 'slideshow';
74
                $width = isset($configdata['width']) ? $configdata['width'] : 400;
Evan Goldenberg's avatar
Evan Goldenberg committed
75
                break;
76
77
78
79
            case 2: // square thumbnails
                $template = 'squarethumbs';
                $width = isset($configdata['width']) ? $configdata['width'] : 75;
                break;
Evan Goldenberg's avatar
Evan Goldenberg committed
80
81
        }

82
83
84
85
        $images = array();
        $slimbox2 = get_config_plugin('blocktype', 'gallery', 'useslimbox2');
        if ($slimbox2) {
            $slimbox2attr = 'lightbox_' . $instance->get('id');
86
        }
87
88
        else {
            $slimbox2attr = null;
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
        // if we're trying to embed external gallery (thumbnails or slideshow)
        if (isset($configdata['select']) && $configdata['select'] == 2) {
            $gallery = self::make_gallery_url($configdata['external']);
            if (empty($gallery)) {
                return get_string('externalnotsupported', 'blocktype.file/gallery');
            }
            $url  = isset($gallery['url']) ? hsc($gallery['url']) : null;
            $type = isset($gallery['type']) ? hsc($gallery['type']) : null;
            $var1 = isset($gallery['var1']) ? hsc($gallery['var1']) : null;
            $var2 = isset($gallery['var2']) ? hsc($gallery['var2']) : null;

            switch ($type) {
                case 'widget':
                /*****************************
                  Roy Tanck's FLICKR WIDGET
                  for Flickr RSS & Picasa RSS
          http://www.roytanck.com/get-my-flickr-widget/
                 *****************************/
                    $widget_sizes = array(100, 200, 300);
                    $width = self::find_nearest($widget_sizes, $width);
                    $images = urlencode(str_replace('&amp;', '&', $url));
                    $template = 'imagecloud';
                    break;
                case 'picasa':
                    // Slideshow
                    if ($style == 1) {
                        $picasa_show_sizes = array(144, 288, 400, 600, 800);
                        $width = self::find_nearest($picasa_show_sizes, $width);
                        $height = round($width * 0.75);
                        $images = array('user' => $var1, 'gallery' => $var2);
                        $template = 'picasashow';
                    }
                    // Thumbnails
                    else {
                        $picasa_thumbnails = array(32, 48, 64, 72, 104, 144, 150, 160);
                        $width = self::find_nearest($picasa_thumbnails, $width);
                        // If the Thumbnails should be Square...
                        if ($style == 2) {
                            $small = 's' . $width . '-c';
                            $URL = 'http://picasaweb.google.com/data/feed/api/user/' . $var1 . '/album/' . $var2 . '?kind=photo&thumbsize=' . $width . 'c';
                        }
                        else {
                            $small = 's' . $width;
                            $URL = 'http://picasaweb.google.com/data/feed/api/user/' . $var1 . '/album/' . $var2 . '?kind=photo&thumbsize=' . $width;
                        }
                        $big = 's' . get_config_plugin('blocktype', 'gallery', 'previewwidth');

                        $xmlDoc = new DOMDocument('1.0', 'UTF-8');
                        $config = array(
                            CURLOPT_URL => $URL,
                            CURLOPT_RETURNTRANSFER => true,
                        );
                        $result = mahara_http_request($config);
                        $xmlDoc->loadXML($result->data);
                        $photos = $xmlDoc->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'group');
                        foreach ($photos as $photo) {
                            $children = $photo->cloneNode(true);
                            $thumb = $children->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'thumbnail')->item(0)->getAttribute('url');
                            $description = null;
                            if (isset($children->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'description')->item(0)->firstChild->nodeValue)) {
                                $description = $children->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'description')->item(0)->firstChild->nodeValue;
                            }

                            $images[] = array(
                                'link' => str_replace($small, $big, $thumb),
                                'source' => $thumb,
                                'title' => $description,
                                'slimbox2' => $slimbox2attr
                            );
                        }
                    }
                    break;
                case 'flickr':
                    // Slideshow
                    if ($style == 1) {
                        $flickr_show_sizes = array(400, 500, 700, 800);
                        $width = self::find_nearest($flickr_show_sizes, $width);
                        $height = round($width * 0.75);
                        $images = array('user' => $var1, 'gallery' => $var2);
                        $template = 'flickrshow';
                    }
                    // Thumbnails
                    else {
                        $width = 75; // Currently only thumbnail size, that Flickr supports

                        $api_key = get_config_plugin('blocktype', 'gallery', 'flickrapikey');
                        $URL = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&extras=url_sq,url_t&photoset_id=' . $var2 . '&api_key=' . $api_key;
                        $xmlDoc = new DOMDocument('1.0', 'UTF-8');
                        $config = array(
                            CURLOPT_URL => $URL,
                            CURLOPT_RETURNTRANSFER => true,
                        );
                        $result = mahara_http_request($config);
                        $xmlDoc->loadXML($result->data);
                        $photos = $xmlDoc->getElementsByTagName('photo');
                        foreach ($photos as $photo) {
                            // If the Thumbnails should be Square...
                            if ($style == 2) {
                                $thumb = $photo->getAttribute('url_sq');
                                $link = str_replace('_s.jpg', '_b.jpg', $thumb);
                            }
                            else {
                                $thumb = $photo->getAttribute('url_t');
                                $link = str_replace('_t.jpg', '_b.jpg', $thumb);
                            }
                            $description = $photo->getAttribute('title');
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
                            $images[] = array(
                                'link' => $link,
                                'source' => $thumb,
                                'title' => $description,
                                'slimbox2' => $slimbox2attr
                            );
                        }
                    }
                    break;
                case 'panoramio':
                    // Slideshow
                    if ($style == 1) {
                        $height = round($width * 0.75);
                        $images = array('user' => $var1);
                        $template = 'panoramioshow';
                    }
                    // Thumbnails
                    else {
                        $copyright = get_string('panoramiocopyright', 'blocktype.file/gallery');
                        $URL = 'http://www.panoramio.com/map/get_panoramas.php?set=' . $var1 . '&from=0&to=50&size=original&mapfilter=true';
                        $config = array(
                            CURLOPT_URL => $URL,
                            CURLOPT_RETURNTRANSFER => true,
                        );
                        $result = mahara_http_request($config);
                        $data = json_decode($result->data, true);
                        foreach ($data['photos'] as $photo) {
                            $link = str_replace('/original/', '/large/', $photo['photo_file_url']);
                            // If the Thumbnails should be Square...
                            if ($style == 2) {
                                $thumb = str_replace('/original/', '/square/', $photo['photo_file_url']);
                                $width = 60; // Currently only square thumbnail size, that Panoramio supports
                            }
                            else {
                                $thumb = str_replace('/original/', '/thumbnail/', $photo['photo_file_url']);
                            }
                            $title = (!empty($photo['photo_title']) ? $photo['photo_title'] : get_string('Photo', 'blocktype.file/gallery'));
                            $description =  '<a href="' . $photo['photo_url'] . '" target="_blank">' . $title . '</a>'
                                         . '&nbsp;' . get_string('by', 'blocktype.file/gallery') . '&nbsp;'
                                         . '<a href="' . $photo['owner_url'] . '" target="_blank">' . $photo['owner_name'] . '</a>';

                            $images[] = array(
                                'link' => $link,
                                'source' => $thumb,
                                'title' => $description,
                                'slimbox2' => $slimbox2attr
                            );
                        }
                    }
                    break;
                case 'photobucket':
                    // Slideshow
                    if ($style == 1) {
                        $height = round($width * 0.75);
                        $images = array('url' => $url, 'user' => $var1, 'album' => $var2);
                        $template = 'photobucketshow';
                    }
                    // Thumbnails
                    else {
                        $consumer_key = get_config_plugin('blocktype', 'gallery', 'pbapikey'); // PhotoBucket API key
                        $consumer_secret = get_config_plugin('blocktype', 'gallery', 'pbapiprivatekey'); //PhotoBucket API private key

                        $oauth_signature_method = 'HMAC-SHA1';
                        $oauth_version = '1.0';
                        $oauth_timestamp = time();
                        $mt = microtime();
                        $rand = mt_rand();
                        $oauth_nonce = md5($mt . $rand);

                        $method = 'GET';
                        $albumname = $var1 . '/' . $var2;
                        $api_url = 'http://api.photobucket.com/album/' . urlencode($albumname);

                        $params = null;
                        $paramstring = 'oauth_consumer_key=' . $consumer_key . '&oauth_nonce=' . $oauth_nonce . '&oauth_signature_method=' . $oauth_signature_method . '&oauth_timestamp=' . $oauth_timestamp . '&oauth_version=' . $oauth_version;
                        $base = urlencode($method) . '&' . urlencode($api_url) . '&' . urlencode($paramstring);
                        $oauth_signature = base64_encode(hash_hmac('sha1', $base, $consumer_secret.'&', true));

                        $URL = $api_url . '?' . $paramstring . '&oauth_signature=' . urlencode($oauth_signature);
                        $xmlDoc = new DOMDocument('1.0', 'UTF-8');
                        $config = array(
                            CURLOPT_URL => $URL,
                            CURLOPT_HEADER => false,
                            CURLOPT_RETURNTRANSFER => true,
                        );
                        $result = mahara_http_request($config);
                        $xmlDoc->loadXML($result->data);

                        $xmlDoc2 = new DOMDocument('1.0', 'UTF-8');
                        $config2 = array(
                            CURLOPT_URL => $xmlDoc->getElementsByTagName('url')->item(0)->firstChild->nodeValue,
                            CURLOPT_HEADER => false,
                            CURLOPT_RETURNTRANSFER => true,
                        );
                        $result2 = mahara_http_request($config2);
                        $xmlDoc2->loadXML($result->data);

                        $photos = $xmlDoc2->getElementsByTagName('media');
                        foreach ($photos as $photo) {
                            $children = $photo->cloneNode(true);
                            $link = $children->getElementsByTagName('url')->item(0)->firstChild->nodeValue;
                            $thumb = $children->getElementsByTagName('thumb')->item(0)->firstChild->nodeValue;
                            $description = null;
                            if (isset($children->getElementsByTagName('description')->item(0)->firstChild->nodeValue)) {
                                $description = $children->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
                            }

                            $images[] = array(
                                'link' => $link,
                                'source' => $thumb,
                                'title' => $description,
                                'slimbox2' => $slimbox2attr
                            );
                        }
                    }
                    break;
                case 'windowslive':
                    // Slideshow
                    if ($style == 1) {
                        $images = array('url' => $url, 'user' => $var1, 'album' => $var2);
                        $template = 'windowsliveshow';
                    }
                    // Thumbnails
                    else {
                        $config = array(
                            CURLOPT_URL => str_replace(' ', '%20', $url),
                            CURLOPT_HEADER => false,
                            CURLOPT_RETURNTRANSFER => true,
                        );
                        $result = mahara_http_request($config);
                        $data = $result->data;

                        // Extract data about images and thumbs from HTML source - hack!
                        preg_match_all("#previewImageUrl: '([a-zA-Z0-9\_\-\.\\\/]+)'#", $data, $photos);
                        preg_match_all("#thumbnailImageUrl: '([a-zA-Z0-9\_\-\.\\\/]+)'#", $data, $thumbs);

                        for ($i = 0; $i < sizeof($photos[1]); $i++) {
                            $images[] = array(
                                'link' => str_replace(array('\x3a','\x2f','\x25','\x3fpsid\x3d1'), array(':','/','%',''), $photos[1][$i]),
                                'source' => str_replace(array('\x3a','\x2f','\x25','\x3fpsid\x3d1'), array(':','/','%',''), $thumbs[1][$i]),
                                'title' => null,
                                'slimbox2' => $slimbox2attr
                            );
                        }
                    }
                    break;
344
            }
345
346
347
348
349
        }
        else {
            $artefactids = array();
            if (isset($configdata['select']) && $configdata['select'] == 1 && is_array($configdata['artefactids'])) {
                $artefactids = $configdata['artefactids'];
350
            }
351
352
353
            else if (!empty($configdata['artefactid'])) {
                // Get descendents of this folder.
                $artefactids = artefact_get_descendants(array(intval($configdata['artefactid'])));
Evan Goldenberg's avatar
Evan Goldenberg committed
354
            }
355

356
357
358
359
            // This can be either an image or profileicon. They both implement
            // render_self
            foreach ($artefactids as $artefactid) {
                $image = $instance->get_artefact_instance($artefactid);
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
387
388
389
390
391
392
393
394
395
                if ($image instanceof ArtefactTypeProfileIcon) {
                    $src = get_config('wwwroot') . 'thumb.php?type=profileiconbyid&id=' . $artefactid;
                    $description = $image->get('title');
                }
                else if ($image instanceof ArtefactTypeImage) {
                    $src = get_config('wwwroot') . 'artefact/file/download.php?file=' . $artefactid;
                    $src .= '&view=' . $instance->get('view');
                    $description = $image->get('description');
                }
                else {
                    continue;
                }

                if ($slimbox2) {
                    $link = $src . '&maxwidth=' . get_config_plugin('blocktype', 'gallery', 'previewwidth');
                }
                else {
                    $link = get_config('wwwroot') . 'view/artefact.php?artefact=' . $artefactid . '&view=' . $instance->get('view');
                }

                // If the Thumbnails are Square or not...
                if ($style == 2) {
                    $src .= '&size=' . $width . 'x' . $width;
                }
                else {
                    $src .= '&maxwidth=' . $width;
                }

                $images[] = array(
                    'link' => $link,
                    'source' => $src,
                    'title' => $image->get('description'),
                    'slimbox2' => $slimbox2attr
                );
            }
Evan Goldenberg's avatar
Evan Goldenberg committed
396
397
398
399
400
401
        }

        $smarty = smarty_core();
        $smarty->assign('instanceid', $instance->get('id'));
        $smarty->assign('count', count($images));
        $smarty->assign('images', $images);
402
        $smarty->assign('width', $width);
403
404
405
406
407
408
409
410
        if (isset($height)) {
            $smarty->assign('height', $height);
        }
        if (isset($needsapikey)) {
            $smarty->assign('needsapikey', $needsapikey);
        }
        $smarty->assign('frame', get_config_plugin('blocktype', 'gallery', 'photoframe'));
        $smarty->assign('copyright', $copyright);
Evan Goldenberg's avatar
Evan Goldenberg committed
411
412
413
414

        return $smarty->fetch('blocktype:gallery:' . $template . '.tpl');
    }

415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
    public static function has_config() {
        return true;
    }

    public static function get_config_options() {
        $elements = array();
        $elements['gallerysettings'] = array(
            'type' => 'fieldset',
            'legend' => get_string('gallerysettings', 'blocktype.file/gallery'),
            'collapsible' => true,
            'elements' => array(
                'useslimbox2' => array(
                    'type'         => 'checkbox',
                    'title'        => get_string('useslimbox2', 'blocktype.file/gallery'),
                    'description'  => get_string('useslimbox2desc', 'blocktype.file/gallery'),
                    'defaultvalue' => get_config_plugin('blocktype', 'gallery', 'useslimbox2'),
                ),
                'photoframe' => array(
                    'type'         => 'checkbox',
                    'title'        => get_string('photoframe', 'blocktype.file/gallery'),
                    'description'  => get_string('photoframedesc', 'blocktype.file/gallery'),
                    'defaultvalue' => get_config_plugin('blocktype', 'gallery', 'photoframe'),
                ),
                'previewwidth' => array(
                    'type'         => 'text',
                    'size'         => 4,
                    'title'        => get_string('previewwidth', 'blocktype.file/gallery'),
                    'description'  => get_string('previewwidthdesc', 'blocktype.file/gallery'),
                    'defaultvalue' => get_config_plugin('blocktype', 'gallery', 'previewwidth'),
                    'rules'        => array('integer' => true, 'minvalue' => 16, 'maxvalue' => 1600),
                )
            ),
        );
        $elements['flickrsettings'] = array(
            'type' => 'fieldset',
            'legend' => get_string('flickrsettings', 'blocktype.file/gallery'),
            'collapsible' => true,
            'collapsed' => true,
            'elements' => array(
                'flickrapikey' => array(
                    'type'         => 'text',
                    'title'        => get_string('flickrapikey', 'blocktype.file/gallery'),
                    'size'         => 40, // Flickr API key is actually 32 characters long
                    'description'  => get_string('flickrapikeydesc', 'blocktype.file/gallery'),
                    'defaultvalue' => get_config_plugin('blocktype', 'gallery', 'flickrapikey'),
                ),
            ),
        );
        $elements['photobucketsettings'] = array(
            'type' => 'fieldset',
            'legend' => get_string('pbsettings', 'blocktype.file/gallery'),
            'collapsible' => true,
            'collapsed' => true,
            'elements' => array(
                'pbapikey' => array(
                    'type'         => 'text',
                    'title'        => get_string('pbapikey', 'blocktype.file/gallery'),
                    'size'         => 20, // PhotoBucket API key is actually 9 characters long
                    'description'  => get_string('pbapikeydesc', 'blocktype.file/gallery'),
                    'defaultvalue' => get_config_plugin('blocktype', 'gallery', 'pbapikey'),
                ),
                'pbapiprivatekey' => array(
                    'type'         => 'text',
                    'title'        => get_string('pbapiprivatekey', 'blocktype.file/gallery'),
                    'size'         => 40, // PhotoBucket API private key is actually 32 characters long
                    'defaultvalue' => get_config_plugin('blocktype', 'gallery', 'pbapiprivatekey'),
                ),
            ),
        );
        return array(
            'elements' => $elements,
        );

    }

    public static function save_config_options($values) {
        set_config_plugin('blocktype', 'gallery', 'useslimbox2', (int)$values['useslimbox2']);
        set_config_plugin('blocktype', 'gallery', 'photoframe', (int)$values['photoframe']);
        set_config_plugin('blocktype', 'gallery', 'previewwidth', (int)$values['previewwidth']);
        set_config_plugin('blocktype', 'gallery', 'flickrapikey', $values['flickrapikey']);
        set_config_plugin('blocktype', 'gallery', 'pbapikey', $values['pbapikey']);
        set_config_plugin('blocktype', 'gallery', 'pbapiprivatekey', $values['pbapiprivatekey']);
    }

    public static function postinst($prevversion) {
        if ($prevversion == 0) {
            set_config_plugin('blocktype', 'gallery', 'useslimbox2', 1); // Use Slimbox 2 by default
            set_config_plugin('blocktype', 'gallery', 'photoframe', 1); // Show frame around photos
            set_config_plugin('blocktype', 'gallery', 'previewwidth', 1024); // Maximum photo width for slimbox2 preview
        }
    }

Evan Goldenberg's avatar
Evan Goldenberg committed
507
508
509
510
511
512
513
514
515
516
    public static function has_instance_config() {
        return true;
    }

    public static function instance_config_form($instance) {
        $configdata = $instance->get('configdata');
        safe_require('artefact', 'file');
        $instance->set('artefactplugin', 'file');
        $user = $instance->get('view_obj')->get('owner');
        $select_options = array(
517
518
            0 => get_string('selectfolder', 'blocktype.file/gallery'),
            1 => get_string('selectimages', 'blocktype.file/gallery'),
519
            2 => get_string('selectexternal', 'blocktype.file/gallery'),
Evan Goldenberg's avatar
Evan Goldenberg committed
520
521
522
        );
        $style_options = array(
            0 => get_string('stylethumbs', 'blocktype.file/gallery'),
523
            2 => get_string('stylesquares', 'blocktype.file/gallery'),
Evan Goldenberg's avatar
Evan Goldenberg committed
524
525
            1 => get_string('styleslideshow', 'blocktype.file/gallery'),
        );
526
527
528
529
        if (isset($configdata['select']) && $configdata['select'] == 1) {
            $imageids = isset($configdata['artefactids']) ? $configdata['artefactids'] : array();
            $imageselector = self::imageselector($instance, $imageids);
            $folderselector = self::folderselector($instance, null, 'hidden');
530
531
532
533
534
535
536
            $externalurl = self::externalurl($instance, null, 'hidden');
        }
        else if (isset($configdata['select']) && $configdata['select'] == 2) {
            $imageselector = self::imageselector($instance, null, 'hidden');
            $folderselector = self::folderselector($instance, null, 'hidden');
            $url = isset($configdata['external']) ? urldecode($configdata['external']) : null;
            $externalurl = self::externalurl($instance, $url);
537
538
539
540
541
        }
        else {
            $imageselector = self::imageselector($instance, null, 'hidden');
            $folderid = !empty($configdata['artefactid']) ? array(intval($configdata['artefactid'])) : null;
            $folderselector = self::folderselector($instance, $folderid);
542
            $externalurl = self::externalurl($instance, null, 'hidden');
543
        }
Evan Goldenberg's avatar
Evan Goldenberg committed
544
545
546
547
548
549
550
551
552
553
554
555
        return array(
            'user' => array(
                'type' => 'hidden',
                'value' => $user,
            ),
            'select' => array(
                'type' => 'radio',
                'title' => get_string('select', 'blocktype.file/gallery'),
                'options' => $select_options,
                'defaultvalue' => (isset($configdata['select'])) ? $configdata['select'] : 0,
                'separator' => '<br>',
            ),
556
557
            'images' => $imageselector,
            'folder' => $folderselector,
558
            'external' => $externalurl,
Evan Goldenberg's avatar
Evan Goldenberg committed
559
560
561
562
            'style' => array(
                'type' => 'radio',
                'title' => get_string('style', 'blocktype.file/gallery'),
                'options' => $style_options,
563
                'defaultvalue' => (isset($configdata['style'])) ? $configdata['style'] : 2, // Square thumbnails should be default...
Evan Goldenberg's avatar
Evan Goldenberg committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
                'separator' => '<br>',
            ),
            'width' => array(
                'type' => 'text',
                'title' => get_string('width', 'blocktype.file/gallery'),
                'size' => 3,
                'description' => get_string('widthdescription', 'blocktype.file/gallery'),
                'rules' => array(
                    'minvalue' => 16,
                    'maxvalue' => get_config('imagemaxwidth'),
                ),
                'defaultvalue' => (isset($configdata['width'])) ? $configdata['width'] : '75',
            ),
        );
    }

    public static function instance_config_save($values) {
        if ($values['select'] == 0) {
582
583
            $values['artefactid'] = $values['folder'];
            unset($values['artefactids']);
584
            unset($values['external']);
585
586
587
588
        }
        else if ($values['select'] == 1) {
            $values['artefactids'] = $values['images'];
            unset($values['artefactid']);
589
590
591
592
593
            unset($values['external']);
        }
        else if ($values['select'] == 2) {
            unset($values['artefactid']);
            unset($values['artefactids']);
Evan Goldenberg's avatar
Evan Goldenberg committed
594
        }
595
596
        unset($values['folder']);
        unset($values['images']);
Evan Goldenberg's avatar
Evan Goldenberg committed
597
598
599
        return $values;
    }

600
    public static function imageselector(&$instance, $default=array(), $class=null) {
Evan Goldenberg's avatar
Evan Goldenberg committed
601
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
602
603
604
605
606
        $element['title'] = get_string('Images', 'artefact.file');
        $element['name'] = 'images';
        if ($class) {
            $element['class'] = $class;
        }
Evan Goldenberg's avatar
Evan Goldenberg committed
607
608
        $element['config']['selectone'] = false;
        $element['filters'] = array(
609
            'artefacttype'    => array('image', 'profileicon'),
Evan Goldenberg's avatar
Evan Goldenberg committed
610
611
612
613
        );
        return $element;
    }

614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
    public static function folderselector(&$instance, $default=array(), $class=null) {
        $element = ArtefactTypeFileBase::blockconfig_filebrowser_element($instance, $default);
        $element['title'] = get_string('folder', 'artefact.file');
        $element['name'] = 'folder';
        if ($class) {
            $element['class'] = $class;
        }
        $element['config']['upload'] = false;
        $element['config']['selectone'] = true;
        $element['config']['selectfolders'] = true;
        $element['filters'] = array(
            'artefacttype'    => array('folder'),
        );
        return $element;
    }

630
631
632
633
634
635
636
637
    public static function externalurl(&$instance, $default=null, $class=null) {
        $element['title'] = get_string('externalgalleryurl', 'blocktype.file/gallery');
        $element['name'] = 'external';
        $element['type'] = 'textarea';
        if ($class) {
            $element['class'] = $class;
        }
        $element['rows'] = 5;
638
        $element['cols'] = 76;
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
        $element['defaultvalue'] = $default;
        $element['description'] = '<tr id="externalgalleryhelp" class="'.($class ? $class : '').'"><td colspan="2" class="description">'.
                                  get_string('externalgalleryurldesc', 'blocktype.file/gallery') . self::get_supported_external_galleries() .
                                  '</td></tr>';
        $element['help'] = true;
        return $element;
    }

    private static function make_gallery_url($url) {
        static $embedsources = array(
            // PicasaWeb Album (RSS) - for Roy Tanck's widget
            array(
                'match' => '#.*picasaweb.google.([a-zA-Z]{3}).*user\/([a-zA-Z0-9\_\-\=\&\.\/\:\%]+)\/albumid\/(\d+).*#',
                'url'   => 'http://picasaweb.google.$1/data/feed/base/user/$2/albumid/$3?alt=rss&kind=photo',
                'type'  => 'widget',
                'var1' => '$2',
                'var2' => '$3',
            ),
            // PicasaWeb Album (embed code)
            array(
                'match' => '#.*picasaweb.google.([a-zA-Z]{3})\/s\/c.*picasaweb.google.([a-zA-Z]{3})\/([a-zA-Z0-9\_\-\.]+)\/([a-zA-Z0-9\_\-\=\&\.\/\:\%]+).*#',
                'url'   => 'http://picasaweb.google.$2',
                'type'  => 'picasa',
                'var1' => '$3',
                'var2' => '$4',
            ),
            // PicasaWeb Album (direct link)
            array(
                'match' => '#.*picasaweb.google.([a-zA-Z]{3})\/([a-zA-Z0-9\_\-\.]+)\/([a-zA-Z0-9\_\-\=\&\.\/\:\%]+).*#',
                'url'   => 'http://picasaweb.google.$1',
                'type'  => 'picasa',
                'var1' => '$2',
                'var2' => '$3',
            ),
            // Flickr Set (RSS) - for Roy Tanck's widget
            array(
                'match' => '#.*api.flickr.com.*set=(\d+).*nsid=([a-zA-Z0-9\@]+).*#',
                'url'   => 'http://api.flickr.com/services/feeds/photoset.gne?set=$1&nsid=$2',
                'type'  => 'widget',
                'var1' => '$2',
                'var2' => '$1',
            ),
            // Flickr Set (direct link)
            array(
                'match' => '#.*www.flickr.com/photos/([a-zA-Z0-9\_\-\.\@]+).*/sets/([0-9]+).*#',
                'url'   => 'http://www.flickr.com/photos/$1/sets/$2/',
                'type'  => 'flickr',
                'var1' => '$1',
                'var2' => '$2',
            ),
            // Panoramio User Photos (direct link)
            array(
                'match' => '#.*www.panoramio.com/user/(\d+).*#',
                'url'   => 'http://www.panoramio.com/user/$1/',
                'type'  => 'panoramio',
                'var1' => '$1',
                'var2' => null,
            ),
            // Photobucket User Photos (direct link)
            array(
                'match' => '#.*([a-zA-Z0-9]+).photobucket.com/albums/([a-zA-Z0-9]+)/([a-zA-Z0-9\.\,\:\;\@\-\_\+\ ]+).*#',
                'url'   => 'http://$1.photobucket.com/albums/$2/$3',
                'type'  => 'photobucket',
                'var1' => '$3',
                'var2' => null,
            ),
            // Photobucket User Album Photos (direct link)
            array(
                'match' => '#.*([a-zA-Z0-9]+).photobucket.com/albums/([a-zA-Z0-9]+)/([a-zA-Z0-9\.\,\:\;\@\-\_\+\ ]+)/([a-zA-Z0-9\.\,\:\;\@\-\_\+\ ]*).*#',
                'url'   => 'http://$1.photobucket.com/albums/$2/$3/$4',
                'type'  => 'photobucket',
                'var1' => '$3',
                'var2' => '$4',
            ),
            // Windows Live Photo Gallery (MUST be a direct link to one of the photos in the album!)
            // This is a hack - in order to show photos from the album, we require a direct link to one of the photos.
            array(
                'match' => '#.*cid-([a-zA-Z0-9]+).photos.live.com/self.aspx/([a-zA-Z0-9\.\,\:\;\@\-\_\+\%\ ]+)/([a-zA-Z0-9\,\:\;\@\-\_\+\%\ ]+).(gif|png|jpg|jpeg)*#',
                'url'   => 'http://cid-$1.photos.live.com/self.aspx/$2/$3.$4',
                'type'  => 'windowslive',
                'var1' => 'cid-$1',
                'var2' => '$2',
            ),
        );

        foreach ($embedsources as $source) {
            $url = htmlspecialchars_decode($url); // convert &amp; back to &, etc.
            if (preg_match($source['match'], $url)) {
                $images_url = preg_replace($source['match'], $source['url'], $url);
                $images_type = $source['type'];
                $images_var1 = preg_replace($source['match'], $source['var1'], $url);
                $images_var2 = preg_replace($source['match'], $source['var2'], $url);
                return array('url' => $images_url, 'type' => $images_type, 'var1' => $images_var1, 'var2' => $images_var2);
            }
        }
        return array();
    }

    /**
     * Returns a block of HTML that the Gallery block can use to list
     * which external galleries or photo services are supported.
     */
    private static function get_supported_external_galleries() {
        $smarty = smarty_core();
        $smarty->assign('wwwroot', get_config('wwwroot'));
        if (stripos(get_config('wwwroot'), 'https') === 0) {
            $smarty->assign('protocol', 'https');
        }
        else {
            $smarty->assign('protocol', 'http');
        }
        return $smarty->fetch('blocktype:gallery:supported.tpl');
    }

    // Function to find nearest value (in array of values) to given value
    // e.g.: user defined thumbnail width is 75, abvaliable picasa thumbnails are array(32, 48, 64, 72, 104, 144, 150, 160)
    //         so this function should return 72 (which is nearest form available values)
    // Function found at http://www.sitepoint.com/forums/showthread.php?t=537541
    public static function find_nearest($values, $item) {
        if (in_array($item,$values)) {
            $out = $item;
        }
        else {
            sort($values);
            $length = count($values);
            for ($i=0; $i<$length; $i++) {
                if ($values[$i] > $item) {
                    if ($i == 0) {
                        return $values[$i];
                    }
                    $out = ($item - $values[$i-1]) > ($values[$i]-$item) ? $values[$i] : $values[$i-1];
                    break;
                }
            }
        }
        if (!isset($out)) {
            $out = end($values);
        }
        return $out;
    }

Evan Goldenberg's avatar
Evan Goldenberg committed
780
781
782
783
784
785
786
    public static function artefactchooser_element($default=null) {
    }

    public static function default_copy_type() {
        return 'full';
    }
}