Search ext: load saved searches
[civicrm-core.git] / ext / search / CRM / Search / Page / Ang.php
1 <?php
2
3 class CRM_Search_Page_Ang extends CRM_Core_Page {
4 /**
5 * @var string[]
6 */
7 private $loadOptions = ['id', 'name', 'label', 'description', 'color', 'icon'];
8
9 /**
10 * @var array
11 */
12 private $schema = [];
13
14 /**
15 * @var string[]
16 */
17 private $allowedEntities = [];
18
19 public function run() {
20 $breadCrumb = [
21 'title' => ts('Search'),
22 'url' => CRM_Utils_System::url('civicrm/search'),
23 ];
24 CRM_Utils_System::appendBreadCrumb([$breadCrumb]);
25
26 $this->getSchema();
27
28 // If user does not have permission to search any entity, bye bye.
29 if (!$this->allowedEntities) {
30 CRM_Utils_System::permissionDenied();
31 }
32
33 // Add client-side vars for the search UI
34 $vars = [
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(),
41 ];
42
43 Civi::resources()
44 ->addPermissions(['edit groups', 'administer reserved groups'])
45 ->addVars('search', $vars);
46
47 // Load angular module
48 $loader = new Civi\Angular\AngularLoader();
49 $loader->setModules(['search']);
50 $loader->setPageName('civicrm/search');
51 $loader->useApp([
52 'defaultRoute' => '/create/Contact',
53 ]);
54 $loader->load();
55 parent::run();
56 }
57
58 /**
59 * @return string[]
60 */
61 private function getOperators() {
62 return [
63 '=' => '=',
64 '!=' => '≠',
65 '>' => '>',
66 '<' => '<',
67 '>=' => '≥',
68 '<=' => '≤',
69 'CONTAINS' => ts('Contains'),
70 'IN' => ts('Is In'),
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'),
78 ];
79 }
80
81 /**
82 * Populates $this->schema & $this->allowedEntities
83 */
84 private function getSchema() {
85 $schema = \Civi\Api4\Entity::get()
86 ->addSelect('name', 'titlePlural', 'description', 'icon')
87 ->addWhere('name', '!=', 'Entity')
88 ->addOrderBy('titlePlural')
89 ->setChain([
90 'get' => ['$name', 'getActions', ['where' => [['name', '=', 'get']]], ['params']],
91 ])->execute();
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
95 if ($entity['get']) {
96 // Get fields and pre-load options for certain prominent entities
97 $loadOptions = in_array($entity['name'], ['Contact', 'Group']) ? $this->loadOptions : FALSE;
98 if ($loadOptions) {
99 $entity['optionsLoaded'] = TRUE;
100 }
101 $entity['fields'] = civicrm_api4($entity['name'], 'getFields', [
102 'select' => $getFields,
103 'where' => [['permission', 'IS NULL']],
104 'orderBy' => ['label'],
105 'loadOptions' => $loadOptions,
106 ]);
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'];
113 }
114 }
115 }
116
117 /**
118 * @return array
119 */
120 private function getLinks() {
121 $results = [];
122 $keys = array_flip(['alias', 'entity', 'joinType']);
123 foreach (civicrm_api4('Entity', 'getLinks', ['where' => [['entity', 'IN', $this->allowedEntities]]], ['entity' => 'links']) as $entity => $links) {
124 $entityLinks = [];
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);
129 }
130 }
131 $results[$entity] = array_values($entityLinks);
132 }
133 return array_filter($results);
134 }
135
136 /**
137 * @return array[]
138 */
139 private function getActions() {
140 // Note: the placeholder %1 will be replaced with entity name on the clientside
141 $actions = [
142 'export' => [
143 'title' => ts('Export %1'),
144 'icon' => 'fa-file-excel-o',
145 'entities' => array_keys(CRM_Export_BAO_Export::getComponents()),
146 'crmPopup' => [
147 'path' => "'civicrm/export/standalone'",
148 'query' => "{entity: entity, id: ids.join(',')}",
149 ],
150 ],
151 'update' => [
152 'title' => ts('Update %1'),
153 'icon' => 'fa-save',
154 'entities' => [],
155 'uiDialog' => ['templateUrl' => '~/search/crmSearchActions/crmSearchActionUpdate.html'],
156 ],
157 'delete' => [
158 'title' => ts('Delete %1'),
159 'icon' => 'fa-trash',
160 'entities' => [],
161 'uiDialog' => ['templateUrl' => '~/search/crmSearchActions/crmSearchActionDelete.html'],
162 ],
163 ];
164
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']]],
169 ], ['name']);
170 foreach ($result as $action) {
171 // Contacts have their own delete action
172 if (!($entity === 'Contact' && $action === 'delete')) {
173 $actions[$action]['entities'][] = $entity;
174 }
175 }
176 }
177
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',
186 'crmPopup' => [
187 'path' => "'{$task['url']}'",
188 'query' => "{cids: ids.join(',')}",
189 ],
190 ];
191 }
192 }
193
194 return $actions;
195 }
196
197 }