Dotclear

source: themes/ductile/_public.php @ 3874:ab8368569446

Revision 3874:ab8368569446, 21.8 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

short notation for array (array() → [])

Line 
1<?php
2/**
3 * @brief Ductile, a theme for Dotclear 2
4 *
5 * @package Dotclear
6 * @subpackage Themes
7 *
8 * @copyright Olivier Meunier & Association Dotclear
9 * @copyright GPL-2.0-only
10 */
11
12namespace themes\ductile;
13
14if (!defined('DC_RC_PATH')) {return;}
15
16\l10n::set(dirname(__FILE__) . '/locales/' . $_lang . '/main');
17
18# Behaviors
19$core->addBehavior('publicHeadContent', [__NAMESPACE__ . '\tplDuctileTheme', 'publicHeadContent']);
20$core->addBehavior('publicInsideFooter', [__NAMESPACE__ . '\tplDuctileTheme', 'publicInsideFooter']);
21
22# Templates
23$core->tpl->addValue('ductileEntriesList', [__NAMESPACE__ . '\tplDuctileTheme', 'ductileEntriesList']);
24$core->tpl->addBlock('EntryIfContentIsCut', [__NAMESPACE__ . '\tplDuctileTheme', 'EntryIfContentIsCut']);
25$core->tpl->addValue('ductileNbEntryPerPage', [__NAMESPACE__ . '\tplDuctileTheme', 'ductileNbEntryPerPage']);
26$core->tpl->addValue('ductileLogoSrc', [__NAMESPACE__ . '\tplDuctileTheme', 'ductileLogoSrc']);
27$core->tpl->addBlock('IfPreviewIsNotMandatory', [__NAMESPACE__ . '\tplDuctileTheme', 'IfPreviewIsNotMandatory']);
28
29class tplDuctileTheme
30{
31    public static function ductileNbEntryPerPage($attr)
32    {
33        return '<?php ' . __NAMESPACE__ . '\tplDuctileTheme::ductileNbEntryPerPageHelper(); ?>';
34    }
35
36    public static function ductileNbEntryPerPageHelper()
37    {
38        global $_ctx;
39
40        $nb_other = $nb_first = 0;
41
42        $s = $GLOBALS['core']->blog->settings->themes->get($GLOBALS['core']->blog->settings->system->theme . '_entries_counts');
43        if ($s !== null) {
44            $s = @unserialize($s);
45            if (is_array($s)) {
46                switch ($GLOBALS['core']->url->type) {
47                    case 'default':
48                    case 'default-page':
49                        if (isset($s['default'])) {
50                            $nb_first = $nb_other = (integer) $s['default'];
51                        }
52                        if (isset($s['default-page'])) {
53                            $nb_other = (integer) $s['default-page'];
54                        }
55                        break;
56                    default:
57                        if (isset($s[$GLOBALS['core']->url->type])) {
58                            // Nb de billets par page défini par la config du thème
59                            $nb_first = $nb_other = (integer) $s[$GLOBALS['core']->url->type];
60                        }
61                        break;
62                }
63            }
64        }
65
66        if ($nb_other == 0) {
67            if (!empty($attr['nb'])) {
68                // Nb de billets par page défini par défaut dans le template
69                $nb_other = $nb_first = (integer) $attr['nb'];
70            }
71        }
72
73        if ($nb_other > 0) {
74            $_ctx->nb_entry_per_page = $nb_other;
75        }
76        if ($nb_first > 0) {
77            $_ctx->nb_entry_first_page = $nb_first;
78        }
79    }
80
81    public static function EntryIfContentIsCut($attr, $content)
82    {
83        global $core;
84
85        if (empty($attr['cut_string']) || !empty($attr['full'])) {
86            return '';
87        }
88
89        $urls = '0';
90        if (!empty($attr['absolute_urls'])) {
91            $urls = '1';
92        }
93
94        $short              = $core->tpl->getFilters($attr);
95        $cut                = $attr['cut_string'];
96        $attr['cut_string'] = 0;
97        $full               = $core->tpl->getFilters($attr);
98        $attr['cut_string'] = $cut;
99
100        return '<?php if (strlen(' . sprintf($full, '$_ctx->posts->getContent(' . $urls . ')') . ') > ' .
101        'strlen(' . sprintf($short, '$_ctx->posts->getContent(' . $urls . ')') . ')) : ?>' .
102            $content .
103            '<?php endif; ?>';
104    }
105
106    public static function ductileEntriesList($attr)
107    {
108        global $core;
109
110        $tpl_path   = dirname(__FILE__) . '/tpl/';
111        $list_types = ['title', 'short', 'full'];
112
113        // Get all _entry-*.html in tpl folder of theme
114        $list_types_templates = \files::scandir($tpl_path);
115        if (is_array($list_types_templates)) {
116            foreach ($list_types_templates as $v) {
117                if (preg_match('/^_entry\-(.*)\.html$/', $v, $m)) {
118                    if (isset($m[1])) {
119                        if (!in_array($m[1], $list_types)) {
120                            // template not already in full list
121                            $list_types[] = $m[1];
122                        }
123                    }
124                }
125            }
126        }
127
128        $default = isset($attr['default']) ? trim($attr['default']) : 'short';
129        $ret     = '<?php ' . "\n" .
130        'switch (' . __NAMESPACE__ . '\tplDuctileTheme::ductileEntriesListHelper(\'' . $default . '\')) {' . "\n";
131
132        foreach ($list_types as $v) {
133            $ret .= '   case \'' . $v . '\':' . "\n" .
134            '?>' . "\n" .
135            $core->tpl->includeFile(['src' => '_entry-' . $v . '.html']) . "\n" .
136                '<?php ' . "\n" .
137                '       break;' . "\n";
138        }
139
140        $ret .= '}' . "\n" .
141            '?>';
142
143        return $ret;
144    }
145
146    public static function ductileEntriesListHelper($default)
147    {
148        $s = $GLOBALS['core']->blog->settings->themes->get($GLOBALS['core']->blog->settings->system->theme . '_entries_lists');
149        if ($s !== null) {
150            $s = @unserialize($s);
151            if (is_array($s)) {
152                if (isset($s[$GLOBALS['core']->url->type])) {
153                    $model = $s[$GLOBALS['core']->url->type];
154                    return $model;
155                }
156            }
157        }
158        return $default;
159    }
160
161    public static function ductileLogoSrc($attr)
162    {
163        return '<?php echo ' . __NAMESPACE__ . '\tplDuctileTheme::ductileLogoSrcHelper(); ?>';
164    }
165
166    public static function ductileLogoSrcHelper()
167    {
168        $img_url = $GLOBALS['core']->blog->settings->system->themes_url . '/' . $GLOBALS['core']->blog->settings->system->theme . '/img/logo.png';
169
170        $s = $GLOBALS['core']->blog->settings->themes->get($GLOBALS['core']->blog->settings->system->theme . '_style');
171        if ($s === null) {
172            // no settings yet, return default logo
173            return $img_url;
174        }
175        $s = @unserialize($s);
176        if (!is_array($s)) {
177            // settings error, return default logo
178            return $img_url;
179        }
180
181        if (isset($s['logo_src'])) {
182            if ($s['logo_src'] !== null) {
183                if ($s['logo_src'] != '') {
184                    if ((substr($s['logo_src'], 0, 1) == '/') || (parse_url($s['logo_src'], PHP_URL_SCHEME) != '')) {
185                        // absolute URL
186                        $img_url = $s['logo_src'];
187                    } else {
188                        // relative URL (base = img folder of ductile theme)
189                        $img_url = $GLOBALS['core']->blog->settings->system->themes_url . '/' . $GLOBALS['core']->blog->settings->system->theme . '/img/' . $s['logo_src'];
190                    }
191                }
192            }
193        }
194
195        return $img_url;
196    }
197
198    public static function IfPreviewIsNotMandatory($attr, $content)
199    {
200        $s = $GLOBALS['core']->blog->settings->themes->get($GLOBALS['core']->blog->settings->system->theme . '_style');
201        if ($s !== null) {
202            $s = @unserialize($s);
203            if (is_array($s)) {
204                if (isset($s['preview_not_mandatory'])) {
205                    if ($s['preview_not_mandatory']) {
206                        return $content;
207                    }
208                }
209            }
210        }
211        return '';
212    }
213
214    public static function publicInsideFooter($core)
215    {
216        $res     = '';
217        $default = false;
218        $img_url = $core->blog->settings->system->themes_url . '/' . $core->blog->settings->system->theme . '/img/';
219
220        $s = $core->blog->settings->themes->get($core->blog->settings->system->theme . '_stickers');
221
222        if ($s === null) {
223            $default = true;
224        } else {
225            $s = @unserialize($s);
226            if (!is_array($s)) {
227                $default = true;
228            } else {
229                $s = array_filter($s, 'self::cleanStickers');
230                if (count($s) == 0) {
231                    $default = true;
232                } else {
233                    $count = 1;
234                    foreach ($s as $sticker) {
235                        $res .= self::setSticker($count, ($count == count($s)), $sticker['label'], $sticker['url'], $img_url . $sticker['image']);
236                        $count++;
237                    }
238                }
239            }
240        }
241
242        if ($default || $res == '') {
243            $res = self::setSticker(1, true, __('Subscribe'), $core->blog->url .
244                $core->url->getURLFor('feed', 'atom'), $img_url . 'sticker-feed.png');
245        }
246
247        if ($res != '') {
248            $res = '<ul id="stickers">' . "\n" . $res . '</ul>' . "\n";
249            echo $res;
250        }
251    }
252
253    protected static function cleanStickers($s)
254    {
255        if (is_array($s)) {
256            if (isset($s['label']) && isset($s['url']) && isset($s['image'])) {
257                if ($s['label'] != null && $s['url'] != null && $s['image'] != null) {
258                    return true;
259                }
260            }
261        }
262        return false;
263    }
264
265    protected static function setSticker($position, $last, $label, $url, $image)
266    {
267        return '<li id="sticker' . $position . '"' . ($last ? ' class="last"' : '') . '>' . "\n" .
268            '<a href="' . $url . '">' . "\n" .
269            '<img alt="" src="' . $image . '" />' . "\n" .
270            '<span>' . $label . '</span>' . "\n" .
271            '</a>' . "\n" .
272            '</li>' . "\n";
273    }
274
275    public static function publicHeadContent($core)
276    {
277        echo
278        '<style type="text/css">' . "\n" .
279        '/* ' . __('Additionnal style directives') . ' */' . "\n" .
280        self::ductileStyleHelper() .
281            "</style>\n";
282
283        echo
284        '<script type="text/javascript" src="' .
285        $core->blog->settings->system->themes_url . '/' . $core->blog->settings->system->theme .
286            '/ductile.js"></script>' . "\n";
287
288        echo self::ductileWebfontHelper();
289    }
290
291    public static function ductileWebfontHelper()
292    {
293        $s = $GLOBALS['core']->blog->settings->themes->get($GLOBALS['core']->blog->settings->system->theme . '_style');
294
295        if ($s === null) {
296            return;
297        }
298
299        $s = @unserialize($s);
300        if (!is_array($s)) {
301            return;
302        }
303
304        $ret = '';
305        $css = [];
306        $uri = [];
307        if (!isset($s['body_font']) || ($s['body_font'] == '')) {
308            // See if webfont defined for main font
309            if (isset($s['body_webfont_api']) && isset($s['body_webfont_family']) && isset($s['body_webfont_url'])) {
310                $uri[] = $s['body_webfont_url'];
311                switch ($s['body_webfont_api']) {
312                    case 'js':
313                        $ret .= sprintf('<script type="text/javascript" src="%s"></script>', $s['body_webfont_url']) . "\n";
314                        break;
315                    case 'css':
316                        $ret .= sprintf('<link type="text/css" href="%s" rel="stylesheet" />', $s['body_webfont_url']) . "\n";
317                        break;
318                }
319                # Main font
320                $selectors = 'body, .supranav li a span, #comments.me, a.comment-number';
321                \dcThemeConfig::prop($css, $selectors, 'font-family', $s['body_webfont_family']);
322            }
323        }
324        if (!isset($s['alternate_font']) || ($s['alternate_font'] == '')) {
325            // See if webfont defined for secondary font
326            if (isset($s['alternate_webfont_api']) && isset($s['alternate_webfont_family']) && isset($s['alternate_webfont_url'])) {
327                if (!in_array($s['alternate_webfont_url'], $uri)) {
328                    switch ($s['alternate_webfont_api']) {
329                        case 'js':
330                            $ret .= sprintf('<script type="text/javascript" src="%s"></script>', $s['alternate_webfont_url']) . "\n";
331                            break;
332                        case 'css':
333                            $ret .= sprintf('<link type="text/css" href="%s" rel="stylesheet" />', $s['alternate_webfont_url']) . "\n";
334                            break;
335                    }
336                }
337                # Secondary font
338                $selectors = '#blogdesc, .supranav, #content-info, #subcategories, #comments-feed, #sidebar h2, #sidebar h3, #footer';
339                \dcThemeConfig::prop($css, $selectors, 'font-family', $s['alternate_webfont_family']);
340            }
341        }
342        # Style directives
343        $res = '';
344        foreach ($css as $selector => $values) {
345            $res .= $selector . " {\n";
346            foreach ($values as $k => $v) {
347                $res .= $k . ':' . $v . ";\n";
348            }
349            $res .= "}\n";
350        }
351        if ($res != '') {
352            $ret .= '<style type="text/css">' . "\n" . $res . '</style>' . "\n";
353        }
354
355        return $ret;
356    }
357
358    public static function ductileStyleHelper()
359    {
360        $s = $GLOBALS['core']->blog->settings->themes->get($GLOBALS['core']->blog->settings->system->theme . '_style');
361
362        if ($s === null) {
363            return;
364        }
365
366        $s = @unserialize($s);
367        if (!is_array($s)) {
368            return;
369        }
370
371        $css = [];
372
373        # Properties
374
375        # Blog description
376        $selectors = '#blogdesc';
377        if (isset($s['subtitle_hidden'])) {
378            \dcThemeConfig::prop($css, $selectors, 'display', ($s['subtitle_hidden'] ? 'none' : null));
379        }
380
381        # Main font
382        $selectors = 'body, .supranav li a span, #comments.me, a.comment-number';
383        if (isset($s['body_font'])) {
384            \dcThemeConfig::prop($css, $selectors, 'font-family', self::fontDef($s['body_font']));
385        }
386
387        # Secondary font
388        $selectors = '#blogdesc, .supranav, #content-info, #subcategories, #comments-feed, #sidebar h2, #sidebar h3, #footer';
389        if (isset($s['alternate_font'])) {
390            \dcThemeConfig::prop($css, $selectors, 'font-family', self::fontDef($s['alternate_font']));
391        }
392
393        # Inside posts links font weight
394        $selectors = '.post-excerpt a, .post-content a';
395        if (isset($s['post_link_w'])) {
396            \dcThemeConfig::prop($css, $selectors, 'font-weight', ($s['post_link_w'] ? 'bold' : 'normal'));
397        }
398
399        # Inside posts links colors (normal, visited)
400        $selectors = '.post-excerpt a:link, .post-excerpt a:visited, .post-content a:link, .post-content a:visited';
401        if (isset($s['post_link_v_c'])) {
402            \dcThemeConfig::prop($css, $selectors, 'color', $s['post_link_v_c']);
403        }
404
405        # Inside posts links colors (hover, active, focus)
406        $selectors = '.post-excerpt a:hover, .post-excerpt a:active, .post-excerpt a:focus, .post-content a:hover, .post-content a:active, .post-content a:focus';
407        if (isset($s['post_link_f_c'])) {
408            \dcThemeConfig::prop($css, $selectors, 'color', $s['post_link_f_c']);
409        }
410
411        # Style directives
412        $res = '';
413        foreach ($css as $selector => $values) {
414            $res .= $selector . " {\n";
415            foreach ($values as $k => $v) {
416                $res .= $k . ':' . $v . ";\n";
417            }
418            $res .= "}\n";
419        }
420
421        # Large screens
422        $css_large = [];
423
424        # Blog title font weight
425        $selectors = 'h1, h1 a:link, h1 a:visited, h1 a:hover, h1 a:visited, h1 a:focus';
426        if (isset($s['blog_title_w'])) {
427            \dcThemeConfig::prop($css_large, $selectors, 'font-weight', ($s['blog_title_w'] ? 'bold' : 'normal'));
428        }
429
430        # Blog title font size
431        $selectors = 'h1';
432        if (isset($s['blog_title_s'])) {
433            \dcThemeConfig::prop($css_large, $selectors, 'font-size', $s['blog_title_s']);
434        }
435
436        # Blog title color
437        $selectors = 'h1 a:link, h1 a:visited, h1 a:hover, h1 a:visited, h1 a:focus';
438        if (isset($s['blog_title_c'])) {
439            \dcThemeConfig::prop($css_large, $selectors, 'color', $s['blog_title_c']);
440        }
441
442        # Post title font weight
443        $selectors = 'h2.post-title, h2.post-title a:link, h2.post-title a:visited, h2.post-title a:hover, h2.post-title a:visited, h2.post-title a:focus';
444        if (isset($s['post_title_w'])) {
445            \dcThemeConfig::prop($css_large, $selectors, 'font-weight', ($s['post_title_w'] ? 'bold' : 'normal'));
446        }
447
448        # Post title font size
449        $selectors = 'h2.post-title';
450        if (isset($s['post_title_s'])) {
451            \dcThemeConfig::prop($css_large, $selectors, 'font-size', $s['post_title_s']);
452        }
453
454        # Post title color
455        $selectors = 'h2.post-title a:link, h2.post-title a:visited, h2.post-title a:hover, h2.post-title a:visited, h2.post-title a:focus';
456        if (isset($s['post_title_c'])) {
457            \dcThemeConfig::prop($css_large, $selectors, 'color', $s['post_title_c']);
458        }
459
460        # Simple title color (title without link)
461        $selectors = '#content-info h2, .post-title, .post h3, .post h4, .post h5, .post h6, .arch-block h3';
462        if (isset($s['post_simple_title_c'])) {
463            \dcThemeConfig::prop($css_large, $selectors, 'color', $s['post_simple_title_c']);
464        }
465
466        # Style directives for large screens
467        if (count($css_large)) {
468            $res .= '@media only screen and (min-width: 481px) {' . "\n";
469            foreach ($css_large as $selector => $values) {
470                $res .= $selector . " {\n";
471                foreach ($values as $k => $v) {
472                    $res .= $k . ':' . $v . ";\n";
473                }
474                $res .= "}\n";
475            }
476            $res .= "}\n";
477        }
478
479        # Small screens
480        $css_small = [];
481
482        # Blog title font weight
483        $selectors = 'h1, h1 a:link, h1 a:visited, h1 a:hover, h1 a:visited, h1 a:focus';
484        if (isset($s['blog_title_w_m'])) {
485            \dcThemeConfig::prop($css_small, $selectors, 'font-weight', ($s['blog_title_w_m'] ? 'bold' : 'normal'));
486        }
487
488        # Blog title font size
489        $selectors = 'h1';
490        if (isset($s['blog_title_s_m'])) {
491            \dcThemeConfig::prop($css_small, $selectors, 'font-size', $s['blog_title_s_m']);
492        }
493
494        # Blog title color
495        $selectors = 'h1 a:link, h1 a:visited, h1 a:hover, h1 a:visited, h1 a:focus';
496        if (isset($s['blog_title_c_m'])) {
497            \dcThemeConfig::prop($css_small, $selectors, 'color', $s['blog_title_c_m']);
498        }
499
500        # Post title font weight
501        $selectors = 'h2.post-title, h2.post-title a:link, h2.post-title a:visited, h2.post-title a:hover, h2.post-title a:visited, h2.post-title a:focus';
502        if (isset($s['post_title_w_m'])) {
503            \dcThemeConfig::prop($css_small, $selectors, 'font-weight', ($s['post_title_w_m'] ? 'bold' : 'normal'));
504        }
505
506        # Post title font size
507        $selectors = 'h2.post-title';
508        if (isset($s['post_title_s_m'])) {
509            \dcThemeConfig::prop($css_small, $selectors, 'font-size', $s['post_title_s_m']);
510        }
511
512        # Post title color
513        $selectors = 'h2.post-title a:link, h2.post-title a:visited, h2.post-title a:hover, h2.post-title a:visited, h2.post-title a:focus';
514        if (isset($s['post_title_c_m'])) {
515            \dcThemeConfig::prop($css_small, $selectors, 'color', $s['post_title_c_m']);
516        }
517
518        # Style directives for small screens
519        if (count($css_small)) {
520            $res .= '@media only screen and (max-width: 480px) {' . "\n";
521            foreach ($css_small as $selector => $values) {
522                $res .= $selector . " {\n";
523                foreach ($values as $k => $v) {
524                    $res .= $k . ':' . $v . ";\n";
525                }
526                $res .= "}\n";
527            }
528            $res .= "}\n";
529        }
530
531        return $res;
532    }
533
534    protected static $fonts = [
535        // Theme standard
536        'Ductile body'      => '"Century Schoolbook", "Century Schoolbook L", Georgia, serif',
537        'Ductile alternate' => '"Franklin gothic medium", "arial narrow", "DejaVu Sans Condensed", "helvetica neue", helvetica, sans-serif',
538
539        // Serif families
540        'Times New Roman'   => 'Cambria, "Hoefler Text", Utopia, "Liberation Serif", "Nimbus Roman No9 L Regular", Times, "Times New Roman", serif',
541        'Georgia'           => 'Constantia, "Lucida Bright", Lucidabright, "Lucida Serif", Lucida, "DejaVu Serif", "Bitstream Vera Serif", "Liberation Serif", Georgia, serif',
542        'Garamond'          => '"Palatino Linotype", Palatino, Palladio, "URW Palladio L", "Book Antiqua", Baskerville, "Bookman Old Style", "Bitstream Charter", "Nimbus Roman No9 L", Garamond, "Apple Garamond", "ITC Garamond Narrow", "New Century Schoolbook", "Century Schoolbook", "Century Schoolbook L", Georgia, serif',
543
544        // Sans-serif families
545        'Helvetica/Arial'   => 'Frutiger, "Frutiger Linotype", Univers, Calibri, "Gill Sans", "Gill Sans MT", "Myriad Pro", Myriad, "DejaVu Sans Condensed", "Liberation Sans", "Nimbus Sans L", Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif',
546        'Verdana'           => 'Corbel, "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", "Bitstream Vera Sans", "Liberation Sans", Verdana, "Verdana Ref", sans-serif',
547        'Trebuchet MS'      => '"Segoe UI", Candara, "Bitstream Vera Sans", "DejaVu Sans", "Bitstream Vera Sans", "Trebuchet MS", Verdana, "Verdana Ref", sans-serif',
548
549        // Cursive families
550        'Impact'            => 'Impact, Haettenschweiler, "Franklin Gothic Bold", Charcoal, "Helvetica Inserat", "Bitstream Vera Sans Bold", "Arial Black", sans-serif',
551
552        // Monospace families
553        'Monospace'         => 'Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier, monospace'
554    ];
555
556    protected static function fontDef($c)
557    {
558        return isset(self::$fonts[$c]) ? self::$fonts[$c] : null;
559    }
560}
Note: See TracBrowser for help on using the repository browser.

Sites map