Skip to content

Commit

Permalink
SavedSearch - Add name and label columns
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Oct 20, 2020
1 parent 7dc9cb4 commit 53b13ff
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 39 deletions.
20 changes: 10 additions & 10 deletions CRM/Contact/BAO/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,23 +503,23 @@ public static function create(&$params) {
}

/**
* 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;
}
Expand Down
20 changes: 15 additions & 5 deletions CRM/Contact/BAO/SavedSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,28 @@ public static function getName($id, $value = 'name') {
}

/**
* 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);
}

/**
Expand Down
64 changes: 62 additions & 2 deletions CRM/Contact/DAO/SavedSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contact/SavedSearch.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:d863f8b0b8659633bc84578e1d6cbf10)
* (GenCodeChecksum:2a23a737d07cbfc49ce1d60a642fee3e)
*/

/**
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -126,6 +140,42 @@ public static function &fields() {
'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,
Expand Down Expand Up @@ -262,7 +312,17 @@ public static function &export($prefix = FALSE) {
* @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;
}

Expand Down
33 changes: 11 additions & 22 deletions CRM/Upgrade/Incremental/php/FiveThirtyTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*/

/**
* Upgrade logic for FiveThirtyTwo */
* Upgrade logic for FiveThirtyTwo
*/
class CRM_Upgrade_Incremental_php_FiveThirtyTwo extends CRM_Upgrade_Incremental_Base {

/**
Expand Down Expand Up @@ -46,27 +47,15 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
// }
}

/*
* 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'");
}

}
40 changes: 40 additions & 0 deletions xml/schema/Contact/SavedSearch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@
<name>id</name>
<autoincrement>false</autoincrement>
</primaryKey>

<field>
<name>name</name>
<title>Saved Search Name</title>
<type>varchar</type>
<length>255</length>
<default>NULL</default>
<comment>Unique name of saved search</comment>
<html>
<type>Text</type>
</html>
<add>1.0</add>
</field>
<index>
<name>UI_name</name>
<fieldName>name</fieldName>
<unique>true</unique>
<add>5.32</add>
</index>

<field>
<name>label</name>
<title>Saved Search Label</title>
<type>varchar</type>
<length>255</length>
<default>NULL</default>
<comment>Administrative label for search</comment>
<html>
<type>Text</type>
</html>
<add>5.32</add>
</field>

<field>
<name>form_values</name>
<title>Submitted Form Values</title>
Expand All @@ -27,6 +60,7 @@
<serialize>PHP</serialize>
<add>1.1</add>
</field>

<field>
<name>mapping_id</name>
<type>int unsigned</type>
Expand All @@ -41,13 +75,15 @@
<onDelete>SET NULL</onDelete>
<add>1.5</add>
</foreignKey>

<field>
<name>search_custom_id</name>
<type>int unsigned</type>
<title>Option Value ID</title>
<comment>Foreign key to civicrm_option value table used for saved custom searches.</comment>
<add>2.0</add>
</field>

<field>
<name>where_clause</name>
<type>text</type>
Expand All @@ -56,6 +92,7 @@
<add>1.6</add>
<drop>5.24</drop>
</field>

<field>
<name>select_tables</name>
<type>text</type>
Expand All @@ -65,6 +102,7 @@
<add>1.6</add>
<drop>5.24</drop>
</field>

<field>
<name>where_tables</name>
<type>text</type>
Expand All @@ -74,6 +112,7 @@
<add>1.6</add>
<drop>5.24</drop>
</field>

<field>
<name>api_entity</name>
<type>varchar</type>
Expand All @@ -82,6 +121,7 @@
<comment>Entity name for API based search</comment>
<add>5.24</add>
</field>

<field>
<name>api_params</name>
<type>text</type>
Expand Down

0 comments on commit 53b13ff

Please sign in to comment.