xref: /plugin/combo/ComboStrap/Site.php (revision 1fa8c418ed5809db58049141be41b7738471dd32)
1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15
16class Site
17{
18
19    const CONF_SITE_ISO_COUNTRY = "siteIsoCountry";
20    const STRAP_TEMPLATE_NAME = "strap";
21
22    const SVG_LOGO_IDS = array(
23        ':wiki:logo.svg',
24        ':logo.svg'
25    );
26
27    const PNG_LOGO_IDS = array(
28        ':logo.png',
29        ':wiki:logo.png',
30        ':favicon-32×32.png',
31        ':favicon-16×16.png',
32        ':apple-touch-icon.png',
33        ':android-chrome-192x192.png'
34    );
35
36
37    /**
38     * @return string|null the html img tag or null
39     */
40    public static function getLogoImgHtmlTag($tagAttributes = null): ?string
41    {
42        $logoIds = self::getLogoIds();
43        foreach ($logoIds as $logoId) {
44            if ($logoId->exists()) {
45                $mediaLink = MediaLink::createMediaLinkFromNonQualifiedPath($logoId->getAbsolutePath(), null, $tagAttributes);
46                $mediaLink->setLazyLoad(false);
47                return $mediaLink->renderMediaTag();
48            }
49        }
50        return null;
51    }
52
53    /**
54     * @return Image[]
55     */
56    private static function getLogoIds(): array
57    {
58        $logosPaths = PluginUtility::mergeAttributes(self::PNG_LOGO_IDS,self::SVG_LOGO_IDS);
59        $logos = [];
60        foreach ($logosPaths as $logoPath){
61            $logos[]=Image::createImageFromAbsolutePath($logoPath);
62        }
63        return $logos;
64    }
65
66
67    /**
68     * @return string|null
69     */
70    public static function getLogoUrlAsSvg()
71    {
72
73
74        $url = null;
75        foreach (self::SVG_LOGO_IDS as $svgLogo) {
76
77            $svgLogoFN = mediaFN($svgLogo);
78
79            if (file_exists($svgLogoFN)) {
80                $url = ml($svgLogo, '', true, '', true);
81                break;
82            };
83        }
84        return $url;
85    }
86
87    public static function getLogoUrlAsPng()
88    {
89
90        $url = null;
91        foreach (self::PNG_LOGO_IDS as $svgLogo) {
92
93            $svgLogoFN = mediaFN($svgLogo);
94
95            if (file_exists($svgLogoFN)) {
96                $url = ml($svgLogo, '', true, '', true);
97                break;
98            };
99        }
100        return $url;
101    }
102
103    /**
104     * https://www.dokuwiki.org/config:title
105     * @return mixed
106     */
107    public static function getTitle()
108    {
109        global $conf;
110        return $conf['title'];
111    }
112
113    /**
114     * @param string $sep - the separator - generally ("-") but not always
115     * @return string
116     *
117     * Locale always canonicalizes to upper case.
118     */
119    public static function getLocale($sep = "-")
120    {
121
122        $locale = null;
123
124        $lang = self::getLang();
125        if ($lang != null) {
126            $country = self::getCountry();
127            if ($country != null) {
128                $locale = strtolower($lang) . $sep . strtoupper($country);
129            }
130        }
131
132        return $locale;
133    }
134
135    /**
136     *
137     * ISO 3166 alpha-2 country code
138     *
139     */
140    public static function getCountry()
141    {
142        $country = PluginUtility::getConfValue(self::CONF_SITE_ISO_COUNTRY);
143        if (!empty($country)) {
144            if (!StringUtility::match($country, "[a-zA-Z]{2}")) {
145                LogUtility::msg("The country configuration value ($country) does not have two letters (ISO 3166 alpha-2 country code)", LogUtility::LVL_MSG_ERROR, "country");
146            }
147            return $country;
148        } else {
149            return null;
150        }
151
152    }
153
154    /**
155     * @return mixed|null
156     * Wrapper around  https://www.dokuwiki.org/config:lang
157     */
158    private static function getLang()
159    {
160
161        global $conf;
162        $locale = $conf['lang'];
163        return ($locale ? $locale : null);
164    }
165
166    public static function getUrl()
167    {
168
169        /**
170         * In a {@link PluginUtility::isDevOrTest()} dev environment,
171         * don't set the
172         * https://www.dokuwiki.org/config:baseurl
173         * to be able to test the metadata / social integration
174         * via a tunnel
175         */
176
177        return DOKU_URL;
178
179    }
180
181    public static function getTag()
182    {
183        global $conf;
184        $tag = $conf['tag'];
185        return ($tag ? $tag : null);
186    }
187
188    /**
189     * @return string - the name of the sidebar page
190     */
191    public static function getSidebarName()
192    {
193        global $conf;
194        return $conf["sidebar"];
195    }
196
197    public static function setTemplate($template)
198    {
199        global $conf;
200        $conf['template'] = $template;
201    }
202
203    public static function setRenderingCacheOn()
204    {
205        // ensure the value is not -1, which disables caching
206        // https://www.dokuwiki.org/config:cachetime
207        global $conf;
208        $conf['cachetime'] = 60 * 60;
209    }
210
211    public static function debugIsOn()
212    {
213        global $conf;
214        return $conf['allowdebug'];
215    }
216
217    public static function setTemplateToStrap()
218    {
219        global $conf;
220        $conf['template'] = 'strap';
221    }
222
223    public static function setTemplateToDefault()
224    {
225        global $conf;
226        $conf['template'] = 'dokuwiki';
227    }
228
229    public static function setCacheDefault()
230    {
231        // The value is -1, which disables caching
232        // https://www.dokuwiki.org/config:cachetime
233        global $conf;
234        $conf['cachetime'] = -1;
235    }
236
237    public static function useHeadingAsTitle()
238    {
239        // https://www.dokuwiki.org/config:useheading
240        global $conf;
241        $conf['useheading'] = 1;
242    }
243
244    public static function useHeadingDefault()
245    {
246        // https://www.dokuwiki.org/config:useheading
247        global $conf;
248        $conf['useheading'] = 0;
249    }
250
251    public static function getTemplate()
252    {
253        global $conf;
254        return $conf['template'];
255
256    }
257
258    public static function isStrapTemplate()
259    {
260        global $conf;
261        return $conf['template'] == self::STRAP_TEMPLATE_NAME;
262    }
263
264    public static function getAjaxUrl()
265    {
266        return self::getUrl() . "lib/exe/ajax.php";
267    }
268
269    public static function getPageDirectory()
270    {
271        global $conf;
272        return $conf['datadir'];
273    }
274
275    public static function disableHeadingSectionEditing()
276    {
277        global $conf;
278        $conf['maxseclevel'] = 0;
279    }
280
281    public static function setBreadCrumbOn()
282    {
283        global $conf;
284        $conf['youarehere'] = 1;
285    }
286
287
288}
289