Merge pull request #22464 from colemanw/extRequirementCheck
[civicrm-core.git] / ext / search_kit / search_kit.php
CommitLineData
25523059
CW
1<?php
2
fbf3a315 3require_once 'search_kit.civix.php';
25523059
CW
4
5/**
6 * Implements hook_civicrm_config().
7 *
8 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
9 */
fbf3a315
CW
10function search_kit_civicrm_config(&$config) {
11 _search_kit_civix_civicrm_config($config);
a92d81a1 12 Civi::dispatcher()->addListener('hook_civicrm_alterAngular', ['\Civi\Search\AfformSearchMetadataInjector', 'preprocess'], 1000);
25523059
CW
13}
14
5623bf2a
CW
15/**
16 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
17 */
18function search_kit_civicrm_container($container) {
19 $container->getDefinition('dispatcher')
20 ->addMethodCall('addListener', [
21 'civi.api4.authorizeRecord::SavedSearch',
22 ['CRM_Search_BAO_SearchDisplay', 'savedSearchCheckAccessByDisplay'],
23 ]);
24}
25
10329730
CW
26/**
27 * Implements hook_civicrm_alterApiRoutePermissions().
28 *
29 * Allow anonymous users to run a search display. Permissions are checked internally.
30 *
31 * @see CRM_Utils_Hook::alterApiRoutePermissions
32 */
33function search_kit_civicrm_alterApiRoutePermissions(&$permissions, $entity, $action) {
34 if ($entity === 'SearchDisplay') {
35 if ($action === 'run' || $action === 'download' || $action === 'getSearchTasks') {
36 $permissions = CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION;
37 }
38 }
39}
40
25523059
CW
41/**
42 * Implements hook_civicrm_angularModules().
43 *
44 * Generate a list of Angular modules.
45 *
46 * Note: This hook only runs in CiviCRM 4.5+. It may
47 * use features only available in v4.6+.
48 *
49 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_angularModules
50 */
fbf3a315 51function search_kit_civicrm_angularModules(&$angularModules) {
14564ba5
CW
52 // Fetch all search tasks provided by extensions and add their Angular modules as crmSearchTasks dependencies
53 $tasks = [];
1e313889 54 $null = NULL;
14564ba5
CW
55 $checkPermissions = FALSE;
56 \CRM_Utils_Hook::singleton()->invoke(['tasks', 'checkPermissions', 'userId'],
57 $tasks, $checkPermissions, $null,
58 $null, $null, $null, 'civicrm_searchKitTasks'
1e313889
CW
59 );
60 foreach ($tasks as $entityTasks) {
61 foreach ($entityTasks as $task) {
62 if (isset($task['module']) && $task['module'] !== 'crmSearchTasks' &&
63 !in_array($task['module'], $angularModules['crmSearchTasks']['requires'], TRUE)
64 ) {
65 $angularModules['crmSearchTasks']['requires'][] = $task['module'];
66 }
67 }
68 }
25523059
CW
69}
70
25523059
CW
71/**
72 * Implements hook_civicrm_entityTypes().
73 *
74 * Declare entity types provided by this module.
75 *
76 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
77 */
fbf3a315
CW
78function search_kit_civicrm_entityTypes(&$entityTypes) {
79 _search_kit_civix_civicrm_entityTypes($entityTypes);
25523059
CW
80}
81
e991ce44
CW
82/**
83 * Implements hook_civicrm_pre().
84 */
fbf3a315 85function search_kit_civicrm_pre($op, $entity, $id, &$params) {
e991ce44
CW
86 // Supply default name/label when creating new SearchDisplay
87 if ($entity === 'SearchDisplay' && $op === 'create') {
88 if (empty($params['label'])) {
89 $params['label'] = $params['name'];
90 }
91 elseif (empty($params['name'])) {
92 $params['name'] = \CRM_Utils_String::munge($params['label']);
93 }
94 }
1c3d1f8d
CW
95 // When deleting a saved search, also delete the displays
96 // This would happen anyway in sql because of the ON DELETE CASCADE foreign key,
97 // But this ensures that pre and post hooks are called
98 if ($entity === 'SavedSearch' && $op === 'delete') {
99 \Civi\Api4\SearchDisplay::delete(FALSE)
100 ->addWhere('saved_search_id', '=', $id)
101 ->execute();
102 }
e991ce44 103}