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),
));
}
* @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);
}
/**
}
// 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(
}
/**
- * 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)),
'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));
}
/**
<page_callback>CRM_Core_Page_Angular</page_callback>
<access_arguments>access CiviCRM</access_arguments>
</item>
- <item>
- <path>civicrm/ajax/localizationjs</path>
- <page_callback>CRM_Core_Resources::outputLocalizationJS</page_callback>
- </item>
</menu>
}
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);
+ }
+ }
+ }
}