3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * This class gets the name of the file to upload
23 class CRM_Event_Import_Form_MapField
extends CRM_Import_Form_MapField
{
26 * Set variables up before form is built.
30 public function preProcess() {
31 $this->_mapperFields
= $this->get('fields');
32 asort($this->_mapperFields
);
33 unset($this->_mapperFields
['participant_is_test']);
34 $this->_columnCount
= $this->get('columnCount');
35 $this->assign('columnCount', $this->_columnCount
);
36 $this->_dataValues
= $this->get('dataValues');
37 $this->assign('dataValues', $this->_dataValues
);
39 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
40 $this->_onDuplicate
= $this->get('onDuplicate');
41 $highlightedFields = array();
42 if ($skipColumnHeader) {
43 $this->assign('skipColumnHeader', $skipColumnHeader);
44 $this->assign('rowDisplayCount', 3);
45 /* if we had a column header to skip, stash it for later */
47 $this->_columnHeaders
= $this->_dataValues
[0];
50 $this->assign('rowDisplayCount', 2);
52 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
53 $remove = array('participant_contact_id', 'email', 'first_name', 'last_name', 'external_identifier');
54 foreach ($remove as $value) {
55 unset($this->_mapperFields
[$value]);
57 $highlightedFieldsArray = array('participant_id', 'event_id', 'event_title', 'participant_status_id');
58 foreach ($highlightedFieldsArray as $name) {
59 $highlightedFields[] = $name;
62 elseif ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP ||
63 $this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_NOCHECK
65 unset($this->_mapperFields
['participant_id']);
66 $highlightedFieldsArray = array(
67 'participant_contact_id',
72 'external_identifier',
73 'participant_status_id',
75 foreach ($highlightedFieldsArray as $name) {
76 $highlightedFields[] = $name;
79 $this->assign('highlightedFields', $highlightedFields);
83 * Build the form object.
87 public function buildQuickForm() {
89 //to save the current mappings
90 if (!$this->get('savedMapping')) {
91 $saveDetailsName = ts('Save this field mapping');
92 $this->applyFilter('saveMappingName', 'trim');
93 $this->add('text', 'saveMappingName', ts('Name'));
94 $this->add('text', 'saveMappingDesc', ts('Description'));
97 $savedMapping = $this->get('savedMapping');
99 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
101 $mappingName = $mappingName[1];
102 $mappingContactType = $mappingContactType[1];
103 $mappingLocation = $mappingLocation['1'] ??
NULL;
104 $mappingPhoneType = $mappingPhoneType['1'] ??
NULL;
105 $mappingRelation = $mappingRelation['1'] ??
NULL;
107 //mapping is to be loaded from database
109 $params = array('id' => $savedMapping);
111 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
113 $this->assign('loadedMapping', $mappingDetails->name
);
114 $this->set('loadedMapping', $savedMapping);
116 $getMappingName = new CRM_Core_DAO_Mapping();
117 $getMappingName->id
= $savedMapping;
118 $getMappingName->mapping_type
= 'Import Participants';
119 $getMappingName->find();
120 while ($getMappingName->fetch()) {
121 $mapperName = $getMappingName->name
;
124 $this->assign('savedName', $mapperName);
126 $this->add('hidden', 'mappingId', $savedMapping);
128 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
129 $saveDetailsName = ts('Save as a new field mapping');
130 $this->add('text', 'saveMappingName', ts('Name'));
131 $this->add('text', 'saveMappingDesc', ts('Description'));
134 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
136 $this->addFormRule(array('CRM_Event_Import_Form_MapField', 'formRule'), $this);
139 $mapperKeys = array_keys($this->_mapperFields
);
140 $hasHeaders = !empty($this->_columnHeaders
);
141 $headerPatterns = $this->get('headerPatterns');
142 $dataPatterns = $this->get('dataPatterns');
143 $hasLocationTypes = $this->get('fieldTypes');
144 /* Initialize all field usages to false */
146 foreach ($mapperKeys as $key) {
147 $this->_fieldUsed
[$key] = FALSE;
149 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
150 $sel1 = $this->_mapperFields
;
153 $js = "<script type='text/javascript'>\n";
154 $formName = 'document.forms.' . $this->_name
;
156 //used to warn for mismatch column count or mismatch mapping
158 for ($i = 0; $i < $this->_columnCount
; $i++
) {
159 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
161 if ($this->get('savedMapping')) {
162 if (isset($mappingName[$i])) {
163 if ($mappingName[$i] != ts('- do not import -')) {
165 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
167 if (!isset($locationId) ||
!$locationId) {
168 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
171 if (!isset($phoneType) ||
!$phoneType) {
172 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
175 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
176 $defaults["mapper[$i]"] = array(
178 (isset($locationId)) ?
$locationId : "",
179 (isset($phoneType)) ?
$phoneType : "",
184 $defaults["mapper[$i]"] = array();
187 for ($k = 1; $k < 4; $k++
) {
188 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
193 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
194 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
197 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns));
200 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
203 //end of load mapping
206 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
208 // Infer the default from the skipped headers if we have them
209 $defaults["mapper[$i]"] = array(
210 $this->defaultFromHeader($this->_columnHeaders
[$i],
213 // $defaultLocationType->id
218 // Otherwise guess the default from the form of the data
219 $defaults["mapper[$i]"] = array(
220 $this->defaultFromData($dataPatterns, $i),
221 // $defaultLocationType->id
226 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ?
$sel3 : "", (isset($sel4)) ?
$sel4 : ""));
228 $js .= "</script>\n";
229 $this->assign('initHideBoxes', $js);
231 //set warning if mismatch in more than
232 if (isset($mappingName)) {
233 if (($this->_columnCount
!= count($mappingName))) {
237 if ($warning != 0 && $this->get('savedMapping')) {
238 $session = CRM_Core_Session
::singleton();
239 $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.'));
242 $session = CRM_Core_Session
::singleton();
243 $session->setStatus(NULL);
246 $this->setDefaults($defaults);
248 $this->addButtons(array(
251 'name' => ts('Previous'),
255 'name' => ts('Continue'),
256 'spacing' => ' ',
261 'name' => ts('Cancel'),
267 * Global validation rules for the form.
269 * @param array $fields
270 * Posted values of the form.
276 * list of errors to be posted back to the form
278 public static function formRule($fields, $files, $self) {
280 // define so we avoid notices below
281 $errors['_qf_default'] = '';
282 $fieldMessage = NULL;
283 if (!array_key_exists('savedMapping', $fields)) {
284 $importKeys = array();
285 foreach ($fields['mapper'] as $mapperPart) {
286 $importKeys[] = $mapperPart[0];
288 // FIXME: should use the schema titles, not redeclare them
289 $requiredFields = array(
290 'participant_contact_id' => ts('Contact ID'),
291 'event_id' => ts('Event ID'),
294 $contactTypeId = $self->get('contactType');
295 $contactTypes = array(
296 CRM_Import_Parser
::CONTACT_INDIVIDUAL
=> 'Individual',
297 CRM_Import_Parser
::CONTACT_HOUSEHOLD
=> 'Household',
298 CRM_Import_Parser
::CONTACT_ORGANIZATION
=> 'Organization',
301 'used' => 'Unsupervised',
302 'contact_type' => $contactTypes[$contactTypeId],
304 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
306 foreach ($importKeys as $key => $val) {
307 if (array_key_exists($val, $ruleFields)) {
308 $weightSum +
= $ruleFields[$val];
311 foreach ($ruleFields as $field => $weight) {
312 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
315 foreach ($requiredFields as $field => $title) {
316 if (!in_array($field, $importKeys)) {
317 if ($field == 'participant_contact_id') {
318 if ($weightSum >= $threshold ||
in_array('external_identifier', $importKeys) ||
319 in_array('participant_id', $importKeys)
323 if ($self->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
324 $errors['_qf_default'] .= ts('Missing required field: Provide Participant ID') . '<br />';
327 $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $fieldMessage " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(
329 )) . ' ' . ts('Or Provide Contact ID or External ID.') . '<br />';
332 elseif (!in_array('event_title', $importKeys)) {
333 $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2',
334 array(1 => $title, 2 => 'Event Title')
341 if (!empty($fields['saveMapping'])) {
342 $nameField = $fields['saveMappingName'] ??
NULL;
343 if (empty($nameField)) {
344 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
347 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Participant'))) {
348 $errors['saveMappingName'] = ts('Duplicate Import Participant Mapping Name');
353 //display Error if loaded mapping is not selected
354 if (array_key_exists('loadMapping', $fields)) {
355 $getMapName = $fields['savedMapping'] ??
NULL;
356 if (empty($getMapName)) {
357 $errors['savedMapping'] = ts('Select saved mapping');
361 if (empty($errors['_qf_default'])) {
362 unset($errors['_qf_default']);
364 if (!empty($errors)) {
365 if (!empty($errors['saveMappingName'])) {
367 $assignError = new CRM_Core_Page();
368 $assignError->assign('mappingDetailsError', $_flag);
377 * Process the mapped fields and map it into the uploaded file
378 * preview the file and extract some summary statistics
382 public function postProcess() {
383 $params = $this->controller
->exportValues('MapField');
384 //reload the mapfield if load mapping is pressed
385 if (!empty($params['savedMapping'])) {
386 $this->set('savedMapping', $params['savedMapping']);
387 $this->controller
->resetPage($this->_name
);
391 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
392 $seperator = $this->controller
->exportValue('DataSource', 'fieldSeparator');
393 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
395 $mapperKeys = array();
397 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
398 $mapperKeysMain = array();
400 for ($i = 0; $i < $this->_columnCount
; $i++
) {
401 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
402 $mapperKeysMain[$i] = $mapperKeys[$i][0];
405 $this->set('mapper', $mapper);
407 // store mapping Id to display it in the preview page
408 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
410 //Updating Mapping Records
411 if (!empty($params['updateMapping'])) {
413 $mappingFields = new CRM_Core_DAO_MappingField();
414 $mappingFields->mapping_id
= $params['mappingId'];
415 $mappingFields->find();
417 $mappingFieldsId = array();
418 while ($mappingFields->fetch()) {
419 if ($mappingFields->id
) {
420 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
424 for ($i = 0; $i < $this->_columnCount
; $i++
) {
425 $updateMappingFields = new CRM_Core_DAO_MappingField();
426 $updateMappingFields->id
= $mappingFieldsId[$i];
427 $updateMappingFields->mapping_id
= $params['mappingId'];
428 $updateMappingFields->column_number
= $i;
430 $explodedValues = explode('_', $mapperKeys[$i][0]);
431 $id = $explodedValues[0] ??
NULL;
432 $first = $explodedValues[1] ??
NULL;
433 $second = $explodedValues[2] ??
NULL;
435 $updateMappingFields->name
= $mapper[$i];
436 $updateMappingFields->save();
440 //Saving Mapping Details and Records
441 if (!empty($params['saveMapping'])) {
442 $mappingParams = array(
443 'name' => $params['saveMappingName'],
444 'description' => $params['saveMappingDesc'],
445 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Participant'),
447 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
449 for ($i = 0; $i < $this->_columnCount
; $i++
) {
450 $saveMappingFields = new CRM_Core_DAO_MappingField();
451 $saveMappingFields->mapping_id
= $saveMapping->id
;
452 $saveMappingFields->column_number
= $i;
454 $explodedValues = explode('_', $mapperKeys[$i][0]);
455 $id = $explodedValues[0] ??
NULL;
456 $first = $explodedValues[1] ??
NULL;
457 $second = $explodedValues[2] ??
NULL;
459 $saveMappingFields->name
= $mapper[$i];
460 $saveMappingFields->save();
462 $this->set('savedMapping', $saveMappingFields->mapping_id
);
465 $parser = new CRM_Event_Import_Parser_Participant($mapperKeysMain);
466 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
467 CRM_Import_Parser
::MODE_PREVIEW
, $this->get('contactType')
469 // add all the necessary variables to the form