diff --git a/CRM/Core/Payment/MJWTrait.php b/CRM/Core/Payment/MJWTrait.php index a7975199acaef8af9d6c777bbe4c3e4ad690378f..4ae02d545e063ce9be8e1759075d1daea519002b 100644 --- a/CRM/Core/Payment/MJWTrait.php +++ b/CRM/Core/Payment/MJWTrait.php @@ -1,6 +1,12 @@ <?php -/** - * https://civicrm.org/licensing +/* + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC. All rights reserved. | + | | + | This work is published under the GNU AGPLv3 license with some | + | permitted exceptions and without any warranty. For full license | + | and copyright information, see https://civicrm.org/licensing | + +--------------------------------------------------------------------+ */ use CRM_Mjwshared_ExtensionUtil as E; diff --git a/CRM/Mjwshared/Resources.php b/CRM/Mjwshared/Resources.php new file mode 100644 index 0000000000000000000000000000000000000000..01dfa14e0ecf29c97be98e21167cdfc77482e610 --- /dev/null +++ b/CRM/Mjwshared/Resources.php @@ -0,0 +1,100 @@ +<?php + +/** + * Class CRM_Mjwshared_Resources + * + * Currently (CiviCRM 5.24) \Civi::resources()->addVars() / CRM_Core_Resources::singleton()->addVars() + * only allows adding to ajax-snippet or html-header region. But we need to add to billing-block region + * @todo See https://github.com/civicrm/civicrm-core/pull/16888 for a core fix which will allow us to + * remove these compatibility functions + */ +class CRM_Mjwshared_Resources extends CRM_Core_Resources { + + /** + * Added settings. + * + * Format is ($regionName => bool). + * + * @var array + */ + protected $addedSettingsToRegion = []; + + /** + * Add JavaScript variables to CRM.vars + * + * Example: + * From the server: + * CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar')); + * Access var from javascript: + * CRM.vars.myNamespace.foo // "bar" + * + * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference + * + * @param string $nameSpace + * Usually the name of your extension. + * @param array $vars + * @param string $region + * The region to add settings to (eg. for payment processors usually billing-block + * + * @return CRM_Core_Resources + */ + public static function compatAddVars($nameSpace, $vars, $region = NULL) { + $resourcesObject = CRM_Core_Resources::singleton(); + $existing = CRM_Utils_Array::value($nameSpace, CRM_Utils_Array::value('vars', $resourcesObject->settings), []); + $vars = $resourcesObject->mergeSettings($existing, $vars); + self::compatAddSetting($resourcesObject, ['vars' => [$nameSpace => $vars]], $region); + return $resourcesObject; + } + + /** + * Add JavaScript variables to the root of the CRM object. + * This function is usually reserved for low-level system use. + * Extensions and components should generally use addVars instead. + * + * @param \CRM_Core_Resources $resourcesObject + * @param array $settings + * @param string $region + * The region to add settings to (eg. for payment processors usually billing-block + * + * @return CRM_Core_Resources + */ + public static function compatAddSetting($resourcesObject, $settings, $region = NULL) { + if (!$region) { + $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header'; + } + $resourcesObject->settings = $resourcesObject->mergeSettings($resourcesObject->settings, $settings); + if (isset($resourcesObject->addedSettingsToRegion[$region])) { + return $resourcesObject; + } + $resources = $resourcesObject; + $settingsResource = [ + 'callback' => function (&$snippet, &$html) use ($resources, $region) { + $html .= "\n" . self::compatRenderSetting($resources, $region); + }, + 'weight' => -100000, + ]; + CRM_Core_Region::instance($region)->add($settingsResource); + $resourcesObject->addedSettingsToRegion[$region] = TRUE; + return $resourcesObject; + } + + /** + * Helper fn for addSetting. + * Render JavaScript variables for the global CRM object. + * + * @param \CRM_Core_Resources $resourcesObject + * + * @return string + */ + public static function compatRenderSetting($resourcesObject, $region) { + // On a standard page request we construct the CRM object from scratch + if ($region === 'html-header') { + $js = 'var CRM = ' . json_encode($resourcesObject->getSettings()) . ';'; + } + // For an ajax request we append to it + else { + $js = 'CRM.$.extend(true, CRM, ' . json_encode($resourcesObject->getSettings()) . ');'; + } + return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js); + } +}