Skip to content

Commit dd33e56

Browse files
committed
Optimizations and reorganization of the code (renamed, optimized and created new functions)
1 parent 17f4942 commit dd33e56

12 files changed

+306
-93
lines changed

tpl_admin.php

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
88
*/
99

10+
// must be run from within DokuWiki
11+
if (!defined('DOKU_INC')) die();
12+
1013
global $ID, $auth;
1114

1215
$admin_plugins = plugin_list('admin');

tpl_badges.php

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
88
*/
99

10+
// must be run from within DokuWiki
11+
if (!defined('DOKU_INC')) die();
12+
1013
?>
1114
<?php if ($showBadges): ?>
1215
<div class="text-center hidden-print">

tpl_cookielaw.php

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
88
*/
99

10+
// must be run from within DokuWiki
11+
if (!defined('DOKU_INC')) die();
12+
1013
?>
1114
<?php if ( $showCookieLawBanner && ! (get_doku_pref('cookieNoticeAccepted', null) || get_doku_pref('cookieNoticeAccepted', '')) ): ?>
1215
<div id="cookieNotice" class="navbar <?php echo (($inverseNavbar) ? 'navbar-inverse' : 'navbar-default') ?> navbar-fixed-bottom">

tpl_functions.php

+225-43
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,6 @@ function _tpl_action($type, $link=0, $wrapper=0, $return=0) {
9696
}
9797

9898

99-
function bootstrap3_toolsevent($toolsname, $items, $view='main', $return = false) {
100-
101-
$output = '';
102-
103-
$data = array(
104-
'view' => $view,
105-
'items' => $items
106-
);
107-
$hook = 'TEMPLATE_'.strtoupper($toolsname).'_DISPLAY';
108-
$evt = new Doku_Event($hook, $data);
109-
if($evt->advise_before()){
110-
foreach($evt->data['items'] as $k => $html) $output .= $html;
111-
}
112-
$evt->advise_after();
113-
114-
$search = array('<span>');
115-
$replace = array('<i class="fa fa-puzzle-piece fa-fw"></i> <span>');
116-
117-
$output = str_replace($search, $replace, $output);
118-
119-
if ($return) return $output;
120-
echo $output;
121-
122-
}
123-
12499

