Skip to content

Commit

Permalink
Merge pull request civicrm#24828 from eileenmcnaughton/options_form
Browse files Browse the repository at this point in the history
[REF] Cleanup on CRM_Admin_Form_Options
  • Loading branch information
colemanw authored Oct 28, 2022
2 parents 097b478 + 3c08b28 commit 437469f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
76 changes: 53 additions & 23 deletions CRM/Admin/Form/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Api4\OptionGroup;
use Civi\Api4\OptionValue;

/**
* This class generates form components for Options.
*/
Expand Down Expand Up @@ -124,7 +127,7 @@ public function setDefaultValues() {
'postal_greeting',
'addressee',
])) {
$defaults['contactOptions'] = (CRM_Utils_Array::value('filter', $defaults)) ? $defaults['filter'] : NULL;
$defaults['contact_type_id'] = (CRM_Utils_Array::value('filter', $defaults)) ? $defaults['filter'] : NULL;
}
// CRM-11516
if ($this->_gName == 'payment_instrument' && $this->_id) {
Expand All @@ -138,16 +141,18 @@ public function setDefaultValues() {

/**
* Build the form object.
*
* @throws \CRM_Core_Exception
*/
public function buildQuickForm() {
public function buildQuickForm(): void {
parent::buildQuickForm();
$this->setPageTitle(ts('%1 Option', [1 => $this->_gLabel]));

if ($this->_action & CRM_Core_Action::DELETE) {
return;
}

$optionGroup = \Civi\Api4\OptionGroup::get(FALSE)
$optionGroup = OptionGroup::get(FALSE)
->addWhere('id', '=', $this->_gid)
->execute()->first();

Expand Down Expand Up @@ -313,16 +318,20 @@ public function buildQuickForm() {
}

// get contact type for which user want to create a new greeting/addressee type, CRM-4575
if (in_array($this->_gName, ['email_greeting', 'postal_greeting', 'addressee'])
if (in_array($optionGroup['name'], ['email_greeting', 'postal_greeting', 'addressee'], TRUE)
&& !$isReserved
) {
$values = [
1 => ts('Individual'),
2 => ts('Household'),
3 => ts('Organization'),
4 => ts('Multiple Contact Merge'),
];
$this->add('select', 'contactOptions', ts('Contact Type'), ['' => '-select-'] + $values, TRUE);
if ($optionGroup['name'] !== 'email_greeting') {
// This isn't really a contact type - but it becomes available when exporting
// if 'Merge All Contacts with the Same Address' is selected.
$values[4] = ts('Multiple Contact Merge during Export');
}
$this->add('select', 'contact_type_id', ts('Contact Type'), ['' => '-select-'] + $values, TRUE);
$this->assign('showContactFilter', TRUE);
}

Expand Down Expand Up @@ -355,28 +364,24 @@ public function buildQuickForm() {
*/
public static function formRule($fields, $files, $self) {
$errors = [];
if ($self->_gName == 'case_status' && empty($fields['grouping'])) {
$optionGroupName = $self->_gName;
if ($optionGroupName === 'case_status' && empty($fields['grouping'])) {
$errors['grouping'] = ts('Status class is a required field');
}

if (in_array($self->_gName, ['email_greeting', 'postal_greeting', 'addressee'])
&& empty($self->_defaultValues['is_reserved'])
if (
// We are checking no other option value exists for this label+contact type combo.
// @todo - bypassing reserved is historical - why would we not do this check for reserved options?
empty($self->_defaultValues['is_reserved'])
&& in_array($optionGroupName, ['email_greeting', 'postal_greeting', 'addressee'], TRUE)
) {
$label = $fields['label'];
$condition = " AND v.label = '{$label}' ";
$values = CRM_Core_OptionGroup::values($self->_gName, FALSE, FALSE, FALSE, $condition, 'filter');
$checkContactOptions = TRUE;

if ($self->_id && ($self->_defaultValues['contactOptions'] == $fields['contactOptions'])) {
$checkContactOptions = FALSE;
}

if ($checkContactOptions && in_array($fields['contactOptions'], $values)) {
if (self::greetingExists($self->_id, $fields['label'], $fields['contact_type_id'], $optionGroupName)) {
$errors['label'] = ts('This Label already exists in the database for the selected contact type.');
}

}

if ($self->_gName == 'from_email_address') {
if ($optionGroupName === 'from_email_address') {
$formEmail = CRM_Utils_Mail::pluckEmailFromHeader($fields['label']);
if (!CRM_Utils_Rule::email($formEmail)) {
$errors['label'] = ts('Please enter a valid email address.');
Expand All @@ -388,8 +393,8 @@ public static function formRule($fields, $files, $self) {
}
}

$dataType = self::getOptionGroupDataType($self->_gName);
if ($dataType && $self->_gName !== 'activity_type') {
$dataType = self::getOptionGroupDataType($optionGroupName);
if ($dataType && $optionGroupName !== 'activity_type') {
$validate = CRM_Utils_Type::validate($fields['value'], $dataType, FALSE);
if ($validate === FALSE) {
CRM_Core_Session::setStatus(
Expand All @@ -400,6 +405,31 @@ public static function formRule($fields, $files, $self) {
return $errors;
}

/**
* Does an existing option already have this label for this contact type.
*
* @param int|null $id
* @param string $label
* @param int $contactTypeID
* @param string $optionGroupName
*
* @return bool
*
* @throws \CRM_Core_Exception
*/
protected static function greetingExists(?int $id, string $label, int $contactTypeID, string $optionGroupName): bool {
$query = OptionValue::get(FALSE)
->addWhere('label', '=', $label)
->addWhere('option_group_id.name', '=', $optionGroupName)
->addWhere('is_active', '=', TRUE)
->addWhere('filter', '=', (int) $contactTypeID)
->addSelect('rowCount');
if ($id) {
$query->addWhere('id', '<>', $id);
}
return (bool) $query->execute()->count();
}

/**
* Get the DataType for a specified Option Group.
*
Expand Down Expand Up @@ -443,7 +473,7 @@ public function postProcess() {
if ($this->_gName == 'from_email_address') {
$params['reset_default_for'] = ['domain_id' => CRM_Core_Config::domainID()];
}
elseif ($filter = CRM_Utils_Array::value('contactOptions', $params)) {
elseif ($filter = CRM_Utils_Array::value('contact_type_id', $params)) {
$params['filter'] = $filter;
$params['reset_default_for'] = ['filter' => "0, " . $params['filter']];
}
Expand Down
4 changes: 2 additions & 2 deletions templates/CRM/Admin/Form/Options.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@
{/if}
{if !empty($showContactFilter)}{* contactOptions is exposed for email/postal greeting and addressee types to set filter for contact types *}
<tr class="crm-admin-options-form-block-contactOptions">
<td class="label">{$form.contactOptions.label}</td>
<td>{$form.contactOptions.html}</td>
<td class="label">{$form.contact_type_id.label}</td>
<td>{$form.contact_type_id.html}</td>
</tr>
{/if}
</table>
Expand Down

0 comments on commit 437469f

Please sign in to comment.