Skip to content

Commit

Permalink
Extract metadata definition into a trait.
Browse files Browse the repository at this point in the history
This metadata needs to be shared between multiple import forms. They do not have a common parent.

Curently this is done via calculating once & passing via quickform. However, the formats
are convoluted & hard to read.

Information that is user generated should be calculated per form - not so much metadata
  • Loading branch information
eileenmcnaughton committed Aug 12, 2019
1 parent f39da48 commit c67a47b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 43 deletions.
62 changes: 62 additions & 0 deletions CRM/Contact/Import/MetadataTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* Trait CRM_Contact_Import_MetadataTrait
*
* Trait for handling contact import specific metadata so it
* does not need to be passed from one form to the next.
*/
trait CRM_Contact_Import_MetadataTrait {

/**
* Get metadata for contact importable fields.
*
* @return array
*/
protected function getContactImportMetadata(): array {
$contactFields = CRM_Contact_BAO_Contact::importableFields($this->_contactType);
// exclude the address options disabled in the Address Settings
$fields = CRM_Core_BAO_Address::validateAddressOptions($contactFields);

//CRM-5125
//supporting import for contact subtypes
$csType = NULL;
if (!empty($this->_contactSubType)) {
//custom fields for sub type
$subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($this->_contactSubType);

if (!empty($subTypeFields)) {
foreach ($subTypeFields as $customSubTypeField => $details) {
$fields[$customSubTypeField] = $details;
}
}
}

//Relationship importables
$this->_relationships = $relations
= CRM_Contact_BAO_Relationship::getContactRelationshipType(
NULL, NULL, NULL, $this->_contactType,
FALSE, 'label', TRUE, $this->_contactSubType
);
asort($relations);

foreach ($relations as $key => $var) {
list($type) = explode('_', $key);
$relationshipType[$key]['title'] = $var;
$relationshipType[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
$relationshipType[$key]['import'] = TRUE;
$relationshipType[$key]['relationship_type_id'] = $type;
$relationshipType[$key]['related'] = TRUE;
}

if (!empty($relationshipType)) {
$fields = array_merge($fields, [
'related' => [
'title' => ts('- related contact info -'),
],
], $relationshipType);
}
return $fields;
}

}
47 changes: 4 additions & 43 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
* class to parse contact csv files
*/
class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {

use CRM_Contact_Import_MetadataTrait;

protected $_mapperKeys = [];
protected $_mapperLocType = [];
protected $_mapperPhoneType;
Expand Down Expand Up @@ -2054,49 +2057,7 @@ public static function getParameterForParser($count) {
* Set field metadata.
*/
protected function setFieldMetadata() {
$contactFields = CRM_Contact_BAO_Contact::importableFields($this->_contactType);
// exclude the address options disabled in the Address Settings
$fields = CRM_Core_BAO_Address::validateAddressOptions($contactFields);

//CRM-5125
//supporting import for contact subtypes
$csType = NULL;
if (!empty($this->_contactSubType)) {
//custom fields for sub type
$subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($this->_contactSubType);

if (!empty($subTypeFields)) {
foreach ($subTypeFields as $customSubTypeField => $details) {
$fields[$customSubTypeField] = $details;
}
}
}

//Relationship importables
$this->_relationships = $relations
= CRM_Contact_BAO_Relationship::getContactRelationshipType(
NULL, NULL, NULL, $this->_contactType,
FALSE, 'label', TRUE, $this->_contactSubType
);
asort($relations);

foreach ($relations as $key => $var) {
list($type) = explode('_', $key);
$relationshipType[$key]['title'] = $var;
$relationshipType[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
$relationshipType[$key]['import'] = TRUE;
$relationshipType[$key]['relationship_type_id'] = $type;
$relationshipType[$key]['related'] = TRUE;
}

if (!empty($relationshipType)) {
$fields = array_merge($fields, [
'related' => [
'title' => ts('- related contact info -'),
],
], $relationshipType);
}
$this->setImportableFieldsMetadata($fields);
$this->setImportableFieldsMetadata($this->getContactImportMetadata());
}

}

0 comments on commit c67a47b

Please sign in to comment.