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