static function create(&$params) {
if (!isset($params['id']) && !isset($params['column_name'])) {
// if add mode & column_name not present, calculate it.
- $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32));
+ $columnName = strtolower(CRM_Utils_String::munge($params['label'], '_', 32));
$params['name'] = CRM_Utils_String::munge($params['label'], '_', 64);
}
elseif (isset($params['id'])) {
- $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
+ $columnName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
$params['id'],
'column_name'
);
if ($params['option_type'] == 1) {
// first create an option group for this custom group
$optionGroup = new CRM_Core_DAO_OptionGroup();
- $optionGroup->name = "{$params['column_name']}_" . date('YmdHis');
+ $optionGroup->name = "{$columnName}_" . date('YmdHis');
$optionGroup->title = $params['label'];
$optionGroup->is_active = 1;
$optionGroup->save();
self::createField($customField, 'modify', $indexExist);
}
else {
- $customField->column_name .= "_{$customField->id}";
+ if (!isset($params['column_name'])) {
+ $columnName .= "_{$customField->id}";
+ }
+ $customField->column_name = $columnName;
$customField->save();
// make sure all values are present in the object
$customField->find(TRUE);
$group->name = CRM_Utils_String::munge($group->title, '_', 64);
}
- // lets create the table associated with the group and save it
- $tableName = $group->table_name = "civicrm_value_" . strtolower($group->name);
+ if (isset($params['table_name'])) {
+ $tableName = $params['table_name'];
+
+ if (CRM_Core_DAO_AllCoreTables::isCoreTable($tableName)) {
+ // Bad idea. Prevent group creation because it might lead to a broken configuration.
+ CRM_Core_Error::fatal(ts("Cannot create custom table because %1 is already a core table.", array('1' => $tableName)));
+ }
+ }
}
// enclose the below in a transaction
$transaction = new CRM_Core_Transaction();
$group->save();
- if ($tableName) {
- // now append group id to table name, this prevent any name conflicts
- // like CRM-2742
- $tableName .= "_{$group->id}";
+ if (!isset($params['id'])) {
+ if (!isset($params['table_name'])) {
+ $munged_title = strtolower(CRM_Utils_String::munge($group->title, '_', 32));
+ $tableName = "civicrm_value_{$munged_title}_{$group->id}";
+ }
$group->table_name = $tableName;
CRM_Core_DAO::setFieldValue('CRM_Core_DAO_CustomGroup',
$group->id,
$this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.');
$this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.');
+ $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
+ $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
+ $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName,
+ "Column name ends in ID");
+
+ Custom::deleteGroup($customGroup);
+ }
+
+ function testCreateCustomfieldColumnName() {
+ $customGroup = Custom::createGroup(array(), 'Individual');
+ $fields = array(
+ 'label' => 'testFld 2',
+ 'column_name' => 'special_colname',
+ 'data_type' => 'String',
+ 'html_type' => 'Text',
+ 'custom_group_id' => $customGroup->id,
+ );
+ $customField = CRM_Core_BAO_CustomField::create($fields);
+ $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id',
+ 'Database check for created CustomField.'
+ );
+ $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
+ $this->assertEquals($fields['column_name'], $dbColumnName,
+ "Column name set as specified");
+
Custom::deleteGroup($customGroup);
}
function testDeleteCustomfield() {
$customGroup = Custom::createGroup(array(), 'Individual');
$fields = array(
- 'groupId' => $customGroup->id,
- 'dataType' => 'Memo',
- 'htmlType' => 'TextArea',
+ 'label' => 'Throwaway Field',
+ 'custom_group_id' => $customGroup->id,
+ 'data_type' => 'Memo',
+ 'html_type' => 'TextArea',
);
$customField = Custom::createField(array(), $fields);
'Database check for custom group record.'
);
$this->assertEquals($params['title'], $dbCustomGroupTitle);
+
+ $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
+ 'Database check for custom group record.'
+ );
+ $this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
+ "The table name should be suffixed with '_ID' unless specified.");
+
+ Custom::deleteGroup($customGroup);
+ }
+
+ /**
+ * Function to test create() given a table_name
+ */
+ function testCreateTableName() {
+ $params = array(
+ 'title' => 'Test_Group_2',
+ 'name' => 'test_group_2',
+ 'table_name' => 'test_otherTableName',
+ 'extends' => array(0 => 'Individual', 1 => array()),
+ 'weight' => 4,
+ 'collapse_display' => 1,
+ 'style' => 'Inline',
+ 'help_pre' => 'This is Pre Help For Test Group 1',
+ 'help_post' => 'This is Post Help For Test Group 1',
+ 'is_active' => 1,
+ 'version' => 3,
+ );
+ $customGroup = CRM_Core_BAO_CustomGroup::create($params);
+
+ $dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
+ 'Database check for custom group record.'
+ );
+ $this->assertEquals($params['title'], $dbCustomGroupTitle);
+
+ $dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
+ 'Database check for custom group record.'
+ );
+ $this->assertEquals($params['table_name'], $dbCustomGroupTableName);
+
Custom::deleteGroup($customGroup);
}