[Import] [Ref] Extract shared form rule code
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 10 Aug 2022 07:04:44 +0000 (19:04 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 10 Aug 2022 21:40:05 +0000 (09:40 +1200)
CRM/Activity/Import/Form/MapField.php
CRM/Contact/Import/Form/MapField.php
CRM/Import/Form/MapField.php

index 155d6357e424e893a0092c77cb2d946a73c01429..6597972c63c966cc1dc8b7761c0d97a637efc6cf 100644 (file)
@@ -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;
   }
 
   /**
index 833eb631cc3c6863721288ab644801e6257f9ea8..504f0f1cef4f8fa6118c0a9f20347173fd90da7e 100644 (file)
@@ -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);
   }
 
   /**
index c374e1d9560dd5bdc8939f546d145bf6667b326c..7961538babe047323ab90cdf654da6233517d101 100644 (file)
@@ -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;
+  }
+
 }