1<?php 2/** 3 * DokuWiki plugin template helper 4 * 5 * @license GPL3 (http://www.gnu.org/licenses/gpl.html) 6 * @author Samuel Fischer <sf@notomorrow.de> 7 */ 8 9 10if(!defined('DOKU_INC')) die(); 11 12 13class action_plugin_templateconfhelper_templateaction extends DokuWiki_Action_Plugin { 14 15 function getInfo(){ 16 return array( 17 'author' => 'sf', 18 'email' => 'sf@notomorrow.de', 19 'date' => '2010-02-07', 20 'name' => 'template actions', 21 'desc' => 'switch template based on user selection', 22 'url' => 'samfisch.de', 23 ); 24 } 25 26 function register(&$controller) {/*{{{*/ 27 28 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'template_action' ); 29 30 }/*}}}*/ 31 32 function template_action( ) {/*{{{*/ 33 global $conf; 34 $tpl = $conf['template']; 35 $theme = ''; 36 $switch = false; 37 38 if( $conf['plugin']['templateconfhelper']['base_tpl'] != $tpl ) { 39 $tpl = $conf['plugin']['templateconfhelper']['base_tpl']; 40 $switch = true; 41 } 42 43 $u = $this->get_user( ); // init user data 44 45 if( isset( $u['template'] ) && $u['template'] && $u['template'] != $tpl ) { 46 $tpl = $u['template']; 47 $switch = true; 48 } 49 if( isset( $u['template_theme'] ) && $u['template_theme'] != $tpl ) { 50 $theme = $u['template_theme']; 51 $switch = true; 52 } 53 54 if( isset( $_GET['utpl'] ) && preg_match( '/^[\w-]+$/', $_GET['utpl'] )) { 55 if( $_GET['utpl'] != $tpl && $_GET['utpl'] != $conf['template'] ) { 56 $switch = true; 57 } 58 $tpl = $_GET['utpl']; 59 $this->save_session( 'template', $tpl ); 60 } 61 if( isset( $_GET['utpl'] ) && $_GET['utpl'] == "" ) { 62 $tpl = $conf['default_tpl']; 63 $this->save_session( 'template', '' ); // fix subconf switch 64 $switch = false; 65 } 66 67 68 if( isset( $_GET['utpl_theme'] ) && preg_match( '/^[\w-]*$/', $_GET['utpl_theme'] )) { 69 if( $_GET['utpl_theme'] ) { 70 $theme = $_GET['utpl_theme']; 71 $switch = true; 72 } 73 $this->save_session( 'template_theme', $_GET['utpl_theme'] ); 74 } 75 76 if( $switch && preg_match( '/^[\w-]+$/', $tpl )) { 77 $this->_switch( $tpl, $theme ); 78 } 79 80 }/*}}}*/ 81 82 public function get_user( $var=false ) {/*{{{*/ 83 if( !defined('NOSESSION' )) { 84 if( !isset( $this->u['load'] )) { 85 @session_start(); 86 $this->u = ( isset( $_SESSION[DOKU_COOKIE]['tpl'] )) ? $_SESSION[DOKU_COOKIE]['tpl'] : false; 87 } 88 89 if( !isset( $this->u['load'] )) { 90 $this->u['load'] = 1; 91 // TODO: load wikiuser selection from file 92 } 93 if( $var ) return isset( $this->u[$var] ) ? $this->u[$var] : false; 94 return $this->u; 95 } else { 96 return array( ); 97 } 98 }/*}}}*/ 99 100 public function save_session( $var, $val ) {/*{{{*/ 101 if( !defined('NOSESSION' )) { 102 $this->u[$var] = $val; 103 104 @session_start(); 105 $_SESSION[DOKU_COOKIE]['tpl'] = $this->u; 106 session_write_close(); 107 } 108 }/*}}}*/ 109 110 public function save_user( $var, $val ) { 111 return false; 112 } 113 114 /** 115 * actual helper function 116 * changes style after doku init 117 */ 118 119 public function tpl_switch( $tpl ) {/*{{{*/ 120 global $conf; 121 if( $conf['template'] == $tpl ) { return ''; } 122 123 // prevent userstyle from beeing overwritten ... one or the other way 124 if( $this->get_user( 'template' )) { return ''; } 125 if( preg_match( '/^[\w-]+$/', $tpl )) { 126 $this->_switch( $tpl ); 127 } 128 }/*}}}*/ 129 130 function _switch( $tpl, $theme='' ) {/*{{{*/ 131 global $conf; 132 global $tpl_configloaded; 133 134 if( $theme ) { 135 $conf['template_theme'] = $theme; 136 } 137 138 $conf['default_tpl'] = $conf['template']; 139 $conf['template'] = $tpl; 140 141 $tconf = $this->tpl_loadconfig( $tpl ); 142 if ($tconf !== false){ 143 foreach ($tconf as $key => $value){ 144 if (isset($conf['tpl'][$tpl][$key])) continue; 145 $conf['tpl'][$tpl][$key] = $value; 146 } 147 $tpl_configloaded = true; 148 } 149 }/*}}}*/ 150 151 public function tpl_loadconfig( $tpl ) {/* {{{*/ 152 $file = DOKU_TPLINC.'../'.$tpl.'/conf/default.php'; 153 $conf = array(); 154 if (!@file_exists($file)) return false; 155 156 include($file); 157 return $conf; 158 } /*}}}*/ 159 160} 161 162// pirating into css.php 163require_once( DOKU_PLUGIN.'/templateconfhelper/inc/preload.php' ); // tpl_... functions 164