3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class gets the name of the file to upload
39 class CRM_Contribute_Import_Form_MapField
extends CRM_Import_Form_MapField
{
43 * Set variables up before form is built.
47 public function preProcess() {
48 $this->_mapperFields
= $this->get('fields');
49 asort($this->_mapperFields
);
51 $this->_columnCount
= $this->get('columnCount');
52 $this->assign('columnCount', $this->_columnCount
);
53 $this->_dataValues
= $this->get('dataValues');
54 $this->assign('dataValues', $this->_dataValues
);
56 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
57 $this->_onDuplicate
= $this->get('onDuplicate', isset($onDuplicate) ?
$onDuplicate : "");
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 */
64 $this->_columnHeaders
= $this->_dataValues
[0];
67 $this->assign('rowDisplayCount', 2);
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]);
78 //modify field title only for update mode. CRM-3245
84 $this->_mapperFields
[$key] .= ' (match to contribution record)';
85 $highlightedFields[] = $key;
88 elseif ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP
) {
89 unset($this->_mapperFields
['contribution_id']);
90 $highlightedFieldsArray = array(
91 'contribution_contact_id',
95 'external_identifier',
97 foreach ($highlightedFieldsArray as $name) {
98 $highlightedFields[] = $name;
102 // modify field title for contribution status
103 $this->_mapperFields
['contribution_status_id'] = ts('Contribution Status');
105 $this->assign('highlightedFields', $highlightedFields);
109 * Build the form object.
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'));
122 $savedMapping = $this->get('savedMapping');
124 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
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));
132 //mapping is to be loaded from database
134 $params = array('id' => $savedMapping);
136 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
138 $this->assign('loadedMapping', $mappingDetails->name
);
139 $this->set('loadedMapping', $savedMapping);
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
;
149 $this->assign('savedName', $mapperName);
151 $this->add('hidden', 'mappingId', $savedMapping);
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'));
159 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
161 $this->addFormRule(array('CRM_Contribute_Import_Form_MapField', 'formRule'), $this);
163 //-------- end of saved mapping stuff ---------
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');
173 /* 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 // 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',
195 $contactType = isset($contactTypes[$contactTypeId]) ?
$contactTypes[$contactTypeId] : '';
197 // get importable fields for contact type
198 $contactFields = CRM_Contact_BAO_Contact
::importableFields($contactType, NULL);
200 // get the Dedupe rule for this contact type and build soft credit array
202 'contact_type' => $contactType,
203 'used' => 'Unsupervised',
205 $fieldsArray = CRM_Dedupe_BAO_Rule
::dedupeRuleFields($ruleParams);
207 $softCreditFields['contact_id'] = ts('Contact ID');
208 $softCreditFields['external_identifier'] = ts('External ID');
209 $softCreditFields['email'] = ts('Email');
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');
215 // end of soft credit section
217 $js = "<script type='text/javascript'>\n";
218 $formName = 'document.forms.' . $this->_name
;
220 //used to warn for mismatch column count or mismatch mapping
223 for ($i = 0; $i < $this->_columnCount
; $i++
) {
224 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
226 if ($this->get('savedMapping')) {
227 if (isset($mappingName[$i])) {
228 if ($mappingName[$i] != ts('- do not import -')) {
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;
235 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
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 : "",
249 $defaults["mapper[$i]"] = array();
252 for ($k = 1; $k < 4; $k++
) {
253 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
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";
262 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns));
265 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
268 //end of load mapping
271 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
273 // do array search first to see if has mapped key
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;
281 // Infer the default from the column names if we have them
282 $defaults["mapper[$i]"] = array(
283 $this->defaultFromHeader($this->_columnHeaders
[$i],
291 // Otherwise guess the default from the form of the data
292 $defaults["mapper[$i]"] = array(
293 $this->defaultFromData($dataPatterns, $i),
294 // $defaultLocationType->id
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";
303 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
305 $js .= "</script>\n";
306 $this->assign('initHideBoxes', $js);
308 //set warning if mismatch in more than
309 if (isset($mappingName)) {
310 if (($this->_columnCount
!= count($mappingName))) {
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.'));
319 $session = CRM_Core_Session
::singleton();
320 $session->setStatus(NULL);
323 $this->setDefaults($defaults);
325 $this->addButtons(array(
328 'name' => ts('Previous'),
332 'name' => ts('Continue'),
333 'spacing' => ' ',
338 'name' => ts('Cancel'),
345 * Global validation rules for the form.
347 * @param array $fields
348 * Posted values of the form.
354 * list of errors to be posted back to the form
356 public static function formRule($fields, $files, $self) {
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];
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',
373 'used' => 'Unsupervised',
374 'contact_type' => isset($contactTypes[$contactTypeId]) ?
$contactTypes[$contactTypeId] : '',
376 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
378 foreach ($importKeys as $key => $val) {
379 if (array_key_exists($val, $ruleFields)) {
380 $weightSum +
= $ruleFields[$val];
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'] = '';
388 $errors['_qf_default'] .= ts('Missing required fields: Soft Credit') . '<br />';
392 foreach ($ruleFields as $field => $weight) {
393 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
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'),
403 foreach ($requiredFields as $field => $title) {
404 if (!in_array($field, $importKeys)) {
405 if (empty($errors['_qf_default'])) {
406 $errors['_qf_default'] = '';
408 if ($field == $contactORContributionId) {
409 if (!($weightSum >= $threshold ||
in_array('external_identifier', $importKeys)) &&
410 $self->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_UPDATE
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(
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)
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 />';
425 $errors['_qf_default'] .= ts('Missing required field: %1', array(
432 //at least one field should be mapped during update.
433 if ($self->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
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'))
444 $errors['_qf_default'] .= ts('At least one contribution field needs to be mapped for update during update mode.') . '<br />';
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');
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');
462 if (!empty($errors)) {
463 if (!empty($errors['saveMappingName'])) {
465 $assignError = new CRM_Core_Page();
466 $assignError->assign('mappingDetailsError', $_flag);
468 if (!empty($errors['_qf_default'])) {
469 CRM_Core_Session
::setStatus($errors['_qf_default'], ts("Error"), "error");
478 * Process the mapped fields and map it into the uploaded file
479 * preview the file and extract some summary statistics
483 public function postProcess() {
484 $params = $this->controller
->exportValues('MapField');
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
);
493 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
494 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
496 $config = CRM_Core_Config
::singleton();
497 $seperator = $config->fieldSeparator
;
499 $mapper = $mapperKeys = $mapperKeysMain = $mapperSoftCredit = $softCreditFields = $mapperPhoneType = $mapperSoftCreditType = array();
500 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
502 $softCreditTypes = CRM_Core_OptionGroup
::values('soft_credit_type');
504 for ($i = 0; $i < $this->_columnCount
; $i++
) {
505 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
506 $mapperKeysMain[$i] = $mapperKeys[$i][0];
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);
515 $softCreditFields[$i] = $mapperSoftCredit[$i];
517 $mapperSoftCreditType[$i] = array(
518 'value' => isset($mapperKeys[$i][2]) ?
$mapperKeys[$i][2] : '',
519 'label' => isset($softCreditTypes[$mapperKeys[$i][2]]) ?
$softCreditTypes[$mapperKeys[$i][2]] : '',
523 $mapperSoftCredit[$i] = $softCreditFields[$i] = $mapperSoftCreditType[$i] = NULL;
527 $this->set('mapper', $mapper);
528 $this->set('softCreditFields', $softCreditFields);
529 $this->set('mapperSoftCreditType', $mapperSoftCreditType);
531 // store mapping Id to display it in the preview page
532 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
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();
540 $mappingFieldsId = array();
541 while ($mappingFields->fetch()) {
542 if ($mappingFields->id
) {
543 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
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];
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();
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',
570 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
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];
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();
582 $this->set('savedMapping', $saveMappingFields->mapping_id
);
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')
590 // add all the necessary variables to the form