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

dev/core#155 Fix optiongroup is_reserved data and use when selecting option group for custom fields #12423

Merged
merged 2 commits into from
Jul 16, 2018
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
4 changes: 4 additions & 0 deletions CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ public static function create(&$params) {
$optionGroup->name = "{$columnName}_" . date('YmdHis');
$optionGroup->title = $params['label'];
$optionGroup->is_active = 1;
// Don't set reserved as it's not a built-in option group and may be useful for other custom fields.
$optionGroup->is_reserved = 0;
$optionGroup->data_type = $dataType;
$optionGroup->save();
$params['option_group_id'] = $optionGroup->id;
Expand Down Expand Up @@ -2030,6 +2032,8 @@ public static function getTableColumnGroup($fieldID, $force = FALSE) {
/**
* Get custom option groups.
*
* @deprecated Use the API OptionGroup.get
*
* @param array $includeFieldIds
* Ids of custom fields for which option groups must be included.
*
Expand Down
44 changes: 26 additions & 18 deletions CRM/Custom/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,26 @@ public function buildQuickForm() {
if ($this->_action == CRM_Core_Action::UPDATE) {
$this->freeze('data_type');
}
$includeFieldIds = NULL;

$optionGroupParams = [
'is_reserved' => 0,
'is_active' => 1,
'options' => ['limit' => 0, 'sort' => "title ASC"],
'return' => ['title'],
];
if ($this->_action == CRM_Core_Action::UPDATE) {
$includeFieldIds = $this->_values['id'];
}
$optionGroups = CRM_Core_BAO_CustomField::customOptionGroup($includeFieldIds);
$emptyOptGroup = FALSE;
if (empty($optionGroups)) {
$emptyOptGroup = TRUE;
$optionTypes = array('1' => ts('Create a new set of options'));
$optionGroupParams['id'] = $this->_values['id'];
}
else {
$optionTypes = array(
'1' => ts('Create a new set of options'),
'2' => ts('Reuse an existing set'),
);
// Get all custom (is_reserved=0) option groups
$optionGroupMetadata = civicrm_api3('OptionGroup', 'get', $optionGroupParams);

// OptionGroup selection
$optionTypes = array('1' => ts('Create a new set of options'));

if (!empty($optionGroupMetadata['values'])) {
$emptyOptGroup = FALSE;
$optionGroups = CRM_Utils_Array::collect('title', $optionGroupMetadata['values']);
$optionTypes['2'] = ts('Reuse an existing set');

$this->add('select',
'option_group_id',
Expand All @@ -342,6 +347,10 @@ public function buildQuickForm() {
) + $optionGroups
);
}
else {
// No custom (non-reserved) option groups
$emptyOptGroup = TRUE;
}

$element = &$this->addRadio('option_type',
ts('Option Type'),
Expand All @@ -350,6 +359,10 @@ public function buildQuickForm() {
'onclick' => "showOptionSelect();",
), '<br/>'
);
// if empty option group freeze the option type.
if ($emptyOptGroup) {
$element->freeze();
}

$contactGroups = CRM_Core_PseudoConstant::group();
asort($contactGroups);
Expand All @@ -370,11 +383,6 @@ public function buildQuickForm() {

$this->add('hidden', 'filter_selected', 'Group', array('id' => 'filter_selected'));

//if empty option group freeze the option type.
if ($emptyOptGroup) {
$element->freeze();
}

// form fields of Custom Option rows
$defaultOption = array();
$_showHide = new CRM_Core_ShowHideBlocks('', '');
Expand Down
7 changes: 7 additions & 0 deletions CRM/Upgrade/Incremental/sql/5.5.alpha1.mysql.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ ALTER TABLE civicrm_option_group MODIFY COLUMN is_locked TINYINT(4) NOT NULL DE
#is_reserved already has a default so is effectively required but let's be explicit.
UPDATE civicrm_option_group SET `is_reserved` = 0 WHERE `is_reserved` IS NULL;
ALTER TABLE civicrm_option_group MODIFY COLUMN `is_reserved` tinyint(4) NOT NULL DEFAULT 1 COMMENT 'Is this a predefined system option group (i.e. it can not be deleted)?';

#https://lab.civicrm.org/dev/core/issues/155
{* Fix is_reserved flag on civicrm_option_group table *}
UPDATE civicrm_option_group AS cog INNER JOIN civicrm_custom_field AS ccf
ON cog.id = ccf.option_group_id
SET cog.is_reserved = 0 WHERE cog.is_active = 1 AND ccf.is_active = 1;
UPDATE civicrm_option_group SET is_reserved = 1 WHERE name='environment';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The upgrade code does not affect new installs, so the environment group needs to be set to reserved in the install sql as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw I've updated the install sql as well now.

2 changes: 1 addition & 1 deletion xml/templates/civicrm_data.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ VALUES
('wysiwyg_presets' , '{ts escape="sql"}WYSIWYG Editor Presets{/ts}' , NULL, 1, 1, 0),
('relative_date_filters' , '{ts escape="sql"}Relative Date Filters{/ts}' , NULL, 1, 1, 0),
('pledge_status' , '{ts escape="sql"}Pledge Status{/ts}' , NULL, 1, 1, 1),
('environment' , '{ts escape="sql"}Environment{/ts}' , NULL, 0, 1, 0);
('environment' , '{ts escape="sql"}Environment{/ts}' , NULL, 1, 1, 0);

SELECT @option_group_id_pcm := max(id) from civicrm_option_group where name = 'preferred_communication_method';
SELECT @option_group_id_act := max(id) from civicrm_option_group where name = 'activity_type';
Expand Down