3 class CRM_Search_Page_Ang
extends CRM_Core_Page
{
7 private $loadOptions = ['id', 'name', 'label', 'description', 'color', 'icon'];
17 private $allowedEntities = [];
19 public function run() {
21 'title' => ts('Search'),
22 'url' => CRM_Utils_System
::url('civicrm/search'),
24 CRM_Utils_System
::appendBreadCrumb([$breadCrumb]);
28 // If user does not have permission to search any entity, bye bye.
29 if (!$this->allowedEntities
) {
30 CRM_Utils_System
::permissionDenied();
33 // Add client-side vars for the search UI
35 'operators' => CRM_Utils_Array
::makeNonAssociative($this->getOperators()),
36 'schema' => $this->schema
,
37 'links' => $this->getLinks(),
38 'loadOptions' => $this->loadOptions
,
39 'actions' => $this->getActions(),
40 'functions' => CRM_Api4_Page_Api4Explorer
::getSqlFunctions(),
44 ->addPermissions(['edit groups', 'administer reserved groups'])
45 ->addVars('search', $vars);
47 // Load angular module
48 $loader = new Civi\Angular\
AngularLoader();
49 $loader->setModules(['search']);
50 $loader->setPageName('civicrm/search');
52 'defaultRoute' => '/create/Contact',
61 private function getOperators() {
69 'CONTAINS' => ts('Contains'),
71 'NOT IN' => ts('Not In'),
72 'LIKE' => ts('Is Like'),
73 'NOT LIKE' => ts('Not Like'),
74 'BETWEEN' => ts('Is Between'),
75 'NOT BETWEEN' => ts('Not Between'),
76 'IS NULL' => ts('Is Null'),
77 'IS NOT NULL' => ts('Not Null'),
82 * Populates $this->schema & $this->allowedEntities
84 private function getSchema() {
85 $schema = \Civi\Api4\Entity
::get()
86 ->addSelect('name', 'titlePlural', 'description', 'icon')
87 ->addWhere('name', '!=', 'Entity')
88 ->addOrderBy('titlePlural')
90 'get' => ['$name', 'getActions', ['where' => [['name', '=', 'get']]], ['params']],
92 $getFields = ['name', 'label', 'description', 'options', 'input_type', 'input_attrs', 'data_type', 'serialize'];
93 foreach ($schema as $entity) {
94 // Skip if entity doesn't have a 'get' action or the user doesn't have permission to use get
96 // Get fields and pre-load options for certain prominent entities
97 $loadOptions = in_array($entity['name'], ['Contact', 'Group']) ?
$this->loadOptions
: FALSE;
99 $entity['optionsLoaded'] = TRUE;
101 $entity['fields'] = civicrm_api4($entity['name'], 'getFields', [
102 'select' => $getFields,
103 'where' => [['permission', 'IS NULL']],
104 'orderBy' => ['label'],
105 'loadOptions' => $loadOptions,
107 // Get the names of params this entity supports (minus some obvious ones)
108 $params = $entity['get'][0];
109 CRM_Utils_Array
::remove($params, 'checkPermissions', 'debug', 'chain', 'language');
110 unset($entity['get']);
111 $this->schema
[] = ['params' => array_keys($params)] +
array_filter($entity);
112 $this->allowedEntities
[] = $entity['name'];
120 private function getLinks() {
122 $keys = array_flip(['alias', 'entity', 'joinType']);
123 foreach (civicrm_api4('Entity', 'getLinks', ['where' => [['entity', 'IN', $this->allowedEntities
]]], ['entity' => 'links']) as $entity => $links) {
125 foreach ($links as $link) {
126 if (!empty($link['entity']) && in_array($link['entity'], $this->allowedEntities
)) {
127 // Use entity.alias as array key to avoid duplicates
128 $entityLinks[$link['entity'] . $link['alias']] = array_intersect_key($link, $keys);
131 $results[$entity] = array_values($entityLinks);
133 return array_filter($results);
139 private function getActions() {
140 // Note: the placeholder %1 will be replaced with entity name on the clientside
143 'title' => ts('Export %1'),
144 'icon' => 'fa-file-excel-o',
145 'entities' => array_keys(CRM_Export_BAO_Export
::getComponents()),
147 'path' => "'civicrm/export/standalone'",
148 'query' => "{entity: entity, id: ids.join(',')}",
152 'title' => ts('Update %1'),
155 'uiDialog' => ['templateUrl' => '~/search/crmSearchActions/crmSearchActionUpdate.html'],
158 'title' => ts('Delete %1'),
159 'icon' => 'fa-trash',
161 'uiDialog' => ['templateUrl' => '~/search/crmSearchActions/crmSearchActionDelete.html'],
165 // Check permissions for update & delete actions
166 foreach ($this->allowedEntities
as $entity) {
167 $result = civicrm_api4($entity, 'getActions', [
168 'where' => [['name', 'IN', ['update', 'delete']]],
170 foreach ($result as $action) {
171 // Contacts have their own delete action
172 if (!($entity === 'Contact' && $action === 'delete')) {
173 $actions[$action]['entities'][] = $entity;
178 // Add contact tasks which support standalone mode (with a 'url' property)
179 $contactTasks = CRM_Contact_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission());
180 foreach (CRM_Contact_Task
::tasks() as $id => $task) {
181 if (isset($contactTasks[$id]) && !empty($task['url'])) {
182 $actions['contact.' . $id] = [
183 'title' => $task['title'],
184 'entities' => ['Contact'],
185 'icon' => $task['icon'] ??
'fa-gear',
187 'path' => "'{$task['url']}'",
188 'query' => "{cids: ids.join(',')}",