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