From 4b31ecdaace925f8f84921fd3d4f091d3a098be6 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 26 Apr 2017 10:09:57 -0500 Subject: [PATCH] Update: implement custom language support in regular paths in the same way internal paths implement custom languages --- common/base/classes/base_path.php | 116 +++++++++++++----- program/reservation/internal/ja/access_denied.php | 4 + program/reservation/internal/ja/bad_method.php | 4 + program/reservation/internal/ja/not_found.php | 4 + program/reservation/internal/ja/server_error.php | 4 + program/reservation/paths/u/dashboard.php | 8 +- program/reservation/paths/u/ja/dashboard.php | 54 +++++++++ program/reservation/paths/u/ja/login.php | 28 +++++ program/reservation/paths/u/ja/logout.php | 26 ++++ program/reservation/paths/u/login.php | 1 + program/reservation/paths/u/logout.php | 1 + program/reservation/reservation_paths.php | 139 +++++++++++++++------- 12 files changed, 313 insertions(+), 76 deletions(-) create mode 100644 program/reservation/paths/u/ja/dashboard.php create mode 100644 program/reservation/paths/u/ja/login.php create mode 100644 program/reservation/paths/u/ja/logout.php diff --git a/common/base/classes/base_path.php b/common/base/classes/base_path.php index c3cde25..87380ec 100644 --- a/common/base/classes/base_path.php +++ b/common/base/classes/base_path.php @@ -84,7 +84,8 @@ class c_base_path extends c_base_rfc_string { protected $date_changed = NULL; protected $date_locked = NULL; - protected $include = NULL; + protected $include_directory = NULL; + protected $include_name = NULL; /** * Class constructor. @@ -109,7 +110,8 @@ class c_base_path extends c_base_rfc_string { $this->date_changed = NULL; $this->date_locked = NULL; - $this->include = NULL; + $this->include_directory = NULL; + $this->include_name = NULL; } /** @@ -132,7 +134,8 @@ class c_base_path extends c_base_rfc_string { unset($this->date_changed); unset($this->date_locked); - unset($this->include); + unset($this->include_directory); + unset($this->include_name); parent::__destruct(); } @@ -682,21 +685,44 @@ class c_base_path extends c_base_rfc_string { } /** - * Assign an include file needed to process this path. + * Assign an include path directory needed to process this path. * - * @param string $path + * This is the prefix part of the path. + * + * @param string|null $directory * A path to a file that may be found via the PHP search path. * * @return c_base_return_status * TRUE on success, FALSE otherwise. */ - public function set_include($path) { - if (!is_string($path)) { - $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_name' => 'path', ':function_name' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); + public function set_include_directory($directory) { + if (!is_string($directory) && !is_null($directory)) { + $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_directory' => 'directory', ':function_directory' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $this->include_directory = $directory; + return new c_base_return_true(); + } + + /** + * Assign an include path name needed to process this path. + * + * This is the suffix part of the path. + * + * @param string|null $path + * A path to a file that may be found via the PHP search path. + * + * @return c_base_return_status + * TRUE on success, FALSE otherwise. + */ + public function set_include_name($name) { + if (!is_string($name) && !is_null($name)) { + $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_name' => 'name', ':function_name' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); return c_base_return_error::s_false($error); } - $this->include = $path; + $this->include_name = $name; return new c_base_return_true(); } @@ -892,19 +918,39 @@ class c_base_path extends c_base_rfc_string { } /** - * Get the assigned include path. + * Get the assigned include path directory. + * + * This is the prefix part of the path. * * @return c_base_return_string|c_base_return_null * Include path string on success. * NULL is returned if the include path is not assigned. * Error bit is set on error. */ - public function get_include() { - if (!is_string($this->include)) { + public function get_include_directory() { + if (!is_string($this->include_directory)) { return new c_base_return_null(); } - return c_base_return_string::s_new($this->include); + return c_base_return_string::s_new($this->include_directory); + } + + /** + * Get the assigned include path name. + * + * This is the suffix part of the path. + * + * @return c_base_return_string|c_base_return_null + * Include path string on success. + * NULL is returned if the include path is not assigned. + * Error bit is set on error. + */ + public function get_include_name() { + if (!is_string($this->include_name)) { + return new c_base_return_null(); + } + + return c_base_return_string::s_new($this->include_name); } /** @@ -924,13 +970,13 @@ class c_base_path extends c_base_rfc_string { * An executed array object with error bit set is returned on error. */ public function do_execute(&$http, &$database, &$session, $settings = array()) { - if (!($database instanceof c_base_database)) { - $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_name' => 'database', ':function_name' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); + if (!($http instanceof c_base_http)) { + $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_name' => 'http', ':function_name' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); return c_base_return_error::s_value(array(), 'c_base_path_executed', $error); } - if (!($http instanceof c_base_http)) { - $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_name' => 'http', ':function_name' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); + if (!($database instanceof c_base_database)) { + $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_name' => 'database', ':function_name' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); return c_base_return_error::s_value(array(), 'c_base_path_executed', $error); } @@ -1210,18 +1256,24 @@ class c_base_paths extends c_base_return { * * @todo: should redirect and alias booleans be added as parameters? * + * @pram string $directory + * The first part of the file path * @pram string $path * The url path in which the handler applies to. * @param string $handler * The name of an implementation of c_base_path. - * @param string|null $include - * (optional) The file path (relative to the PHP includes) to include that contains the requested path. + * @param string|null $directory + * (optional) The prefix path (relative to the PHP includes) to include that contains the requested path. + * When not NULL, both $directory and $name must not be NULL. + * @param string|null $name + * (optional) The suffix path (relative to the PHP includes) to include that contains the requested path. + * When not NULL, both $directory and $name must not be NULL. * * @return c_base_return_status * TRUE is returned on success. * FALSE with error bit set is returned on error. */ - public function set_path($path, $handler, $include = NULL) { + public function set_path($path, $handler, $include_directory = NULL, $include_name = NULL) { if (!is_string($path)) { $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_name' => 'path', ':function_name' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); return c_base_return_error::s_false($error); @@ -1232,13 +1284,18 @@ class c_base_paths extends c_base_return { return c_base_return_error::s_false($error); } - if (!is_null($include) && !is_string($include)) { - $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_name' => 'include', ':function_name' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); + if ((!is_null($include_directory) || (is_null($include_directory) && !is_null($include_name))) && !is_string($include_directory)) { + $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_name' => 'include_directory', ':function_name' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if ((!is_null($include_name) || (is_null($include_name) && !is_null($include_directory))) && !is_string($include_name)) { + $error = c_base_error::s_log(NULL, array('arguments' => array(':argument_name' => 'include_name', ':function_name' => __CLASS__ . '->' . __FUNCTION__)), i_base_error_messages::INVALID_ARGUMENT); return c_base_return_error::s_false($error); } if (mb_strlen($path) == 0) { - $this->root = array('handler' => $handler, 'include' => $include, 'is_root' => TRUE); + $this->root = array('handler' => $handler, 'include_directory' => $include_directory, 'include_name' => $include_name, 'is_root' => TRUE); return new c_base_return_true(); } @@ -1288,7 +1345,8 @@ class c_base_paths extends c_base_return { $depth_total = count($path_parts); foreach ($path_parts as $path_part) { if ($depth_current == $depth_total) { - $path_tree['include'] = $include; + $path_tree['include_directory'] = $include_directory; + $path_tree['include_name'] = $include_name; $path_tree['handler'] = $handler; break; } @@ -1296,7 +1354,8 @@ class c_base_paths extends c_base_return { if (!isset($path_tree['paths'][$path_part])) { $path_tree['paths'][$path_part] = array( 'paths' => array(), - 'include' => NULL, + 'include_directory' => NULL, + 'include_name' => NULL, 'handler' => NULL, ); } @@ -1322,7 +1381,8 @@ class c_base_paths extends c_base_return { * * @return c_base_return_array|c_base_return_int|c_base_return_null * An array containing: - * - 'include': the file to include that contains the handler class implementation. + * - 'include_directory': the prefix path of the file to include that contains the handler class implementation. + * - 'include_name': the suffix path of the file to include that contains the handler class implementation. * - 'handler': the name of the handler class. * - 'redirect': if specified, then a redirect path (instead of include/handler). * - 'code': if redirect is specified, then the http response code associated with the redirect. @@ -1388,7 +1448,7 @@ class c_base_paths extends c_base_return { foreach ($path_parts as $path_part) { if ($depth_current == $depth_total) { if (isset($path_tree['handler'])) { - $found = array('include' => $path_tree['include'], 'handler' => $path_tree['handler']); + $found = array('include_directory' => $path_tree['include_directory'], 'include_name' => $path_tree['include_name'], 'handler' => $path_tree['handler']); break; } } @@ -1396,7 +1456,7 @@ class c_base_paths extends c_base_return { if (!isset($path_tree['paths'][$path_part])) { if ($depth_current == $depth_total) { if (isset($path_tree['handler'])) { - $found = array('include' => $path_tree['include'], 'handler' => $path_tree['handler']); + $found = array('include_directory' => $path_tree['include_directory'], 'include_name' => $path_tree['include_name'], 'handler' => $path_tree['handler']); break; } } diff --git a/program/reservation/internal/ja/access_denied.php b/program/reservation/internal/ja/access_denied.php index b197b8c..f7914d0 100644 --- a/program/reservation/internal/ja/access_denied.php +++ b/program/reservation/internal/ja/access_denied.php @@ -4,7 +4,11 @@ * Provides path handler for the access denied pages. */ +/** + * Implements c_reservation_path_access_denied(). + */ final class c_reservation_path_access_denied_ja extends c_reservation_path_access_denied { + /** * Implements pr_get_title(). */ diff --git a/program/reservation/internal/ja/bad_method.php b/program/reservation/internal/ja/bad_method.php index 07dad1c..1850130 100644 --- a/program/reservation/internal/ja/bad_method.php +++ b/program/reservation/internal/ja/bad_method.php @@ -4,7 +4,11 @@ * Provides path handler for the not found pages. */ +/** + * Implements c_reservation_path_bad_method(). + */ final class c_reservation_path_bad_method_ja extends c_reservation_path_bad_method { + /** * Implements pr_get_title(). */ diff --git a/program/reservation/internal/ja/not_found.php b/program/reservation/internal/ja/not_found.php index f3a24cb..51865ff 100644 --- a/program/reservation/internal/ja/not_found.php +++ b/program/reservation/internal/ja/not_found.php @@ -4,7 +4,11 @@ * Provides path handler for the not found pages. */ +/** + * Implements c_reservation_path_not_found(). + */ final class c_reservation_path_not_found_ja extends c_reservation_path_not_found { + /** * Implements pr_get_title(). */ diff --git a/program/reservation/internal/ja/server_error.php b/program/reservation/internal/ja/server_error.php index a2271b2..13e778a 100644 --- a/program/reservation/internal/ja/server_error.php +++ b/program/reservation/internal/ja/server_error.php @@ -4,7 +4,11 @@ * Provides path handler for the server error pages. */ +/** + * Implements c_reservation_path_server_error(). + */ final class c_reservation_path_server_error_ja extends c_reservation_path_server_error { + /** * Implements pr_get_title(). */ diff --git a/program/reservation/paths/u/dashboard.php b/program/reservation/paths/u/dashboard.php index 1e48187..e3811d9 100644 --- a/program/reservation/paths/u/dashboard.php +++ b/program/reservation/paths/u/dashboard.php @@ -7,13 +7,11 @@ require_once('common/base/classes/base_error.php'); require_once('common/base/classes/base_return.php'); require_once('common/base/classes/base_path.php'); -require_once('common/base/classes/base_html.php'); -require_once('common/base/classes/base_cookie.php'); -require_once('common/base/classes/base_session.php'); require_once('common/theme/classes/theme_html.php'); class c_reservation_path_user_dashboard extends c_base_path { + /** * Implements do_execute(). */ @@ -48,7 +46,7 @@ class c_reservation_path_user_dashboard extends c_base_path { unset($roles_object); $tag = c_theme_html::s_create_tag(c_base_markup_tag::TYPE_DIVIDER); - $tag->set_text($this->pr_get_text(2) . $settings['database_user']); + $tag->set_text($this->pr_get_text(2) . ' ' . $settings['database_user']); $wrapper->set_tag($tag); unset($tag); @@ -132,7 +130,7 @@ class c_reservation_path_user_dashboard extends c_base_path { case 1: return 'All links will go here.'; case 2: - return 'You are currently logged in as: '; + return 'You are currently logged in as:'; case 3: return 'You are currently assigned the following roles:'; case 4: diff --git a/program/reservation/paths/u/ja/dashboard.php b/program/reservation/paths/u/ja/dashboard.php new file mode 100644 index 0000000..8af1d4a --- /dev/null +++ b/program/reservation/paths/u/ja/dashboard.php @@ -0,0 +1,54 @@ +session = NULL; $this->output = NULL; $this->logged_in = NULL; - $this->paths = NULL; - $this->path = NULL; + + $this->paths = NULL; + $this->path = NULL; + + $this->alias = NULL; } /** @@ -64,8 +71,11 @@ class c_reservation_paths { unset($this->session); unset($this->output); unset($this->logged_in); + unset($this->paths); unset($this->path); + + unset($this->alias); } /** @@ -122,6 +132,7 @@ class c_reservation_paths { $this->output = NULL; $this->logged_in = $logged_in; + $this->p_get_language_alias(); // require HTTPS for access to any part of this website. if (!isset($_SERVER["HTTPS"])) { @@ -183,20 +194,50 @@ class c_reservation_paths { return $redirect->do_execute($this->http, $this->database, $this->session, $this->settings); } else { - if (!empty($handler_settings['include']) && is_string($handler_settings['include'])) { - require_once($handler_settings['include']); + if (!empty($handler_settings['include_name']) && is_string($handler_settings['include_name'])) { + require_once($handler_settings['include_directory'] . $handler_settings['include_name']); + } + + // execute path handler, using custom-language if defined. + if (empty($handler_settings['handler'])) { + $server_error = $this->p_get_path_server_error(); + return $server_error->do_execute($this->http, $this->database, $this->session, $this->settings); } + elseif (is_string($this->alias)) { + @include_once($handler_settings['include_directory'] . $this->alias . '/' . $handler_settings['include_name']); - if (empty($handler_settings['handler']) || !class_exists($handler_settings['handler'])) { - $not_found = $this->p_get_path_server_error(); - return $not_found->do_execute($this->http, $this->database, $this->session, $this->settings); + $handler_class = $handler_settings['handler'] . '_' . $this->alias; + if (class_exists($handler_class)) { + $this->path = new $handler_class(); + + unset($handler_class); + } + else { + unset($handler_class); + + // attempt to fallback to default handler if the language-specific handler class is not found. + if (!class_exists($handler_settings['handler'])) { + $server_error = $this->p_get_path_server_error(); + return $server_error->do_execute($this->http, $this->database, $this->session, $this->settings); + } + else { + $this->path = new $handler_settings['handler'](); + } + } } else { - $this->path = new $handler_settings['handler'](); - if (isset($handler_settings['is_root']) && $handler_settings['is_root']) { - $this->path->set_is_root(TRUE); + if (class_exists($handler_settings['handler'])) { + $this->path = new $handler_settings['handler'](); + } + else { + $server_error = $this->p_get_path_server_error(); + return $server_error->do_execute($this->http, $this->database, $this->session, $this->settings); } } + + if (isset($handler_settings['is_root']) && $handler_settings['is_root']) { + $this->path->set_is_root(TRUE); + } } unset($handler_settings); @@ -236,14 +277,14 @@ class c_reservation_paths { $this->paths = new c_base_paths(); // set root path to be the user dashboard. - $this->paths->set_path('', 'c_reservation_path_user_dashboard', 'program/reservation/paths/u/dashboard.php'); + $this->paths->set_path('', 'c_reservation_path_user_dashboard', 'program/reservation/paths/u/', 'dashboard.php'); // create login/logout paths - $this->paths->set_path('/u/login', 'c_reservation_path_user_login', 'program/reservation/login.php'); - $this->paths->set_path('/u/logout', 'c_reservation_path_user_logout', 'program/reservation/logout.php'); + $this->paths->set_path('/u/login', 'c_reservation_path_user_login', 'program/reservation/', 'login.php'); + $this->paths->set_path('/u/logout', 'c_reservation_path_user_logout', 'program/reservation/', 'logout.php'); // user dashboard - $this->paths->set_path('/u/dashboard', 'c_reservation_path_user_dashboard', 'program/reservation/paths/u/dashboard.php'); + $this->paths->set_path('/u/dashboard', 'c_reservation_path_user_dashboard', 'program/reservation/paths/u/', 'dashboard.php'); } /** @@ -391,6 +432,43 @@ class c_reservation_paths { } /** + * Load and save the current preferred language alias. + * + * This will be stored in $this->alias. + */ + private function p_get_language_alias() { + $aliases = array(); + $languages = $this->http->get_response_content_language()->get_value_exact(); + if (is_array($languages) && !empty($languages)) { + $language = reset($languages); + + // us-english is the default, so do not attempt to include any external files. + if ($language == i_base_language::ENGLISH_US || $language == i_base_language::ENGLISH) { + unset($language); + unset($aliases); + unset($languages); + + $this->alias = NULL; + return; + } + + $aliases = c_base_defaults_global::s_get_languages()::s_get_aliases_by_id($language)->get_value_exact(); + } + unset($language); + + // use default if no aliases are found. + if (empty($aliases)) { + unset($aliases); + unset($languages); + + $this->alias = NULL; + return; + } + + $this->alias = end($aliases); + } + + /** * Will include a custom language path if one exists. * * The default language files ends in "${path}${name}.php". @@ -415,43 +493,18 @@ class c_reservation_paths { private function p_include_path($path, $name, $class) { require_once($path . $name . '.php'); - $aliases = array(); - $languages = $this->http->get_response_content_language()->get_value_exact(); - if (is_array($languages) && !empty($languages)) { - $language = reset($languages); - - // us-english is the default, so do not attempt to include any external files. - if ($language == i_base_language::ENGLISH_US || $language == i_base_language::ENGLISH) { - unset($language); - unset($aliases); - unset($languages); - return new $class(); - } - - $aliases = c_base_defaults_global::s_get_languages()::s_get_aliases_by_id($language)->get_value_exact(); - } - unset($language); - // use default if no aliases are found. - if (empty($aliases)) { - unset($aliases); - unset($languages); + if (is_null($this->alias)) { return new $class(); } - $alias = end($aliases); - unset($aliases); - // use include_once instead of require_require to allow for failsafe behavior. - @include_once($path . $alias . '/' . $name . '.php'); + @include_once($path . $this->alias . '/' . $name . '.php'); - $language_class = $class . '_' . $alias; + $language_class = $class . '_' . $this->alias; if (class_exists($language_class)) { - unset($alias); - return new $language_class(); } - unset($alias); unset($language_class); // if unable to find, fallback to original class -- 1.8.3.1