Skip to content

Commit

Permalink
Theme Selector
Browse files Browse the repository at this point in the history
(Credits to HerculesWS/FluxCP:a1f4227)

Signed-off-by: Akkarinage <akkarin@rathena.org>
  • Loading branch information
Akkarinage committed Aug 26, 2014
1 parent d2d928e commit 5f07de7
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 38 deletions.
2 changes: 1 addition & 1 deletion config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'DefaultCharMapServer' => null,
'DefaultLanguage' => 'en_us', // Specify the default control panel language (see FLUX_ROOT/lang/ directory for available languages.)
'SiteTitle' => 'Flux Control Panel', // This value is only used if the theme decides to use it.
'ThemeName' => 'default', // The theme name of the theme you would like to use. Themes are in FLUX_ROOT/themes.
'ThemeName' => array('default', 'bootstrap'), // Names of the themes you would like list for use in the footer. Themes are in FLUX_ROOT/themes.
'ScriptTimeLimit' => 0, // Script execution time limit. Specifies (in seconds) how long a page should run before timing out. (0 means forever)
'MissingEmblemBMP' => 'empty.bmp', //
'ItemIconNameFormat' => '%d.png', // The filename format for item icons (defaults to {itemid}.png).
Expand Down
3 changes: 2 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@
'basePath' => Flux::config('BaseURI'),
'useCleanUrls' => Flux::config('UseCleanUrls'),
'modulePath' => FLUX_MODULE_DIR,
'themePath' => FLUX_THEME_DIR.'/'.Flux::config('ThemeName'),
'themePath' => FLUX_THEME_DIR,
'themeName' => Flux::$sessionData->theme,
'missingActionModuleAction' => Flux::config('DebugMode') ? array('errors', 'missing_action') : array('main', 'page_not_found'),
'missingViewModuleAction' => Flux::config('DebugMode') ? array('errors', 'missing_view') : array('main', 'page_not_found')
));
Expand Down
13 changes: 8 additions & 5 deletions lib/Flux.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,16 @@ public static function parseAppConfigFile($filename)
if (!$config->getServerAddress()) {
self::raise("ServerAddress must be specified in your application config.");
}
if (!$config->getThemeName()) {
if (count($themes = $config->get('ThemeName', false)) < 1) {
self::raise('ThemeName is required in application configuration.');
}
elseif (!self::themeExists($themeName=$config->getThemeName())) {
self::raise("The selected theme '$themeName' does not exist.");
}
elseif (!($config->getPayPalReceiverEmails() instanceOf Flux_Config)) {
else {
foreach ($themes as $themeName) {
if (!self::themeExists($themeName)) {
self::raise("The selected theme '$themeName' does not exist.");
}
} }
if (!($config->getPayPalReceiverEmails() instanceOf Flux_Config)) {
self::raise("PayPalReceiverEmails must be an array.");
}

Expand Down
13 changes: 2 additions & 11 deletions lib/Flux/Addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct($name, $addonDir = null)
$this->addonDir = is_null($addonDir) ? FLUX_ADDON_DIR."/$name" : $addonDir;
$this->configDir = "{$this->addonDir}/config";
$this->moduleDir = "{$this->addonDir}/modules";
$this->themeDir = "{$this->addonDir}/themes/".Flux::config('ThemeName');
$this->themeDir = "{$this->addonDir}/themes";

$files = array(
'addonConfig' => "{$this->configDir}/addon.php",
Expand Down Expand Up @@ -51,15 +51,6 @@ public function respondsTo($module, $action = null)
}
}

public function hasView($module, $action)
{
$path = "{$this->themeDir}/$module/$action.php";
if (file_exists($path)) {
return true;
}
else {
return false;
}
}

}
?>
2 changes: 2 additions & 0 deletions lib/Flux/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function dispatch($options = array())
$modulePath = $config->get('modulePath');
$themePath = $config->get('themePath');
$defaultModule = $config->get('defaultModule');
$themeName = $config->get('themeName');
$defaultAction = $config->get('defaultAction');
$missingActionModuleAction = $config->get('missingActionModuleAction');
$missingViewModuleAction = $config->get('missingViewModuleAction');
Expand Down Expand Up @@ -145,6 +146,7 @@ public function dispatch($options = array())
'modulePath' => $modulePath,
'moduleName' => $moduleName,
'themePath' => $themePath,
'themeName' => $themeName,
'actionName' => $actionName,
'viewName' => $actionName,
'headerName' => 'header',
Expand Down
4 changes: 4 additions & 0 deletions lib/Flux/SessionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ private function initialize($force = false)
}
}

