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

Hide Adding Option Value for Locked Groups #11962

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
1 change: 1 addition & 0 deletions CRM/Admin/Page/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class CRM_Admin_Page_Options extends CRM_Core_Page_Basic {
public function preProcess() {
if (!self::$_gName && !empty($this->urlPath[3])) {
self::$_gName = $this->urlPath[3];
self::$_isLocked = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', self::$_gName, 'is_locked', 'name');
}
// If an id arg is passed instead of a group name in the path
elseif (!self::$_gName && !empty($_GET['gid'])) {
Expand Down
12 changes: 12 additions & 0 deletions CRM/Core/BAO/CustomOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ static public function getOptionListSelector(&$params) {
$class .= ' disabled';
$action -= CRM_Core_Action::DISABLE;
}

$isGroupLocked = (bool) CRM_Core_DAO::getFieldValue(
CRM_Core_DAO_OptionGroup::class,
$field->option_group_id,
'is_locked'
);

// disable deletion of option values for locked option groups
if ($isGroupLocked) {
$action -= CRM_Core_Action::DELETE;
}

if (in_array($field->html_type, array('CheckBox', 'AdvMulti-Select', 'Multi-Select'))) {
if (isset($defVal) && in_array($dao->value, $defVal)) {
$options[$dao->id]['is_default'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
Expand Down
11 changes: 8 additions & 3 deletions CRM/Core/OptionValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ class CRM_Core_OptionValue {
*/
public static function getRows($groupParams, $links, $orderBy = 'weight', $skipEmptyComponents = TRUE) {
$optionValue = array();

$optionGroupID = NULL;
$isGroupLocked = FALSE;

if (!isset($groupParams['id']) || !$groupParams['id']) {
if ($groupParams['name']) {
$config = CRM_Core_Config::singleton();

$optionGroup = CRM_Core_BAO_OptionGroup::retrieve($groupParams, $dnc);
$optionGroupID = $optionGroup->id;
$isGroupLocked = (bool) $optionGroup->is_locked;
}
}
else {
Expand Down Expand Up @@ -146,6 +146,11 @@ public static function getRows($groupParams, $links, $orderBy = 'weight', $skipE
}
}

// disallow deletion of option values for locked groups
if ($isGroupLocked) {
$action -= CRM_Core_Action::DELETE;
}

$optionValue[$dao->id]['label'] = htmlspecialchars($optionValue[$dao->id]['label']);
$optionValue[$dao->id]['order'] = $optionValue[$dao->id]['weight'];
$optionValue[$dao->id]['icon'] = CRM_Utils_Array::value('icon', $optionValue[$dao->id], '');
Expand Down
43 changes: 39 additions & 4 deletions CRM/Custom/Page/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ public static function &actionLinks() {
CRM_Core_Action::DISABLE => array(
'name' => ts('Disable'),
'ref' => 'crm-enable-disable',
'title' => ts('Disable Mutliple Choice Option'),
'title' => ts('Disable Multiple Choice Option'),
),
CRM_Core_Action::ENABLE => array(
'name' => ts('Enable'),
'ref' => 'crm-enable-disable',
'title' => ts('Enable Mutliple Choice Option'),
'title' => ts('Enable Multiple Choice Option'),
),
CRM_Core_Action::DELETE => array(
'name' => ts('Delete'),
'url' => 'civicrm/admin/custom/group/field/option',
'qs' => 'action=delete&id=%%id%%&fid=%%fid%%',
'title' => ts('Disable Multiple Choice Option'),
'title' => ts('Delete Multiple Choice Option'),
),
);
}
Expand Down Expand Up @@ -229,9 +229,14 @@ public function run() {
);

if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) {
CRM_Core_Error::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
CRM_Core_Error::fatal("You cannot add or edit multiple choice options in a reserved custom field-set.");
}

$optionGroupId = $this->getOptionGroupId($this->_fid);
$isOptionGroupLocked = $optionGroupId ? $this->isOptionGroupLocked($optionGroupId) : FALSE;
$this->assign('optionGroupId', $optionGroupId);
$this->assign('isOptionGroupLocked', $isOptionGroupLocked);

//as url contain $gid so append breadcrumb dynamically.
$breadcrumb = array(
array(
Expand Down Expand Up @@ -281,4 +286,34 @@ public function run() {
return parent::run();
}

/**
* Gets the "is_locked" status for the provided option group
*
* @param int $optionGroupId
*
* @return bool
*/
private function isOptionGroupLocked($optionGroupId) {
return (bool) CRM_Core_DAO::getFieldValue(
CRM_Core_DAO_OptionGroup::class,
$optionGroupId,
'is_locked'
);
}

/**
* Gets the associated "option_group_id" for a custom field
*
* @param int $customFieldId
*
* @return int
*/
private function getOptionGroupId($customFieldId) {
return (int) CRM_Core_DAO::getFieldValue(
CRM_Core_DAO_CustomField::class,
$customFieldId,
'option_group_id'
);
}

}
4 changes: 3 additions & 1 deletion templates/CRM/Custom/Page/Option.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@

<div class="action-link">
{crmButton q="reset=1&action=map&fid=$fid&gid=$gid" class="action-item open-inline-noreturn" icon="sort-alpha-asc"}{ts}Alphabetize Options{/ts}{/crmButton}
{crmButton q="reset=1&action=add&fid=$fid&gid=$gid" class="action-item" icon="plus-circle"}{ts}Add Option{/ts}{/crmButton}
{if !$isOptionGroupLocked}
{crmButton q="reset=1&action=add&fid=$fid&gid=$gid" class="action-item" icon="plus-circle"}{ts}Add Option{/ts}{/crmButton}
{/if}
{crmButton p="civicrm/admin/custom/group/field" q="reset=1&action=browse&gid=$gid" class="action-item cancel" icon="times"}{ts}Done{/ts}{/crmButton}
</div>
</div>
Expand Down