Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
347e061b | 35 | * This class gets the name of the file to upload. |
6a488035 | 36 | */ |
b26295b8 | 37 | class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { |
6a488035 | 38 | |
6a488035 | 39 | /** |
fe482240 | 40 | * Set variables up before form is built. |
6a488035 TO |
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 | ||
bf3adfdb | 51 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
6a488035 TO |
52 | $this->_onDuplicate = $this->get('onDuplicate', isset($onDuplicate) ? $onDuplicate : ""); |
53 | ||
54 | if ($skipColumnHeader) { | |
55 | $this->assign('skipColumnHeader', $skipColumnHeader); | |
56 | $this->assign('rowDisplayCount', 3); | |
98ca76db | 57 | // If we had a column header to skip, stash it for later |
6a488035 TO |
58 | |
59 | $this->_columnHeaders = $this->_dataValues[0]; | |
60 | } | |
61 | else { | |
62 | $this->assign('rowDisplayCount', 2); | |
63 | } | |
98ca76db | 64 | $highlightedFields = ['financial_type', 'total_amount']; |
6a488035 TO |
65 | //CRM-2219 removing other required fields since for updation only |
66 | //invoice id or trxn id or contribution id is required. | |
a05662ef | 67 | if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { |
98ca76db | 68 | $remove = [ |
69 | 'contribution_contact_id', | |
70 | 'email', | |
71 | 'first_name', | |
72 | 'last_name', | |
73 | 'external_identifier', | |
74 | ]; | |
6a488035 TO |
75 | foreach ($remove as $value) { |
76 | unset($this->_mapperFields[$value]); | |
77 | } | |
78 | ||
79 | //modify field title only for update mode. CRM-3245 | |
98ca76db | 80 | foreach ([ |
1330f57a SL |
81 | 'contribution_id', |
82 | 'invoice_id', | |
83 | 'trxn_id', | |
84 | ] as $key) { | |
6a488035 TO |
85 | $this->_mapperFields[$key] .= ' (match to contribution record)'; |
86 | $highlightedFields[] = $key; | |
87 | } | |
88 | } | |
a05662ef | 89 | elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
6a488035 | 90 | unset($this->_mapperFields['contribution_id']); |
98ca76db | 91 | $highlightedFieldsArray = [ |
353ffa53 TO |
92 | 'contribution_contact_id', |
93 | 'email', | |
94 | 'first_name', | |
95 | 'last_name', | |
389bcebf | 96 | 'external_identifier', |
98ca76db | 97 | ]; |
6a488035 TO |
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 | /** | |
fe482240 | 110 | * Build the form object. |
6a488035 TO |
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 | ||
91bb24a7 | 125 | $mappingName = $mappingName[1]; |
6a488035 | 126 | $mappingContactType = $mappingContactType[1]; |
91bb24a7 | 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)); | |
6a488035 TO |
130 | |
131 | //mapping is to be loaded from database | |
132 | ||
98ca76db | 133 | $params = ['id' => $savedMapping]; |
134 | $temp = []; | |
6a488035 TO |
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 | ||
98ca76db | 158 | $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]); |
6a488035 | 159 | |
98ca76db | 160 | $this->addFormRule([ |
161 | 'CRM_Contribute_Import_Form_MapField', | |
162 | 'formRule', | |
163 | ], $this); | |
6a488035 TO |
164 | |
165 | //-------- end of saved mapping stuff --------- | |
166 | ||
98ca76db | 167 | $defaults = []; |
91bb24a7 | 168 | $mapperKeys = array_keys($this->_mapperFields); |
169 | $hasHeaders = !empty($this->_columnHeaders); | |
170 | $headerPatterns = $this->get('headerPatterns'); | |
171 | $dataPatterns = $this->get('dataPatterns'); | |
377fa510 | 172 | $mapperKeysValues = $this->controller->exportValue($this->_name, 'mapper'); |
6a488035 | 173 | |
6a488035 | 174 | /* Initialize all field usages to false */ |
6a488035 TO |
175 | foreach ($mapperKeys as $key) { |
176 | $this->_fieldUsed[$key] = FALSE; | |
177 | } | |
b2b0530a | 178 | $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
179 | $sel1 = $this->_mapperFields; |
180 | ||
181 | if (!$this->get('onDuplicate')) { | |
182 | unset($sel1['id']); | |
183 | unset($sel1['contribution_id']); | |
184 | } | |
185 | ||
6a488035 | 186 | $softCreditFields['contact_id'] = ts('Contact ID'); |
7b99ead3 | 187 | $softCreditFields['external_identifier'] = ts('External ID'); |
377fa510 | 188 | $softCreditFields['email'] = ts('Email'); |
6a488035 TO |
189 | |
190 | $sel2['soft_credit'] = $softCreditFields; | |
377fa510 | 191 | $sel3['soft_credit']['contact_id'] = $sel3['soft_credit']['external_identifier'] = $sel3['soft_credit']['email'] = CRM_Core_OptionGroup::values('soft_credit_type'); |
1221efe9 | 192 | $sel4 = NULL; |
6a488035 TO |
193 | |
194 | // end of soft credit section | |
6a488035 TO |
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++) { | |
98ca76db | 202 | $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL); |
6a488035 TO |
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"; | |
98ca76db | 218 | $defaults["mapper[$i]"] = [ |
91bb24a7 | 219 | CRM_Utils_Array::value(0, $mappingHeader), |
6a488035 TO |
220 | ($softField) ? $softField : "", |
221 | (isset($locationId)) ? $locationId : "", | |
222 | (isset($phoneType)) ? $phoneType : "", | |
98ca76db | 223 | ]; |
6a488035 TO |
224 | $jsSet = TRUE; |
225 | } | |
226 | else { | |
98ca76db | 227 | $defaults["mapper[$i]"] = []; |
6a488035 TO |
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 | |
377fa510 | 237 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n"; |
6a488035 TO |
238 | |
239 | if ($hasHeaders) { | |
98ca76db | 240 | $defaults["mapper[$i]"] = [$this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns)]; |
6a488035 TO |
241 | } |
242 | else { | |
98ca76db | 243 | $defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)]; |
6a488035 TO |
244 | } |
245 | } | |
246 | //end of load mapping | |
247 | } | |
248 | else { | |
377fa510 | 249 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n"; |
6a488035 | 250 | if ($hasHeaders) { |
377fa510 | 251 | // do array search first to see if has mapped key |
377fa510 | 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 | |
98ca76db | 259 | $defaults["mapper[$i]"] = [ |
260 | $this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns), | |
377fa510 | 261 | 0, |
98ca76db | 262 | ]; |
377fa510 | 263 | } |
6a488035 TO |
264 | } |
265 | else { | |
266 | // Otherwise guess the default from the form of the data | |
98ca76db | 267 | $defaults["mapper[$i]"] = [ |
6a488035 | 268 | $this->defaultFromData($dataPatterns, $i), |
6a488035 | 269 | 0, |
98ca76db | 270 | ]; |
6a488035 | 271 | } |
22e263ad | 272 | if (!empty($mapperKeysValues) && $mapperKeysValues[$i][0] == 'soft_credit') { |
92fcb95f TO |
273 | $js .= "cj('#mapper_" . $i . "_1').val($mapperKeysValues[$i][1]);\n"; |
274 | $js .= "cj('#mapper_" . $i . "_2').val($mapperKeysValues[$i][2]);\n"; | |
377fa510 | 275 | } |
6a488035 | 276 | } |
98ca76db | 277 | $sel->setOptions([$sel1, $sel2, $sel3, $sel4]); |
6a488035 TO |
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 | ||
98ca76db | 299 | $this->addButtons([ |
300 | [ | |
301 | 'type' => 'back', | |
302 | 'name' => ts('Previous'), | |
303 | ], | |
304 | [ | |
305 | 'type' => 'next', | |
306 | 'name' => ts('Continue'), | |
307 | 'spacing' => ' ', | |
308 | 'isDefault' => TRUE, | |
309 | ], | |
310 | [ | |
311 | 'type' => 'cancel', | |
312 | 'name' => ts('Cancel'), | |
313 | ], | |
314 | ]); | |
6a488035 TO |
315 | } |
316 | ||
317 | /** | |
fe482240 | 318 | * Global validation rules for the form. |
6a488035 | 319 | * |
014c4014 TO |
320 | * @param array $fields |
321 | * Posted values of the form. | |
6a488035 | 322 | * |
2a6da8d7 EM |
323 | * @param $files |
324 | * @param $self | |
325 | * | |
a6c01b45 CW |
326 | * @return array |
327 | * list of errors to be posted back to the form | |
6a488035 | 328 | */ |
00be9182 | 329 | public static function formRule($fields, $files, $self) { |
98ca76db | 330 | $errors = []; |
6a488035 | 331 | $fieldMessage = NULL; |
1221efe9 | 332 | $contactORContributionId = $self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE ? 'contribution_id' : 'contribution_contact_id'; |
6a488035 | 333 | if (!array_key_exists('savedMapping', $fields)) { |
98ca76db | 334 | $importKeys = []; |
6a488035 TO |
335 | foreach ($fields['mapper'] as $mapperPart) { |
336 | $importKeys[] = $mapperPart[0]; | |
337 | } | |
338 | ||
339 | $contactTypeId = $self->get('contactType'); | |
98ca76db | 340 | $contactTypes = [ |
a05662ef CW |
341 | CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', |
342 | CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', | |
343 | CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization', | |
98ca76db | 344 | ]; |
345 | $params = [ | |
91bb24a7 | 346 | 'used' => 'Unsupervised', |
1221efe9 | 347 | 'contact_type' => isset($contactTypes[$contactTypeId]) ? $contactTypes[$contactTypeId] : '', |
98ca76db | 348 | ]; |
6a488035 TO |
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 | } | |
1221efe9 | 355 | if ($val == "soft_credit") { |
356 | $mapperKey = CRM_Utils_Array::key('soft_credit', $importKeys); | |
377fa510 | 357 | if (empty($fields['mapper'][$mapperKey][1])) { |
1221efe9 | 358 | if (empty($errors['_qf_default'])) { |
359 | $errors['_qf_default'] = ''; | |
360 | } | |
361 | $errors['_qf_default'] .= ts('Missing required fields: Soft Credit') . '<br />'; | |
362 | } | |
363 | } | |
6a488035 TO |
364 | } |
365 | foreach ($ruleFields as $field => $weight) { | |
366 | $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; | |
367 | } | |
368 | ||
369 | // FIXME: should use the schema titles, not redeclare them | |
98ca76db | 370 | $requiredFields = [ |
1221efe9 | 371 | $contactORContributionId == 'contribution_id' ? 'contribution_id' : 'contribution_contact_id' => $contactORContributionId == 'contribution_id' ? ts('Contribution ID') : ts('Contact ID'), |
6a488035 | 372 | 'total_amount' => ts('Total Amount'), |
21dfd5f5 | 373 | 'financial_type' => ts('Financial Type'), |
98ca76db | 374 | ]; |
6a488035 TO |
375 | |
376 | foreach ($requiredFields as $field => $title) { | |
377 | if (!in_array($field, $importKeys)) { | |
1221efe9 | 378 | if (empty($errors['_qf_default'])) { |
379 | $errors['_qf_default'] = ''; | |
380 | } | |
381 | if ($field == $contactORContributionId) { | |
6a488035 | 382 | if (!($weightSum >= $threshold || in_array('external_identifier', $importKeys)) && |
a05662ef | 383 | $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE |
6a488035 | 384 | ) { |
98ca76db | 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).', [ |
1330f57a SL |
386 | 1 => $threshold, |
387 | ]) . '<br />'; | |
6a488035 | 388 | } |
a05662ef | 389 | elseif ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE && |
6a488035 TO |
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 { | |
98ca76db | 398 | $errors['_qf_default'] .= ts('Missing required field: %1', [ |
1330f57a SL |
399 | 1 => $title, |
400 | ]) . '<br />'; | |
6a488035 TO |
401 | } |
402 | } | |
403 | } | |
404 | ||
405 | //at least one field should be mapped during update. | |
a05662ef | 406 | if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 TO |
407 | $atleastOne = FALSE; |
408 | foreach ($self->_mapperFields as $key => $field) { | |
409 | if (in_array($key, $importKeys) && | |
98ca76db | 410 | !in_array($key, [ |
411 | 'doNotImport', | |
412 | 'contribution_id', | |
413 | 'invoice_id', | |
414 | 'trxn_id', | |
415 | ]) | |
6a488035 TO |
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 | ||
a7488080 | 427 | if (!empty($fields['saveMapping'])) { |
6a488035 TO |
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 { | |
95f52e3b | 433 | if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'))) { |
6a488035 TO |
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 | } | |
377fa510 | 445 | if (!empty($errors['_qf_default'])) { |
446 | CRM_Core_Session::setStatus($errors['_qf_default'], ts("Error"), "error"); | |
447 | return $errors; | |
448 | } | |
6a488035 TO |
449 | } |
450 | ||
451 | return TRUE; | |
452 | } | |
453 | ||
454 | /** | |
347e061b | 455 | * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics. |
6a488035 TO |
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 | ||
bf3adfdb | 467 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); |
063338e5 | 468 | $seperator = $this->controller->exportValue('DataSource', 'fieldSeparator'); |
bf3adfdb | 469 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
6a488035 | 470 | |
98ca76db | 471 | $mapper = $mapperKeys = $mapperKeysMain = $mapperSoftCredit = $softCreditFields = $mapperPhoneType = $mapperSoftCreditType = []; |
6a488035 TO |
472 | $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); |
473 | ||
1221efe9 | 474 | $softCreditTypes = CRM_Core_OptionGroup::values('soft_credit_type'); |
475 | ||
6a488035 TO |
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]; | |
874c9be7 | 482 | if (strpos($mapperSoftCredit[$i], '_') !== FALSE) { |
377fa510 | 483 | list($first, $second) = explode('_', $mapperSoftCredit[$i]); |
484 | $softCreditFields[$i] = ucwords($first . " " . $second); | |
485 | } | |
486 | else { | |
487 | $softCreditFields[$i] = $mapperSoftCredit[$i]; | |
488 | } | |
98ca76db | 489 | $mapperSoftCreditType[$i] = [ |
1221efe9 | 490 | 'value' => isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : '', |
491 | 'label' => isset($softCreditTypes[$mapperKeys[$i][2]]) ? $softCreditTypes[$mapperKeys[$i][2]] : '', | |
98ca76db | 492 | ]; |
6a488035 TO |
493 | } |
494 | else { | |
874c9be7 | 495 | $mapperSoftCredit[$i] = $softCreditFields[$i] = $mapperSoftCreditType[$i] = NULL; |
6a488035 TO |
496 | } |
497 | } | |
498 | ||
499 | $this->set('mapper', $mapper); | |
500 | $this->set('softCreditFields', $softCreditFields); | |
1221efe9 | 501 | $this->set('mapperSoftCreditType', $mapperSoftCreditType); |
6a488035 TO |
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 | |
a7488080 | 507 | if (!empty($params['updateMapping'])) { |
6a488035 TO |
508 | $mappingFields = new CRM_Core_DAO_MappingField(); |
509 | $mappingFields->mapping_id = $params['mappingId']; | |
510 | $mappingFields->find(); | |
511 | ||
98ca76db | 512 | $mappingFieldsId = []; |
6a488035 TO |
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 | |
a7488080 | 533 | if (!empty($params['saveMapping'])) { |
98ca76db | 534 | $mappingParams = [ |
6a488035 TO |
535 | 'name' => $params['saveMappingName'], |
536 | 'description' => $params['saveMappingDesc'], | |
95f52e3b | 537 | 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'), |
98ca76db | 538 | ]; |
6a488035 TO |
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, | |
a05662ef | 556 | CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType') |
6a488035 TO |
557 | ); |
558 | ||
559 | // add all the necessary variables to the form | |
560 | $parser->set($this); | |
561 | } | |
96025800 | 562 | |
6a488035 | 563 | } |