Dotclear

source: themes/ductile/_public.php @ 2716:b4abeeea33f2

Revision 2716:b4abeeea33f2, 17.5 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Use default Ductile logo if themes prefs not yet saved

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

Sites map