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
37 * This class gets the name of the file to upload
39 class CRM_Member_Import_Form_MapField
extends CRM_Import_Form_MapField
{
47 static $_contactType = NULL;
51 * Set variables up before form is built.
55 public function preProcess() {
56 $this->_mapperFields
= $this->get('fields');
57 asort($this->_mapperFields
);
59 $this->_columnCount
= $this->get('columnCount');
60 $this->assign('columnCount', $this->_columnCount
);
61 $this->_dataValues
= $this->get('dataValues');
62 $this->assign('dataValues', $this->_dataValues
);
64 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
65 $this->_onDuplicate
= $this->get('onDuplicate', isset($onDuplicate) ?
$onDuplicate : "");
67 $highlightedFields = array();
68 if ($skipColumnHeader) {
69 $this->assign('skipColumnHeader', $skipColumnHeader);
70 $this->assign('rowDisplayCount', 3);
71 /* if we had a column header to skip, stash it for later */
73 $this->_columnHeaders
= $this->_dataValues
[0];
76 $this->assign('rowDisplayCount', 2);
79 //CRM-2219 removing other required fields since for updation only
80 //membership id is required.
81 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
82 $remove = array('membership_contact_id', 'email', 'first_name', 'last_name', 'external_identifier');
83 foreach ($remove as $value) {
84 unset($this->_mapperFields
[$value]);
86 $highlightedFieldsArray = array('membership_id', 'membership_start_date', 'membership_type_id');
87 foreach ($highlightedFieldsArray as $name) {
88 $highlightedFields[] = $name;
91 elseif ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP
) {
92 unset($this->_mapperFields
['membership_id']);
93 $highlightedFieldsArray = array(
94 'membership_contact_id',
96 'external_identifier',
97 'membership_start_date',
100 foreach ($highlightedFieldsArray as $name) {
101 $highlightedFields[] = $name;
105 // modify field title
106 $this->_mapperFields
['status_id'] = ts('Membership Status');
107 $this->_mapperFields
['membership_type_id'] = ts('Membership Type');
109 self
::$_contactType = $this->get('contactType');
110 $this->assign('highlightedFields', $highlightedFields);
114 * Build the form object.
118 public function buildQuickForm() {
119 //to save the current mappings
120 if (!$this->get('savedMapping')) {
121 $saveDetailsName = ts('Save this field mapping');
122 $this->applyFilter('saveMappingName', 'trim');
123 $this->add('text', 'saveMappingName', ts('Name'));
124 $this->add('text', 'saveMappingDesc', ts('Description'));
127 $savedMapping = $this->get('savedMapping');
129 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
131 $mappingName = $mappingName[1];
132 $mappingContactType = $mappingContactType[1];
133 $mappingLocation = CRM_Utils_Array
::value('1', $mappingLocation);
134 $mappingPhoneType = CRM_Utils_Array
::value('1', $mappingPhoneType);
135 $mappingRelation = CRM_Utils_Array
::value('1', $mappingRelation);
137 //mapping is to be loaded from database
139 $params = array('id' => $savedMapping);
141 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
143 $this->assign('loadedMapping', $mappingDetails->name
);
144 $this->set('loadedMapping', $savedMapping);
146 $getMappingName = new CRM_Core_DAO_Mapping();
147 $getMappingName->id
= $savedMapping;
148 $getMappingName->mapping_type
= 'Import Memberships';
149 $getMappingName->find();
150 while ($getMappingName->fetch()) {
151 $mapperName = $getMappingName->name
;
154 $this->assign('savedName', $mapperName);
156 $this->add('hidden', 'mappingId', $savedMapping);
158 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
159 $saveDetailsName = ts('Save as a new field mapping');
160 $this->add('text', 'saveMappingName', ts('Name'));
161 $this->add('text', 'saveMappingDesc', ts('Description'));
164 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
166 $this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this);
168 //-------- end of saved mapping stuff ---------
171 $mapperKeys = array_keys($this->_mapperFields
);
172 $hasHeaders = !empty($this->_columnHeaders
);
173 $headerPatterns = $this->get('headerPatterns');
174 $dataPatterns = $this->get('dataPatterns');
175 $hasLocationTypes = $this->get('fieldTypes');
177 /* Initialize all field usages to false */
179 foreach ($mapperKeys as $key) {
180 $this->_fieldUsed
[$key] = FALSE;
182 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
183 $sel1 = $this->_mapperFields
;
184 if (!$this->get('onDuplicate')) {
186 unset($sel1['membership_id']);
191 $js = "<script type='text/javascript'>\n";
192 $formName = 'document.forms.' . $this->_name
;
194 //used to warn for mismatch column count or mismatch mapping
197 for ($i = 0; $i < $this->_columnCount
; $i++
) {
198 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
200 if ($this->get('savedMapping')) {
201 if (isset($mappingName[$i])) {
202 if ($mappingName[$i] != ts('- do not import -')) {
204 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
206 //When locationType is not set
207 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
209 //When phoneType is not set
210 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
212 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
214 $defaults["mapper[$i]"] = array($mappingHeader[0]);
218 $defaults["mapper[$i]"] = array();
221 for ($k = 1; $k < 4; $k++
) {
222 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
227 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
228 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
231 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns));
234 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
237 //end of load mapping
240 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
242 // Infer the default from the skipped headers if we have them
243 $defaults["mapper[$i]"] = array(
244 $this->defaultFromHeader($this->_columnHeaders
[$i],
247 // $defaultLocationType->id
252 // Otherwise guess the default from the form of the data
253 $defaults["mapper[$i]"] = array(
254 $this->defaultFromData($dataPatterns, $i),
255 // $defaultLocationType->id
260 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ?
$sel3 : "", (isset($sel4)) ?
$sel4 : ""));
262 $js .= "</script>\n";
263 $this->assign('initHideBoxes', $js);
265 //set warning if mismatch in more than
266 if (isset($mappingName)) {
267 if (($this->_columnCount
!= count($mappingName))) {
271 if ($warning != 0 && $this->get('savedMapping')) {
272 $session = CRM_Core_Session
::singleton();
273 $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.'));
276 $session = CRM_Core_Session
::singleton();
277 $session->setStatus(NULL);
280 $this->setDefaults($defaults);
282 $this->addButtons(array(
285 'name' => ts('Previous'),
289 'name' => ts('Continue'),
290 'spacing' => ' ',
295 'name' => ts('Cancel'),
301 * Global validation rules for the form.
303 * @param array $fields
304 * Posted values of the form.
310 * list of errors to be posted back to the form
312 public static function formRule($fields, $files, $self) {
315 if (!array_key_exists('savedMapping', $fields)) {
316 $importKeys = array();
317 foreach ($fields['mapper'] as $mapperPart) {
318 $importKeys[] = $mapperPart[0];
320 // FIXME: should use the schema titles, not redeclare them
321 $requiredFields = array(
322 'membership_contact_id' => ts('Contact ID'),
323 'membership_type_id' => ts('Membership Type'),
324 'membership_start_date' => ts('Membership Start Date'),
327 $contactTypeId = $self->get('contactType');
328 $contactTypes = array(
329 CRM_Import_Parser
::CONTACT_INDIVIDUAL
=> 'Individual',
330 CRM_Import_Parser
::CONTACT_HOUSEHOLD
=> 'Household',
331 CRM_Import_Parser
::CONTACT_ORGANIZATION
=> 'Organization',
334 'used' => 'Unsupervised',
335 'contact_type' => $contactTypes[$contactTypeId],
337 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
339 foreach ($importKeys as $key => $val) {
340 if (array_key_exists($val, $ruleFields)) {
341 $weightSum +
= $ruleFields[$val];
345 foreach ($ruleFields as $field => $weight) {
346 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
349 foreach ($requiredFields as $field => $title) {
350 if (!in_array($field, $importKeys)) {
351 if ($field == 'membership_contact_id') {
352 if ((($weightSum >= $threshold ||
in_array('external_identifier', $importKeys)) &&
353 $self->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_UPDATE
355 in_array('membership_id', $importKeys)
360 if (!isset($errors['_qf_default'])) {
361 $errors['_qf_default'] = '';
363 $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(
365 )) . ' ' . ts('(OR Membership ID if update mode.)') . '<br />';
369 if (!isset($errors['_qf_default'])) {
370 $errors['_qf_default'] = '';
372 $errors['_qf_default'] .= ts('Missing required field: %1', array(
380 if (!empty($fields['saveMapping'])) {
381 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
382 if (empty($nameField)) {
383 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
386 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'))) {
387 $errors['saveMappingName'] = ts('Duplicate Import Membership Mapping Name');
392 if (!empty($errors)) {
393 if (!empty($errors['saveMappingName'])) {
395 $assignError = new CRM_Core_Page();
396 $assignError->assign('mappingDetailsError', $_flag);
405 * Process the mapped fields and map it into the uploaded file
406 * preview the file and extract some summary statistics
410 public function postProcess() {
411 $params = $this->controller
->exportValues('MapField');
412 //reload the mapfield if load mapping is pressed
413 if (!empty($params['savedMapping'])) {
414 $this->set('savedMapping', $params['savedMapping']);
415 $this->controller
->resetPage($this->_name
);
419 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
420 $seperator = $this->controller
->exportValue('DataSource', 'fieldSeparator');
421 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
423 $mapperKeys = array();
425 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
426 $mapperKeysMain = array();
427 $mapperLocType = array();
428 $mapperPhoneType = array();
430 for ($i = 0; $i < $this->_columnCount
; $i++
) {
431 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
432 $mapperKeysMain[$i] = $mapperKeys[$i][0];
434 if (!empty($mapperKeys[$i][1]) && is_numeric($mapperKeys[$i][1])) {
435 $mapperLocType[$i] = $mapperKeys[$i][1];
438 $mapperLocType[$i] = NULL;
441 if (!empty($mapperKeys[$i][2]) && (!is_numeric($mapperKeys[$i][2]))) {
442 $mapperPhoneType[$i] = $mapperKeys[$i][2];
445 $mapperPhoneType[$i] = NULL;
449 $this->set('mapper', $mapper);
451 // store mapping Id to display it in the preview page
452 if (!empty($params['mappingId'])) {
453 $this->set('loadMappingId', $params['mappingId']);
455 //Updating Mapping Records
456 if (!empty($params['updateMapping'])) {
457 $mappingFields = new CRM_Core_DAO_MappingField();
458 $mappingFields->mapping_id
= $params['mappingId'];
459 $mappingFields->find();
461 $mappingFieldsId = array();
462 while ($mappingFields->fetch()) {
463 if ($mappingFields->id
) {
464 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
468 for ($i = 0; $i < $this->_columnCount
; $i++
) {
469 $updateMappingFields = new CRM_Core_DAO_MappingField();
470 $updateMappingFields->id
= $mappingFieldsId[$i];
471 $updateMappingFields->mapping_id
= $params['mappingId'];
472 $updateMappingFields->column_number
= $i;
474 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
475 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
476 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
477 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
478 $updateMappingFields->name
= $mapper[$i];
479 $updateMappingFields->save();
483 //Saving Mapping Details and Records
484 if (!empty($params['saveMapping'])) {
485 $mappingParams = array(
486 'name' => $params['saveMappingName'],
487 'description' => $params['saveMappingDesc'],
488 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'),
490 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
492 for ($i = 0; $i < $this->_columnCount
; $i++
) {
494 $saveMappingFields = new CRM_Core_DAO_MappingField();
495 $saveMappingFields->mapping_id
= $saveMapping->id
;
496 $saveMappingFields->column_number
= $i;
498 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
499 $id = isset($mapperKeyParts[0]) ?
$mapperKeyParts[0] : NULL;
500 $first = isset($mapperKeyParts[1]) ?
$mapperKeyParts[1] : NULL;
501 $second = isset($mapperKeyParts[2]) ?
$mapperKeyParts[2] : NULL;
502 $saveMappingFields->name
= $mapper[$i];
503 $saveMappingFields->save();
505 $this->set('savedMapping', $saveMappingFields->mapping_id
);
508 $parser = new CRM_Member_Import_Parser_Membership($mapperKeysMain, $mapperLocType, $mapperPhoneType);
509 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
510 CRM_Import_Parser
::MODE_PREVIEW
, $this->get('contactType')
512 // add all the necessary variables to the form