Add extra shared import functions
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 6 Jun 2022 03:18:53 +0000 (15:18 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 6 Jun 2022 04:46:38 +0000 (16:46 +1200)
This is an attempt to get away from fixing known breakage
being blocked by
https://github.com/civicrm/civicrm-core/pull/23689

These functions should be mostly as-yet-unused, or only minorly changed
but required to fix the known issues

CRM/Import/Form/MapField.php
CRM/Import/Form/Preview.php
CRM/Import/Form/Summary.php
CRM/Import/Parser.php

index 11581f2a4699395d8bb50520d111de74667f78e1..26f90307782bcf46b62090b1e708cbe3f25f3fcd 100644 (file)
@@ -76,6 +76,22 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms {
     parent::preProcess();
   }
 
+  /**
+   * Process the mapped fields and map it into the uploaded file
+   * preview the file and extract some summary statistics
+   *
+   * @return void
+   * @noinspection PhpDocSignatureInspection
+   * @noinspection PhpUnhandledExceptionInspection
+   */
+  public function postProcess() {
+    $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
+    $this->saveMapping($this->getMappingTypeName());
+    $parser = $this->getParser();
+    $parser->init();
+    $parser->validate();
+  }
+
   /**
    * Attempt to match header labels with our mapper fields.
    *
@@ -230,6 +246,9 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms {
   protected function saveMappingField(int $mappingID, int $columnNumber, bool $isUpdate = FALSE): void {
     $fieldMapping = (array) $this->getSubmittedValue('mapper')[$columnNumber];
     $mappedField = $this->getMappedField($fieldMapping, $mappingID, $columnNumber);
+    if (empty($mappedField['name'])) {
+      $mappedField['name'] = 'do_not_import';
+    }
     if ($isUpdate) {
       Civi\Api4\MappingField::update(FALSE)
         ->setValues($mappedField)
index cce75402e92894a8b428262955b9768ed5f33c1d..69e302d8c99aae9695a3911065cdec620d787a90 100644 (file)
@@ -117,4 +117,14 @@ abstract class CRM_Import_Form_Preview extends CRM_Import_Forms {
     $this->assign('rowDisplayCount', $this->getSubmittedValue('skipColumnHeader') ? 3 : 2);
   }
 
+  /**
+   * Process the mapped fields and map it into the uploaded file
+   * preview the file and extract some summary statistics
+   *
+   * @return void
+   */
+  public function postProcess() {
+    CRM_Import_Parser::runImport(NULL, $this->getUserJobID(), 0);
+  }
+
 }
index b27af617eea1d085e0965d99ccd891438a6e9a09..72c8be2f2744c619c6229ceb1ceff86613774648 100644 (file)
  */
 abstract class CRM_Import_Form_Summary extends CRM_Import_Forms {
 
+  /**
+   * Set variables up before form is built.
+   *
+   * @return void
+   */
+  public function preProcess() {
+    $this->assignOutputURLs();
+  }
+
   /**
    * Build the form object.
    */
@@ -45,4 +54,17 @@ abstract class CRM_Import_Form_Summary extends CRM_Import_Forms {
     return ts('Summary');
   }
 
+  protected function assignOutputURLs(): void {
+    $this->assign('totalRowCount', $this->getRowCount());
+    $this->assign('validRowCount', $this->getRowCount(CRM_Import_Parser::VALID) + $this->getRowCount(CRM_Import_Parser::UNPARSED_ADDRESS_WARNING));
+    $this->assign('invalidRowCount', $this->getRowCount(CRM_Import_Parser::ERROR));
+    $this->assign('duplicateRowCount', $this->getRowCount(CRM_Import_Parser::DUPLICATE));
+    $this->assign('unMatchCount', $this->getRowCount(CRM_Import_Parser::NO_MATCH));
+    $this->assign('unparsedAddressCount', $this->getRowCount(CRM_Import_Parser::UNPARSED_ADDRESS_WARNING));
+    $this->assign('downloadDuplicateRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::DUPLICATE));
+    $this->assign('downloadErrorRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::ERROR));
+    $this->assign('downloadMismatchRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::NO_MATCH));
+    $this->assign('downloadAddressRecordsUrl', $this->getDownloadURL(CRM_Import_Parser::UNPARSED_ADDRESS_WARNING));
+  }
+
 }
index 30b8c8ae5ff407ebc6ea909bb3fb9559be979787..ef1e94583cda3ecf79d3b9e18eb24ce7facf3aad 100644 (file)
@@ -635,8 +635,8 @@ abstract class CRM_Import_Parser {
 
   protected function doPostImportActions() {
     $userJob = $this->getUserJob();
-    $summaryInfo = $userJob['metadata']['summary_info'];
-    $actions = $userJob['metadata']['post_actions'];
+    $summaryInfo = $userJob['metadata']['summary_info'] ?? [];
+    $actions = $userJob['metadata']['post_actions'] ?? [];
     if (!empty($actions['group'])) {
       $groupAdditions = $this->addImportedContactsToNewGroup($this->createdContacts, $actions['group']);
       foreach ($actions['group'] as $groupID) {
@@ -1575,7 +1575,8 @@ abstract class CRM_Import_Parser {
   protected function getInvalidValues($value, string $key = '', string $prefixString = ''): array {
     $errors = [];
     if ($value === 'invalid_import_value') {
-      $errors[] = $prefixString . $this->getFieldMetadata($key)['title'];
+      $metadata = $this->getFieldMetadata($key);
+      $errors[] = $prefixString . ($metadata['html']['label'] ?? $metadata['title']);
     }
     elseif (is_array($value)) {
       foreach ($value as $innerKey => $innerValue) {
@@ -1666,6 +1667,40 @@ abstract class CRM_Import_Parser {
     ];
   }
 
+  /**
+   * @param array $mappedField
+   *   Field detail as would be saved in field_mapping table
+   *   or as returned from getMappingFieldFromMapperInput
+   *
+   * @return string
+   * @throws \API_Exception
+   */
+  public function getMappedFieldLabel(array $mappedField): string {
+    $this->setFieldMetadata();
+    return $this->getFieldMetadata($mappedField['name'])['title'];
+  }
+
+  /**
+   * Get the row from the csv mapped to our parameters.
+   *
+   * @param array $values
+   *
+   * @return array
+   * @throws \API_Exception
+   */
+  public function getMappedRow(array $values): array {
+    $params = [];
+    foreach ($this->getFieldMappings() as $i => $mappedField) {
+      if ($mappedField['name'] === 'do_not_import') {
+        continue;
+      }
+      if ($mappedField['name']) {
+        $params[$this->getFieldMetadata($mappedField['name'])['name']] = $this->getTransformedFieldValue($mappedField['name'], $values[$i]);
+      }
+    }
+    return $params;
+  }
+
   /**
    * Get the field mappings for the import.
    *
@@ -1678,7 +1713,8 @@ abstract class CRM_Import_Parser {
    */
   protected function getFieldMappings(): array {
     $mappedFields = [];
-    foreach ($this->getSubmittedValue('mapper') as $i => $mapperRow) {
+    $mapper = $this->getSubmittedValue('mapper');
+    foreach ($mapper as $i => $mapperRow) {
       $mappedField = $this->getMappingFieldFromMapperInput($mapperRow, 0, $i);
       // Just for clarity since 0 is a pseudo-value
       unset($mappedField['mapping_id']);