3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * This class gets the name of the file to upload.
37 class CRM_Contribute_Import_Form_MapField
extends CRM_Import_Form_MapField
{
41 * Set variables up before form is built.
43 public function preProcess() {
44 $this->_mapperFields
= $this->get('fields');
45 asort($this->_mapperFields
);
47 $this->_columnCount
= $this->get('columnCount');
48 $this->assign('columnCount', $this->_columnCount
);
49 $this->_dataValues
= $this->get('dataValues');
50 $this->assign('dataValues', $this->_dataValues
);
52 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
53 $this->_onDuplicate
= $this->get('onDuplicate', isset($onDuplicate) ?
$onDuplicate : "");
55 if ($skipColumnHeader) {
56 $this->assign('skipColumnHeader', $skipColumnHeader);
57 $this->assign('rowDisplayCount', 3);
58 /* if we had a column header to skip, stash it for later */
60 $this->_columnHeaders
= $this->_dataValues
[0];
63 $this->assign('rowDisplayCount', 2);
65 $highlightedFields = array('financial_type', 'total_amount');
66 //CRM-2219 removing other required fields since for updation only
67 //invoice id or trxn id or contribution id is required.
68 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
69 $remove = array('contribution_contact_id', 'email', 'first_name', 'last_name', 'external_identifier');
70 foreach ($remove as $value) {
71 unset($this->_mapperFields
[$value]);
74 //modify field title only for update mode. CRM-3245
80 $this->_mapperFields
[$key] .= ' (match to contribution record)';
81 $highlightedFields[] = $key;
84 elseif ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP
) {
85 unset($this->_mapperFields
['contribution_id']);
86 $highlightedFieldsArray = array(
87 'contribution_contact_id',
91 'external_identifier',
93 foreach ($highlightedFieldsArray as $name) {
94 $highlightedFields[] = $name;
98 // modify field title for contribution status
99 $this->_mapperFields
['contribution_status_id'] = ts('Contribution Status');
101 $this->assign('highlightedFields', $highlightedFields);
105 * Build the form object.
107 public function buildQuickForm() {
108 //to save the current mappings
109 if (!$this->get('savedMapping')) {
110 $saveDetailsName = ts('Save this field mapping');
111 $this->applyFilter('saveMappingName', 'trim');
112 $this->add('text', 'saveMappingName', ts('Name'));
113 $this->add('text', 'saveMappingDesc', ts('Description'));
116 $savedMapping = $this->get('savedMapping');
118 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
120 $mappingName = $mappingName[1];
121 $mappingContactType = $mappingContactType[1];
122 $mappingLocation = CRM_Utils_Array
::value('1', CRM_Utils_Array
::value(1, $mappingLocation));
123 $mappingPhoneType = CRM_Utils_Array
::value('1', CRM_Utils_Array
::value(1, $mappingPhoneType));
124 $mappingRelation = CRM_Utils_Array
::value('1', CRM_Utils_Array
::value(1, $mappingRelation));
126 //mapping is to be loaded from database
128 $params = array('id' => $savedMapping);
130 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
132 $this->assign('loadedMapping', $mappingDetails->name
);
133 $this->set('loadedMapping', $savedMapping);
135 $getMappingName = new CRM_Core_DAO_Mapping();
136 $getMappingName->id
= $savedMapping;
137 $getMappingName->mapping_type
= 'Import Contributions';
138 $getMappingName->find();
139 while ($getMappingName->fetch()) {
140 $mapperName = $getMappingName->name
;
143 $this->assign('savedName', $mapperName);
145 $this->add('hidden', 'mappingId', $savedMapping);
147 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
148 $saveDetailsName = ts('Save as a new field mapping');
149 $this->add('text', 'saveMappingName', ts('Name'));
150 $this->add('text', 'saveMappingDesc', ts('Description'));
153 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
155 $this->addFormRule(array('CRM_Contribute_Import_Form_MapField', 'formRule'), $this);
157 //-------- end of saved mapping stuff ---------
160 $mapperKeys = array_keys($this->_mapperFields
);
161 $hasHeaders = !empty($this->_columnHeaders
);
162 $headerPatterns = $this->get('headerPatterns');
163 $dataPatterns = $this->get('dataPatterns');
164 $mapperKeysValues = $this->controller
->exportValue($this->_name
, 'mapper');
166 /* Initialize all field usages to false */
167 foreach ($mapperKeys as $key) {
168 $this->_fieldUsed
[$key] = FALSE;
170 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
171 $sel1 = $this->_mapperFields
;
173 if (!$this->get('onDuplicate')) {
175 unset($sel1['contribution_id']);
178 $softCreditFields['contact_id'] = ts('Contact ID');
179 $softCreditFields['external_identifier'] = ts('External ID');
180 $softCreditFields['email'] = ts('Email');
182 $sel2['soft_credit'] = $softCreditFields;
183 $sel3['soft_credit']['contact_id'] = $sel3['soft_credit']['external_identifier'] = $sel3['soft_credit']['email'] = CRM_Core_OptionGroup
::values('soft_credit_type');
186 // end of soft credit section
187 $js = "<script type='text/javascript'>\n";
188 $formName = 'document.forms.' . $this->_name
;
190 //used to warn for mismatch column count or mismatch mapping
193 for ($i = 0; $i < $this->_columnCount
; $i++
) {
194 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
196 if ($this->get('savedMapping')) {
197 if (isset($mappingName[$i])) {
198 if ($mappingName[$i] != ts('- do not import -')) {
200 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
201 // reusing contact_type field array for soft credit
202 $softField = isset($mappingContactType[$i]) ?
$mappingContactType[$i] : 0;
205 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
208 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
209 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
210 $defaults["mapper[$i]"] = array(
211 CRM_Utils_Array
::value(0, $mappingHeader),
212 ($softField) ?
$softField : "",
213 (isset($locationId)) ?
$locationId : "",
214 (isset($phoneType)) ?
$phoneType : "",
219 $defaults["mapper[$i]"] = array();
222 for ($k = 1; $k < 4; $k++
) {
223 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
228 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
229 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
232 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns));
235 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
238 //end of load mapping
241 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
243 // do array search first to see if has mapped key
244 $columnKey = array_search($this->_columnHeaders
[$i], $this->_mapperFields
);
245 if (isset($this->_fieldUsed
[$columnKey])) {
246 $defaults["mapper[$i]"] = $columnKey;
247 $this->_fieldUsed
[$key] = TRUE;
250 // Infer the default from the column names if we have them
251 $defaults["mapper[$i]"] = array(
252 $this->defaultFromHeader($this->_columnHeaders
[$i],
260 // Otherwise guess the default from the form of the data
261 $defaults["mapper[$i]"] = array(
262 $this->defaultFromData($dataPatterns, $i),
263 // $defaultLocationType->id
267 if (!empty($mapperKeysValues) && $mapperKeysValues[$i][0] == 'soft_credit') {
268 $js .= "cj('#mapper_" . $i . "_1').val($mapperKeysValues[$i][1]);\n";
269 $js .= "cj('#mapper_" . $i . "_2').val($mapperKeysValues[$i][2]);\n";
272 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
274 $js .= "</script>\n";
275 $this->assign('initHideBoxes', $js);
277 //set warning if mismatch in more than
278 if (isset($mappingName)) {
279 if (($this->_columnCount
!= count($mappingName))) {
283 if ($warning != 0 && $this->get('savedMapping')) {
284 $session = CRM_Core_Session
::singleton();
285 $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.'));
288 $session = CRM_Core_Session
::singleton();
289 $session->setStatus(NULL);
292 $this->setDefaults($defaults);
294 $this->addButtons(array(
297 'name' => ts('Previous'),
301 'name' => ts('Continue'),
302 'spacing' => ' ',
307 'name' => ts('Cancel'),
314 * Global validation rules for the form.
316 * @param array $fields
317 * Posted values of the form.
323 * list of errors to be posted back to the form
325 public static function formRule($fields, $files, $self) {
327 $fieldMessage = NULL;
328 $contactORContributionId = $self->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE ?
'contribution_id' : 'contribution_contact_id';
329 if (!array_key_exists('savedMapping', $fields)) {
330 $importKeys = array();
331 foreach ($fields['mapper'] as $mapperPart) {
332 $importKeys[] = $mapperPart[0];
335 $contactTypeId = $self->get('contactType');
336 $contactTypes = array(
337 CRM_Import_Parser
::CONTACT_INDIVIDUAL
=> 'Individual',
338 CRM_Import_Parser
::CONTACT_HOUSEHOLD
=> 'Household',
339 CRM_Import_Parser
::CONTACT_ORGANIZATION
=> 'Organization',
342 'used' => 'Unsupervised',
343 'contact_type' => isset($contactTypes[$contactTypeId]) ?
$contactTypes[$contactTypeId] : '',
345 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
347 foreach ($importKeys as $key => $val) {
348 if (array_key_exists($val, $ruleFields)) {
349 $weightSum +
= $ruleFields[$val];
351 if ($val == "soft_credit") {
352 $mapperKey = CRM_Utils_Array
::key('soft_credit', $importKeys);
353 if (empty($fields['mapper'][$mapperKey][1])) {
354 if (empty($errors['_qf_default'])) {
355 $errors['_qf_default'] = '';
357 $errors['_qf_default'] .= ts('Missing required fields: Soft Credit') . '<br />';
361 foreach ($ruleFields as $field => $weight) {
362 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
365 // FIXME: should use the schema titles, not redeclare them
366 $requiredFields = array(
367 $contactORContributionId == 'contribution_id' ?
'contribution_id' : 'contribution_contact_id' => $contactORContributionId == 'contribution_id' ?
ts('Contribution ID') : ts('Contact ID'),
368 'total_amount' => ts('Total Amount'),
369 'financial_type' => ts('Financial Type'),
372 foreach ($requiredFields as $field => $title) {
373 if (!in_array($field, $importKeys)) {
374 if (empty($errors['_qf_default'])) {
375 $errors['_qf_default'] = '';
377 if ($field == $contactORContributionId) {
378 if (!($weightSum >= $threshold ||
in_array('external_identifier', $importKeys)) &&
379 $self->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_UPDATE
381 $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(
385 elseif ($self->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
&&
386 !(in_array('invoice_id', $importKeys) ||
in_array('trxn_id', $importKeys) ||
387 in_array('contribution_id', $importKeys)
390 $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 />';
394 $errors['_qf_default'] .= ts('Missing required field: %1', array(
401 //at least one field should be mapped during update.
402 if ($self->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
404 foreach ($self->_mapperFields
as $key => $field) {
405 if (in_array($key, $importKeys) &&
406 !in_array($key, array('doNotImport', 'contribution_id', 'invoice_id', 'trxn_id'))
413 $errors['_qf_default'] .= ts('At least one contribution field needs to be mapped for update during update mode.') . '<br />';
418 if (!empty($fields['saveMapping'])) {
419 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
420 if (empty($nameField)) {
421 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
424 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'))) {
425 $errors['saveMappingName'] = ts('Duplicate Import Contribution Mapping Name');
430 if (!empty($errors)) {
431 if (!empty($errors['saveMappingName'])) {
433 $assignError = new CRM_Core_Page();
434 $assignError->assign('mappingDetailsError', $_flag);
436 if (!empty($errors['_qf_default'])) {
437 CRM_Core_Session
::setStatus($errors['_qf_default'], ts("Error"), "error");
446 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
448 public function postProcess() {
449 $params = $this->controller
->exportValues('MapField');
451 //reload the mapfield if load mapping is pressed
452 if (!empty($params['savedMapping'])) {
453 $this->set('savedMapping', $params['savedMapping']);
454 $this->controller
->resetPage($this->_name
);
458 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
459 $seperator = $this->controller
->exportValue('DataSource', 'fieldSeparator');
460 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
462 $mapper = $mapperKeys = $mapperKeysMain = $mapperSoftCredit = $softCreditFields = $mapperPhoneType = $mapperSoftCreditType = array();
463 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
465 $softCreditTypes = CRM_Core_OptionGroup
::values('soft_credit_type');
467 for ($i = 0; $i < $this->_columnCount
; $i++
) {
468 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
469 $mapperKeysMain[$i] = $mapperKeys[$i][0];
471 if (isset($mapperKeys[$i][0]) && $mapperKeys[$i][0] == 'soft_credit') {
472 $mapperSoftCredit[$i] = $mapperKeys[$i][1];
473 if (strpos($mapperSoftCredit[$i], '_') !== FALSE) {
474 list($first, $second) = explode('_', $mapperSoftCredit[$i]);
475 $softCreditFields[$i] = ucwords($first . " " . $second);
478 $softCreditFields[$i] = $mapperSoftCredit[$i];
480 $mapperSoftCreditType[$i] = array(
481 'value' => isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : '',
482 'label' => isset($softCreditTypes[$mapperKeys[$i][2]]) ?
$softCreditTypes[$mapperKeys[$i][2]] : '',
486 $mapperSoftCredit[$i] = $softCreditFields[$i] = $mapperSoftCreditType[$i] = NULL;
490 $this->set('mapper', $mapper);
491 $this->set('softCreditFields', $softCreditFields);
492 $this->set('mapperSoftCreditType', $mapperSoftCreditType);
494 // store mapping Id to display it in the preview page
495 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
497 //Updating Mapping Records
498 if (!empty($params['updateMapping'])) {
499 $mappingFields = new CRM_Core_DAO_MappingField();
500 $mappingFields->mapping_id
= $params['mappingId'];
501 $mappingFields->find();
503 $mappingFieldsId = array();
504 while ($mappingFields->fetch()) {
505 if ($mappingFields->id
) {
506 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
510 for ($i = 0; $i < $this->_columnCount
; $i++
) {
511 $updateMappingFields = new CRM_Core_DAO_MappingField();
512 $updateMappingFields->id
= $mappingFieldsId[$i];
513 $updateMappingFields->mapping_id
= $params['mappingId'];
514 $updateMappingFields->column_number
= $i;
515 $updateMappingFields->name
= $mapper[$i];
517 //reuse contact_type field in db to store fields associated with soft credit
518 $updateMappingFields->contact_type
= isset($mapperSoftCredit[$i]) ?
$mapperSoftCredit[$i] : NULL;
519 $updateMappingFields->save();
523 //Saving Mapping Details and Records
524 if (!empty($params['saveMapping'])) {
525 $mappingParams = array(
526 'name' => $params['saveMappingName'],
527 'description' => $params['saveMappingDesc'],
528 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'),
530 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
532 for ($i = 0; $i < $this->_columnCount
; $i++
) {
533 $saveMappingFields = new CRM_Core_DAO_MappingField();
534 $saveMappingFields->mapping_id
= $saveMapping->id
;
535 $saveMappingFields->column_number
= $i;
536 $saveMappingFields->name
= $mapper[$i];
538 //reuse contact_type field in db to store fields associated with soft credit
539 $saveMappingFields->contact_type
= isset($mapperSoftCredit[$i]) ?
$mapperSoftCredit[$i] : NULL;
540 $saveMappingFields->save();
542 $this->set('savedMapping', $saveMappingFields->mapping_id
);
545 $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeysMain, $mapperSoftCredit, $mapperPhoneType);
546 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
547 CRM_Import_Parser
::MODE_PREVIEW
, $this->get('contactType')
550 // add all the necessary variables to the form