<?php
+use Civi\Api4\Mapping;
+use Civi\Api4\MappingField;
+
/**
* Class CRM_Import_ImportProcessor.
*
*/
protected $metadata = [];
+ /**
+ * Id of the created user job.
+ *
+ * @var int
+ */
+ protected $userJobID;
+
+ /**
+ * @return int
+ */
+ public function getUserJobID(): int {
+ return $this->userJobID;
+ }
+
+ /**
+ * @param int $userJobID
+ */
+ public function setUserJobID(int $userJobID): void {
+ $this->userJobID = $userJobID;
+ }
+
/**
* Metadata keyed by field title.
*
$this->getFieldWebsiteTypes()
// $mapperRelatedContactWebsiteType = []
);
+ $importer->setUserJobID($this->getUserJobID());
$importer->init();
- $importer->_contactType = $this->getContactType();
return $importer;
}
* @throws \CiviCRM_API3_Exception
*/
protected function loadSavedMapping() {
+ $fields = civicrm_api3('MappingField', 'get', [
+ 'mapping_id' => $this->getMappingID(),
+ 'options' => ['limit' => 0],
+ ])['values'];
+ foreach ($fields as $index => $field) {
+ $fieldSpec = $this->getMetadata()[$fields[$index]['name']];
+ $fields[$index]['label'] = $fieldSpec['title'];
+ if (empty($field['location_type_id']) && !empty($fieldSpec['hasLocationType'])) {
+ $fields[$index]['location_type_id'] = 'Primary';
+ }
+ }
+ $this->mappingFields = $this->rekeyBySortedColumnNumbers($fields);
+ }
+
+ /**
+ * Load the mapping from the database into the pre-5.50 format.
+ *
+ * This is preserved as a copy the upgrade script can use - since the
+ * upgrade allows the other to be 'fixed'.
+ *
+ * @throws \CiviCRM_API3_Exception
+ */
+ protected function legacyLoadSavedMapping() {
$fields = civicrm_api3('MappingField', 'get', [
'mapping_id' => $this->getMappingID(),
'options' => ['limit' => 0],
return [(string) $this->getFieldName($column), $this->getLocationTypeID($column), $this->getPhoneOrIMTypeID($column)];
}
+ /**
+ * This exists for use in the FiveFifty Upgrade
+ *
+ * @throws \API_Exception|\CiviCRM_API3_Exception
+ */
+ public static function convertSavedFields(): void {
+ $mappings = Mapping::get(FALSE)
+ ->setSelect(['id', 'contact_type'])
+ ->addWhere('mapping_type_id:name', '=', 'Import Contact')
+ ->execute();
+
+ foreach ($mappings as $mapping) {
+ $processor = new CRM_Import_ImportProcessor();
+ $processor->setMappingID($mapping['id']);
+ $processor->setMetadata(CRM_Contact_BAO_Contact::importableFields('All'));
+ $processor->legacyLoadSavedMapping();;
+ foreach ($processor->getMappingFields() as $field) {
+ // The if is mostly precautionary against running this more than once
+ // - which is common in dev if not live...
+ if ($field['name']) {
+ MappingField::update(FALSE)
+ ->setValues(['name' => $field['name']])
+ ->addWhere('id', '=', $field['id'])
+ ->execute();
+ }
+ }
+ }
+ }
+
}