Dotclear

source: themes/ductile/_public.php @ 2658:0686a9a71a3f

Revision 2658:0686a9a71a3f, 17.4 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

New utility lib for theme configuration, applied to ductile theme, addresses #904

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          $s = $GLOBALS['core']->blog->settings->themes->get($GLOBALS['core']->blog->settings->system->theme.'_style');
167          if ($s === null) {
168               return;
169          }
170          $s = @unserialize($s);
171          if (!is_array($s)) {
172               return;
173          }
174
175          $img_url = $GLOBALS['core']->blog->settings->system->themes_url.'/'.$GLOBALS['core']->blog->settings->system->theme.'/img/logo.png';
176          if (isset($s['logo_src'])) {
177               if ($s['logo_src'] !== null) {
178                    if ($s['logo_src'] != '') {
179                         if ((substr($s['logo_src'],0,1) == '/') || (parse_url($s['logo_src'],PHP_URL_SCHEME) != '')) {
180                              // absolute URL
181                              $img_url = $s['logo_src'];
182                         } else {
183                              // relative URL (base = img folder of ductile theme)
184                              $img_url = $GLOBALS['core']->blog->settings->system->themes_url.'/'.$GLOBALS['core']->blog->settings->system->theme.'/img/'.$s['logo_src'];
185                         }
186                    }
187               }
188          }
189
190          return $img_url;
191     }
192
193     public static function IfPreviewIsNotMandatory($attr,$content)
194     {
195          $s = $GLOBALS['core']->blog->settings->themes->get($GLOBALS['core']->blog->settings->system->theme.'_style');
196          if ($s !== null) {
197               $s = @unserialize($s);
198               if (is_array($s)) {
199                    if (isset($s['preview_not_mandatory'])) {
200                         if ($s['preview_not_mandatory']) {
201                              return $content;
202                         }
203                    }
204               }
205          }
206          return '';
207     }
208
209     public static function publicInsideFooter($core)
210     {
211          $res = '';
212          $default = false;
213          $img_url = $core->blog->settings->system->themes_url.'/'.$core->blog->settings->system->theme.'/img/';
214
215          $s = $core->blog->settings->themes->get($core->blog->settings->system->theme.'_stickers');
216
217          if ($s === null) {
218               $default = true;
219          } else {
220               $s = @unserialize($s);
221               if (!is_array($s)) {
222                    $default = true;
223               } else {
224                    $s = array_filter($s,array('tplDuctileTheme', 'cleanStickers'));
225                    if (count($s) == 0) {
226                         $default = true;
227                    } else {
228                         $count = 1;
229                         foreach ($s as $sticker) {
230                              $res .= self::setSticker($count,($count == count($s)),$sticker['label'],$sticker['url'],$img_url.$sticker['image']);
231                              $count++;
232                         }
233                    }
234               }
235          }
236
237          if ($default || $res == '') {
238               $res = self::setSticker(1,true,__('Subscribe'),$core->blog->url.
239                    $core->url->getURLFor('feed','atom'),$img_url.'sticker-feed.png');
240          }
241
242          if ($res != '') {
243               $res = '<ul id="stickers">'."\n".$res.'</ul>'."\n";
244               echo $res;
245          }
246     }
247
248     protected static function cleanStickers($s)
249     {
250          if (is_array($s)) {
251               if (isset($s['label']) && isset($s['url']) && isset($s['image'])) {
252                    if ($s['label'] != null && $s['url'] != null && $s['image'] != null) {
253                         return true;
254                    }
255               }
256          }
257          return false;
258     }
259
260     protected static function setSticker($position,$last,$label,$url,$image)
261     {
262          return '<li id="sticker'.$position.'"'.($last ? ' class="last"' : '').'>'."\n".
263               '<a href="'.$url.'">'."\n".
264               '<img alt="" src="'.$image.'" />'."\n".
265               '<span>'.$label.'</span>'."\n".
266               '</a>'."\n".
267               '</li>'."\n";
268     }
269
270     public static function publicHeadContent($core)
271     {
272          echo
273               '<style type="text/css">'."\n".
274               '/* '.__('Additionnal style directives').' */'."\n".
275               self::ductileStyleHelper().
276               "</style>\n";
277
278          echo
279               '<script type="text/javascript" src="'.
280               $core->blog->settings->system->themes_url.'/'.$core->blog->settings->system->theme.
281               '/ductile.js"></script>'."\n";
282
283          echo self::ductileWebfontHelper();
284     }
285
286     public static function ductileWebfontHelper()
287     {
288          $s = $GLOBALS['core']->blog->settings->themes->get($GLOBALS['core']->blog->settings->system->theme.'_style');
289
290          if ($s === null) {
291               return;
292          }
293
294          $s = @unserialize($s);
295          if (!is_array($s)) {
296               return;
297          }
298
299          $ret = '';
300          $css = array();
301          $uri = array();
302          if (!isset($s['body_font']) || ($s['body_font'] == '')) {
303               // See if webfont defined for main font
304               if (isset($s['body_webfont_api']) && isset($s['body_webfont_family']) && isset($s['body_webfont_url'])) {
305                    $uri[] = $s['body_webfont_url'];
306                    switch ($s['body_webfont_api']) {
307                         case 'js':
308                              $ret .= sprintf('<script type="text/javascript" src="%s"></script>',$s['body_webfont_url'])."\n";
309                              break;
310                         case 'css':
311                              $ret .= sprintf('<link type="text/css" href="%s" rel="stylesheet" />',$s['body_webfont_url'])."\n";
312                              break;
313                    }
314                    # Main font
315                    $selectors = 'body, .supranav li a span, #comments.me, a.comment-number';
316                    dcThemeConfig::prop($css,$selectors,'font-family',$s['body_webfont_family']);
317               }
318          }
319          if (!isset($s['alternate_font']) || ($s['alternate_font'] == '')) {
320               // See if webfont defined for secondary font
321               if (isset($s['alternate_webfont_api']) && isset($s['alternate_webfont_family']) && isset($s['alternate_webfont_url'])) {
322                    if (!in_array($s['alternate_webfont_url'], $uri)) {
323                         switch ($s['alternate_webfont_api']) {
324                              case 'js':
325                                   $ret .= sprintf('<script type="text/javascript" src="%s"></script>',$s['alternate_webfont_url'])."\n";
326                                   break;
327                              case 'css':
328                                   $ret .= sprintf('<link type="text/css" href="%s" rel="stylesheet" />',$s['alternate_webfont_url'])."\n";
329                                   break;
330                         }
331                    }
332                    # Secondary font
333                    $selectors = '#blogdesc, .supranav, #content-info, #subcategories, #comments-feed, #sidebar h2, #sidebar h3, #footer';
334                    dcThemeConfig::prop($css,$selectors,'font-family',$s['alternate_webfont_family']);
335               }
336          }
337          # Style directives
338          $res = '';
339          foreach ($css as $selector => $values) {
340               $res .= $selector." {\n";
341               foreach ($values as $k => $v) {
342                    $res .= $k.':'.$v.";\n";
343               }
344               $res .= "}\n";
345          }
346          if ($res != '') {
347               $ret .= '<style type="text/css">'."\n".$res.'</style>'."\n";
348          }
349
350          return $ret;
351     }
352
353     public static function ductileStyleHelper()
354     {
355          $s = $GLOBALS['core']->blog->settings->themes->get($GLOBALS['core']->blog->settings->system->theme.'_style');
356
357          if ($s === null) {
358               return;
359          }
360
361          $s = @unserialize($s);
362          if (!is_array($s)) {
363               return;
364          }
365
366          $css = array();
367
368          # Properties
369
370          # Blog description
371          $selectors = '#blogdesc';
372          if (isset($s['subtitle_hidden'])) dcThemeConfig::prop($css,$selectors,'display',($s['subtitle_hidden'] ? 'none' : null));
373
374          # Main font
375          $selectors = 'body, .supranav li a span, #comments.me, a.comment-number';
376          if (isset($s['body_font'])) dcThemeConfig::prop($css,$selectors,'font-family',self::fontDef($s['body_font']));
377
378          # Secondary font
379          $selectors = '#blogdesc, .supranav, #content-info, #subcategories, #comments-feed, #sidebar h2, #sidebar h3, #footer';
380          if (isset($s['alternate_font'])) dcThemeConfig::prop($css,$selectors,'font-family',self::fontDef($s['alternate_font']));
381
382          # Inside posts links font weight
383          $selectors = '.post-excerpt a, .post-content a';
384          if (isset($s['post_link_w'])) dcThemeConfig::prop($css,$selectors,'font-weight',($s['post_link_w'] ? 'bold' : 'normal'));
385
386          # Inside posts links colors (normal, visited)
387          $selectors = '.post-excerpt a:link, .post-excerpt a:visited, .post-content a:link, .post-content a:visited';
388          if (isset($s['post_link_v_c'])) dcThemeConfig::prop($css,$selectors,'color',$s['post_link_v_c']);
389
390          # Inside posts links colors (hover, active, focus)
391          $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';
392          if (isset($s['post_link_f_c'])) dcThemeConfig::prop($css,$selectors,'color',$s['post_link_f_c']);
393
394          # Style directives
395          $res = '';
396          foreach ($css as $selector => $values) {
397               $res .= $selector." {\n";
398               foreach ($values as $k => $v) {
399                    $res .= $k.':'.$v.";\n";
400               }
401               $res .= "}\n";
402          }
403
404          # Large screens
405          $css_large = array();
406
407          # Blog title font weight
408          $selectors = 'h1, h1 a:link, h1 a:visited, h1 a:hover, h1 a:visited, h1 a:focus';
409          if (isset($s['blog_title_w'])) dcThemeConfig::prop($css_large,$selectors,'font-weight',($s['blog_title_w'] ? 'bold' : 'normal'));
410
411          # Blog title font size
412          $selectors = 'h1';
413          if (isset($s['blog_title_s'])) dcThemeConfig::prop($css_large,$selectors,'font-size',$s['blog_title_s']);
414
415          # Blog title color
416          $selectors = 'h1 a:link, h1 a:visited, h1 a:hover, h1 a:visited, h1 a:focus';
417          if (isset($s['blog_title_c'])) dcThemeConfig::prop($css_large,$selectors,'color',$s['blog_title_c']);
418
419          # Post title font weight
420          $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';
421          if (isset($s['post_title_w'])) dcThemeConfig::prop($css_large,$selectors,'font-weight',($s['post_title_w'] ? 'bold' : 'normal'));
422
423          # Post title font size
424          $selectors = 'h2.post-title';
425          if (isset($s['post_title_s'])) dcThemeConfig::prop($css_large,$selectors,'font-size',$s['post_title_s']);
426
427          # Post title color
428          $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';
429          if (isset($s['post_title_c'])) dcThemeConfig::prop($css_large,$selectors,'color',$s['post_title_c']);
430
431          # Simple title color (title without link)
432          $selectors = '#content-info h2, .post-title, .post h3, .post h4, .post h5, .post h6, .arch-block h3';
433          if (isset($s['post_simple_title_c'])) dcThemeConfig::prop($css_large,$selectors,'color',$s['post_simple_title_c']);
434
435          # Style directives for large screens
436          if (count($css_large)) {
437               $res .= '@media only screen and (min-width: 481px) {'."\n";
438               foreach ($css_large as $selector => $values) {
439                    $res .= $selector." {\n";
440                    foreach ($values as $k => $v) {
441                         $res .= $k.':'.$v.";\n";
442                    }
443                    $res .= "}\n";
444               }
445               $res .= "}\n";
446          }
447
448          # Small screens
449          $css_small = array();
450
451          # Blog title font weight
452          $selectors = 'h1, h1 a:link, h1 a:visited, h1 a:hover, h1 a:visited, h1 a:focus';
453          if (isset($s['blog_title_w_m'])) dcThemeConfig::prop($css_small,$selectors,'font-weight',($s['blog_title_w_m'] ? 'bold' : 'normal'));
454
455          # Blog title font size
456          $selectors = 'h1';
457          if (isset($s['blog_title_s_m'])) dcThemeConfig::prop($css_small,$selectors,'font-size',$s['blog_title_s_m']);
458
459          # Blog title color
460          $selectors = 'h1 a:link, h1 a:visited, h1 a:hover, h1 a:visited, h1 a:focus';
461          if (isset($s['blog_title_c_m'])) dcThemeConfig::prop($css_small,$selectors,'color',$s['blog_title_c_m']);
462
463          # Post title font weight
464          $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';
465          if (isset($s['post_title_w_m'])) dcThemeConfig::prop($css_small,$selectors,'font-weight',($s['post_title_w_m'] ? 'bold' : 'normal'));
466
467          # Post title font size
468          $selectors = 'h2.post-title';
469          if (isset($s['post_title_s_m'])) dcThemeConfig::prop($css_small,$selectors,'font-size',$s['post_title_s_m']);
470
471          # Post title color
472          $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';
473          if (isset($s['post_title_c_m'])) dcThemeConfig::prop($css_small,$selectors,'color',$s['post_title_c_m']);
474
475          # Style directives for small screens
476          if (count($css_small)) {
477               $res .= '@media only screen and (max-width: 480px) {'."\n";
478               foreach ($css_small as $selector => $values) {
479                    $res .= $selector." {\n";
480                    foreach ($values as $k => $v) {
481                         $res .= $k.':'.$v.";\n";
482                    }
483                    $res .= "}\n";
484               }
485               $res .= "}\n";
486          }
487
488          return $res;
489     }
490
491     protected static $fonts = array(
492          // Theme standard
493          'Ductile body' => '"Century Schoolbook", "Century Schoolbook L", Georgia, serif',
494          'Ductile alternate' => '"Franklin gothic medium", "arial narrow", "DejaVu Sans Condensed", "helvetica neue", helvetica, sans-serif',
495
496          // Serif families
497          'Times New Roman' => 'Cambria, "Hoefler Text", Utopia, "Liberation Serif", "Nimbus Roman No9 L Regular", Times, "Times New Roman", serif',
498          'Georgia' => 'Constantia, "Lucida Bright", Lucidabright, "Lucida Serif", Lucida, "DejaVu Serif", "Bitstream Vera Serif", "Liberation Serif", Georgia, serif',
499          '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',
500
501          // Sans-serif families
502          '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',
503          'Verdana' => 'Corbel, "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "DejaVu Sans", "Bitstream Vera Sans", "Liberation Sans", Verdana, "Verdana Ref", sans-serif',
504          'Trebuchet MS' => '"Segoe UI", Candara, "Bitstream Vera Sans", "DejaVu Sans", "Bitstream Vera Sans", "Trebuchet MS", Verdana, "Verdana Ref", sans-serif',
505
506          // Cursive families
507          'Impact' => 'Impact, Haettenschweiler, "Franklin Gothic Bold", Charcoal, "Helvetica Inserat", "Bitstream Vera Sans Bold", "Arial Black", sans-serif',
508
509          // Monospace families
510          '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'
511     );
512
513     protected static function fontDef($c)
514     {
515          return isset(self::$fonts[$c]) ? self::$fonts[$c] : null;
516     }
517}
Note: See TracBrowser for help on using the repository browser.

Sites map