4 * Trait CRM_Contact_Import_MetadataTrait
6 * Trait for handling contact import specific metadata so it
7 * does not need to be passed from one form to the next.
9 trait CRM_Contact_Import_MetadataTrait
{
12 * Get metadata for contact importable fields.
16 protected function getContactImportMetadata(): array {
17 $cacheKey = 'importable_contact_field_metadata' . $this->getContactType() . $this->getContactSubType();
18 if (Civi
::cache('fields')->has($cacheKey)) {
19 return Civi
::cache('fields')->get($cacheKey);
21 $contactFields = CRM_Contact_BAO_Contact
::importableFields($this->getContactType());
22 // exclude the address options disabled in the Address Settings
23 $fields = CRM_Core_BAO_Address
::validateAddressOptions($contactFields);
26 //supporting import for contact subtypes
28 if ($this->getContactSubType()) {
29 //custom fields for sub type
30 $subTypeFields = CRM_Core_BAO_CustomField
::getFieldsForImport($this->getContactSubType());
32 if (!empty($subTypeFields)) {
33 foreach ($subTypeFields as $customSubTypeField => $details) {
34 $fields[$customSubTypeField] = $details;
39 foreach ($this->getRelationships() as $key => $var) {
40 list($type) = explode('_', $key);
41 $relationshipType[$key]['title'] = $var;
42 $relationshipType[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
43 $relationshipType[$key]['import'] = TRUE;
44 $relationshipType[$key]['relationship_type_id'] = $type;
45 $relationshipType[$key]['related'] = TRUE;
48 if (!empty($relationshipType)) {
49 $fields = array_merge($fields, [
51 'title' => ts('- related contact info -'),
53 ], $relationshipType);
55 Civi
::cache('fields')->set($cacheKey, $fields);
60 * Get sorted available relationships.
64 protected function getRelationships(): array {
65 $cacheKey = 'importable_contact_relationship_field_metadata' . $this->getContactType() . $this->getContactSubType();
66 if (Civi
::cache('fields')->has($cacheKey)) {
67 return Civi
::cache('fields')->get($cacheKey);
69 //Relationship importables
70 $relations = CRM_Contact_BAO_Relationship
::getContactRelationshipType(
71 NULL, NULL, NULL, $this->getContactType(),
72 FALSE, 'label', TRUE, $this->getContactSubType()
75 Civi
::cache('fields')->set($cacheKey, $relations);
80 * Get an array of header patterns for importable keys.
84 public function getHeaderPatterns(): array {
85 return CRM_Utils_Array
::collect('headerPattern', $this->getContactImportMetadata());
89 * Get an array of header patterns for importable keys.
93 public function getDataPatterns(): array {
94 return CRM_Utils_Array
::collect('dataPattern', $this->getContactImportMetadata());
98 * Get an array of header patterns for importable keys.
102 public function getFieldTitles() {
103 return CRM_Utils_Array
::collect('title', $this->getContactImportMetadata());