From cbcb9da2ee30b8b5b4395865312934b1ca3c8ffa Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Sat, 27 Jan 2018 12:23:43 +0100 Subject: [PATCH] MVC/templates, replace javascript_include_when_exists() for more universal theme_file_or_default() template function. The legacy code will still use a plain file_exists() for the optional theme.js, which keeps both in a working state after this commit. To be able to move more files into the template directories for https://github.com/opnsense/core/pull/2124 we need to keep existing plugins functional, so we could integrate these changes into a minor release. --- src/opnsense/mvc/app/config/services.php | 26 +++++++++++++++---- .../mvc/app/views/layouts/default.volt | 3 +-- src/opnsense/www/js/theme.js | 1 + 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 src/opnsense/www/js/theme.js diff --git a/src/opnsense/mvc/app/config/services.php b/src/opnsense/mvc/app/config/services.php index 4395400a8..15c8bb68c 100644 --- a/src/opnsense/mvc/app/config/services.php +++ b/src/opnsense/mvc/app/config/services.php @@ -9,6 +9,26 @@ use Phalcon\Session\Adapter\Files as SessionAdapter; use OPNsense\Core\Config; use OPNsense\Core\Routing; +/** + * search for a themed filename or return distribution standard + * @param string $url relative url + * @param array $theme theme name + * @return string + */ +function view_fetch_themed_filename($url, $theme) { + $search_pattern = array( + "/themes/{$theme}/build/", + "/" + ); + foreach ($search_pattern as $pattern) { + $filename = "/usr/local/opnsense/www{$pattern}{$url}"; + if (file_exists($filename)) { + return str_replace("//", "/", "/ui{$pattern}{$url}"); + } + } + return $url; // not found, return source +} + /** * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework */ @@ -43,11 +63,7 @@ $di->set('view', function () use ($config) { 'compiledSeparator' => '_' )); // register additional volt template functions - $volt->getCompiler()->addFunction('javascript_include_when_exists', function ($local_url) { - $chk_path = "str_replace('/ui/','/usr/local/opnsense/www/',".$local_url.")"; - $js_tag = "''"; - return "file_exists(".$chk_path.") ? ".$js_tag." :''"; - }); + $volt->getCompiler()->addFunction('theme_file_or_default', view_fetch_themed_filename); return $volt; }, diff --git a/src/opnsense/mvc/app/views/layouts/default.volt b/src/opnsense/mvc/app/views/layouts/default.volt index 21a5dc005..ffd60fdf5 100644 --- a/src/opnsense/mvc/app/views/layouts/default.volt +++ b/src/opnsense/mvc/app/views/layouts/default.volt @@ -236,8 +236,7 @@ - {{javascript_include_when_exists('/ui/themes/' ~ theme_name ~ '/build/js/theme.js')}} - +
diff --git a/src/opnsense/www/js/theme.js b/src/opnsense/www/js/theme.js new file mode 100644 index 000000000..57bb89fe2 --- /dev/null +++ b/src/opnsense/www/js/theme.js @@ -0,0 +1 @@ +/* Empty placeholder for theme javascript override */ \ No newline at end of file