Dotclear

source: themes/ductile/_public.php @ 1081:862f566d2372

Revision 1081:862f566d2372, 16.7 KB checked in by franck <carnet.franck.paul@…>, 13 years ago (diff)

Just for fun, Ductile may now use web fonts (from Google, Adobe and similar providers)

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

Sites map