Merge pull request #15028 from eileenmcnaughton/import_processor
[civicrm-core.git] / CRM / Import / ImportProcessor.php
1 <?php
2
3 /**
4 * Class CRM_Import_ImportProcessor.
5 *
6 * Import processor class. This is intended to provide a sanitising wrapper around
7 * the form-oriented import classes. In particular it is intended to provide a clear translation
8 * between the saved mapping field format and the quick form & parser formats.
9 *
10 * In the first instance this is only being used in unit tests but the intent is to migrate
11 * to it on a trajectory similar to the ExportProcessor so it is not in the tests.
12 */
13 class CRM_Import_ImportProcessor {
14
15 /**
16 * An array of fields in the format used in the table civicrm_mapping_field.
17 *
18 * @var array
19 */
20 protected $mappingFields = [];
21
22 /**
23 * Get contact type being imported.
24 *
25 * @var string
26 */
27 protected $contactType;
28
29 /**
30 * @return string
31 */
32 public function getContactType(): string {
33 return $this->contactType;
34 }
35
36 /**
37 * @param string $contactType
38 */
39 public function setContactType(string $contactType) {
40 $this->contactType = $contactType;
41 }
42
43 /**
44 * @return array
45 */
46 public function getMappingFields(): array {
47 return $this->mappingFields;
48 }
49
50 /**
51 * @param array $mappingFields
52 */
53 public function setMappingFields(array $mappingFields) {
54 $this->mappingFields = CRM_Utils_Array::rekey($mappingFields, 'column_number');
55 ksort($this->mappingFields);
56 $this->mappingFields = array_values($this->mappingFields);
57 }
58
59 /**
60 * Get the names of the mapped fields.
61 */
62 public function getFieldNames() {
63 return CRM_Utils_Array::collect('name', $this->getMappingFields());
64 }
65
66 /**
67 * Get the location types of the mapped fields.
68 */
69 public function getFieldLocationTypes() {
70 return CRM_Utils_Array::collect('location_type_id', $this->getMappingFields());
71 }
72
73 /**
74 * Get the phone types of the mapped fields.
75 */
76 public function getFieldPhoneTypes() {
77 return CRM_Utils_Array::collect('phone_type_id', $this->getMappingFields());
78 }
79
80 /**
81 * Get the names of the im_provider fields.
82 */
83 public function getFieldIMProviderTypes() {
84 return CRM_Utils_Array::collect('im_provider_id', $this->getMappingFields());
85 }
86
87 /**
88 * Get the names of the website fields.
89 */
90 public function getFieldWebsiteTypes() {
91 return CRM_Utils_Array::collect('im_provider_id', $this->getMappingFields());
92 }
93
94 /**
95 * Get an instance of the importer object.
96 *
97 * @return CRM_Contact_Import_Parser_Contact
98 */
99 public function getImporterObject() {
100 $importer = new CRM_Contact_Import_Parser_Contact(
101 $this->getFieldNames(),
102 $this->getFieldLocationTypes(),
103 $this->getFieldPhoneTypes(),
104 $this->getFieldIMProviderTypes(),
105 // @todo - figure out related mappings.
106 // $mapperRelated = [], $mapperRelatedContactType = [], $mapperRelatedContactDetails = [], $mapperRelatedContactLocType = [], $mapperRelatedContactPhoneType = [], $mapperRelatedContactImProvider = [],
107 [],
108 [],
109 [],
110 [],
111 [],
112 [],
113 $this->getFieldWebsiteTypes()
114 // $mapperRelatedContactWebsiteType = []
115 );
116 $importer->init();
117 $importer->_contactType = $this->getContactType();
118 return $importer;
119 }
120
121 }