125100
/**
126101
* copied to core (available since Detritus)
@@ -160,6 +135,44 @@ function tpl_classes() {
160135
}
161136

162137

138+
/**
139+
* Create event for tools menues
140+
*
141+
* @author Anika Henke <[email protected]>
142+
* @author Giuseppe Di Terlizzi <[email protected]>
143+
*
144+
* @param string $toolsname name of menu
145+
* @param array $items
146+
* @param string $view e.g. 'main', 'detail', ...
147+
* @param boolean $return
148+
* @return string
149+
*/
150+
function bootstrap3_toolsevent($toolsname, $items, $view='main', $return = false) {
151+
152+
$output = '';
153+
154+
$data = array(
155+
'view' => $view,
156+
'items' => $items
157+
);
158+
$hook = 'TEMPLATE_'.strtoupper($toolsname).'_DISPLAY';
159+
$evt = new Doku_Event($hook, $data);
160+
if($evt->advise_before()){
161+
foreach($evt->data['items'] as $k => $html) $output .= $html;
162+
}
163+
$evt->advise_after();
164+
165+
$search = array('<span>');
166+
$replace = array('<i class="fa fa-puzzle-piece fa-fw"></i> <span>');
167+
168+
$output = str_replace($search, $replace, $output);
169+
170+
if ($return) return $output;
171+
echo $output;
172+
173+
}
174+
175+
163176
/**
164177
* Include left or right sidebar
165178
*
@@ -192,7 +205,7 @@ function bootstrap3_action_item($action, $icon, $return = false) {
192205

193206
if ($action == 'discussion') {
194207

195-
if (tpl_getConf('showDiscussion')) {
208+
if (bootstrap3_conf('showDiscussion')) {
196209
$out = _tpl_action('discussion', 1, 'li', 1);
197210
$out = str_replace(array('<bdi>', '</bdi>'), '', $out);
198211
return preg_replace('/(<a (.*?)>)/m', '$1<i class="'.$icon.'"></i> ', $out);
@@ -218,6 +231,7 @@ function bootstrap3_action_item($action, $icon, $return = false) {
218231

219232
}
220233

234+
221235
/**
222236
* Calculate automatically the grid size for main container
223237
*
@@ -234,29 +248,29 @@ function bootstrap3_container_grid() {
234248
$grids = array();
235249
$result = '';
236250

237-
$showRightSidebar = page_findnearest(tpl_getConf('rightSidebar')) && ($ACT=='show');
238-
$showLeftSidebar = page_findnearest($conf['sidebar']) && ($ACT=='show');
239-
$fluidContainer = tpl_getConf('fluidContainer');
251+
$showRightSidebar = bootstrap3_conf('showRightSidebar');
252+
$showLeftSidebar = bootstrap3_conf('showSidebar');
253+
$fluidContainer = bootstrap3_conf('fluidContainer');
240254

241-
if(tpl_getConf('fluidContainerBtn')) {
255+
if(bootstrap3_conf('fluidContainerBtn')) {
242256
$fluidContainer = bootstrap3_fluid_container_button();
243257
}
244258

245-
if ( tpl_getConf('showLandingPage')
246-
&& (bool) preg_match_all(sprintf('/%s/', tpl_getConf('landingPages')), $ID) ) {
259+
if ( bootstrap3_conf('showLandingPage')
260+
&& (bool) preg_match_all(sprintf('/%s/', bootstrap3_conf('landingPages')), $ID) ) {
247261
$showLeftSidebar = false;
248262
}
249263

250264
if (! $showLeftSidebar) {
251265
return 'container' . (($fluidContainer) ? '-fluid' : '');
252266
}
253267

254-
foreach (explode(' ', tpl_getConf('leftSidebarGrid')) as $grid) {
268+
foreach (explode(' ', bootstrap3_conf('leftSidebarGrid')) as $grid) {
255269
list($col, $media, $size) = explode('-', $grid);
256270
$grids[$media]['left'] = (int) $size;
257271
}
258272

259-
foreach (explode(' ', tpl_getConf('rightSidebarGrid')) as $grid) {
273+
foreach (explode(' ', bootstrap3_conf('rightSidebarGrid')) as $grid) {
260274
list($col, $media, $size) = explode('-', $grid);
261275
$grids[$media]['right'] = (int) $size;
262276
}
@@ -271,6 +285,7 @@ function bootstrap3_container_grid() {
271285

272286
}
273287

288+
274289
/**
275290
* Return the user home-page link
276291
*
@@ -288,9 +303,16 @@ function bootstrap3_user_homepage_link() {
288303
}
289304

290305

306+
/**
307+
* Check if the fluid container button is enabled (from the user cookie)
308+
*
309+
* @author Giuseppe Di Terlizzi <[email protected]>
310+
*
311+
* @return boolean
312+
*/
291313
function bootstrap3_fluid_container_button() {
292314

293-
if (! tpl_getConf('fluidContainerBtn')) return false;
315+
if (! bootstrap3_conf('fluidContainerBtn')) return false;
294316

295317
if ( get_doku_pref('fluidContainer', null) !== null
296318
&& get_doku_pref('fluidContainer', null) !== ''
@@ -354,6 +376,51 @@ function bootstrap3_sidebar($sidebar, $return = false) {
354376
}
355377

356378

379+
/**
380+
* Normalize DokuWiki list items
381+
*
382+
* @author Giuseppe Di Terlizzi <[email protected]>
383+
*
384+
* @param string $html
385+
* @return string
386+
*/
387+
function bootstrap3_lists($html) {
388+
$output = preg_replace('/<div class="li">(.*?)<\/div>/', '$1', $html);
389+
return $output;
390+
}
391+
392+
393+
394+
/**
395+
* Make a Bootstrap3 Nav
396+
*
397+
* @author Giuseppe Di Terlizzi <[email protected]>
398+
*
399+
* @param string $html
400+
* @param boolean $staked
401+
* @param string $type (= pills, tabs)
402+
* @return string
403+
**/
404+
function bootstrap3_nav($html, $stacked = false, $type = '') {
405+
406+
$class = 'nav ';
407+
408+
if ($stacked) $class .= 'nav-stacked ';
409+
if ($type) $class .= "nav-$type ";
410+
411+
$class = trim($class);
412+
413+
$output = str_replace(array('<ul class="', '<ul>'),
414+
array("<ul class=\"$class", "<ul class=\"$class\">"),
415+
$html);
416+
417+
$output = bootstrap3_lists($output);
418+
419+
return $output;
420+
421+
}
422+
423+
357424
/**
358425
* Print the search form in Bootstrap Style
359426
*
@@ -511,6 +578,13 @@ function bootstrap3_tools() {
511578
}
512579

513580

581+
/**
582+
* Return an array for a tools menu
583+
*
584+
* @author Giuseppe Di Terlizzi <[email protected]>
585+
*
586+
* @return array of tools
587+
*/
514588
function bootstrap3_tools_menu() {
515589

516590
$tools = bootstrap3_tools();
@@ -526,6 +600,13 @@ function bootstrap3_tools_menu() {
526600
}
527601

528602

603+
/**
604+
* Return an array for a toolbar
605+
*
606+
* @author Giuseppe Di Terlizzi <[email protected]>
607+
*
608+
* @return array of tools
609+
*/
529610
function bootstrap3_toolbar() {
530611

531612
$tools = _tpl_tools();
@@ -544,14 +625,14 @@ function bootstrap3_toolbar() {
544625
/**
545626
* Get either a Gravatar URL or complete image tag for a specified email address.
546627
*
547-
* @param string $email The email address
548-
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
549-
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
550-
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
551-
* @param boolean $img True to return a complete IMG tag False for just the URL
552-
* @param array $atts Optional, additional key/value attributes to include in the IMG tag
553-
* @return String containing either just a URL or a complete image tag
554-
* @source http://gravatar.com/site/implement/images/php/
628+
* @param string $email The email address
629+
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
630+
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
631+
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
632+
* @param boolean $img True to return a complete IMG tag False for just the URL
633+
* @param array $atts Optional, additional key/value attributes to include in the IMG tag
634+
* @return String containing either just a URL or a complete image tag
635+
* @source http://gravatar.com/site/implement/images/php/
555636
*/
556637
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
557638
$url = 'http://www.gravatar.com/avatar/';
@@ -565,3 +646,104 @@ function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts
565646
}
566647
return $url;
567648
}
649+
650+
651+
/**
652+
* Get the template metadata
653+
*
654+
* @author Giuseppe Di Terlizzi <[email protected]>
655+
*
656+
* @param string $key
657+
* @return array
658+
*/
659+
function bootstrap3_metadata($key = null) {
660+
661+
$meta = array();
662+
$file = tpl_incdir() . 'conf/metadata.php';
663+
include $file;
664+
665+
if ($key) return $meta[$key];
666+
667+
return $meta;
668+
669+
}
670+
671+
672+
/**
673+
* A simple wrapper for tpl_getConf
674+
*
675+
* @author Giuseppe Di Terlizzi <[email protected]>
676+
*
677+
* @param string $key
678+
* @param mixed $default value
679+
* @return mixed
680+
*/
681+
function bootstrap3_conf($key, $default = false) {
682+
683+
global $ACT, $INFO, $ID, $conf;
684+
685+
$value = tpl_getConf($key, $default);
686+
687+
switch ($key) {
688+
689+
case 'showTools':
690+
return $value !== 'never' && ( $value == 'always' || ! empty($_SERVER['REMOTE_USER']) );
691+
692+
case 'showSearchForm':
693+
return $value !== 'never' && ( $value == 'always' || ! empty($_SERVER['REMOTE_USER']) );
694+
695+
case 'showPageTools':
696+
return $value !== 'never' && ( $value == 'always' || ! empty($_SERVER['REMOTE_USER']) );
697+
698+
case 'showIndividualTool':
699+
return explode(',', $value);
700+
701+
case 'showAdminMenu':
702+
return $value && $INFO['isadmin'];
703+
704+
case 'hideLoginLink':
705+
return ! $value || ! empty($_SERVER['REMOTE_USER']);
706+
707+
case 'hideInThemeSwitcher':
708+
return explode(',', $value);
709+
710+
case 'browserTitle':
711+
return str_replace(array('@WIKI@', '@TITLE@'),
712+
array(strip_tags($conf['title']), tpl_pagetitle(null, true)),
713+
$value);
714+
715+
case 'showSidebar':
716+
return page_findnearest($conf['sidebar']) && ($ACT=='show');
717+
718+
case 'showRightSidebar':
719+
return page_findnearest(tpl_getConf('rightSidebar')) && ($ACT=='show');
720+
721+
case 'landingPages':
722+
return sprintf('/%s/', $value);
723+
724+
}
725+
726+
//$type = bootstrap3_metadata($key);
727+
728+
//if ($type[0] == 'regex') {
729+
// return sprintf('/%s/', $value);
730+
//}
731+
732+
return $value;
733+
734+
}
735+
736+
737+
/**
738+
* Return the Bootswatch.com theme lists defined in metadata.php
739+
*
740+
* @author Giuseppe Di Terlizzi <[email protected]>
741+
*
742+
* @return array
743+
*/
744+
function bootstrap3_bootswatch_theme_list() {
745+
746+
$bootswatch_themes = bootstrap3_metadata('bootswatchTheme');
747+
return $bootswatch_themes['_choices'];
748+
749+
}

0 commit comments

Comments
 (0)