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