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