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