[REF] Remove setting on unused variables
[civicrm-core.git] / ext / search / CRM / Search / Upgrader.php
1 <?php
2 use CRM_Search_ExtensionUtil as E;
3
4 /**
5 * Collection of upgrade steps.
6 */
7 class CRM_Search_Upgrader extends CRM_Search_Upgrader_Base {
8
9 /**
10 * Add menu item when enabled.
11 */
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)
21 ->execute();
22 }
23
24 /**
25 * Delete menu item when disabled.
26 */
27 public function disable() {
28 \Civi\Api4\Navigation::delete(FALSE)
29 ->addWhere('name', '=', 'search_kit')
30 ->addWhere('domain_id', '=', 'current_domain')
31 ->execute();
32 }
33
34 /**
35 * Upgrade 1000 - install schema
36 * @return bool
37 */
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');
43 }
44 CRM_Core_DAO::executeQuery("UPDATE civicrm_navigation SET url = 'civicrm/admin/search', name = 'search_kit' WHERE url = 'civicrm/search'");
45 return TRUE;
46 }
47
48 /**
49 * Upgrade 1001 - normalize search display column keys
50 * @return bool
51 */
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'))
57 ->execute();
58 foreach ($savedSearches as $savedSearch) {
59 $newAliases = [];
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;
65 }
66 }
67 if ($newAliases) {
68 \Civi\Api4\SavedSearch::update(FALSE)
69 ->setValues(array_diff_key($savedSearch, ['displays' => 0]))
70 ->execute();
71 }
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';
78 }
79 \Civi\Api4\SearchDisplay::update(FALSE)
80 ->setValues($display)
81 ->execute();
82 }
83 }
84 return TRUE;
85 }
86
87 }