Merge pull request #22555 from eileenmcnaughton/stricter
[civicrm-core.git] / CRM / Core / Region.php
index 0d6cfc63301dbe75ec8006c2df71e05de343fcaf..4d9f733068c67003c28ad68cd6345be2b37c3d3e 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * Maintain a set of markup/templates to inject inside various regions
  */
-class CRM_Core_Region {
+class CRM_Core_Region implements CRM_Core_Resources_CollectionInterface, CRM_Core_Resources_CollectionAdderInterface {
 
   /**
    * Obtain the content for a given region.
@@ -20,9 +20,7 @@ class CRM_Core_Region {
     return Civi::$statics[__CLASS__][$name];
   }
 
-  use CRM_Core_Resources_CollectionTrait {
-    CRM_Core_Resources_CollectionTrait::add as _add;
-  }
+  use CRM_Core_Resources_CollectionTrait;
 
   /**
    * Symbolic name of this region
@@ -36,7 +34,7 @@ class CRM_Core_Region {
    */
   public function __construct($name) {
     $this->_name = $name;
-    $this->types = ['markup', 'template', 'callback', 'scriptUrl', 'script', 'jquery', 'settings', 'style', 'styleUrl'];
+    $this->types = ['markup', 'template', 'callback', 'scriptFile', 'scriptUrl', 'script', 'jquery', 'settings', 'style', 'styleFile', 'styleUrl'];
     $this->defaults['region'] = $name;
 
     // Placeholder which represents any of the default content generated by the main Smarty template
@@ -62,17 +60,16 @@ class CRM_Core_Region {
     if (is_array($this->snippets['default'])) {
       $this->snippets['default']['markup'] = $default;
     }
-    // We hand as much of the work off to the CMS as possible
-    $cms = CRM_Core_Config::singleton()->userSystem;
+
+    Civi::dispatcher()->dispatch('civi.region.render', \Civi\Core\Event\GenericHookEvent::create(['region' => $this]));
 
     $this->sort();
 
+    $cms = CRM_Core_Config::singleton()->userSystem;
     $smarty = CRM_Core_Smarty::singleton();
     $html = '';
-    foreach ($this->snippets as $snippet) {
-      if ($snippet['disabled']) {
-        continue;
-      }
+
+    $renderSnippet = function($snippet) use (&$html, $smarty, $cms, $allowCmsOverride, &$renderSnippet) {
       switch ($snippet['type']) {
         case 'markup':
           $html .= $snippet['markup'];
@@ -97,14 +94,30 @@ class CRM_Core_Region {
           break;
 
         case 'jquery':
-          $snippet['script'] = sprintf("CRM.\$(function(\$) {\n%s\n});", $snippet['jquery']);
-          // no break - continue processing as script
+          $renderSnippet([
+            'type' => 'script',
+            'script' => sprintf("CRM.\$(function(\$) {\n%s\n});", $snippet['jquery']),
+          ]);
+          break;
+
+        case 'scriptFile':
+          foreach ($snippet['scriptFileUrls'] as $url) {
+            $html .= $renderSnippet(['type' => 'scriptUrl', 'scriptUrl' => $url] + $snippet);
+          }
+          break;
+
         case 'script':
           if (!$allowCmsOverride || !$cms->addScript($snippet['script'], $this->_name)) {
             $html .= sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $snippet['script']);
           }
           break;
 
+        case 'styleFile':
+          foreach ($snippet['styleFileUrls'] as $url) {
+            $html .= $renderSnippet(['type' => 'styleUrl', 'styleUrl' => $url] + $snippet);
+          }
+          break;
+
         case 'styleUrl':
           if (!$allowCmsOverride || !$cms->addStyleUrl($snippet['styleUrl'], $this->_name)) {
             $html .= sprintf("<link href=\"%s\" rel=\"stylesheet\" type=\"text/css\"/>\n", $snippet['styleUrl']);
@@ -118,7 +131,7 @@ class CRM_Core_Region {
           break;
 
         case 'settings':
-          $settingsData = json_encode(Civi::resources()->getSettings($this->_name), JSON_UNESCAPED_SLASHES);
+          $settingsData = json_encode($this->getSettings(), JSON_UNESCAPED_SLASHES);
           $js = "(function(vars) {
             if (window.CRM) CRM.$.extend(true, CRM, vars); else window.CRM = vars;
             })($settingsData)";
@@ -129,6 +142,12 @@ class CRM_Core_Region {
           throw new CRM_Core_Exception(ts('Snippet type %1 is unrecognized',
             [1 => $snippet['type']]));
       }
+    };
+
+    foreach ($this->snippets as $snippet) {
+      if (empty($snippet['disabled'])) {
+        $renderSnippet($snippet);
+      }
     }
     return $html;
   }