Skip to content

Commit

Permalink
implemented mobile template switcher (closes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
selfthinker committed Sep 10, 2012
1 parent 3a2e5e4 commit dbe362d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
26 changes: 21 additions & 5 deletions action.php
Expand Up @@ -60,8 +60,13 @@ function _defineConstants(&$event, $param) {
function _handleConf(&$event, $param) {
global $conf;

$tpl = $this->getTpl();
// store original template in cookie
$tplOrigCookie = $_SESSION[DOKU_COOKIE]['loadskinOrig'];
if ((!$tplOrigCookie) || ($tplOrigCookie != $conf['template']))
$_SESSION[DOKU_COOKIE]['loadskinOrig'] = $conf['template'];

// set template
$tpl = $this->getTpl();
if($tpl && $_REQUEST['do'] != 'admin') {
$conf['template'] = $tpl;
}
Expand Down Expand Up @@ -127,36 +132,47 @@ function _getTplPerUser() {
$helper = $this->loadHelper('loadskin', true);
$tpls = $helper->getTemplates();

$mobileSwitch = $this->getConf('mobileSwitch');
$user = $_SERVER['REMOTE_USER'];

$tplRequest = $_REQUEST['tpl'];
$actSelect = $_REQUEST['act'] && ($_REQUEST['act']=='select');

// if template switcher was used
if ($tplRequest && $actSelect && (in_array($tplRequest, $tpls) || ($tplRequest == '*') )) {
// "secret" way of deleting the cookie and config values
if ($tplRequest == '*')
$tplRequest = '';
// store in cookie
$_SESSION[DOKU_COOKIE]['loadskinTpl'] = $tplRequest;
// if registered user, store also in conf file
if ($user)
// if registered user, store also in conf file (not for mobile switcher)
if ($user && !$mobileSwitch)
$this->_tplUserConfig('set', $user, $tplRequest);
return $tplRequest;
}

$tplUser = $this->_tplUserConfig('get', $user);// from user conf file
$tplCookie = $_SESSION[DOKU_COOKIE]['loadskinTpl'];
// if logged in and user is in conf
if ($user && $tplUser && in_array($tplUser, $tpls)){
// if logged in and user is in conf (not for mobile)
if ($user && $tplUser && in_array($tplUser, $tpls) && !$mobileSwitch){
if ($tplCookie && ($tplCookie == $tplUser))
return $tplCookie;
// store in cookie
$_SESSION[DOKU_COOKIE]['loadskinTpl'] = $tplUser;
return $tplUser;
}
// if template is stored in cookie
if ($tplCookie && in_array($tplCookie, $tpls))
return $tplCookie;

// if viewed on a mobile and mobile switcher is used, set mobile template as default
global $INFO;
$mobileTpl = $this->getConf('mobileTemplate');
if ($mobileTpl && $INFO['ismobile']) {
$_SESSION[DOKU_COOKIE]['loadskinTpl'] = $mobileTpl;
return $mobileTpl;
}

return false;
}

Expand Down
2 changes: 2 additions & 0 deletions conf/default.php
Expand Up @@ -6,3 +6,5 @@

$conf['automaticOutput'] = 0;
$conf['excludeTemplates'] = '';
$conf['mobileSwitch'] = 0;
$conf['mobileTemplate'] = '';
2 changes: 2 additions & 0 deletions conf/metadata.php
Expand Up @@ -6,3 +6,5 @@

$meta['automaticOutput'] = array('onoff');
$meta['excludeTemplates'] = array('string');
$meta['mobileSwitch'] = array('onoff');
$meta['mobileTemplate'] = array('dirchoice', '_dir' => DOKU_INC.'lib/tpl/', '_pattern' => '/^[\w-]+$/');
17 changes: 15 additions & 2 deletions helper.php
Expand Up @@ -37,14 +37,27 @@ function getTemplates() {
/**
* Builds a select box with all available templates
* (unless excluded in 'excludeTemplates')
* or show only two templates for mobile switcher: standard plus mobile template
*
* @author Anika Henke <anika@selfthinker.org>
*/
function showTemplateSwitcher() {
global $conf;
global $ID;
$excludeTemplates = array_map('trim', explode(",", $this->getConf('excludeTemplates')));
$templates = array_diff($this->getTemplates(),$excludeTemplates);

$mobileSwitch = $this->getConf('mobileSwitch');
$mobileTpl = $this->getConf('mobileTemplate');
if ($mobileSwitch && $mobileTpl) {
// templates for mobile switcher
$templates = array(
$mobileTpl => $this->getLang('switchMobile'),
$_SESSION[DOKU_COOKIE]['loadskinOrig'] => $this->getLang('switchFull')
);
} else {
// all templates (minus excluded templates)
$excludeTemplates = array_map('trim', explode(",", $this->getConf('excludeTemplates')));
$templates = array_diff($this->getTemplates(),$excludeTemplates);
}

$form = new Doku_Form(array(
'id' => 'tpl__switcher',
Expand Down
10 changes: 6 additions & 4 deletions lang/en/lang.php
Expand Up @@ -3,7 +3,9 @@
* English language file for DokuWiki Plugin Loadskin
*/

$lang['template'] = 'Template';
$lang['menu'] = 'LoadSkin Template Manager';
$lang['switchTpl'] = 'Switch template';
$lang['switch'] = 'Switch';
$lang['template'] = 'Template';
$lang['menu'] = 'LoadSkin Template Manager';
$lang['switchTpl'] = 'Switch template';
$lang['switch'] = 'Switch';
$lang['switchFull'] = 'Full version';
$lang['switchMobile'] = 'Mobile version';
2 changes: 2 additions & 0 deletions lang/en/settings.php
Expand Up @@ -6,3 +6,5 @@

$lang['automaticOutput'] = 'Automatically output the template switcher on every page';
$lang['excludeTemplates'] = 'Exclude templates from template switcher (comma-separated list)';
$lang['mobileSwitch'] = 'Show mobile template switcher (with only 1 normal and 1 mobile template)?';
$lang['mobileTemplate'] = 'Mobile template (if showing mobile template switcher)';

0 comments on commit dbe362d

Please sign in to comment.