streams/Code/Render/Theme.php

285 lines
8.5 KiB
PHP
Raw Normal View History

<?php
2022-02-16 04:08:28 +00:00
namespace Code\Render;
use App;
2022-02-16 04:08:28 +00:00
use Code\Lib\Infocon;
use Code\Lib\Addon;
2022-03-07 05:31:28 +00:00
use Code\Lib\Yaml;
2022-08-15 11:19:29 +00:00
use Exception;
2022-02-12 20:43:29 +00:00
2021-12-02 23:02:31 +00:00
class Theme
{
2021-12-03 03:01:39 +00:00
public static $system_theme = null;
public static $session_theme = null;
2021-12-02 23:02:31 +00:00
/**
* @brief Array with base or fallback themes.
*/
2022-09-03 05:23:54 +00:00
public static $base_themes = ['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
2022-02-12 20:43:29 +00:00
$info = self::get_info($themepair[0]);
$compatible = Addon::check_versions($info);
2021-12-02 23:02:31 +00:00
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')
) {
2022-09-24 21:37:08 +00:00
return ([$t]);
2021-12-02 23:02:31 +00:00
}
}
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)) {
2022-09-24 21:37:08 +00:00
return ([str_replace('view/theme/', '', substr($fallback[0], 0, -14))]);
2021-12-03 03:01:39 +00:00
}
2022-08-27 04:01:22 +00:00
return [];
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 = ((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');
}
public static function include($file, $root = '')
{
// Make sure $root ends with a slash / if it's not blank
if ($root) {
$root = rtrim($root,'/') . '/';
}
$theme_info = App::$theme_info;
if (array_key_exists('extends', $theme_info)) {
$parent = $theme_info['extends'];
} else {
$parent = 'NOPATH';
}
$theme = self::current();
$thname = $theme[0];
$ext = substr($file, strrpos($file, '.') + 1);
2022-09-24 21:37:08 +00:00
$paths = [
"{$root}view/theme/$thname/$ext/$file",
"{$root}view/theme/$parent/$ext/$file",
"{$root}view/site/$ext/$file",
"{$root}view/$ext/$file",
2022-09-24 21:37:08 +00:00
];
foreach ($paths as $p) {
2022-09-24 21:37:08 +00:00
if (str_contains($p, 'NOPATH')) {
continue;
}
if (file_exists($p)) {
return $p;
}
}
return '';
}
2022-02-24 21:00:41 +00:00
public static function get_info($theme) {
2022-02-12 20:43:29 +00:00
$info = null;
2022-03-07 05:31:28 +00:00
$has_yaml = true;
if (is_file("view/theme/$theme.yml")) {
2022-02-12 20:43:29 +00:00
$info = Infocon::from_file("view/theme/$theme.yml");
}
elseif (is_file("view/theme/$theme/php/theme.php")) {
2022-03-07 05:31:28 +00:00
$has_yaml = false;
2022-02-12 20:43:29 +00:00
$info = Infocon::from_c_comment("view/theme/$theme/php/theme.php");
}
2022-03-07 05:31:28 +00:00
if ($info && ! $has_yaml) {
try {
file_put_contents("view/theme/$theme.yml",Yaml::encode($info));
}
2022-08-15 11:19:29 +00:00
catch (Exception $e) {
2022-03-07 05:31:28 +00:00
}
}
2022-09-24 21:37:08 +00:00
return $info ?: [ 'name' => $theme ] ;
2022-02-12 20:43:29 +00:00
}
2022-02-24 21:00:41 +00:00
public static function get_email_template($s, $root = '')
2022-02-12 20:43:29 +00:00
{
$testroot = ($root=='') ? $testroot = "ROOT" : $root;
$t = App::template_engine();
if (isset(App::$override_intltext_templates[$testroot][$s]["content"])) {
return App::$override_intltext_templates[$testroot][$s]["content"];
} else {
if (isset(App::$override_intltext_templates[$testroot][$s]["root"]) &&
isset(App::$override_intltext_templates[$testroot][$s]["file"])) {
$s = App::$override_intltext_templates[$testroot][$s]["file"];
$root = App::$override_intltext_templates[$testroot][$s]["root"];
} elseif (App::$override_templateroot) {
$newroot = App::$override_templateroot.$root;
2022-09-24 21:37:08 +00:00
if ($newroot != '' && !str_ends_with($newroot, '/')) {
2022-02-12 20:43:29 +00:00
$newroot .= '/';
}
2022-03-02 22:54:19 +00:00
$template = $t->get_email_template($s, $newroot);
2022-02-12 20:43:29 +00:00
}
2022-03-02 22:54:19 +00:00
$template = $t->get_email_template($s, $root);
return $template;
2022-02-12 20:43:29 +00:00
}
}
2022-02-24 21:00:41 +00:00
public static function get_template($s, $root = '')
2022-02-12 20:43:29 +00:00
{
$testroot = ($root=='') ? $testroot = "ROOT" : $root;
$t = App::template_engine();
if (isset(App::$override_markup_templates[$testroot][$s]["content"])) {
return App::$override_markup_templates[$testroot][$s]["content"];
} else {
if (isset(App::$override_markup_templates[$testroot][$s]["root"]) &&
isset(App::$override_markup_templates[$testroot][$s]["file"])) {
$s = App::$override_markup_templates[$testroot][$s]["file"];
$root = App::$override_markup_templates[$testroot][$s]["root"];
} elseif (App::$override_templateroot) {
$newroot = App::$override_templateroot.$root;
2022-09-24 21:37:08 +00:00
if ($newroot != '' && !str_ends_with($newroot, '/')) {
2022-02-12 20:43:29 +00:00
$newroot .= '/';
}
$template = $t->get_template($s, $newroot);
}
$template = $t->get_template($s, $root);
return $template;
}
}
2022-02-24 21:00:41 +00:00
/**
* @brief Returns the theme's screenshot.
*
* The screenshot is expected as view/theme/$theme/img/screenshot.[png|jpg].
*
* @param string $theme The name of the theme
* @return string
*/
public static function get_screenshot($theme)
{
2022-09-24 21:37:08 +00:00
$exts = ['.png', '.jpg'];
2022-02-24 21:00:41 +00:00
foreach ($exts as $ext) {
if (file_exists('view/theme/' . $theme . '/img/screenshot' . $ext)) {
return(z_root() . '/view/theme/' . $theme . '/img/screenshot' . $ext);
}
}
return(z_root() . '/images/blank.png');
}
2022-02-12 20:43:29 +00:00
2022-02-24 21:00:41 +00:00
public static function debug()
2021-12-02 23:02:31 +00:00
{
logger('system_theme: ' . self::$system_theme);
logger('session_theme: ' . self::$session_theme);
}
}