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