dev/core#183 Convert Temporary Table creation in CRM_Dedupe_BAO_RuleGroup to CRM_Util...
[civicrm-core.git] / CRM / Import / Parser.php
index cf01fdd054821daa1bdca2506520ea979d334f2e..1510b80db569adefdf957cb63c5eef5c19a09174 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
+ | Copyright CiviCRM LLC (c) 2004-2020                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC (c) 2004-2020
  */
 abstract class CRM_Import_Parser {
   /**
@@ -137,6 +137,33 @@ abstract class CRM_Import_Parser {
    */
   protected $_fields;
 
+  /**
+   * Metadata for all available fields, keyed by unique name.
+   *
+   * This is intended to supercede $_fields which uses a special sauce format which
+   * importableFieldsMetadata uses the standard getfields type format.
+   *
+   * @var array
+   */
+  protected $importableFieldsMetadata = [];
+
+  /**
+   * Get metadata for all importable fields in std getfields style format.
+   *
+   * @return array
+   */
+  public function getImportableFieldsMetadata(): array {
+    return $this->importableFieldsMetadata;
+  }
+
+  /**
+   * Set metadata for all importable fields in std getfields style format.
+   * @param array $importableFieldsMetadata
+   */
+  public function setImportableFieldsMetadata(array $importableFieldsMetadata) {
+    $this->importableFieldsMetadata = $importableFieldsMetadata;
+  }
+
   /**
    * Array of the fields that are actually part of the import process
    * the position in the array also dictates their position in the import
@@ -520,4 +547,40 @@ abstract class CRM_Import_Parser {
     return $error;
   }
 
+  /**
+   * Parse a field which could be represented by a label or name value rather than the DB value.
+   *
+   * We will try to match name first or (per https://lab.civicrm.org/dev/core/issues/1285 if we have an id.
+   *
+   * but if not available then see if we have a label that can be converted to a name.
+   *
+   * @param string|int|null $submittedValue
+   * @param array $fieldSpec
+   *   Metadata for the field
+   *
+   * @return mixed
+   */
+  protected function parsePseudoConstantField($submittedValue, $fieldSpec) {
+    // dev/core#1289 Somehow we have wound up here but the BAO has not been specified in the fieldspec so we need to check this but future us problem, for now lets just return the submittedValue
+    if (!isset($fieldSpec['bao'])) {
+      return $submittedValue;
+    }
+    /* @var \CRM_Core_DAO $bao */
+    $bao = $fieldSpec['bao'];
+    // For historical reasons use validate as context - ie disabled name matches ARE permitted.
+    $nameOptions = $bao::buildOptions($fieldSpec['name'], 'validate');
+    if (isset($nameOptions[$submittedValue])) {
+      return $submittedValue;
+    }
+    if (in_array($submittedValue, $nameOptions)) {
+      return array_search($submittedValue, $nameOptions, TRUE);
+    }
+
+    $labelOptions = array_flip($bao::buildOptions($fieldSpec['name'], 'match'));
+    if (isset($labelOptions[$submittedValue])) {
+      return array_search($labelOptions[$submittedValue], $nameOptions, TRUE);
+    }
+    return '';
+  }
+
 }