From b698e2d5c188403c054175d6745b9fbb7fd29c7a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 17 Sep 2015 00:51:02 -0700 Subject: [PATCH] CRM-16373 - Path, URL Admin - Explain variables --- CRM/Core/Resources.php | 18 +++++- .../Smarty/plugins/function.crmResPath.php | 56 +++++++++++++++++++ Civi/Core/Paths.php | 50 +++++++++-------- templates/CRM/Admin/Form/Setting/Path.hlp | 22 ++++++++ templates/CRM/Admin/Form/Setting/Path.tpl | 13 ++++- templates/CRM/Admin/Form/Setting/Url.hlp | 23 ++++++++ templates/CRM/Admin/Form/Setting/Url.tpl | 9 ++- 7 files changed, 162 insertions(+), 29 deletions(-) create mode 100644 CRM/Core/Smarty/plugins/function.crmResPath.php create mode 100644 templates/CRM/Admin/Form/Setting/Path.hlp diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index f2bb53a18f..11fd31f002 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -98,6 +98,11 @@ class CRM_Core_Resources { */ public $ajaxPopupsEnabled; + /** + * @var \Civi\Core\Paths + */ + protected $paths; + /** * Get or set the single instance of CRM_Core_Resources. * @@ -141,6 +146,7 @@ class CRM_Core_Resources { $this->resetCacheCode(); } $this->ajaxPopupsEnabled = (bool) Civi::settings()->get('ajaxPopupsEnabled'); + $this->paths = Civi::paths(); } /** @@ -465,10 +471,13 @@ class CRM_Core_Resources { */ public function getPath($ext, $file = NULL) { // TODO consider caching results + $base = $this->paths->hasVariable($ext) + ? rtrim($this->paths->getVariable($ext, 'path'), '/') + : $this->extMapper->keyToBasePath($ext); if ($file === NULL) { - return $this->extMapper->keyToBasePath($ext); + return $base; } - $path = $this->extMapper->keyToBasePath($ext) . '/' . $file; + $path = $base . '/' . $file; if (is_file($path)) { return $path; } @@ -494,7 +503,10 @@ class CRM_Core_Resources { $file .= '?r=' . $this->getCacheCode(); } // TODO consider caching results - return $this->extMapper->keyToUrl($ext) . '/' . $file; + $base = $this->paths->hasVariable($ext) + ? $this->paths->getVariable($ext, 'url') + : ($this->extMapper->keyToUrl($ext) . '/'); + return $base . $file; } /** diff --git a/CRM/Core/Smarty/plugins/function.crmResPath.php b/CRM/Core/Smarty/plugins/function.crmResPath.php new file mode 100644 index 0000000000..257797ab23 --- /dev/null +++ b/CRM/Core/Smarty/plugins/function.crmResPath.php @@ -0,0 +1,56 @@ +getPath($params['ext'], $params['file']); +} diff --git a/Civi/Core/Paths.php b/Civi/Core/Paths.php index 230ea24254..8ec074f304 100644 --- a/Civi/Core/Paths.php +++ b/Civi/Core/Paths.php @@ -22,15 +22,12 @@ class Paths { * @var array * Array(string $name => array(url => $, path => $)). */ - private $containers = array(); + private $variables = array(); - protected $containerFactory = array(); + private $variableFactory = array(); public function __construct() { $this - //->register('civicrm', function () { - // return \CRM_Core_Config::singleton()->userSystem->getCiviSourceStorage(); - //}) ->register('civicrm.root', function () { return \CRM_Core_Config::singleton()->userSystem->getCiviSourceStorage(); }) @@ -56,7 +53,7 @@ class Paths { * Register a new URL/file path mapping. * * @param string $name - * The name of the container. + * The name of the variable. * @param callable $factory * Function which returns an array with keys: * - path: string. @@ -64,28 +61,39 @@ class Paths { * @return $this */ public function register($name, $factory) { - $this->containerFactory[$name] = $factory; + $this->variableFactory[$name] = $factory; return $this; } - protected function getContainerAttr($name, $attr) { - if (!isset($this->containers[$name])) { - $this->containers[$name] = call_user_func($this->containerFactory[$name]); + /** + * @param string $name + * Ex: 'civicrm.root'. + * @param string $attr + * Ex: 'url', 'path'. + * @return mixed + */ + public function getVariable($name, $attr) { + if (!isset($this->variables[$name])) { + $this->variables[$name] = call_user_func($this->variableFactory[$name]); } - if (!isset($this->containers[$name][$attr])) { + if (!isset($this->variables[$name][$attr])) { throw new \RuntimeException("Cannot resolve path using \"$name.$attr\""); } - return $this->containers[$name][$attr]; + return $this->variables[$name][$attr]; + } + + public function hasVariable($name) { + return isset($this->variableFactory[$name]); } /** * Determine the absolute path to a file, given that the file is most likely - * in a given particular container. + * in a given particular variable. * * @param string $value - * The file path (which is probably relative to $container). - * Use "." to reference to container root. - * Values may explicitly specify the a container, e.g. "[civicrm.files]/upload". + * The file path. + * Use "." to reference to default file root. + * Values may begin with a variable, e.g. "[civicrm.files]/upload". * @return mixed|string */ public function getPath($value) { @@ -100,16 +108,14 @@ class Paths { if ($value === '.') { $value = ''; } - return \CRM_Utils_File::absoluteDirectory($value, $this->getContainerAttr($defaultContainer, 'path')); + return \CRM_Utils_File::absoluteDirectory($value, $this->getVariable($defaultContainer, 'path')); } /** - * Determine the absolute URL to a file, given that the file is most likely - * in a given particular container. + * Determine the URL to a file. * * @param string $value - * The file path (which is probably relative to $container). - * Values may explicitly specify the a container, e.g. "[civicrm.files]/upload". + * The file path. The path may begin with a variable, e.g. "[civicrm.files]/upload". * @param string $preferFormat * The preferred format ('absolute', 'relative'). * The result data may not meet the preference -- if the setting @@ -136,7 +142,7 @@ class Paths { return $value; } - $value = $this->getContainerAttr($defaultContainer, 'url') . $value; + $value = $this->getVariable($defaultContainer, 'url') . $value; if ($preferFormat === 'relative') { $parsed = parse_url($value); diff --git a/templates/CRM/Admin/Form/Setting/Path.hlp b/templates/CRM/Admin/Form/Setting/Path.hlp new file mode 100644 index 0000000000..a79eeea10f --- /dev/null +++ b/templates/CRM/Admin/Form/Setting/Path.hlp @@ -0,0 +1,22 @@ +{htxt id='id-path_vars'} +{ts}Path Variables{/ts} + + + + + + + + + + + + + + + +
[cms.root]{crmResPath ext='cms.root'}
[civicrm.root]{crmResPath ext='civicrm.root'}
[civicrm.files]{crmResPath ext='civicrm.files'}
+

+ {ts}These variables are computed automatically using civicrm.settings.php and its options, such as CIVICRM_TEMPLATE_COMPILEDIR.{/ts} +

+{/htxt} diff --git a/templates/CRM/Admin/Form/Setting/Path.tpl b/templates/CRM/Admin/Form/Setting/Path.tpl index 9549f692dd..185404c2d4 100644 --- a/templates/CRM/Admin/Form/Setting/Path.tpl +++ b/templates/CRM/Admin/Form/Setting/Path.tpl @@ -24,9 +24,16 @@ +--------------------------------------------------------------------+ *}
-
- {ts}Default values will be supplied for these upload directories the first time you access CiviCRM - based on the CIVICRM_TEMPLATE_COMPILEDIR specified in civicrm.settings.php. If you need to modify the defaults, make sure that your web server has write access to the directories.{/ts} -
+
+

+ {ts}You may configure these upload directories using absolute paths or path variables.{/ts} + {help id='id-path_vars'} +

+

+ {ts}If you modify the defaults, make sure that your web server has write access to the directories.{/ts} +

+ +
{include file="CRM/common/formButtons.tpl" location="top"}
diff --git a/templates/CRM/Admin/Form/Setting/Url.hlp b/templates/CRM/Admin/Form/Setting/Url.hlp index 03430f1738..d31e45f129 100644 --- a/templates/CRM/Admin/Form/Setting/Url.hlp +++ b/templates/CRM/Admin/Form/Setting/Url.hlp @@ -85,3 +85,26 @@ {htxt id='id-css_url'}

{ts}You can modify the look and feel of CiviCRM by adding your own stylesheet. For small to medium sized modifications, use your css file to override some of the styles in civicrm.css. Or if you need to make drastic changes, you can choose to disable civicrm.css completely.{/ts}

{/htxt} + +{htxt id='id-url_vars'} +{ts}URL Variables{/ts} +
+ + + + + + + + + + + + + + +
[cms.root]{crmResURL ext='cms.root'}
[civicrm.root]{crmResURL ext='civicrm.root'}
[civicrm.files]{crmResURL ext='civicrm.files'}
+

+ {ts}These variables are computed automatically using civicrm.settings.php and its options, such as CIVICRM_TEMPLATE_COMPILEDIR.{/ts} +

+{/htxt} diff --git a/templates/CRM/Admin/Form/Setting/Url.tpl b/templates/CRM/Admin/Form/Setting/Url.tpl index 5a524c0935..96d331c004 100644 --- a/templates/CRM/Admin/Form/Setting/Url.tpl +++ b/templates/CRM/Admin/Form/Setting/Url.tpl @@ -25,7 +25,14 @@ *}
- {ts}These settings define the URLs used to access CiviCRM resources (CSS files, Javascript files, images, etc.). Default values will be inserted the first time you access CiviCRM - based on the CIVICRM_UF_BASEURL specified in your installation's settings file (civicrm.settings.php).{/ts} +

+ {ts}These settings define the URLs used to access CiviCRM resources (CSS files, Javascript files, images, etc.).{/ts} +

+

+ {ts}You may configure these settings using absolute URLs or URL variables.{/ts} + {help id='id-url_vars'} +

+
{include file="CRM/common/formButtons.tpl" location="top"}
-- 2.25.1