From 53b13ffec702a4840fe2ebe941552114afbbd478 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 20 Oct 2020 09:39:02 -0400 Subject: [PATCH] SavedSearch - Add name and label columns --- CRM/Contact/BAO/Group.php | 20 +++--- CRM/Contact/BAO/SavedSearch.php | 20 ++++-- CRM/Contact/DAO/SavedSearch.php | 64 ++++++++++++++++++- CRM/Upgrade/Incremental/php/FiveThirtyTwo.php | 33 ++++------ xml/schema/Contact/SavedSearch.xml | 40 ++++++++++++ 5 files changed, 138 insertions(+), 39 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index 1da376121f..5861b05757 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -503,23 +503,23 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { } /** - * Defines a new smart group. + * Takes a sloppy mismash of params and creates two entities: a Group and a SavedSearch + * Currently only used by unit tests. * * @param array $params - * Associative array of parameters. - * * @return CRM_Contact_BAO_Group|NULL - * The new group BAO (if created) + * @deprecated */ - public static function createSmartGroup(&$params) { + public static function createSmartGroup($params) { if (!empty($params['formValues'])) { $ssParams = $params; - unset($ssParams['id']); - if (isset($ssParams['saved_search_id'])) { - $ssParams['id'] = $ssParams['saved_search_id']; + // Remove group parameters from sloppy mismash + unset($ssParams['id'], $ssParams['name'], $ssParams['title'], $ssParams['formValues'], $ssParams['saved_search_id']); + if (isset($params['saved_search_id'])) { + $ssParams['id'] = $params['saved_search_id']; } - $params['form_values'] = $params['formValues']; - $savedSearch = CRM_Contact_BAO_SavedSearch::create($params); + $ssParams['form_values'] = $params['formValues']; + $savedSearch = CRM_Contact_BAO_SavedSearch::create($ssParams); $params['saved_search_id'] = $savedSearch->id; } diff --git a/CRM/Contact/BAO/SavedSearch.php b/CRM/Contact/BAO/SavedSearch.php index d2ed700e7d..afd7a762f3 100644 --- a/CRM/Contact/BAO/SavedSearch.php +++ b/CRM/Contact/BAO/SavedSearch.php @@ -335,18 +335,28 @@ LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_ } /** - * Create a smart group from normalised values. + * Create or update SavedSearch record. * * @param array $params * * @return \CRM_Contact_DAO_SavedSearch */ public static function create(&$params) { - $savedSearch = new CRM_Contact_DAO_SavedSearch(); - $savedSearch->copyValues($params); - $savedSearch->save(); + // Auto-create unique name from label if supplied + if (empty($params['id']) && empty($params['name']) && !empty($params['label'])) { + $name = CRM_Utils_String::munge($params['label']); + $existing = Civi\Api4\SavedSearch::get(FALSE) + ->addWhere('name', 'LIKE', $name . '%') + ->addSelect('name') + ->execute()->column('name'); + $suffix = ''; + while (in_array($name . $suffix, $existing)) { + $suffix = '_' . (1 + str_replace('_', '', $suffix)); + } + $params['name'] = $name . $suffix; + } - return $savedSearch; + return self::writeRecord($params); } /** diff --git a/CRM/Contact/DAO/SavedSearch.php b/CRM/Contact/DAO/SavedSearch.php index c532997a2a..86d7d917d9 100644 --- a/CRM/Contact/DAO/SavedSearch.php +++ b/CRM/Contact/DAO/SavedSearch.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Contact/SavedSearch.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:d863f8b0b8659633bc84578e1d6cbf10) + * (GenCodeChecksum:2a23a737d07cbfc49ce1d60a642fee3e) */ /** @@ -37,6 +37,20 @@ class CRM_Contact_DAO_SavedSearch extends CRM_Core_DAO { */ public $id; + /** + * Unique name of saved search + * + * @var string + */ + public $name; + + /** + * Administrative label for search + * + * @var string + */ + public $label; + /** * Submitted form values for this search * @@ -126,6 +140,42 @@ class CRM_Contact_DAO_SavedSearch extends CRM_Core_DAO { 'localizable' => 0, 'add' => '1.1', ], + 'name' => [ + 'name' => 'name', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('Saved Search Name'), + 'description' => ts('Unique name of saved search'), + 'maxlength' => 255, + 'size' => CRM_Utils_Type::HUGE, + 'where' => 'civicrm_saved_search.name', + 'default' => 'NULL', + 'table_name' => 'civicrm_saved_search', + 'entity' => 'SavedSearch', + 'bao' => 'CRM_Contact_BAO_SavedSearch', + 'localizable' => 0, + 'html' => [ + 'type' => 'Text', + ], + 'add' => '1.0', + ], + 'label' => [ + 'name' => 'label', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => ts('Saved Search Label'), + 'description' => ts('Administrative label for search'), + 'maxlength' => 255, + 'size' => CRM_Utils_Type::HUGE, + 'where' => 'civicrm_saved_search.label', + 'default' => 'NULL', + 'table_name' => 'civicrm_saved_search', + 'entity' => 'SavedSearch', + 'bao' => 'CRM_Contact_BAO_SavedSearch', + 'localizable' => 0, + 'html' => [ + 'type' => 'Text', + ], + 'add' => '5.32', + ], 'form_values' => [ 'name' => 'form_values', 'type' => CRM_Utils_Type::T_TEXT, @@ -262,7 +312,17 @@ class CRM_Contact_DAO_SavedSearch extends CRM_Core_DAO { * @return array */ public static function indices($localize = TRUE) { - $indices = []; + $indices = [ + 'UI_name' => [ + 'name' => 'UI_name', + 'field' => [ + 0 => 'name', + ], + 'localizable' => FALSE, + 'unique' => TRUE, + 'sig' => 'civicrm_saved_search::1::name', + ], + ]; return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices; } diff --git a/CRM/Upgrade/Incremental/php/FiveThirtyTwo.php b/CRM/Upgrade/Incremental/php/FiveThirtyTwo.php index a68bb9c20e..04ecec4fbf 100644 --- a/CRM/Upgrade/Incremental/php/FiveThirtyTwo.php +++ b/CRM/Upgrade/Incremental/php/FiveThirtyTwo.php @@ -10,7 +10,8 @@ */ /** - * Upgrade logic for FiveThirtyTwo */ + * Upgrade logic for FiveThirtyTwo + */ class CRM_Upgrade_Incremental_php_FiveThirtyTwo extends CRM_Upgrade_Incremental_Base { /** @@ -46,27 +47,15 @@ class CRM_Upgrade_Incremental_php_FiveThirtyTwo extends CRM_Upgrade_Incremental_ // } } - /* - * Important! All upgrade functions MUST add a 'runSql' task. - * Uncomment and use the following template for a new upgrade version - * (change the x in the function name): + /** + * Upgrade function. + * + * @param string $rev */ - - // /** - // * Upgrade function. - // * - // * @param string $rev - // */ - // public function upgrade_5_0_x($rev) { - // $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); - // $this->addTask('Do the foo change', 'taskFoo', ...); - // // Additional tasks here... - // // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex. - // // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable. - // } - - // public static function taskFoo(CRM_Queue_TaskContext $ctx, ...) { - // return TRUE; - // } + public function upgrade_5_32_alpha1($rev) { + $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); + $this->addTask('Add column civicrm_saved_search.name', 'addColumn', 'civicrm_saved_search', 'name', "varchar(255) DEFAULT NULL COMMENT 'Unique name of saved search'"); + $this->addTask('Add column civicrm_saved_search.label', 'addColumn', 'civicrm_saved_search', 'label', "varchar(255) DEFAULT NULL COMMENT 'Administrative label for search'"); + } } diff --git a/xml/schema/Contact/SavedSearch.xml b/xml/schema/Contact/SavedSearch.xml index 2d73182ac5..1edf5f9ac9 100644 --- a/xml/schema/Contact/SavedSearch.xml +++ b/xml/schema/Contact/SavedSearch.xml @@ -18,6 +18,39 @@ id false + + + name + Saved Search Name + varchar + 255 + NULL + Unique name of saved search + + Text + + 1.0 + + + UI_name + name + true + 5.32 + + + + label + Saved Search Label + varchar + 255 + NULL + Administrative label for search + + Text + + 5.32 + + form_values Submitted Form Values @@ -27,6 +60,7 @@ PHP 1.1 + mapping_id int unsigned @@ -41,6 +75,7 @@ SET NULL 1.5 + search_custom_id int unsigned @@ -48,6 +83,7 @@ Foreign key to civicrm_option value table used for saved custom searches. 2.0 + where_clause text @@ -56,6 +92,7 @@ 1.6 5.24 + select_tables text @@ -65,6 +102,7 @@ 1.6 5.24 + where_tables text @@ -74,6 +112,7 @@ 1.6 5.24 + api_entity varchar @@ -82,6 +121,7 @@ Entity name for API based search 5.24 + api_params text -- 2.25.1