| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC |
| 16 | * $Id$ |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * Add a Javascript file to a specific part of the page |
| 22 | * |
| 23 | * @param array $params |
| 24 | * Array with keys: |
| 25 | * - ext: string, extension name. see CRM_Core_Resources::addScriptFile |
| 26 | * - file: string, relative file path. see CRM_Core_Resources::addScriptFile |
| 27 | * - url: string. see CRM_Core_Resources::addScriptURL |
| 28 | * - weight: int; default: CRM_Core_Resources::DEFAULT_WEIGHT (0) |
| 29 | * - region: string; default: CRM_Core_Resources::DEFAULT_REGION ('html-header') |
| 30 | * @param CRM_Core_Smarty $smarty |
| 31 | * |
| 32 | * @throws Exception |
| 33 | */ |
| 34 | function smarty_function_crmScript($params, &$smarty) { |
| 35 | $params += [ |
| 36 | 'weight' => CRM_Core_Resources::DEFAULT_WEIGHT, |
| 37 | 'region' => CRM_Core_Resources::DEFAULT_REGION, |
| 38 | 'ext' => 'civicrm', |
| 39 | ]; |
| 40 | |
| 41 | if (array_key_exists('file', $params)) { |
| 42 | Civi::resources()->addScriptFile($params['ext'], $params['file'], $params['weight'], $params['region']); |
| 43 | } |
| 44 | elseif (array_key_exists('url', $params)) { |
| 45 | Civi::resources()->addScriptUrl($params['url'], $params['weight'], $params['region']); |
| 46 | } |
| 47 | else { |
| 48 | CRM_Core_Error::debug_var('crmScript_params', $params); |
| 49 | throw new Exception("crmScript requires url or ext+file"); |
| 50 | } |
| 51 | } |