if (!$this->theme) {
$this->setThemeData(Flux::config('ThemeName.0'));
}

return true;
}

Expand Down
37 changes: 19 additions & 18 deletions lib/Flux/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,21 @@ class Flux_Template {
protected $moduleName;

/**
* Theme path. This is the path to the selected theme itself, not the real
* theme path which contains several themes.
* Theme path.
*
* @access protected
* @var string
*/
protected $themePath;

/**
* Theme name.
*
* @access protected
* @var string
*/
protected $themeName;

/**
* Action name. Actions exist as modulePath/moduleName/actionName.php.
*
Expand Down Expand Up @@ -195,6 +202,7 @@ public function __construct(Flux_Config $config)
$this->modulePath = $config->get('modulePath');
$this->moduleName = $config->get('moduleName');
$this->themePath = $config->get('themePath');
$this->themeName = $config->get('themeName');
$this->actionName = $config->get('actionName');
$this->viewName = $config->get('viewName');
$this->headerName = $config->get('headerName');
Expand Down Expand Up @@ -256,25 +264,18 @@ public function render(array $dataArr = array())
}

$viewExists = false;
$this->viewPath = sprintf('%s/%s/%s.php', $this->themePath, $this->moduleName, $this->actionName);
$this->viewPath = sprintf('%s/%s/%s/%s.php', $addon ? $addon->themeDir : $this->themePath, $this->themeName, $this->moduleName, $this->actionName);

if (!file_exists($this->viewPath)) {
if ($addon) {
$this->viewPath = sprintf('%s/%s/%s.php', $addon->themeDir, $this->moduleName, $this->actionName);
$viewExists = $addon->hasView($this->moduleName, $this->actionName);
}

if (!$viewExists) {
$this->moduleName = $this->missingViewModuleAction[0];
$this->actionName = $this->missingViewModuleAction[1];
$this->viewName = $this->missingViewModuleAction[1];
$this->actionPath = sprintf('%s/%s/%s.php', $this->modulePath, $this->moduleName, $this->actionName);
$this->viewPath = sprintf('%s/%s/%s.php', $this->themePath, $this->moduleName, $this->viewName);
}
$this->moduleName = $this->missingViewModuleAction[0];
$this->actionName = $this->missingViewModuleAction[1];
$this->viewName = $this->missingViewModuleAction[1];
$this->actionPath = sprintf('%s/%s/%s.php', $this->modulePath, $this->moduleName, $this->actionName);
$this->viewPath = sprintf('%s/%s/%s/%s.php', $this->themePath, $this->themeName, $this->moduleName, $this->viewName);
}

$this->headerPath = sprintf('%s/%s.php', $this->themePath, $this->headerName);
$this->footerPath = sprintf('%s/%s.php', $this->themePath, $this->footerName);
$this->headerPath = sprintf('%s/%s/%s.php', $this->themePath, $this->themeName, $this->headerName);
$this->footerPath = sprintf('%s/%s/%s.php', $this->themePath, $this->themeName, $this->footerName);
$this->url = $this->url($this->moduleName, $this->actionName);
$this->urlWithQS = $this->url;

Expand Down Expand Up @@ -521,7 +522,7 @@ public function themePath($path)
if (is_array($path)) {
$path = implode('/', $path);
}
return $this->path("{$this->themePath}/$path");
return $this->path("{$this->themePath}/{$this->themeName}/$path");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/errors/missing_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
if (!defined('FLUX_ROOT')) exit;

$title = Flux::message('MissingViewTitle');
$realViewPath = sprintf('%s/%s/%s/%s.php', FLUX_ROOT, $this->themePath, $this->params->get('module'), $this->params->get('action'));
$realViewPath = sprintf('%s/%s/%s/%s.php', FLUX_ROOT, $addon ? $addon->themeDir : $this->themePath, $this->params->get('module'), $this->params->get('action'));
?>
7 changes: 7 additions & 0 deletions modules/main/preprocess.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@
}
}

if (($preferred_theme = $params->get('preferred_theme'))) {
$session->setThemeData($params->get('preferred_theme'));
if (!array_key_exists('preferred_theme', $_GET)) {
$this->redirect($this->urlWithQs);
}
}

// Preferred server.
$server = $session->getAthenaServer();

Expand Down
22 changes: 21 additions & 1 deletion themes/bootstrap/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div id="footer">
<div class="container">
<p class="text-muted">
<?php if (Flux::config('ShowCopyright')): ?>
<?php if (Flux::config('ShowCopyright')): ?>
Powered by <a href="https://github.com/rathena/FluxCP" target="_blank">FluxCP</a>
<?php endif ?>
<?php if (Flux::config('ShowRenderDetails')): ?>
Expand All @@ -13,7 +13,21 @@
Number of queries executed: <strong><?php echo (int)Flux::$numberOfQueries ?></strong>.
<?php if (Flux::config('GzipCompressOutput')): ?>Gzip Compression: <strong>Enabled</strong>.<?php endif ?>

<?php endif ?>
<?php if (count(Flux::$appConfig->get('ThemeName', false)) > 1): ?>

<span>Theme:
<select name="preferred_theme" onchange="updatePreferredTheme(this)">
<?php foreach (Flux::$appConfig->get('ThemeName', false) as $themeName): ?>
<option value="<?php echo htmlspecialchars($themeName) ?>"<?php if ($session->theme == $themeName) echo ' selected="selected"' ?>><?php echo htmlspecialchars($themeName) ?></option>
<?php endforeach ?>
</select>
</span>
<?php endif ?>
<form action="<?php echo $this->urlWithQs ?>" method="post" name="preferred_theme_form" style="display: none">
<input type="hidden" name="preferred_theme" value="" />
</form>

</p>
</div>
</div>
Expand Down Expand Up @@ -81,6 +95,12 @@ function updatePreferredServer(sel){
document.preferred_server_form.submit();
}

function updatePreferredTheme(sel){
var preferred = sel.options[sel.selectedIndex].value;
document.preferred_theme_form.preferred_theme.value = preferred;
document.preferred_theme_form.submit();
}

// Preload spinner image.
var spinner = new Image();
spinner.src = '<?php echo $this->themePath('img/spinner.gif') ?>';
Expand Down
19 changes: 19 additions & 0 deletions themes/default/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@
</td>
<td></td>
</tr>
<?php endif ?>
<?php if (count(Flux::$appConfig->get('ThemeName', false)) > 1): ?>
<tr>
<td colspan="3"></td>
<td align="right">
<span>Theme:
<select name="preferred_theme" onchange="updatePreferredTheme(this)">
<?php foreach (Flux::$appConfig->get('ThemeName', false) as $themeName): ?>
<option value="<?php echo htmlspecialchars($themeName) ?>"<?php if ($session->theme == $themeName) echo ' selected="selected"' ?>><?php echo htmlspecialchars($themeName) ?></option>
<?php endforeach ?>
</select>
</span>

<form action="<?php echo $this->urlWithQs ?>" method="post" name="preferred_theme_form" style="display: none">
<input type="hidden" name="preferred_theme" value="" />
</form>
</td>
<td></td>
</tr>
<?php endif ?>
</table>
</body>
Expand Down
6 changes: 6 additions & 0 deletions themes/default/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ function updatePreferredServer(sel){
document.preferred_server_form.submit();
}

function updatePreferredTheme(sel){
var preferred = sel.options[sel.selectedIndex].value;
document.preferred_theme_form.preferred_theme.value = preferred;
document.preferred_theme_form.submit();
}

// Preload spinner image.
var spinner = new Image();
spinner.src = '<?php echo $this->themePath('img/spinner.gif') ?>';
Expand Down

0 comments on commit 5f07de7

Please sign in to comment.