}
/**
- * 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;
}
}
/**
- * 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);
}
/**
*
* Generated from xml/schema/CRM/Contact/SavedSearch.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:d863f8b0b8659633bc84578e1d6cbf10)
+ * (GenCodeChecksum:2a23a737d07cbfc49ce1d60a642fee3e)
*/
/**
*/
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
*
'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,
* @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;
}
*/
/**
- * Upgrade logic for FiveThirtyTwo */
+ * Upgrade logic for FiveThirtyTwo
+ */
class CRM_Upgrade_Incremental_php_FiveThirtyTwo extends CRM_Upgrade_Incremental_Base {
/**
// }
}
- /*
- * 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'");
+ }
}
<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>
<serialize>PHP</serialize>
<add>1.1</add>
</field>
+
<field>
<name>mapping_id</name>
<type>int unsigned</type>
<onDelete>SET NULL</onDelete>
<add>1.5</add>
</foreignKey>
+
<field>
<name>search_custom_id</name>
<type>int unsigned</type>
<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>
<add>1.6</add>
<drop>5.24</drop>
</field>
+
<field>
<name>select_tables</name>
<type>text</type>
<add>1.6</add>
<drop>5.24</drop>
</field>
+
<field>
<name>where_tables</name>
<type>text</type>
<add>1.6</add>
<drop>5.24</drop>
</field>
+
<field>
<name>api_entity</name>
<type>varchar</type>
<comment>Entity name for API based search</comment>
<add>5.24</add>
</field>
+
<field>
<name>api_params</name>
<type>text</type>