From 76b53f28710d30e1e4c6cd0793abadee72253288 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 10 Aug 2022 19:04:44 +1200 Subject: [PATCH] [Import] [Ref] Extract shared form rule code --- CRM/Activity/Import/Form/MapField.php | 40 ++++------------------ CRM/Contact/Import/Form/MapField.php | 22 +++--------- CRM/Import/Form/MapField.php | 48 ++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 53 deletions(-) diff --git a/CRM/Activity/Import/Form/MapField.php b/CRM/Activity/Import/Form/MapField.php index 155d6357e4..6597972c63 100644 --- a/CRM/Activity/Import/Form/MapField.php +++ b/CRM/Activity/Import/Form/MapField.php @@ -46,9 +46,9 @@ class CRM_Activity_Import_Form_MapField extends CRM_Import_Form_MapField { $contactFieldsBelowWeightMessage = self::validateRequiredContactMatchFields('Individual', $importKeys); foreach ($requiredFields as $field => $title) { - if (!in_array($field, $importKeys)) { + if (!in_array($field, $importKeys, TRUE)) { if ($field === 'target_contact_id') { - if (!$contactFieldsBelowWeightMessage || in_array('external_identifier', $importKeys)) { + if (!$contactFieldsBelowWeightMessage || in_array('external_identifier', $importKeys, TRUE)) { continue; } $fieldMessage .= ts('Missing required contact matching fields.') @@ -67,8 +67,7 @@ class CRM_Activity_Import_Form_MapField extends CRM_Import_Form_MapField { * @throws \CRM_Core_Exception */ public function buildQuickForm(): void { - $savedMappingID = (int) $this->getSubmittedValue('savedMapping'); - $this->buildSavedMappingFields($savedMappingID); + $this->addSavedMappingFields(); $this->addFormRule(['CRM_Activity_Import_Form_MapField', 'formRule']); //-------- end of saved mapping stuff --------- @@ -141,13 +140,11 @@ class CRM_Activity_Import_Form_MapField extends CRM_Import_Form_MapField { * @param array $fields * Posted values of the form. * - * @return array + * @return array|bool * list of errors to be posted back to the form */ - public static function formRule($fields) { + public static function formRule(array $fields) { $errors = []; - // define so we avoid notices below - $errors['_qf_default'] = ''; if (!array_key_exists('savedMapping', $fields)) { $importKeys = []; @@ -159,32 +156,7 @@ class CRM_Activity_Import_Form_MapField extends CRM_Import_Form_MapField { $errors['_qf_default'] = $missingFields; } } - - if (!empty($fields['saveMapping'])) { - $nameField = $fields['saveMappingName'] ?? NULL; - if (empty($nameField)) { - $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); - } - else { - if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Activity'))) { - $errors['saveMappingName'] = ts('Duplicate Import Mapping Name'); - } - } - } - - if (empty($errors['_qf_default'])) { - unset($errors['_qf_default']); - } - if (!empty($errors)) { - if (!empty($errors['saveMappingName'])) { - $_flag = 1; - $assignError = new CRM_Core_Page(); - $assignError->assign('mappingDetailsError', $_flag); - } - return $errors; - } - - return TRUE; + return $errors ?: TRUE; } /** diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index 833eb631cc..504f0f1cef 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -333,28 +333,14 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { * @param array $fields * Posted values of the form. * - * @return array|true + * @return array|bool * list of errors to be posted back to the form */ - public static function formRule($fields) { - $errors = []; + public static function formRule(array $fields) { if (!empty($fields['saveMapping'])) { - $nameField = $fields['saveMappingName'] ?? NULL; - if (empty($nameField)) { - $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); - } - else { - $mappingTypeId = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contact'); - if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { - $errors['saveMappingName'] = ts('Duplicate Import Mapping Name'); - } - } - } - $template = CRM_Core_Smarty::singleton(); - if (!empty($fields['saveMapping'])) { - $template->assign('isCheked', TRUE); + CRM_Core_Smarty::singleton()->assign('isCheked', TRUE); } - return empty($errors) ? TRUE : $errors; + return parent::mappingRule($fields); } /** diff --git a/CRM/Import/Form/MapField.php b/CRM/Import/Form/MapField.php index c374e1d956..7961538bab 100644 --- a/CRM/Import/Form/MapField.php +++ b/CRM/Import/Form/MapField.php @@ -186,6 +186,7 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms { * * @param int|null $savedMappingID * + * @deprecated - working to remove this in favour of `addSavedMappingFields` * @throws \CiviCRM_API3_Exception */ protected function buildSavedMappingFields($savedMappingID) { @@ -299,8 +300,9 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms { //Updating Mapping Records if ($this->getSubmittedValue('updateMapping')) { foreach (array_keys($this->getColumnHeaders()) as $i) { - $this->saveMappingField($this->getSubmittedValue('mappingId'), $i, TRUE); + $this->saveMappingField((int) $this->getSubmittedValue('mappingId'), $i, TRUE); } + $this->updateUserJobMetadata('mapping', ['id' => (int) $this->getSubmittedValue('mappingId')]); } //Saving Mapping Details and Records if ($this->getSubmittedValue('saveMapping')) { @@ -314,6 +316,7 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms { $this->saveMappingField($savedMappingID, $i, FALSE); } $this->set('savedMapping', $savedMappingID); + $this->updateUserJobMetadata('mapping', ['id' => $savedMappingID]); } } @@ -410,4 +413,47 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms { return [$sel, $headerPatterns]; } + /** + * Add the saved mapping fields to the form. + * + * @throws \CRM_Core_Exception + */ + protected function addSavedMappingFields(): void { + $savedMappingID = (int) $this->getSubmittedValue('savedMapping'); + $this->buildSavedMappingFields($savedMappingID); + $this->addFormRule(['CRM_Import_Form_MapField', 'mappingRule']); + } + + /** + * Global validation rules for the form. + * + * @param array $fields + * Posted values of the form. + * + * @return array|true + * list of errors to be posted back to the form + */ + public static function mappingRule($fields) { + $errors = []; + if (!empty($fields['saveMapping'])) { + $nameField = $fields['saveMappingName'] ?? NULL; + if (empty($nameField)) { + $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); + } + else { + $mappingTypeId = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contact'); + if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { + $errors['saveMappingName'] = ts('Duplicate Import Mapping Name'); + } + } + } + // This is horrible & should be removed once gone from tpl + if (!empty($errors['saveMappingName'])) { + $_flag = 1; + $assignError = new CRM_Core_Page(); + $assignError->assign('mappingDetailsError', $_flag); + } + return empty($errors) ? TRUE : $errors; + } + } -- 2.25.1