Merge pull request #14413 from civicrm/5.14
[civicrm-core.git] / CRM / Contribute / Import / Form / MapField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * This class gets the name of the file to upload.
36 */
37 class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
38
39 /**
40 * Set variables up before form is built.
41 */
42 public function preProcess() {
43 $this->_mapperFields = $this->get('fields');
44 asort($this->_mapperFields);
45
46 $this->_columnCount = $this->get('columnCount');
47 $this->assign('columnCount', $this->_columnCount);
48 $this->_dataValues = $this->get('dataValues');
49 $this->assign('dataValues', $this->_dataValues);
50
51 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
52 $this->_onDuplicate = $this->get('onDuplicate', isset($onDuplicate) ? $onDuplicate : "");
53
54 if ($skipColumnHeader) {
55 $this->assign('skipColumnHeader', $skipColumnHeader);
56 $this->assign('rowDisplayCount', 3);
57 // If we had a column header to skip, stash it for later
58
59 $this->_columnHeaders = $this->_dataValues[0];
60 }
61 else {
62 $this->assign('rowDisplayCount', 2);
63 }
64 $highlightedFields = ['financial_type', 'total_amount'];
65 //CRM-2219 removing other required fields since for updation only
66 //invoice id or trxn id or contribution id is required.
67 if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
68 $remove = [
69 'contribution_contact_id',
70 'email',
71 'first_name',
72 'last_name',
73 'external_identifier',
74 ];
75 foreach ($remove as $value) {
76 unset($this->_mapperFields[$value]);
77 }
78
79 //modify field title only for update mode. CRM-3245
80 foreach ([
81 'contribution_id',
82 'invoice_id',
83 'trxn_id',
84 ] as $key) {
85 $this->_mapperFields[$key] .= ' (match to contribution record)';
86 $highlightedFields[] = $key;
87 }
88 }
89 elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) {
90 unset($this->_mapperFields['contribution_id']);
91 $highlightedFieldsArray = [
92 'contribution_contact_id',
93 'email',
94 'first_name',
95 'last_name',
96 'external_identifier',
97 ];
98 foreach ($highlightedFieldsArray as $name) {
99 $highlightedFields[] = $name;
100 }
101 }
102
103 // modify field title for contribution status
104 $this->_mapperFields['contribution_status_id'] = ts('Contribution Status');
105
106 $this->assign('highlightedFields', $highlightedFields);
107 }
108
109 /**
110 * Build the form object.
111 */
112 public function buildQuickForm() {
113 //to save the current mappings
114 if (!$this->get('savedMapping')) {
115 $saveDetailsName = ts('Save this field mapping');
116 $this->applyFilter('saveMappingName', 'trim');
117 $this->add('text', 'saveMappingName', ts('Name'));
118 $this->add('text', 'saveMappingDesc', ts('Description'));
119 }
120 else {
121 $savedMapping = $this->get('savedMapping');
122
123 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping);
124
125 $mappingName = $mappingName[1];
126 $mappingContactType = $mappingContactType[1];
127 $mappingLocation = CRM_Utils_Array::value('1', CRM_Utils_Array::value(1, $mappingLocation));
128 $mappingPhoneType = CRM_Utils_Array::value('1', CRM_Utils_Array::value(1, $mappingPhoneType));
129 $mappingRelation = CRM_Utils_Array::value('1', CRM_Utils_Array::value(1, $mappingRelation));
130
131 //mapping is to be loaded from database
132
133 $params = ['id' => $savedMapping];
134 $temp = [];
135 $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp);
136
137 $this->assign('loadedMapping', $mappingDetails->name);
138 $this->set('loadedMapping', $savedMapping);
139
140 $getMappingName = new CRM_Core_DAO_Mapping();
141 $getMappingName->id = $savedMapping;
142 $getMappingName->mapping_type = 'Import Contributions';
143 $getMappingName->find();
144 while ($getMappingName->fetch()) {
145 $mapperName = $getMappingName->name;
146 }
147
148 $this->assign('savedName', $mapperName);
149
150 $this->add('hidden', 'mappingId', $savedMapping);
151
152 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
153 $saveDetailsName = ts('Save as a new field mapping');
154 $this->add('text', 'saveMappingName', ts('Name'));
155 $this->add('text', 'saveMappingDesc', ts('Description'));
156 }
157
158 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]);
159
160 $this->addFormRule([
161 'CRM_Contribute_Import_Form_MapField',
162 'formRule',
163 ], $this);
164
165 //-------- end of saved mapping stuff ---------
166
167 $defaults = [];
168 $mapperKeys = array_keys($this->_mapperFields);
169 $hasHeaders = !empty($this->_columnHeaders);
170 $headerPatterns = $this->get('headerPatterns');
171 $dataPatterns = $this->get('dataPatterns');
172 $mapperKeysValues = $this->controller->exportValue($this->_name, 'mapper');
173
174 /* Initialize all field usages to false */
175 foreach ($mapperKeys as $key) {
176 $this->_fieldUsed[$key] = FALSE;
177 }
178 $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
179 $sel1 = $this->_mapperFields;
180
181 if (!$this->get('onDuplicate')) {
182 unset($sel1['id']);
183 unset($sel1['contribution_id']);
184 }
185
186 $softCreditFields['contact_id'] = ts('Contact ID');
187 $softCreditFields['external_identifier'] = ts('External ID');
188 $softCreditFields['email'] = ts('Email');
189
190 $sel2['soft_credit'] = $softCreditFields;
191 $sel3['soft_credit']['contact_id'] = $sel3['soft_credit']['external_identifier'] = $sel3['soft_credit']['email'] = CRM_Core_OptionGroup::values('soft_credit_type');
192 $sel4 = NULL;
193
194 // end of soft credit section
195 $js = "<script type='text/javascript'>\n";
196 $formName = 'document.forms.' . $this->_name;
197
198 //used to warn for mismatch column count or mismatch mapping
199 $warning = 0;
200
201 for ($i = 0; $i < $this->_columnCount; $i++) {
202 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);
203 $jsSet = FALSE;
204 if ($this->get('savedMapping')) {
205 if (isset($mappingName[$i])) {
206 if ($mappingName[$i] != ts('- do not import -')) {
207
208 $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]);
209 // reusing contact_type field array for soft credit
210 $softField = isset($mappingContactType[$i]) ? $mappingContactType[$i] : 0;
211
212 if (!$softField) {
213 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
214 }
215
216 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
217 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
218 $defaults["mapper[$i]"] = [
219 CRM_Utils_Array::value(0, $mappingHeader),
220 ($softField) ? $softField : "",
221 (isset($locationId)) ? $locationId : "",
222 (isset($phoneType)) ? $phoneType : "",
223 ];
224 $jsSet = TRUE;
225 }
226 else {
227 $defaults["mapper[$i]"] = [];
228 }
229 if (!$jsSet) {
230 for ($k = 1; $k < 4; $k++) {
231 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
232 }
233 }
234 }
235 else {
236 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
237 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
238
239 if ($hasHeaders) {
240 $defaults["mapper[$i]"] = [$this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns)];
241 }
242 else {
243 $defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)];
244 }
245 }
246 //end of load mapping
247 }
248 else {
249 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
250 if ($hasHeaders) {
251 // do array search first to see if has mapped key
252 $columnKey = array_search($this->_columnHeaders[$i], $this->_mapperFields);
253 if (isset($this->_fieldUsed[$columnKey])) {
254 $defaults["mapper[$i]"] = $columnKey;
255 $this->_fieldUsed[$key] = TRUE;
256 }
257 else {
258 // Infer the default from the column names if we have them
259 $defaults["mapper[$i]"] = [
260 $this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns),
261 0,
262 ];
263 }
264 }
265 else {
266 // Otherwise guess the default from the form of the data
267 $defaults["mapper[$i]"] = [
268 $this->defaultFromData($dataPatterns, $i),
269 0,
270 ];
271 }
272 if (!empty($mapperKeysValues) && $mapperKeysValues[$i][0] == 'soft_credit') {
273 $js .= "cj('#mapper_" . $i . "_1').val($mapperKeysValues[$i][1]);\n";
274 $js .= "cj('#mapper_" . $i . "_2').val($mapperKeysValues[$i][2]);\n";
275 }
276 }
277 $sel->setOptions([$sel1, $sel2, $sel3, $sel4]);
278 }
279 $js .= "</script>\n";
280 $this->assign('initHideBoxes', $js);
281
282 //set warning if mismatch in more than
283 if (isset($mappingName)) {
284 if (($this->_columnCount != count($mappingName))) {
285 $warning++;
286 }
287 }
288 if ($warning != 0 && $this->get('savedMapping')) {
289 $session = CRM_Core_Session::singleton();
290 $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.'));
291 }
292 else {
293 $session = CRM_Core_Session::singleton();
294 $session->setStatus(NULL);
295 }
296
297 $this->setDefaults($defaults);
298
299 $this->addButtons([
300 [
301 'type' => 'back',
302 'name' => ts('Previous'),
303 ],
304 [
305 'type' => 'next',
306 'name' => ts('Continue'),
307 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
308 'isDefault' => TRUE,
309 ],
310 [
311 'type' => 'cancel',
312 'name' => ts('Cancel'),
313 ],
314 ]);
315 }
316
317 /**
318 * Global validation rules for the form.
319 *
320 * @param array $fields
321 * Posted values of the form.
322 *
323 * @param $files
324 * @param $self
325 *
326 * @return array
327 * list of errors to be posted back to the form
328 */
329 public static function formRule($fields, $files, $self) {
330 $errors = [];
331 $fieldMessage = NULL;
332 $contactORContributionId = $self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE ? 'contribution_id' : 'contribution_contact_id';
333 if (!array_key_exists('savedMapping', $fields)) {
334 $importKeys = [];
335 foreach ($fields['mapper'] as $mapperPart) {
336 $importKeys[] = $mapperPart[0];
337 }
338
339 $contactTypeId = $self->get('contactType');
340 $contactTypes = [
341 CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual',
342 CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household',
343 CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization',
344 ];
345 $params = [
346 'used' => 'Unsupervised',
347 'contact_type' => isset($contactTypes[$contactTypeId]) ? $contactTypes[$contactTypeId] : '',
348 ];
349 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params);
350 $weightSum = 0;
351 foreach ($importKeys as $key => $val) {
352 if (array_key_exists($val, $ruleFields)) {
353 $weightSum += $ruleFields[$val];
354 }
355 if ($val == "soft_credit") {
356 $mapperKey = CRM_Utils_Array::key('soft_credit', $importKeys);
357 if (empty($fields['mapper'][$mapperKey][1])) {
358 if (empty($errors['_qf_default'])) {
359 $errors['_qf_default'] = '';
360 }
361 $errors['_qf_default'] .= ts('Missing required fields: Soft Credit') . '<br />';
362 }
363 }
364 }
365 foreach ($ruleFields as $field => $weight) {
366 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
367 }
368
369 // FIXME: should use the schema titles, not redeclare them
370 $requiredFields = [
371 $contactORContributionId == 'contribution_id' ? 'contribution_id' : 'contribution_contact_id' => $contactORContributionId == 'contribution_id' ? ts('Contribution ID') : ts('Contact ID'),
372 'total_amount' => ts('Total Amount'),
373 'financial_type' => ts('Financial Type'),
374 ];
375
376 foreach ($requiredFields as $field => $title) {
377 if (!in_array($field, $importKeys)) {
378 if (empty($errors['_qf_default'])) {
379 $errors['_qf_default'] = '';
380 }
381 if ($field == $contactORContributionId) {
382 if (!($weightSum >= $threshold || in_array('external_identifier', $importKeys)) &&
383 $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE
384 ) {
385 $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $fieldMessage " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', [
386 1 => $threshold,
387 ]) . '<br />';
388 }
389 elseif ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
390 !(in_array('invoice_id', $importKeys) || in_array('trxn_id', $importKeys) ||
391 in_array('contribution_id', $importKeys)
392 )
393 ) {
394 $errors['_qf_default'] .= ts('Invoice ID or Transaction ID or Contribution ID are required to match to the existing contribution records in Update mode.') . '<br />';
395 }
396 }
397 else {
398 $errors['_qf_default'] .= ts('Missing required field: %1', [
399 1 => $title,
400 ]) . '<br />';
401 }
402 }
403 }
404
405 //at least one field should be mapped during update.
406 if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
407 $atleastOne = FALSE;
408 foreach ($self->_mapperFields as $key => $field) {
409 if (in_array($key, $importKeys) &&
410 !in_array($key, [
411 'doNotImport',
412 'contribution_id',
413 'invoice_id',
414 'trxn_id',
415 ])
416 ) {
417 $atleastOne = TRUE;
418 break;
419 }
420 }
421 if (!$atleastOne) {
422 $errors['_qf_default'] .= ts('At least one contribution field needs to be mapped for update during update mode.') . '<br />';
423 }
424 }
425 }
426
427 if (!empty($fields['saveMapping'])) {
428 $nameField = CRM_Utils_Array::value('saveMappingName', $fields);
429 if (empty($nameField)) {
430 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
431 }
432 else {
433 if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'))) {
434 $errors['saveMappingName'] = ts('Duplicate Import Contribution Mapping Name');
435 }
436 }
437 }
438
439 if (!empty($errors)) {
440 if (!empty($errors['saveMappingName'])) {
441 $_flag = 1;
442 $assignError = new CRM_Core_Page();
443 $assignError->assign('mappingDetailsError', $_flag);
444 }
445 if (!empty($errors['_qf_default'])) {
446 CRM_Core_Session::setStatus($errors['_qf_default'], ts("Error"), "error");
447 return $errors;
448 }
449 }
450
451 return TRUE;
452 }
453
454 /**
455 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
456 */
457 public function postProcess() {
458 $params = $this->controller->exportValues('MapField');
459
460 //reload the mapfield if load mapping is pressed
461 if (!empty($params['savedMapping'])) {
462 $this->set('savedMapping', $params['savedMapping']);
463 $this->controller->resetPage($this->_name);
464 return;
465 }
466
467 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
468 $seperator = $this->controller->exportValue('DataSource', 'fieldSeparator');
469 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
470
471 $mapper = $mapperKeys = $mapperKeysMain = $mapperSoftCredit = $softCreditFields = $mapperPhoneType = $mapperSoftCreditType = [];
472 $mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
473
474 $softCreditTypes = CRM_Core_OptionGroup::values('soft_credit_type');
475
476 for ($i = 0; $i < $this->_columnCount; $i++) {
477 $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]];
478 $mapperKeysMain[$i] = $mapperKeys[$i][0];
479
480 if (isset($mapperKeys[$i][0]) && $mapperKeys[$i][0] == 'soft_credit') {
481 $mapperSoftCredit[$i] = $mapperKeys[$i][1];
482 if (strpos($mapperSoftCredit[$i], '_') !== FALSE) {
483 list($first, $second) = explode('_', $mapperSoftCredit[$i]);
484 $softCreditFields[$i] = ucwords($first . " " . $second);
485 }
486 else {
487 $softCreditFields[$i] = $mapperSoftCredit[$i];
488 }
489 $mapperSoftCreditType[$i] = [
490 'value' => isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : '',
491 'label' => isset($softCreditTypes[$mapperKeys[$i][2]]) ? $softCreditTypes[$mapperKeys[$i][2]] : '',
492 ];
493 }
494 else {
495 $mapperSoftCredit[$i] = $softCreditFields[$i] = $mapperSoftCreditType[$i] = NULL;
496 }
497 }
498
499 $this->set('mapper', $mapper);
500 $this->set('softCreditFields', $softCreditFields);
501 $this->set('mapperSoftCreditType', $mapperSoftCreditType);
502
503 // store mapping Id to display it in the preview page
504 $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params));
505
506 //Updating Mapping Records
507 if (!empty($params['updateMapping'])) {
508 $mappingFields = new CRM_Core_DAO_MappingField();
509 $mappingFields->mapping_id = $params['mappingId'];
510 $mappingFields->find();
511
512 $mappingFieldsId = [];
513 while ($mappingFields->fetch()) {
514 if ($mappingFields->id) {
515 $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id;
516 }
517 }
518
519 for ($i = 0; $i < $this->_columnCount; $i++) {
520 $updateMappingFields = new CRM_Core_DAO_MappingField();
521 $updateMappingFields->id = $mappingFieldsId[$i];
522 $updateMappingFields->mapping_id = $params['mappingId'];
523 $updateMappingFields->column_number = $i;
524 $updateMappingFields->name = $mapper[$i];
525
526 //reuse contact_type field in db to store fields associated with soft credit
527 $updateMappingFields->contact_type = isset($mapperSoftCredit[$i]) ? $mapperSoftCredit[$i] : NULL;
528 $updateMappingFields->save();
529 }
530 }
531
532 //Saving Mapping Details and Records
533 if (!empty($params['saveMapping'])) {
534 $mappingParams = [
535 'name' => $params['saveMappingName'],
536 'description' => $params['saveMappingDesc'],
537 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'),
538 ];
539 $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
540
541 for ($i = 0; $i < $this->_columnCount; $i++) {
542 $saveMappingFields = new CRM_Core_DAO_MappingField();
543 $saveMappingFields->mapping_id = $saveMapping->id;
544 $saveMappingFields->column_number = $i;
545 $saveMappingFields->name = $mapper[$i];
546
547 //reuse contact_type field in db to store fields associated with soft credit
548 $saveMappingFields->contact_type = isset($mapperSoftCredit[$i]) ? $mapperSoftCredit[$i] : NULL;
549 $saveMappingFields->save();
550 }
551 $this->set('savedMapping', $saveMappingFields->mapping_id);
552 }
553
554 $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeysMain, $mapperSoftCredit, $mapperPhoneType);
555 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
556 CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType')
557 );
558
559 // add all the necessary variables to the form
560 $parser->set($this);
561 }
562
563 }