From 22149b52c309152b3e0e186159df9e80ae5c28f8 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Fri, 27 Nov 2009 17:12:13 -0800 Subject: Move the theme fallback checking into theme::load_themes() so that we're calling it once per request. --- modules/gallery/helpers/theme.php | 17 +++++++++++++---- modules/gallery/libraries/Admin_View.php | 6 ------ modules/gallery/libraries/Theme_View.php | 6 ------ 3 files changed, 13 insertions(+), 16 deletions(-) (limited to 'modules') diff --git a/modules/gallery/helpers/theme.php b/modules/gallery/helpers/theme.php index 16ed104e..efc9b9e6 100644 --- a/modules/gallery/helpers/theme.php +++ b/modules/gallery/helpers/theme.php @@ -24,6 +24,8 @@ * Note: by design, this class does not do any permission checking. */ class theme_Core { + public static $active_theme; + /** * Load the active theme. This is called at bootstrap time. We will only ever have one theme * active for any given request. @@ -35,15 +37,22 @@ class theme_Core { $path = "/" . $input->get("kohana_uri"); } + $is_admin = $path == "/admin" || !strncmp($path, "/admin/", 7); + $setting_name = $is_admin ? "active_admin_theme" : "active_site_theme"; if (!(identity::active_user()->admin && $theme_name = $input->get("theme"))) { - $theme_name = module::get_var( - "gallery", - $path == "/admin" || !strncmp($path, "/admin/", 7) ? - "active_admin_theme" : "active_site_theme"); + $theme_name = module::get_var("gallery", $setting_name); + + if (!file_exists(THEMEPATH . $theme_name)) { + Kohana::log("error", "Unable to locate theme '$theme_name', switching to default theme."); + $theme_name = $is_admin ? "admin_wind" : "wind"; + module::set_var("gallery", $setting_name, $theme_name); + } } $modules = Kohana::config("core.modules"); array_unshift($modules, THEMEPATH . $theme_name); Kohana::config_set("core.modules", $modules); + + self::$active_theme = $theme_name; } static function get_edit_form_admin() { diff --git a/modules/gallery/libraries/Admin_View.php b/modules/gallery/libraries/Admin_View.php index cbb781a1..a990e4ca 100644 --- a/modules/gallery/libraries/Admin_View.php +++ b/modules/gallery/libraries/Admin_View.php @@ -27,12 +27,6 @@ class Admin_View_Core extends Gallery_View { * @return void */ public function __construct($name) { - $theme_name = module::get_var("gallery", "active_admin_theme"); - if (!file_exists(THEMEPATH . $theme_name)) { - module::set_var("gallery", "active_admin_theme", "admin_wind"); - theme::load_themes(); - Kohana::log("error", "Unable to locate theme '$theme_name', switching to default theme."); - } parent::__construct($name); $this->theme_name = module::get_var("gallery", "active_admin_theme"); diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index b64deab9..817a46ad 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -29,12 +29,6 @@ class Theme_View_Core extends Gallery_View { * @return void */ public function __construct($name, $page_type, $page_subtype) { - $theme_name = module::get_var("gallery", "active_site_theme"); - if (!file_exists(THEMEPATH . $theme_name)) { - module::set_var("gallery", "active_site_theme", "wind"); - theme::load_themes(); - Kohana::log("error", "Unable to locate theme '$theme_name', switching to default theme."); - } parent::__construct($name); $this->theme_name = module::get_var("gallery", "active_site_theme"); -- cgit v1.2.3 From b677778253cdea89e19befbf04441a741010bfc2 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 28 Nov 2009 15:42:11 -0800 Subject: Expose theme::$is_admin --- modules/gallery/helpers/theme.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'modules') diff --git a/modules/gallery/helpers/theme.php b/modules/gallery/helpers/theme.php index efc9b9e6..19737c0e 100644 --- a/modules/gallery/helpers/theme.php +++ b/modules/gallery/helpers/theme.php @@ -25,6 +25,7 @@ */ class theme_Core { public static $active_theme; + public static $is_admin; /** * Load the active theme. This is called at bootstrap time. We will only ever have one theme @@ -37,14 +38,14 @@ class theme_Core { $path = "/" . $input->get("kohana_uri"); } - $is_admin = $path == "/admin" || !strncmp($path, "/admin/", 7); - $setting_name = $is_admin ? "active_admin_theme" : "active_site_theme"; + self::$is_admin = $path == "/admin" || !strncmp($path, "/admin/", 7); + $setting_name = self::$is_admin ? "active_admin_theme" : "active_site_theme"; if (!(identity::active_user()->admin && $theme_name = $input->get("theme"))) { $theme_name = module::get_var("gallery", $setting_name); if (!file_exists(THEMEPATH . $theme_name)) { Kohana::log("error", "Unable to locate theme '$theme_name', switching to default theme."); - $theme_name = $is_admin ? "admin_wind" : "wind"; + $theme_name = self::$is_admin ? "admin_wind" : "wind"; module::set_var("gallery", $setting_name, $theme_name); } } -- cgit v1.2.3 From f3981bbaa9c9e72d147e164a3decea411b6dd54c Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 28 Nov 2009 23:25:07 -0800 Subject: Rework the theme loading code to allow themes to be treated as Gallery modules, and have an admin subdirectory that is treated like a Kohana module (as distinct from a Gallery module). The main advantage of creating the separate admin subdirectory is that we will not load an admin theme and a site theme at the same time. We'll only load a few specialized bits of the site theme while the admin theme is active. Concrete examples. A site theme named "xxx": - will receive events at themes/xxx/helpers/xxx_event.php - will have working controllers at themes/xxx/controllers/xxx.php If theme xxx has an admin subdir, then in admin mode it: - will receive events at themes/xxx/admin/helpers/xxx_event.php - will have working controllers at themes/xxx/admin/controllers/xxx.php --- modules/gallery/helpers/module.php | 15 +++++++++++++++ modules/gallery/helpers/theme.php | 35 +++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 12 deletions(-) (limited to 'modules') diff --git a/modules/gallery/helpers/module.php b/modules/gallery/helpers/module.php index e6c196ce..50abdaae 100644 --- a/modules/gallery/helpers/module.php +++ b/modules/gallery/helpers/module.php @@ -335,6 +335,21 @@ class module_Core { call_user_func_array(array($class, $function), $args); } } + + // Give the admin theme a chance to respond, if we're in admin mode. + if (theme::$is_admin) { + $class = theme::$admin_theme_name . "_event"; + if (method_exists($class, $function)) { + call_user_func_array(array($class, $function), $args); + } + } + + // Give the site theme a chance to respond as well. It gets a chance even in admin mode, as + // long as the theme has an admin subdir. + $class = theme::$site_theme_name . "_event"; + if (method_exists($class, $function)) { + call_user_func_array(array($class, $function), $args); + } } /** diff --git a/modules/gallery/helpers/theme.php b/modules/gallery/helpers/theme.php index 19737c0e..75b48bcc 100644 --- a/modules/gallery/helpers/theme.php +++ b/modules/gallery/helpers/theme.php @@ -24,7 +24,8 @@ * Note: by design, this class does not do any permission checking. */ class theme_Core { - public static $active_theme; + public static $admin_theme_name; + public static $site_theme_name; public static $is_admin; /** @@ -38,22 +39,32 @@ class theme_Core { $path = "/" . $input->get("kohana_uri"); } + $modules = Kohana::config("core.modules"); self::$is_admin = $path == "/admin" || !strncmp($path, "/admin/", 7); - $setting_name = self::$is_admin ? "active_admin_theme" : "active_site_theme"; - if (!(identity::active_user()->admin && $theme_name = $input->get("theme"))) { - $theme_name = module::get_var("gallery", $setting_name); + self::$site_theme_name = module::get_var("gallery", "active_site_theme"); + if (self::$is_admin) { + // Load the admin theme + self::$admin_theme_name = module::get_var("gallery", "active_admin_theme"); + array_unshift($modules, THEMEPATH . self::$admin_theme_name); - if (!file_exists(THEMEPATH . $theme_name)) { - Kohana::log("error", "Unable to locate theme '$theme_name', switching to default theme."); - $theme_name = self::$is_admin ? "admin_wind" : "wind"; - module::set_var("gallery", $setting_name, $theme_name); + // If the site theme has an admin subdir, load that as a module so that + // themes can provide their own code. + if (file_exists(THEMEPATH . self::$site_theme_name . "/admin")) { + array_unshift($modules, THEMEPATH . self::$site_theme_name . "/admin"); + } + } else { + // Admins can override the site theme, temporarily. This lets us preview themes. + if (identity::active_user()->admin && $override = $input->get("theme")) { + if (file_exists(THEMEPATH . $override)) { + self::$site_theme_name = $override; + } else { + Kohana::log("error", "Missing override theme: '$override'"); + } } + array_unshift($modules, THEMEPATH . self::$site_theme_name); } - $modules = Kohana::config("core.modules"); - array_unshift($modules, THEMEPATH . $theme_name); - Kohana::config_set("core.modules", $modules); - self::$active_theme = $theme_name; + Kohana::config_set("core.modules", $modules); } static function get_edit_form_admin() { -- cgit v1.2.3 From 3d4672ba88e2ef8cb47a9769e94fb3a45bdb3882 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 28 Nov 2009 23:48:38 -0800 Subject: Give the theme a chance to handle theme function callbacks as well. --- modules/gallery/libraries/Theme_View.php | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'modules') diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 817a46ad..f78a7018 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -265,6 +265,13 @@ class Theme_View_Core extends Gallery_View { } } + $helper_class = theme::$site_theme_name . "_theme"; + if (method_exists($helper_class, $function)) { + $blocks[] = call_user_func_array( + array($helper_class, $function), + array_merge(array($this), $args)); + } + if ($function == "head") { array_unshift($blocks, $this->combine_files($this->css, "css")); array_unshift($blocks, $this->combine_files($this->scripts, "javascript")); -- cgit v1.2.3