province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Import / Parser.php
index c4989ba3ebf1887634ffbb43bc07746b69bb0a0f..57c367ef19fd550c24c0e5d50d0c19d2861741ca 100644 (file)
@@ -13,17 +13,18 @@ use Civi\Api4\Campaign;
 use Civi\Api4\CustomField;
 use Civi\Api4\Event;
 use Civi\Api4\UserJob;
+use Civi\UserJob\UserJobInterface;
 
 /**
  *
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
-abstract class CRM_Import_Parser {
+abstract class CRM_Import_Parser implements UserJobInterface {
   /**
    * Settings
    */
-  const MAX_WARNINGS = 25, DEFAULT_TIMEOUT = 30;
+  const MAX_ERRORS = 10000, MAX_WARNINGS = 25, DEFAULT_TIMEOUT = 30;
 
   /**
    * Return codes
@@ -699,8 +700,9 @@ abstract class CRM_Import_Parser {
         $batchSize = $totalRows;
       }
       $task = new CRM_Queue_Task(
-        [get_class($this), 'runImport'],
-        ['userJobID' => $this->getUserJobID(), 'limit' => $batchSize],
+        [get_class($this), 'runJob'],
+        // Offset is unused by our import classes, but required by the interface.
+        ['userJobID' => $this->getUserJobID(), 'limit' => $batchSize, 'offset' => 0],
         ts('Processed %1 rows out of %2', [1 => $offset + $batchSize, 2 => $totalRowCount])
       );
       $queue->createItem($task);
@@ -795,7 +797,7 @@ abstract class CRM_Import_Parser {
   /**
    * Determines the file name based on error code.
    *
-   * @var $type error code constant
+   * @var int $type code constant
    * @return string
    */
   public static function saveFileName($type) {
@@ -1275,6 +1277,9 @@ abstract class CRM_Import_Parser {
    * @throws \CRM_Core_Exception Exception thrown if field requirements are not met.
    */
   protected function validateRequiredFields(array $requiredFields, array $params, $prefixString = ''): void {
+    if (empty($requiredFields)) {
+      return;
+    }
     $missingFields = [];
     foreach ($requiredFields as $key => $required) {
       if (!is_array($required)) {
@@ -1296,7 +1301,7 @@ abstract class CRM_Import_Parser {
       }
       else {
         foreach ($required as $field => $label) {
-          if (empty($params[$field])) {
+          if (!isset($params[$field]) || $params[$field] === '') {
             $missing[$field] = $label;
           }
         }
@@ -1385,6 +1390,10 @@ abstract class CRM_Import_Parser {
         return Civi::$statics[__CLASS__][$fieldName][$importedValue] ?? 'invalid_import_value';
       }
     }
+    if ($fieldMetadata['type'] === CRM_Utils_Type::T_INT) {
+      // We have resolved the options now so any remaining ones should be integers.
+      return CRM_Utils_Rule::numeric($importedValue) ? $importedValue : 'invalid_import_value';
+    }
     return $importedValue;
   }
 
@@ -1613,7 +1622,7 @@ abstract class CRM_Import_Parser {
    */
   protected function getFieldEntity(string $fieldName) {
     if ($fieldName === 'do_not_import') {
-      return NULL;
+      return '';
     }
     if (in_array($fieldName, ['email_greeting_id', 'postal_greeting_id', 'addressee_id'], TRUE)) {
       return 'Contact';
@@ -1880,16 +1889,17 @@ abstract class CRM_Import_Parser {
    *
    * @param int $userJobID
    * @param int $limit
+   * @param int $offset
    *
    * @return bool
    * @throws \API_Exception
    * @throws \CRM_Core_Exception
    */
-  public static function runImport($taskContext, $userJobID, $limit) {
-    $userJob = UserJob::get()->addWhere('id', '=', $userJobID)->addSelect('type_id')->execute()->first();
+  public static function runJob(\CRM_Queue_TaskContext $taskContext, int $userJobID, int $limit, int $offset): bool {
+    $userJob = UserJob::get()->addWhere('id', '=', $userJobID)->addSelect('job_type')->execute()->first();
     $parserClass = NULL;
     foreach (CRM_Core_BAO_UserJob::getTypes() as $userJobType) {
-      if ($userJob['type_id'] === $userJobType['id']) {
+      if ($userJob['job_type'] === $userJobType['id']) {
         $parserClass = $userJobType['class'];
       }
     }