From 19f7e35e006a01d17ecd6b4835203d7dcf986144 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 9 Jul 2014 17:26:13 +0100 Subject: [PATCH] CRM-14949 - Switch dynamic js to a file instead of a callback --- CRM/Admin/Page/AJAX.php | 8 ++++++- CRM/Core/Page/AJAX.php | 9 +------- CRM/Core/Resources.php | 21 +++++++++++++---- CRM/Core/xml/Menu/Misc.xml | 4 ---- CRM/Utils/File.php | 47 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 17 deletions(-) diff --git a/CRM/Admin/Page/AJAX.php b/CRM/Admin/Page/AJAX.php index 844f614417..68b71b1a31 100644 --- a/CRM/Admin/Page/AJAX.php +++ b/CRM/Admin/Page/AJAX.php @@ -45,7 +45,13 @@ class CRM_Admin_Page_AJAX { static function getNavigationMenu() { $contactID = CRM_Core_Session::singleton()->get('userID'); if ($contactID) { - CRM_Core_Page_AJAX::returnDynamicJS('CRM/common/navigation.js.tpl', array( + // Set headers to encourage browsers to cache for a long time + $year = 60*60*24*364; + header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $year)); + header('Content-Type: application/javascript'); + header("Cache-Control: max-age=$year, public"); + + print CRM_Core_Page_AJAX::returnDynamicJS('CRM/common/navigation.js.tpl', array( 'navigation' => CRM_Core_BAO_Navigation::createNavigation($contactID), )); } diff --git a/CRM/Core/Page/AJAX.php b/CRM/Core/Page/AJAX.php index dbbd257cb0..4de7604307 100644 --- a/CRM/Core/Page/AJAX.php +++ b/CRM/Core/Page/AJAX.php @@ -205,19 +205,12 @@ class CRM_Core_Page_AJAX { * @param array $vars - template variables */ static function returnDynamicJS($tplFile, $vars = array()) { - // Set headers to encourage browsers to cache for a long time - $year = 60*60*24*364; - header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $year)); - header('Content-Type: application/javascript'); - header("Cache-Control: max-age=$year, public"); - $smarty = CRM_Core_Smarty::singleton(); $vars += array('timeGenerated' => date('d M Y H:i:s')); foreach ($vars as $name => $val) { $smarty->assign($name, $val); } - print $smarty->fetch($tplFile); - CRM_Utils_System::civiExit(); + return $smarty->fetch($tplFile); } /** diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 2ccf22d734..cb6381684a 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -468,7 +468,7 @@ class CRM_Core_Resources { } // Dynamic localization script - $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/localizationjs/' . $config->lcMessages), $jsWeight++, $region); + $this->addScriptUrl($this->addLocalizationJs(), $jsWeight++, $region); // Add global settings $settings = array('config' => array( @@ -547,9 +547,22 @@ class CRM_Core_Resources { } /** - * Callback to add dymanic script for localizing js widgets + * Add dynamic l10n js */ - static function outputLocalizationJS() { + private function addLocalizationJs() { + $config = CRM_Core_Config::singleton(); + $fileName = 'l10n-' . $config->lcMessages . '.js'; + if (!is_file(CRM_Utils_File::dynamicResourcePath($fileName))) { + $this->createLocalizationJs($fileName); + } + // Dynamic localization script + return CRM_Utils_File::dynamicResourceUrl($fileName); + } + + /** + * Create dymanic script for localizing js widgets + */ + private function createLocalizationJs($fileName) { $config = CRM_Core_Config::singleton(); $vars = array( 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)), @@ -557,7 +570,7 @@ class CRM_Core_Resources { 'otherSearch' => json_encode(ts('Enter search term...')), 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(), ); - CRM_Core_Page_AJAX::returnDynamicJS('CRM/common/localization.js.tpl', $vars); + CRM_Utils_File::addDynamicResource($fileName, CRM_Core_Page_AJAX::returnDynamicJS('CRM/common/localization.js.tpl', $vars)); } /** diff --git a/CRM/Core/xml/Menu/Misc.xml b/CRM/Core/xml/Menu/Misc.xml index 020ff717f5..e1f4a1d11d 100644 --- a/CRM/Core/xml/Menu/Misc.xml +++ b/CRM/Core/xml/Menu/Misc.xml @@ -175,8 +175,4 @@ CRM_Core_Page_Angular access CiviCRM - - civicrm/ajax/localizationjs - CRM_Core_Resources::outputLocalizationJS - diff --git a/CRM/Utils/File.php b/CRM/Utils/File.php index 724f269bc1..c459fa0cc8 100644 --- a/CRM/Utils/File.php +++ b/CRM/Utils/File.php @@ -687,5 +687,52 @@ HTACCESS; } return TRUE; } + + /** + * Create a static file (e.g. css or js) in the dynamic resource directory + * Note: if the file already exists it will be overwritten + * @param string $fileName + * @param string $contents + */ + static function addDynamicResource($fileName, $contents) { + // First ensure the directory exists + $path = self::baseFilePath() . 'dynamic'; + if (!is_dir($path)) { + self::createDir($path); + self::restrictBrowsing($path); + } + file_put_contents($path . DIRECTORY_SEPARATOR . $fileName, $contents); + } + + /** + * Get the path of a dynamic resource file + * @param string $fileName + * @return string + */ + static function dynamicResourcePath($fileName) { + return self::baseFilePath() . 'dynamic' . DIRECTORY_SEPARATOR . $fileName; + } + + /** + * Get the URL of a dynamic resource file + * @param string $fileName + * @return string + */ + static function dynamicResourceUrl($fileName) { + $config = CRM_Core_Config::singleton(); + return str_replace('persist/contribute', 'dynamic', $config->imageUploadURL) . $fileName; + } + + /** + * Flush the dynamic resource directory + */ + static function flushDynamicResources() { + $files = glob(self::dynamicResourcePath('*')); + foreach ($files ? $files : array() as $file) { + if (is_file($file)) { + unlink($file); + } + } + } } -- 2.25.1