3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
35 * This class gets the name of the file to upload.
37 class CRM_Contribute_Import_Form_MapField
extends CRM_Import_Form_MapField
{
40 * Set variables up before form is built.
42 public function preProcess() {
43 $this->_mapperFields
= $this->get('fields');
44 asort($this->_mapperFields
);
46 $this->_columnCount
= $this->get('columnCount');
47 $this->assign('columnCount', $this->_columnCount
);
48 $this->_dataValues
= $this->get('dataValues');
49 $this->assign('dataValues', $this->_dataValues
);
51 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
52 $this->_onDuplicate
= $this->get('onDuplicate', isset($onDuplicate) ?
$onDuplicate : "");
54 if ($skipColumnHeader) {
55 $this->assign('skipColumnHeader', $skipColumnHeader);
56 $this->assign('rowDisplayCount', 3);
57 // If we had a column header to skip, stash it for later
59 $this->_columnHeaders
= $this->_dataValues
[0];
62 $this->assign('rowDisplayCount', 2);
64 $highlightedFields = ['financial_type', 'total_amount'];
65 //CRM-2219 removing other required fields since for updation only
66 //invoice id or trxn id or contribution id is required.
67 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
69 'contribution_contact_id',
73 'external_identifier',
75 foreach ($remove as $value) {
76 unset($this->_mapperFields
[$value]);
79 //modify field title only for update mode. CRM-3245
85 $this->_mapperFields
[$key] .= ' (match to contribution record)';
86 $highlightedFields[] = $key;
89 elseif ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP
) {
90 unset($this->_mapperFields
['contribution_id']);
91 $highlightedFieldsArray = [
92 'contribution_contact_id',
96 'external_identifier',
98 foreach ($highlightedFieldsArray as $name) {
99 $highlightedFields[] = $name;
103 // modify field title for contribution status
104 $this->_mapperFields
['contribution_status_id'] = ts('Contribution Status');
106 $this->assign('highlightedFields', $highlightedFields);
110 * Build the form object.
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'));
121 $savedMapping = $this->get('savedMapping');
123 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
125 $mappingName = $mappingName[1];
126 $mappingContactType = $mappingContactType[1];
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));
131 //mapping is to be loaded from database
133 $params = ['id' => $savedMapping];
135 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
137 $this->assign('loadedMapping', $mappingDetails->name
);
138 $this->set('loadedMapping', $savedMapping);
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
;
148 $this->assign('savedName', $mapperName);
150 $this->add('hidden', 'mappingId', $savedMapping);
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'));
158 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]);
161 'CRM_Contribute_Import_Form_MapField',
165 //-------- end of saved mapping stuff ---------
168 $mapperKeys = array_keys($this->_mapperFields
);
169 $hasHeaders = !empty($this->_columnHeaders
);
170 $headerPatterns = $this->get('headerPatterns');
171 $dataPatterns = $this->get('dataPatterns');
172 $mapperKeysValues = $this->controller
->exportValue($this->_name
, 'mapper');
174 /* Initialize all field usages to false */
175 foreach ($mapperKeys as $key) {
176 $this->_fieldUsed
[$key] = FALSE;
178 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
179 $sel1 = $this->_mapperFields
;
181 if (!$this->get('onDuplicate')) {
183 unset($sel1['contribution_id']);
186 $softCreditFields['contact_id'] = ts('Contact ID');
187 $softCreditFields['external_identifier'] = ts('External ID');
188 $softCreditFields['email'] = ts('Email');
190 $sel2['soft_credit'] = $softCreditFields;
191 $sel3['soft_credit']['contact_id'] = $sel3['soft_credit']['external_identifier'] = $sel3['soft_credit']['email'] = CRM_Core_OptionGroup
::values('soft_credit_type');
194 // end of soft credit section
195 $js = "<script type='text/javascript'>\n";
196 $formName = 'document.forms.' . $this->_name
;
198 //used to warn for mismatch column count or mismatch mapping
201 for ($i = 0; $i < $this->_columnCount
; $i++
) {
202 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);
204 if ($this->get('savedMapping')) {
205 if (isset($mappingName[$i])) {
206 if ($mappingName[$i] != ts('- do not import -')) {
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;
213 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
216 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
217 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
218 $defaults["mapper[$i]"] = [
219 CRM_Utils_Array
::value(0, $mappingHeader),
220 ($softField) ?
$softField : "",
221 (isset($locationId)) ?
$locationId : "",
222 (isset($phoneType)) ?
$phoneType : "",
227 $defaults["mapper[$i]"] = [];
230 for ($k = 1; $k < 4; $k++
) {
231 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
236 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
237 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
240 $defaults["mapper[$i]"] = [$this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns)];
243 $defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)];
246 //end of load mapping
249 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
251 // do array search first to see if has mapped key
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;
258 // Infer the default from the column names if we have them
259 $defaults["mapper[$i]"] = [
260 $this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns),
266 // Otherwise guess the default from the form of the data
267 $defaults["mapper[$i]"] = [
268 $this->defaultFromData($dataPatterns, $i),
272 if (!empty($mapperKeysValues) && $mapperKeysValues[$i][0] == 'soft_credit') {
273 $js .= "cj('#mapper_" . $i . "_1').val($mapperKeysValues[$i][1]);\n";
274 $js .= "cj('#mapper_" . $i . "_2').val($mapperKeysValues[$i][2]);\n";
277 $sel->setOptions([$sel1, $sel2, $sel3, $sel4]);
279 $js .= "</script>\n";
280 $this->assign('initHideBoxes', $js);
282 //set warning if mismatch in more than
283 if (isset($mappingName)) {
284 if (($this->_columnCount
!= count($mappingName))) {
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.'));
293 $session = CRM_Core_Session
::singleton();
294 $session->setStatus(NULL);
297 $this->setDefaults($defaults);
302 'name' => ts('Previous'),
306 'name' => ts('Continue'),
307 'spacing' => ' ',
312 'name' => ts('Cancel'),
318 * Global validation rules for the form.
320 * @param array $fields
321 * Posted values of the form.
327 * list of errors to be posted back to the form
329 public static function formRule($fields, $files, $self) {
331 $fieldMessage = NULL;
332 $contactORContributionId = $self->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE ?
'contribution_id' : 'contribution_contact_id';
333 if (!array_key_exists('savedMapping', $fields)) {
335 foreach ($fields['mapper'] as $mapperPart) {
336 $importKeys[] = $mapperPart[0];
339 $contactTypeId = $self->get('contactType');
341 CRM_Import_Parser
::CONTACT_INDIVIDUAL
=> 'Individual',
342 CRM_Import_Parser
::CONTACT_HOUSEHOLD
=> 'Household',
343 CRM_Import_Parser
::CONTACT_ORGANIZATION
=> 'Organization',
346 'used' => 'Unsupervised',
347 'contact_type' => isset($contactTypes[$contactTypeId]) ?
$contactTypes[$contactTypeId] : '',
349 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
351 foreach ($importKeys as $key => $val) {
352 if (array_key_exists($val, $ruleFields)) {
353 $weightSum +
= $ruleFields[$val];
355 if ($val == "soft_credit") {
356 $mapperKey = CRM_Utils_Array
::key('soft_credit', $importKeys);
357 if (empty($fields['mapper'][$mapperKey][1])) {
358 if (empty($errors['_qf_default'])) {
359 $errors['_qf_default'] = '';
361 $errors['_qf_default'] .= ts('Missing required fields: Soft Credit') . '<br />';
365 foreach ($ruleFields as $field => $weight) {
366 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
369 // FIXME: should use the schema titles, not redeclare them
371 $contactORContributionId == 'contribution_id' ?
'contribution_id' : 'contribution_contact_id' => $contactORContributionId == 'contribution_id' ?
ts('Contribution ID') : ts('Contact ID'),
372 'total_amount' => ts('Total Amount'),
373 'financial_type' => ts('Financial Type'),
376 foreach ($requiredFields as $field => $title) {
377 if (!in_array($field, $importKeys)) {
378 if (empty($errors['_qf_default'])) {
379 $errors['_qf_default'] = '';
381 if ($field == $contactORContributionId) {
382 if (!($weightSum >= $threshold ||
in_array('external_identifier', $importKeys)) &&
383 $self->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_UPDATE
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).', [
389 elseif ($self->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
&&
390 !(in_array('invoice_id', $importKeys) ||
in_array('trxn_id', $importKeys) ||
391 in_array('contribution_id', $importKeys)
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 />';
398 $errors['_qf_default'] .= ts('Missing required field: %1', [
405 //at least one field should be mapped during update.
406 if ($self->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
408 foreach ($self->_mapperFields
as $key => $field) {
409 if (in_array($key, $importKeys) &&
422 $errors['_qf_default'] .= ts('At least one contribution field needs to be mapped for update during update mode.') . '<br />';
427 if (!empty($fields['saveMapping'])) {
428 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
429 if (empty($nameField)) {
430 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
433 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'))) {
434 $errors['saveMappingName'] = ts('Duplicate Import Contribution Mapping Name');
439 if (!empty($errors)) {
440 if (!empty($errors['saveMappingName'])) {
442 $assignError = new CRM_Core_Page();
443 $assignError->assign('mappingDetailsError', $_flag);
445 if (!empty($errors['_qf_default'])) {
446 CRM_Core_Session
::setStatus($errors['_qf_default'], ts("Error"), "error");
455 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
457 public function postProcess() {
458 $params = $this->controller
->exportValues('MapField');
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
);
467 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
468 $seperator = $this->controller
->exportValue('DataSource', 'fieldSeparator');
469 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
471 $mapper = $mapperKeys = $mapperKeysMain = $mapperSoftCredit = $softCreditFields = $mapperPhoneType = $mapperSoftCreditType = [];
472 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
474 $softCreditTypes = CRM_Core_OptionGroup
::values('soft_credit_type');
476 for ($i = 0; $i < $this->_columnCount
; $i++
) {
477 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
478 $mapperKeysMain[$i] = $mapperKeys[$i][0];
480 if (isset($mapperKeys[$i][0]) && $mapperKeys[$i][0] == 'soft_credit') {
481 $mapperSoftCredit[$i] = $mapperKeys[$i][1];
482 if (strpos($mapperSoftCredit[$i], '_') !== FALSE) {
483 list($first, $second) = explode('_', $mapperSoftCredit[$i]);
484 $softCreditFields[$i] = ucwords($first . " " . $second);
487 $softCreditFields[$i] = $mapperSoftCredit[$i];
489 $mapperSoftCreditType[$i] = [
490 'value' => isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : '',
491 'label' => isset($softCreditTypes[$mapperKeys[$i][2]]) ?
$softCreditTypes[$mapperKeys[$i][2]] : '',
495 $mapperSoftCredit[$i] = $softCreditFields[$i] = $mapperSoftCreditType[$i] = NULL;
499 $this->set('mapper', $mapper);
500 $this->set('softCreditFields', $softCreditFields);
501 $this->set('mapperSoftCreditType', $mapperSoftCreditType);
503 // store mapping Id to display it in the preview page
504 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
506 //Updating Mapping Records
507 if (!empty($params['updateMapping'])) {
508 $mappingFields = new CRM_Core_DAO_MappingField();
509 $mappingFields->mapping_id
= $params['mappingId'];
510 $mappingFields->find();
512 $mappingFieldsId = [];
513 while ($mappingFields->fetch()) {
514 if ($mappingFields->id
) {
515 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
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];
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();
532 //Saving Mapping Details and Records
533 if (!empty($params['saveMapping'])) {
535 'name' => $params['saveMappingName'],
536 'description' => $params['saveMappingDesc'],
537 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Contribution'),
539 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
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];
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();
551 $this->set('savedMapping', $saveMappingFields->mapping_id
);
554 $parser = new CRM_Contribute_Import_Parser_Contribution($mapperKeysMain, $mapperSoftCredit, $mapperPhoneType);
555 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
556 CRM_Import_Parser
::MODE_PREVIEW
, $this->get('contactType')
559 // add all the necessary variables to the form