670d3ca54594c147efd8b4e3a1109e4451326d27
2 use CRM_Search_ExtensionUtil
as E
;
5 * Collection of upgrade steps.
7 class CRM_Search_Upgrader
extends CRM_Search_Upgrader_Base
{
10 * Add menu item when enabled.
12 public function enable() {
13 \Civi\Api4\Navigation
::create(FALSE)
14 ->addValue('parent_id:name', 'Search')
15 ->addValue('label', E
::ts('Search Kit'))
16 ->addValue('name', 'search_kit')
17 ->addValue('url', 'civicrm/admin/search')
18 ->addValue('icon', 'crm-i fa-search-plus')
19 ->addValue('has_separator', 2)
20 ->addValue('weight', 99)
25 * Delete menu item when disabled.
27 public function disable() {
28 \Civi\Api4\Navigation
::delete(FALSE)
29 ->addWhere('name', '=', 'search_kit')
30 ->addWhere('domain_id', '=', 'current_domain')
35 * Upgrade 1000 - install schema
38 public function upgrade_1000() {
39 $this->ctx
->log
->info('Applying update 1000 - install schema.');
40 // For early, early adopters who installed the extension pre-beta
41 if (!CRM_Core_DAO
::singleValueQuery("SHOW TABLES LIKE 'civicrm_search_display'")) {
42 $this->executeSqlFile('sql/auto_install.sql');
44 CRM_Core_DAO
::executeQuery("UPDATE civicrm_navigation SET url = 'civicrm/admin/search', name = 'search_kit' WHERE url = 'civicrm/search'");
49 * Upgrade 1001 - normalize search display column keys
52 public function upgrade_1001() {
53 $this->ctx
->log
->info('Applying update 1001 - normalize search display columns.');
54 $savedSearches = \Civi\Api4\SavedSearch
::get(FALSE)
55 ->addWhere('api_params', 'IS NOT NULL')
56 ->addChain('displays', \Civi\Api4\SearchDisplay
::get()->addWhere('saved_search_id', '=', '$id'))
58 foreach ($savedSearches as $savedSearch) {
60 foreach ($savedSearch['api_params']['select'] ??
[] as $i => $select) {
61 if (strstr($select, '(') && !strstr($select, ' AS ')) {
62 $alias = CRM_Utils_String
::munge(str_replace(')', '', $select), '_', 256);
63 $newAliases[$select] = $alias;
64 $savedSearch['api_params']['select'][$i] = $select . ' AS ' . $alias;
68 \Civi\Api4\SavedSearch
::update(FALSE)
69 ->setValues(array_diff_key($savedSearch, ['displays' => 0]))
72 foreach ($savedSearch['displays'] ??
[] as $display) {
73 foreach ($display['settings']['columns'] ??
[] as $c => $column) {
74 $key = $newAliases[$column['expr']] ??
$column['expr'];
75 unset($display['settings']['columns'][$c]['expr']);
76 $display['settings']['columns'][$c]['key'] = explode(' AS ', $key)[1] ??
$key;
77 $display['settings']['columns'][$c]['type'] = 'field';
79 \Civi\Api4\SearchDisplay
::update(FALSE)