Skip to content

Commit

Permalink
PCHR-3304: Fix bug with "is_locked" functionality
Browse files Browse the repository at this point in the history
Included in CiviCRM 5.3.0
Core PR: civicrm#11962
  • Loading branch information
mickadoo committed May 16, 2018
1 parent 417fdc6 commit bd5ae9e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
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 @@ -189,9 +189,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 @@ -239,4 +244,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 @@ -157,7 +157,9 @@
{/literal}

<div class="action-link">
{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

0 comments on commit bd5ae9e

Please sign in to comment.