Merge pull request #10602 from agileware/CIVICRM-167
[civicrm-core.git] / CRM / Contribute / Import / Form / MapField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * This class gets the name of the file to upload.
36 */
37 class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {
38
39
40 /**
41 * Set variables up before form is built.
42 */
43 public function preProcess() {
44 $this->_mapperFields = $this->get('fields');
45 asort($this->_mapperFields);
46
47 $this->_columnCount = $this->get('columnCount');
48 $this->assign('columnCount', $this->_columnCount);
49 $this->_dataValues = $this->get('dataValues');
50 $this->assign('dataValues', $this->_dataValues);
51
52 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
53 $this->_onDuplicate = $this->get('onDuplicate', isset($onDuplicate) ? $onDuplicate : "");
54
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 */
59
60 $this->_columnHeaders = $this->_dataValues[0];
61 }
62 else {
63 $this->assign('rowDisplayCount', 2);
64 }
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]);
72 }
73
74 //modify field title only for update mode. CRM-3245
75 foreach (array(
76 'contribution_id',
77 'invoice_id',
78 'trxn_id',
79 ) as $key) {
80 $this->_mapperFields[$key] .= ' (match to contribution record)';
81 $highlightedFields[] = $key;
82 }
83 }
84 elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) {
85 unset($this->_mapperFields['contribution_id']);
86 $highlightedFieldsArray = array(
87 'contribution_contact_id',
88 'email',
89 'first_name',
90 'last_name',
91 'external_identifier',
92 );
93 foreach ($highlightedFieldsArray as $name) {
94 $highlightedFields[] = $name;
95 }
96 }
97
98 // modify field title for contribution status
99 $this->_mapperFields['contribution_status_id'] = ts('Contribution Status');
100
101 $this->assign('highlightedFields', $highlightedFields);
102 }
103
104 /**
105 * Build the form object.
106 */
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'));
114 }
115 else {
116 $savedMapping = $this->get('savedMapping');
117
118 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping);
119
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));
125
126 //mapping is to be loaded from database
127
128 $params = array('id' => $savedMapping);
129 $temp = array();
130 $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp);
131
132 $this->assign('loadedMapping', $mappingDetails->name);
133 $this->set('loadedMapping', $savedMapping);
134
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;
141 }
142
143 $this->assign('savedName', $mapperName);
144
145 $this->add('hidden', 'mappingId', $savedMapping);
146
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'));
151 }
152
153 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
154
155 $this->addFormRule(array('CRM_Contribute_Import_Form_MapField', 'formRule'), $this);
156
157 //-------- end of saved mapping stuff ---------
158
159 $defaults = array();
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');
165
166 /* Initialize all field usages to false */
167 foreach ($mapperKeys as $key) {
168 $this->_fieldUsed[$key] = FALSE;
169 }
170 $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
171 $sel1 = $this->_mapperFields;
172
173 if (!$this->get('onDuplicate')) {
174 unset($sel1['id']);
175 unset($sel1['contribution_id']);
176 }
177
178 $softCreditFields['contact_id'] = ts('Contact ID');
179 $softCreditFields['external_identifier'] = ts('External ID');
180 $softCreditFields['email'] = ts('Email');
181
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');
184 $sel4 = NULL;
185
186 // end of soft credit section
187 $js = "<script type='text/javascript'>\n";
188 $formName = 'document.forms.' . $this->_name;
189
190 //used to warn for mismatch column count or mismatch mapping
191 $warning = 0;
192
193 for ($i = 0; $i < $this->_columnCount; $i++) {
194 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
195 $jsSet = FALSE;
196 if ($this->get('savedMapping')) {
197 if (isset($mappingName[$i])) {
198 if ($mappingName[$i] != ts('- do not import -')) {
199
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;
203
204 if (!$softField) {
205 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
206 }
207
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 : "",
215 );
216 $jsSet = TRUE;
217 }
218 else {
219 $defaults["mapper[$i]"] = array();
220 }
221 if (!$jsSet) {
222 for ($k = 1; $k < 4; $k++) {
223 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
224 }
225 }
226 }
227 else {
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";
230
231 if ($hasHeaders) {
232 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns));
233 }
234 else {
235 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
236 }
237 }
238 //end of load mapping
239 }
240 else {
241 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
242 if ($hasHeaders) {
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;
248 }
249 else {
250 // Infer the default from the column names if we have them
251 $defaults["mapper[$i]"] = array(
252 $this->defaultFromHeader($this->_columnHeaders[$i],
253 $headerPatterns
254 ),
255 0,
256 );
257 }
258 }
259 else {
260 // Otherwise guess the default from the form of the data
261 $defaults["mapper[$i]"] = array(
262 $this->defaultFromData($dataPatterns, $i),
263 // $defaultLocationType->id
264 0,
265 );
266 }
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";
270 }
271 }
272 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
273 }
274 $js .= "</script>\n";
275 $this->assign('initHideBoxes', $js);
276
277 //set warning if mismatch in more than
278 if (isset($mappingName)) {
279 if (($this->_columnCount != count($mappingName))) {
280 $warning++;
281 }
282 }
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.'));
286 }
287 else {
288 $session = CRM_Core_Session::singleton();
289 $session->setStatus(NULL);
290 }
291
292 $this->setDefaults($defaults);
293
294 $this->addButtons(array(
295 array(
296 'type' => 'back',
297 'name' => ts('Previous'),
298 ),
299 array(
300 'type' => 'next',
301 'name' => ts('Continue'),
302 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
303 'isDefault' => TRUE,
304 ),
305 array(
306 'type' => 'cancel',
307 'name' => ts('Cancel'),
308 ),
309 )
310 );
311 }
312
313 /**
314 * Global validation rules for the form.
315 *
316 * @param array $fields
317 * Posted values of the form.
318 *
319 * @param $files
320 * @param $self
321 *
322 * @return array
323 * list of errors to be posted back to the form
324 */
325 public static function formRule($fields, $files, $self) {
326 $errors = array();
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];
333 }
334
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',
340 );
341 $params = array(
342 'used' => 'Unsupervised',
343 'contact_type' => isset($contactTypes[$contactTypeId]) ? $contactTypes[$contactTypeId] : '',
344 );
345 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params);
346 $weightSum = 0;
347 foreach ($importKeys as $key => $val) {
348 if (array_key_exists($val, $ruleFields)) {
349 $weightSum += $ruleFields[$val];
350 }
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'] = '';
356 }
357 $errors['_qf_default'] .= ts('Missing required fields: Soft Credit') . '<br />';
358 }
359 }
360 }
361 foreach ($ruleFields as $field => $weight) {
362 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
363 }
364
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'),
370 );
371
372 foreach ($requiredFields as $field => $title) {
373 if (!in_array($field, $importKeys)) {
374 if (empty($errors['_qf_default'])) {
375 $errors['_qf_default'] = '';
376 }
377 if ($field == $contactORContributionId) {
378 if (!($weightSum >= $threshold || in_array('external_identifier', $importKeys)) &&
379 $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE
380 ) {
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(
382 1 => $threshold,
383 )) . '<br />';
384 }
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)
388 )
389 ) {
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 />';
391 }
392 }
393 else {
394 $errors['_qf_default'] .= ts('Missing required field: %1', array(
395 1 => $title,
396 )) . '<br />';
397 }
398 }
399 }
400
401 //at least one field should be mapped during update.
402 if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
403 $atleastOne = FALSE;
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'))
407 ) {
408 $atleastOne = TRUE;
409 break;
410 }
411 }
412 if (!$atleastOne) {
413 $errors['_qf_default'] .= ts('At least one contribution field needs to be mapped for update during update mode.') . '<br />';
414 }
415 }
416 }
417
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');
422 }
423 else {
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');
426 }
427 }
428 }
429
430 if (!empty($errors)) {
431 if (!empty($errors['saveMappingName'])) {
432 $_flag = 1;
433 $assignError = new CRM_Core_Page();
434 $assignError->assign('mappingDetailsError', $_flag);
435 }
436 if (!empty($errors['_qf_default'])) {
437 CRM_Core_Session::setStatus($errors['_qf_default'], ts("Error"), "error");
438 return $errors;
439 }
440 }
441
442 return TRUE;
443 }
444
445 /**
446 * Process the mapped fields and map it into the uploaded file preview the file and extract some summary statistics.
447 */
448 public function postProcess() {
449 $params = $this->controller->exportValues('MapField');
450
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);
455 return;
456 }
457
458 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
459 $seperator = $this->controller->exportValue('DataSource', 'fieldSeparator');
460 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
461
462 $mapper = $mapperKeys = $mapperKeysMain = $mapperSoftCredit = $softCreditFields = $mapperPhoneType = $mapperSoftCreditType = array();
463 $mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
464
465 $softCreditTypes = CRM_Core_OptionGroup::values('soft_credit_type');
466
467 for ($i = 0; $i < $this->_columnCount; $i++) {
468 $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]];
469 $mapperKeysMain[$i] = $mapperKeys[$i][0];
470
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);
476 }
477 else {
478 $softCreditFields[$i] = $mapperSoftCredit[$i];
479 }
480 $mapperSoftCreditType[$i] = array(
481 'value' => isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : '',
482 'label' => isset($softCreditTypes[$mapperKeys[$i][2]]) ? $softCreditTypes[$mapperKeys[$i][2]] : '',
483 );
484 }
485 else {
486 $mapperSoftCredit[$i] = $softCreditFields[$i] = $mapperSoftCreditType[$i] = NULL;
487 }
488 }
489
490 $this->set('mapper', $mapper);
491 $this->set('softCreditFields', $softCreditFields);
492 $this->set('mapperSoftCreditType', $mapperSoftCreditType);
493
494 // store mapping Id to display it in the preview page
495 $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params));
496
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();
502
503 $mappingFieldsId = array();
504 while ($mappingFields->fetch()) {
505 if ($mappingFields->id) {
506 $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id;
507 }
508 }
509
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];
516
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();
520 }
521 }
522
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'),
529 );
530 $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
531
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];
537
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();
541 }
542 $this->set('savedMapping', $saveMappingFields->mapping_id);
543 }
544
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')
548 );
549
550 // add all the necessary variables to the form
551 $parser->set($this);
552 }
553
554 }