91b4c63e |
1 | <?php |
2 | |
3 | /** |
4 | * Trait CRM_Contact_Import_MetadataTrait |
5 | * |
6 | * Trait for handling contact import specific metadata so it |
7 | * does not need to be passed from one form to the next. |
8 | */ |
9 | trait CRM_Contact_Import_MetadataTrait { |
10 | |
11 | /** |
12 | * Get metadata for contact importable fields. |
13 | * |
14 | * @return array |
15 | */ |
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); |
20 | } |
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); |
24 | |
25 | //CRM-5125 |
26 | //supporting import for contact subtypes |
27 | $csType = NULL; |
28 | if ($this->getContactSubType()) { |
29 | //custom fields for sub type |
30 | $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($this->getContactSubType()); |
31 | |
32 | if (!empty($subTypeFields)) { |
33 | foreach ($subTypeFields as $customSubTypeField => $details) { |
34 | $fields[$customSubTypeField] = $details; |
35 | } |
36 | } |
37 | } |
38 | |
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; |
46 | } |
47 | |
48 | if (!empty($relationshipType)) { |
49 | $fields = array_merge($fields, [ |
50 | 'related' => [ |
51 | 'title' => ts('- related contact info -'), |
52 | ], |
53 | ], $relationshipType); |
54 | } |
55 | Civi::cache('fields')->set($cacheKey, $fields); |
56 | return $fields; |
57 | } |
58 | |
59 | /** |
60 | * Get sorted available relationships. |
61 | * |
62 | * @return array |
63 | */ |
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); |
68 | } |
69 | //Relationship importables |
70 | $relations = CRM_Contact_BAO_Relationship::getContactRelationshipType( |
71 | NULL, NULL, NULL, $this->getContactType(), |
72 | FALSE, 'label', TRUE, $this->getContactSubType() |
73 | ); |
74 | asort($relations); |
75 | Civi::cache('fields')->set($cacheKey, $relations); |
76 | return $relations; |
77 | } |
78 | |
79 | /** |
80 | * Get an array of header patterns for importable keys. |
81 | * |
82 | * @return array |
83 | */ |
84 | public function getHeaderPatterns() { |
85 | return CRM_Utils_Array::collect('headerPattern', $this->getContactImportMetadata()); |
86 | } |
87 | |
88 | /** |
89 | * Get an array of header patterns for importable keys. |
90 | * |
91 | * @return array |
92 | */ |
93 | public function getDataPatterns() { |
94 | return CRM_Utils_Array::collect('dataPattern', $this->getContactImportMetadata()); |
95 | } |
96 | |
97 | /** |
98 | * Get an array of header patterns for importable keys. |
99 | * |
100 | * @return array |
101 | */ |
102 | public function getFieldTitles() { |
103 | return CRM_Utils_Array::collect('title', $this->getContactImportMetadata()); |
104 | } |
105 | |
91b4c63e |
106 | } |