From dbeadc1407293d0c7af36723db6fe5699890b845 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 22 Jul 2009 14:27:57 -0700 Subject: Use the Kohana cascading filesystem to locate resources loaded by the theme. Because the theme comes first, this means that themes can override any module resources, at the cost that we no longer have namespacing for JS and CSS files. The only file getting used outside of this model is themes/default/screen.css which is used in the admin theme. I fixed that by copying screen.css into admin_default and renaming its screen.css to admin_screen.css. I also copied over all the images that it was referencing. Fixes tickets #48 and #539. Theme API changes: - theme_script(), theme_url() and theme_css() are no longer needed - script(), url() and css() now refer to the first matching asset in the module load path, where gallery3/lib is at the end of the path --- modules/user/helpers/user_theme.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/user/helpers/user_theme.php') diff --git a/modules/user/helpers/user_theme.php b/modules/user/helpers/user_theme.php index ad9d4c63..c5351f8e 100644 --- a/modules/user/helpers/user_theme.php +++ b/modules/user/helpers/user_theme.php @@ -26,7 +26,7 @@ class user_theme_Core { static function admin_head($theme) { if (strpos(Router::$current_uri, "admin/users") !== false) { - $theme->script("lib/gallery.panel.js"); + $theme->script("gallery.panel.js"); } } } -- cgit v1.2.3 From b826182b7a7b2d630b478d3e2bcf0628989a92d9 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 6 Aug 2009 22:22:24 +0800 Subject: Cleanup the javascript for enabling dialog or panel links. 1) We don't need to loop over the elements to bind the event handler. 2) Just include gallery.panel.js for all the admin pages. Signed-off-by: Tim Almdal --- modules/gallery/helpers/gallery_theme.php | 1 + modules/user/helpers/user_theme.php | 6 ------ themes/admin_default/js/ui.init.js | 12 +++--------- themes/default/js/ui.init.js | 5 +---- 4 files changed, 5 insertions(+), 19 deletions(-) (limited to 'modules/user/helpers/user_theme.php') diff --git a/modules/gallery/helpers/gallery_theme.php b/modules/gallery/helpers/gallery_theme.php index d3751b80..903827cc 100644 --- a/modules/gallery/helpers/gallery_theme.php +++ b/modules/gallery/helpers/gallery_theme.php @@ -78,6 +78,7 @@ class gallery_theme_Core { } static function admin_head($theme) { + $theme->script("gallery.panel.js"); $session = Session::instance(); if ($session->get("debug")) { $theme->css("debug.css"); diff --git a/modules/user/helpers/user_theme.php b/modules/user/helpers/user_theme.php index c5351f8e..69042aed 100644 --- a/modules/user/helpers/user_theme.php +++ b/modules/user/helpers/user_theme.php @@ -23,10 +23,4 @@ class user_theme_Core { $view->user = user::active(); return $view->render(); } - - static function admin_head($theme) { - if (strpos(Router::$current_uri, "admin/users") !== false) { - $theme->script("gallery.panel.js"); - } - } } diff --git a/themes/admin_default/js/ui.init.js b/themes/admin_default/js/ui.init.js index 3f062a27..06cc1cd5 100644 --- a/themes/admin_default/js/ui.init.js +++ b/themes/admin_default/js/ui.init.js @@ -1,5 +1,5 @@ $(document).ready(function(){ - + // Initialize Superfish menus $("#gSiteAdminMenu ul.gMenu").addClass("sf-menu"); $("ul.gMenu").addClass("sf-menu"); @@ -18,16 +18,10 @@ $(document).ready(function(){ $("#gMessage li").showMessage(); // Initialize modal dialogs - var dialogLinks = $(".gDialogLink"); - for (var i=0; i < dialogLinks.length; i++) { - $(dialogLinks[i]).bind("click", handleDialogEvent); - } + $(".gDialogLink").bind("click", handleDialogEvent); // Initialize panels - var panelLinks = $(".gPanelLink"); - for (i=0; i Date: Thu, 6 Aug 2009 13:32:41 +0800 Subject: Don't show the ajaxy login link in the top right corner if we're on the login page itself. Signed-off-by: Bharat Mediratta --- modules/user/helpers/user_theme.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'modules/user/helpers/user_theme.php') diff --git a/modules/user/helpers/user_theme.php b/modules/user/helpers/user_theme.php index 69042aed..8de2d248 100644 --- a/modules/user/helpers/user_theme.php +++ b/modules/user/helpers/user_theme.php @@ -19,8 +19,10 @@ */ class user_theme_Core { static function header_top($theme) { - $view = new View("login.html"); - $view->user = user::active(); - return $view->render(); + if ($theme->page_type != "login") { + $view = new View("login.html"); + $view->user = user::active(); + return $view->render(); + } } } -- cgit v1.2.3 From 001623c755777846a468255d4396d30b253dcdfb Mon Sep 17 00:00:00 2001 From: Andy Staudacher Date: Fri, 4 Sep 2009 11:06:20 -0700 Subject: Add new locale preferences: Adding per session (cookie) locale preferences and check the browser's / OS' locale preferences. Ticket 582. --- modules/gallery/helpers/locales.php | 85 +++++++++++++++++++++++++++++++++++++ modules/user/helpers/user.php | 18 ++++++++ modules/user/helpers/user_event.php | 24 ++++++++--- modules/user/helpers/user_theme.php | 24 +++++++++++ 4 files changed, 145 insertions(+), 6 deletions(-) (limited to 'modules/user/helpers/user_theme.php') diff --git a/modules/gallery/helpers/locales.php b/modules/gallery/helpers/locales.php index 3762b97b..1320c155 100644 --- a/modules/gallery/helpers/locales.php +++ b/modules/gallery/helpers/locales.php @@ -23,6 +23,7 @@ */ class locales_Core { private static $locales; + private static $language_subtag_to_locale; /** * Return the list of available locales. @@ -105,6 +106,16 @@ class locales_Core { $l["zh_TW"] = "繁體中文"; // Chinese (TW) asort($l, SORT_LOCALE_STRING); self::$locales = $l; + + // Language subtag to (default) locale mapping + foreach ($l as $locale => $name) { + list ($language) = explode("_", $locale . "_"); + // The first one mentioned is the default + if (!isset($d[$language])) { + $d[$language] = $locale; + } + } + self::$language_subtag_to_locale = $d; } static function display_name($locale=null) { @@ -121,4 +132,78 @@ class locales_Core { list ($language, $territory) = explode('_', $locale . "_"); return in_array($language, array("he", "fa", "ar")); } + + /** + * Returns the best match comparing the HTTP accept-language header + * with the installed locales. + */ + static function locale_from_http_request() { + $http_accept_language = Input::instance()->server("HTTP_ACCEPT_LANGUAGE"); + if ($http_accept_language) { + // Parse the HTTP header and build a preference list + // Example value: "de,en-us;q=0.7,en-uk,fr-fr;q=0.2" + $locale_preferences = array(); + foreach (explode(",", $http_accept_language) as $code) { + list ($requested_locale, $qvalue) = explode(";", $code . ";"); + $requested_locale = trim($requested_locale); + $qvalue = trim($qvalue); + if (preg_match("/^([a-z]{2,3})(?:[_-]([a-zA-Z]{2}))?/", $requested_locale, $matches)) { + $requested_locale = strtolower($matches[1]); + if (!empty($matches[2])) { + $requested_locale .= "_" . strtoupper($matches[2]); + } + $requested_locale = trim(str_replace("-", "_", $requested_locale)); + if (!strlen($qvalue)) { + // If not specified, default to 1. + $qvalue = 1; + } else { + // qvalue is expected to be something like "q=0.7" + list ($ignored, $qvalue) = explode("=", $qvalue . "=="); + $qvalue = floatval($qvalue); + } + $locale_preferences[] = array($requested_locale, $qvalue); + } + } + + // Compare and score requested locales with installed ones + $matched_locales = array(); + foreach ($locale_preferences as $requested_value) { + $scored_locale_match = self::_locale_match_score($requested_value); + if ($scored_locale_match) { + $scored_locales[] = $scored_locale_match; + } + } + + usort($matched_locales, array("locales", "_compare_locale_by_qvalue")); + + $best_match = array_shift($scored_locales); + if ($best_match) { + return $best_match[0]; + } + } + + return null; + } + + static function _compare_locale_by_qvalue($a, $b) { + $a = $a[1]; + $b = $b[1]; + if ($a == $b) { + return 0; + } + return $a < $b ? 1 : -1; + } + + private static function _locale_match_score($requested_locale_and_qvalue) { + list ($requested_locale, $qvalue) = $requested_locale_and_qvalue; + $installed = self::installed(); + if (isset($installed[$requested_locale])) { + return $requested_locale_and_qvalue; + } + list ($language) = explode("_", $requested_locale . "_"); + if (isset(self::$language_subtag_to_locale[$language])) { + return array(self::$language_subtag_to_locale[$language], $qvalue * 0.66); + } + return null; + } } \ No newline at end of file diff --git a/modules/user/helpers/user.php b/modules/user/helpers/user.php index 40acc2ec..b9162b92 100644 --- a/modules/user/helpers/user.php +++ b/modules/user/helpers/user.php @@ -87,6 +87,9 @@ class user_Core { private static function _add_locale_dropdown(&$form, $user=null) { $locales = locales::installed(); + foreach ($locales as $locale => $display_name) { + $locales[$locale] = SafeString::of_safe_html($display_name); + } if (count($locales) > 1) { // Put "none" at the first position in the array $locales = array_merge(array("" => t("« none »")), $locales); @@ -336,4 +339,19 @@ class user_Core { } return $salt . md5($salt . $password); } + + static function cookie_locale() { + $cookie_data = Input::instance()->cookie("g_locale"); + $locale = null; + if ($cookie_data) { + if (preg_match("/^([a-z]{2,3}(?:_[A-Z]{2})?)$/", trim($cookie_data), $matches)) { + $requested_locale = $matches[1]; + $installed_locales = locales::installed(); + if (isset($installed_locales[$requested_locale])) { + $locale = $requested_locale; + } + } + } + return $locale; + } } \ No newline at end of file diff --git a/modules/user/helpers/user_event.php b/modules/user/helpers/user_event.php index 4bde224b..ede4e515 100644 --- a/modules/user/helpers/user_event.php +++ b/modules/user/helpers/user_event.php @@ -23,12 +23,7 @@ class user_event_Core { */ static function gallery_ready() { user::load_user(); - - $locale = user::active()->locale; - if (!empty($locale)) { - // TODO(andy_st): Check session data as well. - I18n::instance()->locale($locale); - } + self::set_request_locale(); } static function admin_menu($menu, $theme) { @@ -38,4 +33,21 @@ class user_event_Core { ->label(t("Users/Groups")) ->url(url::site("admin/users"))); } + + static function set_request_locale() { + // 1. Check the session specific preference (cookie) + $locale = user::cookie_locale(); + // 2. Check the user's preference + if (!$locale) { + $locale = user::active()->locale; + } + // 3. Check the browser's / OS' preference + if (!$locale) { + $locale = locales::locale_from_http_request(); + } + // If we have any preference, override the site's default locale + if ($locale) { + I18n::instance()->locale($locale); + } + } } diff --git a/modules/user/helpers/user_theme.php b/modules/user/helpers/user_theme.php index 8de2d248..5b973ef3 100644 --- a/modules/user/helpers/user_theme.php +++ b/modules/user/helpers/user_theme.php @@ -18,6 +18,14 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class user_theme_Core { + static function head($theme) { + if (count(locales::installed())) { + // Needed by the languages block + $theme->script("jquery.cookie.js"); + } + return ""; + } + static function header_top($theme) { if ($theme->page_type != "login") { $view = new View("login.html"); @@ -25,4 +33,20 @@ class user_theme_Core { return $view->render(); } } + + static function sidebar_blocks($theme) { + $locales = locales::installed(); + foreach ($locales as $locale => $display_name) { + $locales[$locale] = SafeString::of_safe_html($display_name); + } + if (count($locales) > 1) { + $block = new Block(); + $block->css_id = "gUserLanguageBlock"; + $block->title = t("Select Language Preference"); + $block->content = new View("user_languages_block.html"); + $block->content->installed_locales = array_merge(array("" => t("« none »")), $locales); + $block->content->selected = (string) user::cookie_locale(); + return $block; + } + } } -- cgit v1.2.3 From a25640f9b26c75e14109f07fcc4863adc300f062 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Fri, 4 Sep 2009 20:28:46 -0700 Subject: Shorten the name in the block so that it doesn't wrap. Use « and » in the << none >> text. --- modules/user/helpers/user_theme.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'modules/user/helpers/user_theme.php') diff --git a/modules/user/helpers/user_theme.php b/modules/user/helpers/user_theme.php index 5b973ef3..184a2bc9 100644 --- a/modules/user/helpers/user_theme.php +++ b/modules/user/helpers/user_theme.php @@ -42,9 +42,10 @@ class user_theme_Core { if (count($locales) > 1) { $block = new Block(); $block->css_id = "gUserLanguageBlock"; - $block->title = t("Select Language Preference"); + $block->title = t("Language Preference"); $block->content = new View("user_languages_block.html"); - $block->content->installed_locales = array_merge(array("" => t("« none »")), $locales); + $block->content->installed_locales = + array_merge(array("" => t("« none »")), $locales); $block->content->selected = (string) user::cookie_locale(); return $block; } -- cgit v1.2.3 From beb232a1cc6f081b432f32a72bddadcaf3f90df4 Mon Sep 17 00:00:00 2001 From: Andy Staudacher Date: Sat, 5 Sep 2009 17:43:47 -0700 Subject: Revert previous edit. Prefer « none » (sweet, nice Unicode characters) instead of HTML entities. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/user/helpers/user_theme.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/user/helpers/user_theme.php') diff --git a/modules/user/helpers/user_theme.php b/modules/user/helpers/user_theme.php index 184a2bc9..098d87fd 100644 --- a/modules/user/helpers/user_theme.php +++ b/modules/user/helpers/user_theme.php @@ -45,7 +45,7 @@ class user_theme_Core { $block->title = t("Language Preference"); $block->content = new View("user_languages_block.html"); $block->content->installed_locales = - array_merge(array("" => t("« none »")), $locales); + array_merge(array("" => t("« none »")), $locales); $block->content->selected = (string) user::cookie_locale(); return $block; } -- cgit v1.2.3