streams/Zotlabs/Render/Theme.php

143 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace Zotlabs\Render;
use App;
2021-12-02 23:02:31 +00:00
class Theme
{
2021-12-03 03:01:39 +00:00
public static $system_theme = null;
2021-12-03 03:01:39 +00:00
public static $session_theme = null;
2021-12-02 23:02:31 +00:00
/**
* @brief Array with base or fallback themes.
*/
2021-12-03 03:01:39 +00:00
public static $base_themes = array('redbasic');
2021-12-02 23:02:31 +00:00
/**
* @brief Figure out the best matching theme and return it.
*
* The theme will depend on channel settings, mobile, session, core compatibility, etc.
*
* @return array
*/
public static function current()
{
2021-12-02 23:02:31 +00:00
self::$system_theme = ((isset(App::$config['system']['theme']))
? App::$config['system']['theme'] : '');
self::$session_theme = ((isset($_SESSION) && x($_SESSION, 'theme'))
? $_SESSION['theme'] : self::$system_theme);
2021-12-02 23:02:31 +00:00
$page_theme = null;
2021-12-02 23:02:31 +00:00
// Find the theme that belongs to the channel whose stuff we are looking at
2021-12-02 23:02:31 +00:00
if (App::$profile_uid) {
2021-12-03 03:01:39 +00:00
$r = q(
"select channel_theme from channel where channel_id = %d limit 1",
2021-12-02 23:02:31 +00:00
intval(App::$profile_uid)
);
if ($r) {
$page_theme = $r[0]['channel_theme'];
}
}
2021-12-02 23:02:31 +00:00
// Themes from Comanche layouts over-ride the channel theme
2021-12-03 03:01:39 +00:00
if (array_key_exists('theme', App::$layout) && App::$layout['theme']) {
2021-12-02 23:02:31 +00:00
$page_theme = App::$layout['theme'];
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$chosen_theme = self::$session_theme;
2021-12-02 23:02:31 +00:00
if ($page_theme) {
$chosen_theme = $page_theme;
}
2021-12-03 03:01:39 +00:00
if (array_key_exists('theme_preview', $_GET)) {
2021-12-02 23:02:31 +00:00
$chosen_theme = $_GET['theme_preview'];
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
// Allow theme selection of the form 'theme_name:schema_name'
$themepair = explode(':', $chosen_theme);
2017-02-27 09:44:50 +00:00
2021-12-02 23:02:31 +00:00
// Check if $chosen_theme is compatible with core. If not fall back to default
$info = get_theme_info($themepair[0]);
$compatible = check_plugin_versions($info);
if (!$compatible) {
$chosen_theme = '';
}
2021-12-02 23:02:31 +00:00
if ($chosen_theme && (file_exists('view/theme/' . $themepair[0] . '/css/style.css') || file_exists('view/theme/' . $themepair[0] . '/php/style.php'))) {
return ($themepair);
}
2021-12-02 23:02:31 +00:00
foreach (self::$base_themes as $t) {
2021-12-03 03:01:39 +00:00
if (
file_exists('view/theme/' . $t . '/css/style.css') ||
file_exists('view/theme/' . $t . '/php/style.php')
) {
2021-12-02 23:02:31 +00:00
return (array($t));
}
}
2021-12-02 23:02:31 +00:00
// Worst case scenario, the default base theme or themes don't exist; perhaps somebody renamed it/them.
2021-12-02 23:02:31 +00:00
// Find any theme at all and use it.
2021-12-02 23:02:31 +00:00
$fallback = array_merge(glob('view/theme/*/css/style.css'), glob('view/theme/*/php/style.php'));
2021-12-03 03:01:39 +00:00
if (count($fallback)) {
2021-12-02 23:02:31 +00:00
return (array(str_replace('view/theme/', '', substr($fallback[0], 0, -14))));
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
2021-12-02 23:02:31 +00:00
/**
* @brief Return full URL to theme which is currently in effect.
*
* Provide a sane default if nothing is chosen or the specified theme does not exist.
*
* @param bool $installing (optional) default false, if true return the name of the first base theme
*
* @return string
*/
public static function url($installing = false)
{
2021-12-03 03:01:39 +00:00
if ($installing) {
2021-12-02 23:02:31 +00:00
return self::$base_themes[0];
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$theme = self::current();
2021-12-02 23:02:31 +00:00
$t = $theme[0];
$s = ((count($theme) > 1) ? $theme[1] : '');
2021-12-02 23:02:31 +00:00
$opts = '';
$opts = ((App::$profile_uid) ? '?f=&puid=' . App::$profile_uid : '');
2021-12-02 23:02:31 +00:00
$schema_str = ((x(App::$layout, 'schema')) ? '&schema=' . App::$layout['schema'] : '');
2021-12-03 03:01:39 +00:00
if (($s) && (!$schema_str)) {
2021-12-02 23:02:31 +00:00
$schema_str = '&schema=' . $s;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$opts .= $schema_str;
2021-12-03 03:01:39 +00:00
if (file_exists('view/theme/' . $t . '/php/style.php')) {
2021-12-02 23:02:31 +00:00
return ('/view/theme/' . $t . '/php/style.pcss' . $opts);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
return ('/view/theme/' . $t . '/css/style.css');
}
2021-12-02 23:02:31 +00:00
public function debug()
{
logger('system_theme: ' . self::$system_theme);
logger('session_theme: ' . self::$session_theme);
}
}