Merge pull request #18704 from mlutfy/partListingOutput
[civicrm-core.git] / CRM / Core / Resources / Common.php
index e5f34cc1d9366169042fdfdfa54d9f5a95ac6a2b..13b86a233dfe6c9688d419279c3f1daf2ced912b 100644 (file)
@@ -16,6 +16,71 @@ class CRM_Core_Resources_Common {
 
   const REGION = 'html-header';
 
+  /**
+   * Create a "basic" (generic) bundle.
+   *
+   * The bundle goes through some lifecycle events (like `hook_alterBundle`).
+   *
+   * To define default content for a basic bundle, you may either give an
+   * `$init` function or subscribe to `hook_alterBundle`.
+   *
+   * @param string $name
+   *   Symbolic name of the bundle.
+   * @param callable|NULL $init
+   *   Optional initialization function. Populate default resources.
+   *   Signature: `function($bundle): void`
+   *   Example: `function myinit($b) { $b->addScriptFile(...)->addStyleFile(...); }`
+   * @param string|string[] $types
+   *   List of resource-types to permit in this bundle. NULL for a default list.
+   *   Example: ['styleFile', 'styleUrl']
+   *   The following aliases are allowed: '*all*', '*default*', '*script*', '*style*'
+   * @return CRM_Core_Resources_Bundle
+   */
+  public static function createBasicBundle($name, $init = NULL, $types = NULL) {
+    $bundle = new CRM_Core_Resources_Bundle($name, $types);
+    if ($init !== NULL) {
+      $init($bundle);
+    }
+    CRM_Utils_Hook::alterBundle($bundle);
+    $bundle->fillDefaults();
+    return $bundle;
+  }
+
+  /**
+   * The 'bundle.bootstrap3' service is a collection of resources which are
+   * loaded when a page needs to support Boostrap CSS v3.
+   *
+   * @param string $name
+   *   i.e. 'bootstrap3'
+   * @return \CRM_Core_Resources_Bundle
+   */
+  public static function createBootstrap3Bundle($name) {
+    $bundle = new CRM_Core_Resources_Bundle($name, ['script', 'scriptFile', 'scriptUrl', 'settings', 'style', 'styleFile', 'styleUrl', 'markup']);
+    // Leave it to the theme/provider to register specific resources.
+    // $bundle->addStyleFile('civicrm', 'css/bootstrap3.css');
+    // $bundle->addScriptFile('civicrm', 'js/bootstrap3.js', [
+    //  'translate' => FALSE,
+    //]);
+
+    //  This warning will show if bootstrap is unavailable. Normally it will be hidden by the bootstrap .collapse class.
+    $bundle->addMarkup('
+      <div id="bootstrap-theme">
+        <div class="messages warning no-popup collapse">
+          <p>
+            <i class="crm-i fa-exclamation-triangle" aria-hidden="true"></i>
+            <strong>' . ts('Bootstrap theme not found.') . '</strong>
+          </p>
+          <p>' . ts('This screen may not work correctly without a bootstrap-based theme such as Shoreditch installed.') . '</p>
+        </div>
+      </div>',
+      ['region' => 'page-header']
+    );
+
+    CRM_Utils_Hook::alterBundle($bundle);
+    $bundle->fillDefaults();
+    return $bundle;
+  }
+
   /**
    * The 'bundle.coreStyles' service is a collection of resources used on some
    * non-Civi pages (wherein Civi may be mixed-in).
@@ -41,7 +106,7 @@ class CRM_Core_Resources_Common {
     $bundle->addStyleFile('civicrm', 'css/crm-i.css', -101);
 
     CRM_Utils_Hook::alterBundle($bundle);
-    self::useRegion($bundle, self::REGION);
+    $bundle->fillDefaults();
     return $bundle;
   }
 
@@ -98,7 +163,7 @@ class CRM_Core_Resources_Common {
     ]);
 
     CRM_Utils_Hook::alterBundle($bundle);
-    self::useRegion($bundle, self::REGION);
+    $bundle->fillDefaults();
     return $bundle;
   }
 
@@ -238,21 +303,4 @@ class CRM_Core_Resources_Common {
     return $items;
   }
 
-  /**
-   * Ensure that all elements of the bundle are in the same region.
-   *
-   * @param CRM_Core_Resources_Bundle $bundle
-   * @param string $region
-   * @return CRM_Core_Resources_Bundle
-   */
-  protected static function useRegion($bundle, $region) {
-    $bundle->filter(function ($s) use ($region) {
-      if ($s['type'] !== 'settings' && !isset($s['region'])) {
-        $s['region'] = $region;
-      }
-      return $s;
-    });
-    return $bundle;
-  }
-
 }