+ /**
+ * Upgrade 1000 - install schema
+ * @return bool
+ */
+ public function upgrade_1000() {
+ $this->ctx->log->info('Applying update 1000 - install schema.');
+ // For early, early adopters who installed the extension pre-beta
+ if (!CRM_Core_DAO::singleValueQuery("SHOW TABLES LIKE 'civicrm_search_display'")) {
+ $this->executeSqlFile('sql/auto_install.sql');
+ }
+ CRM_Core_DAO::executeQuery("UPDATE civicrm_navigation SET url = 'civicrm/admin/search', name = 'search_kit' WHERE url = 'civicrm/search'");
+ return TRUE;
+ }
+
+ /**
+ * Upgrade 1001 - normalize search display column keys
+ * @return bool
+ */
+ public function upgrade_1001() {
+ $this->ctx->log->info('Applying update 1001 - normalize search display columns.');
+ $savedSearches = \Civi\Api4\SavedSearch::get(FALSE)
+ ->addWhere('api_params', 'IS NOT NULL')
+ ->addChain('displays', \Civi\Api4\SearchDisplay::get()->addWhere('saved_search_id', '=', '$id'))
+ ->execute();
+ foreach ($savedSearches as $savedSearch) {
+ $newAliases = [];
+ foreach ($savedSearch['api_params']['select'] ?? [] as $i => $select) {
+ if (strstr($select, '(') && !strstr($select, ' AS ')) {
+ $alias = CRM_Utils_String::munge(str_replace(')', '', $select), '_', 256);
+ $newAliases[$select] = $alias;
+ $savedSearch['api_params']['select'][$i] = $select . ' AS ' . $alias;
+ }
+ }
+ if ($newAliases) {
+ \Civi\Api4\SavedSearch::update(FALSE)
+ ->setValues(array_diff_key($savedSearch, ['displays' => 0]))
+ ->execute();
+ }
+ foreach ($savedSearch['displays'] ?? [] as $display) {
+ foreach ($display['settings']['columns'] ?? [] as $c => $column) {
+ $key = $newAliases[$column['expr']] ?? $column['expr'];
+ unset($display['settings']['columns'][$c]['expr']);
+ $display['settings']['columns'][$c]['key'] = explode(' AS ', $key)[1] ?? $key;
+ $display['settings']['columns'][$c]['type'] = 'field';
+ }
+ \Civi\Api4\SearchDisplay::update(FALSE)
+ ->setValues($display)
+ ->execute();
+ }
+ }
+ return TRUE;
+ }
+