1<?php
2
3namespace dokuwiki\plugin\gallery\classes;
4
5class Options
6{
7    public const SORT_FILE = 'file';
8    public const SORT_CTIME = 'date';
9    public const SORT_MTIME = 'mod';
10    public const SORT_TITLE = 'title';
11    public const SORT_RANDOM = 'random';
12
13    public const ALIGN_FULL = 0;
14    public const ALIGN_LEFT = 1;
15    public const ALIGN_RIGHT = 2;
16    public const ALIGN_CENTER = 3;
17
18    // defaults
19    public $galleryID = '';
20    public $thumbnailWidth = 120;
21    public $thumbnailHeight = 120;
22    public $lightboxWidth = 1600;
23    public $lightboxHeight = 1200;
24    public $columns = 0;
25    public $filter = '';
26    public $lightbox = false;
27    public $direct = false;
28    public $showcaption = false;
29    public $showname = false;
30    public $showtitle = false;
31    public $reverse = false;
32    public $cache = false;
33    public $crop = false;
34    public $recursive = false;
35    public $sort = self::SORT_FILE;
36    public $limit = 0;
37    public $offset = 0;
38    public $paginate = 0;
39    public $align = self::ALIGN_FULL;
40
41    /**
42     * Options constructor.
43     */
44    public function __construct()
45    {
46        // load options from config
47        $plugin = plugin_load('syntax', 'gallery_main');
48        $this->thumbnailWidth = $plugin->getConf('thumbnail_width');
49        $this->thumbnailHeight = $plugin->getConf('thumbnail_height');
50        $this->lightboxWidth = $plugin->getConf('image_width');
51        $this->lightboxHeight = $plugin->getConf('image_height');
52        $this->columns = $plugin->getConf('cols');
53        $this->sort = $plugin->getConf('sort');
54        $this->parseParameters($plugin->getConf('options'));
55    }
56
57    /**
58     * Simple option strings parser
59     *
60     * @param string $params
61     * @return void
62     */
63    public function parseParameters($params)
64    {
65        $params = preg_replace('/[,&?]+/', ' ', $params);
66        $params = explode(' ', $params);
67        foreach ($params as $param) {
68            if ($param === '') continue;
69            if ($param == 'titlesort') {
70                $this->sort = self::SORT_TITLE;
71            } elseif ($param == 'datesort') {
72                $this->sort = self::SORT_CTIME;
73            } elseif ($param == 'modsort') {
74                $this->sort = self::SORT_MTIME;
75            } elseif ($param == 'random') {
76                $this->sort = self::SORT_RANDOM;
77            } elseif ($param == 'left') {
78                $this->align = self::ALIGN_LEFT;
79            } elseif ($param == 'right') {
80                $this->align = self::ALIGN_RIGHT;
81            } elseif ($param == 'center') {
82                $this->align = self::ALIGN_CENTER;
83            } elseif ($param == 'full') {
84                $this->align = self::ALIGN_FULL;
85            } elseif (preg_match('/^=(\d+)$/', $param, $match)) {
86                $this->limit = (int)$match[1];
87            } elseif (preg_match('/^\+(\d+)$/', $param, $match)) {
88                $this->offset = (int)$match[1];
89            } elseif (is_numeric($param)) {
90                $this->columns = (int)$param;
91            } elseif (preg_match('/^~(\d+)$/', $param, $match)) {
92                $this->paginate = (int)$match[1];
93            } elseif (preg_match('/^(\d+)([xX])(\d+)$/', $param, $match)) {
94                if ($match[2] == 'X') {
95                    $this->lightboxWidth = (int)$match[1];
96                    $this->lightboxHeight = (int)$match[3];
97                } else {
98                    $this->thumbnailWidth = (int)$match[1];
99                    $this->thumbnailHeight = (int)$match[3];
100                }
101            } elseif (strpos($param, '*') !== false) {
102                $param = preg_quote($param, '/');
103                $param = '/^' . str_replace('\\*', '.*?', $param) . '$/';
104                $this->filter = $param;
105            } else {
106                if (substr($param, 0, 2) == 'no') {
107                    $opt = substr($param, 2);
108                    $set = false;
109                } else {
110                    $opt = $param;
111                    $set = true;
112                }
113                if (!property_exists($this, $opt) || !is_bool($this->$opt)) continue;
114                $this->$opt = $set;
115            }
116        }
117    }
118}
119