Merge pull request #19407 from eileenmcnaughton/anet
[civicrm-core.git] / ext / search / search.php
index 7c7f9e1a83eac1135efa45bcabc5d0656253956c..ce37a888686bc3e0e4020414f31aabbd56399bd5 100644 (file)
@@ -121,8 +121,58 @@ function search_civicrm_entityTypes(&$entityTypes) {
 }
 
 /**
- * Implements hook_civicrm_thems().
+ * Implements hook_civicrm_themes().
  */
 function search_civicrm_themes(&$themes) {
   _search_civix_civicrm_themes($themes);
 }
+
+/**
+ * Implements hook_civicrm_pre().
+ */
+function search_civicrm_pre($op, $entity, $id, &$params) {
+  // Supply default name/label when creating new SearchDisplay
+  if ($entity === 'SearchDisplay' && $op === 'create') {
+    if (empty($params['label'])) {
+      $params['label'] = $params['name'];
+    }
+    elseif (empty($params['name'])) {
+      $params['name'] = \CRM_Utils_String::munge($params['label']);
+    }
+  }
+}
+
+/**
+ * Injects settings data to search displays embedded in afforms
+ *
+ * @param \Civi\Angular\Manager $angular
+ * @see CRM_Utils_Hook::alterAngular()
+ */
+function search_civicrm_alterAngular($angular) {
+  $changeSet = \Civi\Angular\ChangeSet::create('searchSettings')
+    ->alterHtml(';\\.aff\\.html$;', function($doc, $path) {
+      $displayTypes = array_column(\Civi\Search\Display::getDisplayTypes(['name']), 'name');
+
+      if ($displayTypes) {
+        $componentNames = 'crm-search-display-' . implode(', crm-search-display-', $displayTypes);
+        foreach (pq($componentNames, $doc) as $component) {
+          $searchName = pq($component)->attr('search-name');
+          $displayName = pq($component)->attr('display-name');
+          if ($searchName && $displayName) {
+            $display = \Civi\Api4\SearchDisplay::get(FALSE)
+              ->addWhere('name', '=', $displayName)
+              ->addWhere('saved_search.name', '=', $searchName)
+              ->addSelect('settings', 'saved_search.api_entity', 'saved_search.api_params')
+              ->execute()->first();
+            if ($display) {
+              pq($component)->attr('settings', CRM_Utils_JS::encode($display['settings'] ?? []));
+              pq($component)->attr('api-entity', CRM_Utils_JS::encode($display['saved_search.api_entity']));
+              pq($component)->attr('api-params', CRM_Utils_JS::encode($display['saved_search.api_params']));
+            }
+          }
+        }
+      }
+    });
+  $angular->add($changeSet);
+
+}