Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(NFC) Fix Warnings When Updating Custom Entities #12033

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,29 +193,22 @@ public static function create(&$params) {
}

$transaction = new CRM_Core_Transaction();
// create any option group & values if required
if ($params['html_type'] != 'Text' &&
in_array($params['data_type'], array(
'String',
'Int',
'Float',
'Money',
))
) {

$tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
$params['custom_group_id'],
'table_name'
);
$htmlType = CRM_Utils_Array::value('html_type', $params);
$dataType = CRM_Utils_Array::value('data_type', $params);
$allowedOptionTypes = array('String', 'Int', 'Float', 'Money');

// create any option group & values if required
if ($htmlType != 'Text' && in_array($dataType, $allowedOptionTypes)
) {
//CRM-16659: if option_value then create an option group for this custom field.
if ($params['option_type'] == 1 && (empty($params['option_group_id']) || !empty($params['option_value']))) {
// first create an option group for this custom group
$optionGroup = new CRM_Core_DAO_OptionGroup();
$optionGroup->name = "{$columnName}_" . date('YmdHis');
$optionGroup->title = $params['label'];
$optionGroup->is_active = 1;
$optionGroup->data_type = $params['data_type'];
$optionGroup->data_type = $dataType;
$optionGroup->save();
$params['option_group_id'] = $optionGroup->id;
if (!empty($params['option_value']) && is_array($params['option_value'])) {
Expand All @@ -225,7 +218,7 @@ public static function create(&$params) {
$optionValue->option_group_id = $optionGroup->id;
$optionValue->label = $params['option_label'][$k];
$optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]);
switch ($params['data_type']) {
switch ($dataType) {
case 'Money':
$optionValue->value = CRM_Utils_Rule::cleanMoney($v);
break;
Expand Down Expand Up @@ -262,7 +255,7 @@ public static function create(&$params) {
if (empty($params['default_value'])) {
//don't insert only value separator as default value, CRM-4579
$defaultValue = self::getOptionGroupDefault($params['option_group_id'],
$params['html_type']
$htmlType
);

if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR,
Expand All @@ -275,7 +268,7 @@ public static function create(&$params) {
}

// since we need to save option group id :)
if (!isset($params['attributes']) && strtolower($params['html_type']) == 'textarea') {
if (!isset($params['attributes']) && strtolower($htmlType) == 'textarea') {
$params['attributes'] = 'rows=4, cols=60';
}

Expand Down
32 changes: 16 additions & 16 deletions CRM/Core/BAO/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,37 @@ public static function create(&$params) {
$group->title = $params['title'];
}

if (in_array($params['extends'][0],
array(
'ParticipantRole',
'ParticipantEventName',
'ParticipantEventType',
)
)) {
$extends = CRM_Utils_Array::value('extends', $params, []);
$extendsEntity = CRM_Utils_Array::value(0, $extends);

$participantEntities = array(
'ParticipantRole',
'ParticipantEventName',
'ParticipantEventType',
);

if (in_array($extendsEntity, $participantEntities)) {
$group->extends = 'Participant';
}
else {
$group->extends = $params['extends'][0];
$group->extends = $extendsEntity;
}

$group->extends_entity_column_id = 'null';
if (
$params['extends'][0] == 'ParticipantRole' ||
$params['extends'][0] == 'ParticipantEventName' ||
$params['extends'][0] == 'ParticipantEventType'
if (in_array($extendsEntity, $participantEntities)
) {
$group->extends_entity_column_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $params['extends'][0], 'value', 'name');
$group->extends_entity_column_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $extendsEntity, 'value', 'name');
}

//this is format when form get submit.
$extendsChildType = CRM_Utils_Array::value(1, $params['extends']);
$extendsChildType = CRM_Utils_Array::value(1, $extends);
//lets allow user to pass direct child type value, CRM-6893
if (!empty($params['extends_entity_column_value'])) {
$extendsChildType = $params['extends_entity_column_value'];
}
if (!CRM_Utils_System::isNull($extendsChildType)) {
$extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, $extendsChildType);
if (CRM_Utils_Array::value(0, $params['extends']) == 'Relationship') {
if (CRM_Utils_Array::value(0, $extends) == 'Relationship') {
$extendsChildType = str_replace(array('_a_b', '_b_a'), array(
'',
'',
Expand Down Expand Up @@ -211,7 +211,7 @@ public static function create(&$params) {
$params['id'],
'table_name'
);
CRM_Core_BAO_SchemaHandler::changeFKConstraint($table, self::mapTableName($params['extends'][0]));
CRM_Core_BAO_SchemaHandler::changeFKConstraint($table, self::mapTableName($extendsEntity));
}
$transaction->commit();

Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,17 @@ public function testCustomFileField() {
$this->assertEquals($attachment['id'], $result[$cfId]);
}

public function testUpdateCustomField() {
$customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
$params = array('id' => $customGroup['id'], 'is_active' => 0);
$result = $this->callAPISuccess('CustomGroup', 'create', $params);
$result = array_shift($result['values']);

$this->assertEquals(0, $result['is_active']);

$this->customGroupDelete($customGroup['id']);
}

/**
* @param $getFieldsResult
*
Expand Down
13 changes: 13 additions & 0 deletions tests/phpunit/api/v3/CustomGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,17 @@ public function testGetCustomGroupSuccess() {
}
}

public function testUpdateCustomGroup() {
$customGroup = $this->customGroupCreate();
$customGroupId = $customGroup['id'];

//update is_active
$params = ['id' => $customGroupId, 'is_active' => 0];
$result = $this->callAPISuccess('CustomGroup', 'create', $params);
$result = array_shift($result['values']);

$this->assertEquals(0, $result['is_active']);
$this->customGroupDelete($customGroupId);
}

}