Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class gets the name of the file to upload | |
20 | */ | |
b26295b8 | 21 | class CRM_Event_Import_Form_MapField extends CRM_Import_Form_MapField { |
6a488035 | 22 | |
6a488035 | 23 | /** |
fe482240 | 24 | * Set variables up before form is built. |
6a488035 TO |
25 | * |
26 | * @return void | |
6a488035 TO |
27 | */ |
28 | public function preProcess() { | |
29 | $this->_mapperFields = $this->get('fields'); | |
30 | asort($this->_mapperFields); | |
31 | unset($this->_mapperFields['participant_is_test']); | |
32 | $this->_columnCount = $this->get('columnCount'); | |
33 | $this->assign('columnCount', $this->_columnCount); | |
34 | $this->_dataValues = $this->get('dataValues'); | |
35 | $this->assign('dataValues', $this->_dataValues); | |
36 | ||
353ffa53 | 37 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
6a488035 | 38 | $this->_onDuplicate = $this->get('onDuplicate'); |
affcc9d2 | 39 | $highlightedFields = []; |
6a488035 TO |
40 | if ($skipColumnHeader) { |
41 | $this->assign('skipColumnHeader', $skipColumnHeader); | |
42 | $this->assign('rowDisplayCount', 3); | |
43 | /* if we had a column header to skip, stash it for later */ | |
44 | ||
45 | $this->_columnHeaders = $this->_dataValues[0]; | |
46 | } | |
47 | else { | |
48 | $this->assign('rowDisplayCount', 2); | |
49 | } | |
a05662ef | 50 | if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 TO |
51 | $remove = array('participant_contact_id', 'email', 'first_name', 'last_name', 'external_identifier'); |
52 | foreach ($remove as $value) { | |
53 | unset($this->_mapperFields[$value]); | |
54 | } | |
55 | $highlightedFieldsArray = array('participant_id', 'event_id', 'event_title', 'participant_status_id'); | |
56 | foreach ($highlightedFieldsArray as $name) { | |
57 | $highlightedFields[] = $name; | |
58 | } | |
59 | } | |
a05662ef CW |
60 | elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP || |
61 | $this->_onDuplicate == CRM_Import_Parser::DUPLICATE_NOCHECK | |
6a488035 TO |
62 | ) { |
63 | unset($this->_mapperFields['participant_id']); | |
353ffa53 TO |
64 | $highlightedFieldsArray = array( |
65 | 'participant_contact_id', | |
66 | 'event_id', | |
67 | 'email', | |
68 | 'first_name', | |
69 | 'last_name', | |
70 | 'external_identifier', | |
acb1052e | 71 | 'participant_status_id', |
353ffa53 | 72 | ); |
6a488035 TO |
73 | foreach ($highlightedFieldsArray as $name) { |
74 | $highlightedFields[] = $name; | |
75 | } | |
76 | } | |
77 | $this->assign('highlightedFields', $highlightedFields); | |
78 | } | |
79 | ||
80 | /** | |
fe482240 | 81 | * Build the form object. |
6a488035 TO |
82 | * |
83 | * @return void | |
6a488035 TO |
84 | */ |
85 | public function buildQuickForm() { | |
86 | ||
87 | //to save the current mappings | |
88 | if (!$this->get('savedMapping')) { | |
89 | $saveDetailsName = ts('Save this field mapping'); | |
90 | $this->applyFilter('saveMappingName', 'trim'); | |
91 | $this->add('text', 'saveMappingName', ts('Name')); | |
92 | $this->add('text', 'saveMappingDesc', ts('Description')); | |
93 | } | |
94 | else { | |
95 | $savedMapping = $this->get('savedMapping'); | |
96 | ||
97 | list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping); | |
98 | ||
353ffa53 | 99 | $mappingName = $mappingName[1]; |
6a488035 | 100 | $mappingContactType = $mappingContactType[1]; |
9c1bc317 CW |
101 | $mappingLocation = $mappingLocation['1'] ?? NULL; |
102 | $mappingPhoneType = $mappingPhoneType['1'] ?? NULL; | |
103 | $mappingRelation = $mappingRelation['1'] ?? NULL; | |
6a488035 TO |
104 | |
105 | //mapping is to be loaded from database | |
106 | ||
6a488035 TO |
107 | $this->set('loadedMapping', $savedMapping); |
108 | ||
109 | $getMappingName = new CRM_Core_DAO_Mapping(); | |
110 | $getMappingName->id = $savedMapping; | |
111 | $getMappingName->mapping_type = 'Import Participants'; | |
112 | $getMappingName->find(); | |
113 | while ($getMappingName->fetch()) { | |
114 | $mapperName = $getMappingName->name; | |
115 | } | |
116 | ||
262b7f26 | 117 | $this->assign('savedMappingName', $mapperName); |
6a488035 TO |
118 | |
119 | $this->add('hidden', 'mappingId', $savedMapping); | |
120 | ||
121 | $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); | |
122 | $saveDetailsName = ts('Save as a new field mapping'); | |
123 | $this->add('text', 'saveMappingName', ts('Name')); | |
124 | $this->add('text', 'saveMappingDesc', ts('Description')); | |
125 | } | |
126 | ||
127 | $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)")); | |
128 | ||
129 | $this->addFormRule(array('CRM_Event_Import_Form_MapField', 'formRule'), $this); | |
130 | ||
affcc9d2 | 131 | $defaults = []; |
353ffa53 TO |
132 | $mapperKeys = array_keys($this->_mapperFields); |
133 | $hasHeaders = !empty($this->_columnHeaders); | |
134 | $headerPatterns = $this->get('headerPatterns'); | |
135 | $dataPatterns = $this->get('dataPatterns'); | |
6a488035 | 136 | $hasLocationTypes = $this->get('fieldTypes'); |
6a488035 TO |
137 | /* Initialize all field usages to false */ |
138 | ||
139 | foreach ($mapperKeys as $key) { | |
140 | $this->_fieldUsed[$key] = FALSE; | |
141 | } | |
b2b0530a | 142 | $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
143 | $sel1 = $this->_mapperFields; |
144 | ||
145 | $sel2[''] = NULL; | |
353ffa53 | 146 | $js = "<script type='text/javascript'>\n"; |
6a488035 TO |
147 | $formName = 'document.forms.' . $this->_name; |
148 | ||
149 | //used to warn for mismatch column count or mismatch mapping | |
150 | $warning = 0; | |
151 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
152 | $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL); | |
153 | $jsSet = FALSE; | |
154 | if ($this->get('savedMapping')) { | |
155 | if (isset($mappingName[$i])) { | |
156 | if ($mappingName[$i] != ts('- do not import -')) { | |
157 | ||
158 | $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]); | |
159 | ||
160 | if (!isset($locationId) || !$locationId) { | |
161 | $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n"; | |
162 | } | |
163 | ||
164 | if (!isset($phoneType) || !$phoneType) { | |
165 | $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n"; | |
166 | } | |
167 | ||
168 | $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n"; | |
169 | $defaults["mapper[$i]"] = array( | |
170 | $mappingHeader[0], | |
171 | (isset($locationId)) ? $locationId : "", | |
172 | (isset($phoneType)) ? $phoneType : "", | |
173 | ); | |
174 | $jsSet = TRUE; | |
175 | } | |
176 | else { | |
affcc9d2 | 177 | $defaults["mapper[$i]"] = []; |
6a488035 TO |
178 | } |
179 | if (!$jsSet) { | |
180 | for ($k = 1; $k < 4; $k++) { | |
181 | $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n"; | |
182 | } | |
183 | } | |
184 | } | |
185 | else { | |
186 | // this load section to help mapping if we ran out of saved columns when doing Load Mapping | |
187 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n"; | |
188 | ||
189 | if ($hasHeaders) { | |
190 | $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns)); | |
191 | } | |
192 | else { | |
193 | $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i)); | |
194 | } | |
195 | } | |
196 | //end of load mapping | |
197 | } | |
198 | else { | |
199 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n"; | |
200 | if ($hasHeaders) { | |
201 | // Infer the default from the skipped headers if we have them | |
202 | $defaults["mapper[$i]"] = array( | |
203 | $this->defaultFromHeader($this->_columnHeaders[$i], | |
204 | $headerPatterns | |
205 | ), | |
206 | // $defaultLocationType->id | |
207 | 0, | |
208 | ); | |
209 | } | |
210 | else { | |
211 | // Otherwise guess the default from the form of the data | |
212 | $defaults["mapper[$i]"] = array( | |
213 | $this->defaultFromData($dataPatterns, $i), | |
214 | // $defaultLocationType->id | |
215 | 0, | |
216 | ); | |
217 | } | |
218 | } | |
219 | $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ? $sel3 : "", (isset($sel4)) ? $sel4 : "")); | |
220 | } | |
221 | $js .= "</script>\n"; | |
222 | $this->assign('initHideBoxes', $js); | |
223 | ||
224 | //set warning if mismatch in more than | |
225 | if (isset($mappingName)) { | |
226 | if (($this->_columnCount != count($mappingName))) { | |
227 | $warning++; | |
228 | } | |
229 | } | |
230 | if ($warning != 0 && $this->get('savedMapping')) { | |
231 | $session = CRM_Core_Session::singleton(); | |
232 | $session->setStatus(ts('The data columns in this import file appear to be different from the saved mapping. Please verify that you have selected the correct saved mapping before continuing.')); | |
233 | } | |
234 | else { | |
235 | $session = CRM_Core_Session::singleton(); | |
236 | $session->setStatus(NULL); | |
237 | } | |
238 | ||
239 | $this->setDefaults($defaults); | |
240 | ||
241 | $this->addButtons(array( | |
90b461f1 SL |
242 | array( |
243 | 'type' => 'back', | |
244 | 'name' => ts('Previous'), | |
245 | ), | |
246 | array( | |
247 | 'type' => 'next', | |
248 | 'name' => ts('Continue'), | |
249 | 'spacing' => ' ', | |
250 | 'isDefault' => TRUE, | |
251 | ), | |
252 | array( | |
253 | 'type' => 'cancel', | |
254 | 'name' => ts('Cancel'), | |
255 | ), | |
256 | )); | |
6a488035 TO |
257 | } |
258 | ||
259 | /** | |
fe482240 | 260 | * Global validation rules for the form. |
6a488035 | 261 | * |
d4dd1e85 TO |
262 | * @param array $fields |
263 | * Posted values of the form. | |
6a488035 | 264 | * |
da6b46f4 | 265 | * @param $files |
e8cf95b4 | 266 | * @param self $self |
da6b46f4 | 267 | * |
a6c01b45 CW |
268 | * @return array |
269 | * list of errors to be posted back to the form | |
6a488035 | 270 | */ |
00be9182 | 271 | public static function formRule($fields, $files, $self) { |
affcc9d2 | 272 | $errors = []; |
e1a33ae4 | 273 | // define so we avoid notices below |
274 | $errors['_qf_default'] = ''; | |
02a237ce | 275 | $contactFieldsBelowWeightMessage = NULL; |
6a488035 | 276 | if (!array_key_exists('savedMapping', $fields)) { |
affcc9d2 | 277 | $importKeys = []; |
6a488035 TO |
278 | foreach ($fields['mapper'] as $mapperPart) { |
279 | $importKeys[] = $mapperPart[0]; | |
280 | } | |
281 | // FIXME: should use the schema titles, not redeclare them | |
282 | $requiredFields = array( | |
283 | 'participant_contact_id' => ts('Contact ID'), | |
284 | 'event_id' => ts('Event ID'), | |
285 | ); | |
286 | ||
287 | $contactTypeId = $self->get('contactType'); | |
288 | $contactTypes = array( | |
a05662ef CW |
289 | CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', |
290 | CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', | |
291 | CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization', | |
6a488035 | 292 | ); |
02a237ce | 293 | $contactFieldsBelowWeightMessage = self::validateRequiredContactMatchFields($contactTypes[$contactTypeId], $importKeys); |
6a488035 TO |
294 | |
295 | foreach ($requiredFields as $field => $title) { | |
296 | if (!in_array($field, $importKeys)) { | |
297 | if ($field == 'participant_contact_id') { | |
02a237ce | 298 | if (!$contactFieldsBelowWeightMessage || in_array('external_identifier', $importKeys) || |
6a488035 TO |
299 | in_array('participant_id', $importKeys) |
300 | ) { | |
301 | continue; | |
302 | } | |
a05662ef | 303 | if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { |
ef30e8dc | 304 | $errors['_qf_default'] .= ts('Missing required field: Provide Participant ID') . '<br />'; |
6a488035 TO |
305 | } |
306 | else { | |
02a237ce | 307 | $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $contactFieldsBelowWeightMessage " . ' ' . ts('Or Provide Contact ID or External ID.') . '<br />'; |
6a488035 TO |
308 | } |
309 | } | |
310 | elseif (!in_array('event_title', $importKeys)) { | |
311 | $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2', | |
353ffa53 TO |
312 | array(1 => $title, 2 => 'Event Title') |
313 | ) . '<br />'; | |
6a488035 TO |
314 | } |
315 | } | |
316 | } | |
317 | } | |
318 | ||
a7488080 | 319 | if (!empty($fields['saveMapping'])) { |
9c1bc317 | 320 | $nameField = $fields['saveMappingName'] ?? NULL; |
6a488035 TO |
321 | if (empty($nameField)) { |
322 | $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); | |
323 | } | |
324 | else { | |
95f52e3b | 325 | if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Participant'))) { |
6a488035 TO |
326 | $errors['saveMappingName'] = ts('Duplicate Import Participant Mapping Name'); |
327 | } | |
328 | } | |
329 | } | |
330 | ||
331 | //display Error if loaded mapping is not selected | |
332 | if (array_key_exists('loadMapping', $fields)) { | |
9c1bc317 | 333 | $getMapName = $fields['savedMapping'] ?? NULL; |
6a488035 TO |
334 | if (empty($getMapName)) { |
335 | $errors['savedMapping'] = ts('Select saved mapping'); | |
336 | } | |
337 | } | |
338 | ||
e1a33ae4 | 339 | if (empty($errors['_qf_default'])) { |
340 | unset($errors['_qf_default']); | |
341 | } | |
6a488035 TO |
342 | if (!empty($errors)) { |
343 | if (!empty($errors['saveMappingName'])) { | |
344 | $_flag = 1; | |
345 | $assignError = new CRM_Core_Page(); | |
346 | $assignError->assign('mappingDetailsError', $_flag); | |
347 | } | |
348 | return $errors; | |
349 | } | |
350 | ||
351 | return TRUE; | |
352 | } | |
353 | ||
354 | /** | |
355 | * Process the mapped fields and map it into the uploaded file | |
356 | * preview the file and extract some summary statistics | |
357 | * | |
358 | * @return void | |
6a488035 TO |
359 | */ |
360 | public function postProcess() { | |
361 | $params = $this->controller->exportValues('MapField'); | |
362 | //reload the mapfield if load mapping is pressed | |
363 | if (!empty($params['savedMapping'])) { | |
364 | $this->set('savedMapping', $params['savedMapping']); | |
365 | $this->controller->resetPage($this->_name); | |
366 | return; | |
367 | } | |
368 | ||
de7b9b56 | 369 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); |
f0fed404 | 370 | $separator = $this->controller->exportValue('DataSource', 'fieldSeparator'); |
de7b9b56 | 371 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
6a488035 | 372 | |
affcc9d2 CW |
373 | $mapperKeys = []; |
374 | $mapper = []; | |
353ffa53 | 375 | $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); |
affcc9d2 | 376 | $mapperKeysMain = []; |
6a488035 TO |
377 | |
378 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
379 | $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]]; | |
380 | $mapperKeysMain[$i] = $mapperKeys[$i][0]; | |
381 | } | |
382 | ||
383 | $this->set('mapper', $mapper); | |
384 | ||
385 | // store mapping Id to display it in the preview page | |
386 | $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params)); | |
387 | ||
388 | //Updating Mapping Records | |
a7488080 | 389 | if (!empty($params['updateMapping'])) { |
6a488035 TO |
390 | |
391 | $mappingFields = new CRM_Core_DAO_MappingField(); | |
392 | $mappingFields->mapping_id = $params['mappingId']; | |
393 | $mappingFields->find(); | |
394 | ||
affcc9d2 | 395 | $mappingFieldsId = []; |
6a488035 TO |
396 | while ($mappingFields->fetch()) { |
397 | if ($mappingFields->id) { | |
398 | $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id; | |
399 | } | |
400 | } | |
401 | ||
402 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
403 | $updateMappingFields = new CRM_Core_DAO_MappingField(); | |
404 | $updateMappingFields->id = $mappingFieldsId[$i]; | |
405 | $updateMappingFields->mapping_id = $params['mappingId']; | |
406 | $updateMappingFields->column_number = $i; | |
6a488035 TO |
407 | $updateMappingFields->name = $mapper[$i]; |
408 | $updateMappingFields->save(); | |
409 | } | |
410 | } | |
411 | ||
412 | //Saving Mapping Details and Records | |
a7488080 | 413 | if (!empty($params['saveMapping'])) { |
6a488035 TO |
414 | $mappingParams = array( |
415 | 'name' => $params['saveMappingName'], | |
416 | 'description' => $params['saveMappingDesc'], | |
95f52e3b | 417 | 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Participant'), |
6a488035 TO |
418 | ); |
419 | $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); | |
420 | ||
421 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
422 | $saveMappingFields = new CRM_Core_DAO_MappingField(); | |
423 | $saveMappingFields->mapping_id = $saveMapping->id; | |
424 | $saveMappingFields->column_number = $i; | |
6a488035 TO |
425 | $saveMappingFields->name = $mapper[$i]; |
426 | $saveMappingFields->save(); | |
427 | } | |
428 | $this->set('savedMapping', $saveMappingFields->mapping_id); | |
429 | } | |
430 | ||
431 | $parser = new CRM_Event_Import_Parser_Participant($mapperKeysMain); | |
f0fed404 | 432 | $parser->run($fileName, $separator, $mapper, $skipColumnHeader, |
a05662ef | 433 | CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType') |
6a488035 TO |
434 | ); |
435 | // add all the necessary variables to the form | |
436 | $parser->set($this); | |
437 | } | |
96025800 | 438 | |
6a488035 | 439 | } |