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