Commit | Line | Data |
---|---|---|
9ff5f6c0 N |
1 | <?php |
2 | class CRM_Custom_Import_Form_MapField extends CRM_Contact_Import_Form_MapField { | |
3 | protected $_parser = 'CRM_Custom_Import_Parser_Api'; | |
4 | protected $_mappingType = 'Import Multi value custom data'; | |
5 | protected $_highlightedFields = array(); | |
6 | /** | |
7 | * entity being imported to | |
8 | * @var string | |
9 | */ | |
10 | protected $_entity; | |
11 | /** | |
12 | * Function to set variables up before form is built | |
13 | * | |
14 | * @return void | |
15 | * @access public | |
16 | */ | |
17 | public function preProcess() { | |
18 | $this->_mapperFields = $this->get('fields'); | |
19 | asort($this->_mapperFields); | |
20 | $this->_columnCount = $this->get('columnCount'); | |
21 | $this->_columnCount = $this->get('columnCount'); | |
22 | $this->assign('columnCount', $this->_columnCount); | |
23 | $this->_dataValues = $this->get('dataValues'); | |
24 | $this->assign('dataValues', $this->_dataValues); | |
25 | $this->_entity = $this->_multipleCustomData = $this->get('multipleCustomData'); | |
26 | ||
27 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); | |
28 | $this->_onDuplicate = $this->get('onDuplicate'); | |
29 | if ($skipColumnHeader) { | |
30 | $this->assign('skipColumnHeader', $skipColumnHeader); | |
31 | $this->assign('rowDisplayCount', 3); | |
32 | /* if we had a column header to skip, stash it for later */ | |
33 | ||
34 | $this->_columnHeaders = $this->_dataValues[0]; | |
35 | } | |
36 | else { | |
37 | $this->assign('rowDisplayCount', 2); | |
38 | } | |
39 | $this->assign('highlightedFields', $this->_highlightedFields); | |
40 | } | |
41 | ||
42 | /** | |
43 | * Function to actually build the form | |
44 | * | |
45 | * @return void | |
46 | * @access public | |
47 | */ | |
48 | public function buildQuickForm() { | |
49 | parent::buildQuickForm(); | |
50 | $this->addFormRule(array('CRM_Custom_Import_Form_MapField', 'formRule')); | |
51 | } | |
52 | ||
53 | /** | |
54 | * global validation rules for the form | |
55 | * | |
56 | * @param array $fields posted values of the form | |
57 | * | |
58 | * @return array list of errors to be posted back to the form | |
59 | * @static | |
60 | * @access public | |
61 | */ | |
62 | static function formRule($fields, $files, $self) { | |
63 | $errors = array(); | |
64 | $fieldMessage = NULL; | |
65 | if (!array_key_exists('savedMapping', $fields)) { | |
66 | $importKeys = array(); | |
67 | foreach ($fields['mapper'] as $mapperPart) { | |
68 | $importKeys[] = $mapperPart[0]; | |
69 | } | |
70 | $requiredFields = array( | |
71 | 'contact_id' => ts('Contact ID'), | |
72 | ); | |
73 | foreach ($requiredFields as $field => $title) { | |
74 | if (!in_array($field, $importKeys)) { | |
75 | if (!isset($errors['_qf_default'])) { | |
76 | $errors['_qf_default'] = ''; | |
77 | } | |
78 | $errors['_qf_default'] .= ts('Missing required field: %1', array(1 => $title)); | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | if (CRM_Utils_Array::value('saveMapping', $fields)) { | |
84 | $nameField = CRM_Utils_Array::value('saveMappingName', $fields); | |
85 | if (empty($nameField)) { | |
86 | $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); | |
87 | } | |
88 | else { | |
89 | $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', $self->_mappingType, 'name'); | |
90 | if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { | |
91 | $errors['saveMappingName'] = ts('Duplicate ' . $self->_mappingType . 'Mapping Name'); | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
96 | //display Error if loaded mapping is not selected | |
97 | if (array_key_exists('loadMapping', $fields)) { | |
98 | $getMapName = CRM_Utils_Array::value('savedMapping', $fields); | |
99 | if (empty($getMapName)) { | |
100 | $errors['savedMapping'] = ts('Select saved mapping'); | |
101 | } | |
102 | } | |
103 | ||
104 | if (!empty($errors)) { | |
105 | if (!empty($errors['saveMappingName'])) { | |
106 | $_flag = 1; | |
107 | $assignError = new CRM_Core_Page(); | |
108 | $assignError->assign('mappingDetailsError', $_flag); | |
109 | } | |
110 | return $errors; | |
111 | } | |
112 | ||
113 | return TRUE; | |
114 | } | |
115 | ||
116 | /** | |
117 | * Process the mapped fields and map it into the uploaded file | |
118 | * preview the file and extract some summary statistics | |
119 | * | |
120 | * @return void | |
121 | * @access public | |
122 | */ | |
123 | public function postProcess() { | |
124 | $params = $this->controller->exportValues('MapField'); | |
125 | $this->set('multipleCustomData', $this->_multipleCustomData); | |
126 | ||
127 | //reload the mapfield if load mapping is pressed | |
128 | if (!empty($params['savedMapping'])) { | |
129 | $this->set('savedMapping', $params['savedMapping']); | |
130 | $this->controller->resetPage($this->_name); | |
131 | return; | |
132 | } | |
133 | ||
134 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); | |
135 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); | |
136 | $this->_entity = $this->controller->exportValue('DataSource', 'entity'); | |
137 | ||
138 | $config = CRM_Core_Config::singleton(); | |
139 | $separator = $config->fieldSeparator; | |
140 | ||
141 | $mapperKeys = array(); | |
142 | $mapper = array(); | |
143 | $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); | |
144 | $mapperKeysMain = array(); | |
145 | ||
146 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
147 | $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]]; | |
148 | $mapperKeysMain[$i] = $mapperKeys[$i][0]; | |
149 | } | |
150 | ||
151 | $this->set('mapper', $mapper); | |
152 | ||
153 | // store mapping Id to display it in the preview page | |
154 | $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params)); | |
155 | ||
156 | //Updating Mapping Records | |
157 | if (CRM_Utils_Array::value('updateMapping', $params)) { | |
158 | ||
159 | $mappingFields = new CRM_Core_DAO_MappingField(); | |
160 | $mappingFields->mapping_id = $params['mappingId']; | |
161 | $mappingFields->find(); | |
162 | ||
163 | $mappingFieldsId = array(); | |
164 | while ($mappingFields->fetch()) { | |
165 | if ($mappingFields->id) { | |
166 | $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id; | |
167 | } | |
168 | } | |
169 | ||
170 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
171 | $updateMappingFields = new CRM_Core_DAO_MappingField(); | |
172 | $updateMappingFields->id = $mappingFieldsId[$i]; | |
173 | $updateMappingFields->mapping_id = $params['mappingId']; | |
174 | $updateMappingFields->column_number = $i; | |
175 | ||
176 | $explodedValues = explode('_', $mapperKeys[$i][0]); | |
177 | $id = CRM_Utils_Array::value(0, $explodedValues); | |
178 | $first = CRM_Utils_Array::value(1, $explodedValues); | |
179 | $second = CRM_Utils_Array::value(2, $explodedValues); | |
180 | ||
181 | $updateMappingFields->name = $mapper[$i]; | |
182 | $updateMappingFields->save(); | |
183 | } | |
184 | } | |
185 | ||
186 | //Saving Mapping Details and Records | |
187 | if (CRM_Utils_Array::value('saveMapping', $params)) { | |
188 | $mappingParams = array( | |
189 | 'name' => $params['saveMappingName'], | |
190 | 'description' => $params['saveMappingDesc'], | |
191 | 'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type', | |
192 | $this->_mappingType, | |
193 | 'name' | |
194 | ), | |
195 | ); | |
196 | $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); | |
197 | ||
198 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
199 | $saveMappingFields = new CRM_Core_DAO_MappingField(); | |
200 | $saveMappingFields->mapping_id = $saveMapping->id; | |
201 | $saveMappingFields->column_number = $i; | |
202 | ||
203 | $explodedValues = explode('_', $mapperKeys[$i][0]); | |
204 | $id = CRM_Utils_Array::value(0, $explodedValues); | |
205 | $first = CRM_Utils_Array::value(1, $explodedValues); | |
206 | $second = CRM_Utils_Array::value(2, $explodedValues); | |
207 | ||
208 | $saveMappingFields->name = $mapper[$i]; | |
209 | $saveMappingFields->save(); | |
210 | } | |
211 | $this->set('savedMapping', $saveMappingFields->mapping_id); | |
212 | } | |
213 | $this->set('_entity', $this->_entity); | |
214 | ||
215 | $parser = new $this->_parser($mapperKeysMain); | |
216 | $parser->setEntity($this->_multipleCustomData); | |
217 | $parser->run($fileName, $separator, $mapper, $skipColumnHeader, | |
218 | CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType') | |
219 | ); | |
220 | // add all the necessary variables to the form | |
221 | $parser->set($this); | |
222 | } | |
223 | } |