From 3c08b287a9f5e00ced92871136ae7a45fcd6fb3b Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 27 Oct 2022 12:17:29 +1300 Subject: [PATCH] [REF] Cleanup on CRM_Admin_Form_Options --- CRM/Admin/Form/Options.php | 76 +++++++++++++++++++--------- templates/CRM/Admin/Form/Options.tpl | 4 +- 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/CRM/Admin/Form/Options.php b/CRM/Admin/Form/Options.php index ab4cbedc2f..23753bf351 100644 --- a/CRM/Admin/Form/Options.php +++ b/CRM/Admin/Form/Options.php @@ -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. */ @@ -124,7 +127,7 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { '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) { @@ -138,8 +141,10 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { /** * 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])); @@ -147,7 +152,7 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { return; } - $optionGroup = \Civi\Api4\OptionGroup::get(FALSE) + $optionGroup = OptionGroup::get(FALSE) ->addWhere('id', '=', $this->_gid) ->execute()->first(); @@ -313,16 +318,20 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { } // 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); } @@ -355,28 +364,24 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { */ 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.'); @@ -388,8 +393,8 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { } } - $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( @@ -400,6 +405,31 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { 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. * @@ -443,7 +473,7 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form { 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']]; } diff --git a/templates/CRM/Admin/Form/Options.tpl b/templates/CRM/Admin/Form/Options.tpl index 7ce72eb8ec..2d6ab20691 100644 --- a/templates/CRM/Admin/Form/Options.tpl +++ b/templates/CRM/Admin/Form/Options.tpl @@ -153,8 +153,8 @@ {/if} {if !empty($showContactFilter)}{* contactOptions is exposed for email/postal greeting and addressee types to set filter for contact types *} - {$form.contactOptions.label} - {$form.contactOptions.html} + {$form.contact_type_id.label} + {$form.contact_type_id.html} {/if} -- 2.25.1