3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * This class gets the name of the file to upload
23 class CRM_Member_Import_Form_MapField
extends CRM_Import_Form_MapField
{
31 public static $_contactType = NULL;
34 * Set variables up before form is built.
38 public function preProcess() {
39 $this->_mapperFields
= $this->get('fields');
40 asort($this->_mapperFields
);
42 $this->_columnCount
= $this->get('columnCount');
43 $this->assign('columnCount', $this->_columnCount
);
44 $this->_dataValues
= $this->get('dataValues');
45 $this->assign('dataValues', $this->_dataValues
);
47 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
48 $this->_onDuplicate
= $this->get('onDuplicate', $onDuplicate ??
"");
50 $highlightedFields = [];
51 if ($skipColumnHeader) {
52 $this->assign('skipColumnHeader', $skipColumnHeader);
53 $this->assign('rowDisplayCount', 3);
54 /* if we had a column header to skip, stash it for later */
56 $this->_columnHeaders
= $this->_dataValues
[0];
59 $this->assign('rowDisplayCount', 2);
62 //CRM-2219 removing other required fields since for updation only
63 //membership id is required.
64 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
65 $remove = array('membership_contact_id', 'email', 'first_name', 'last_name', 'external_identifier');
66 foreach ($remove as $value) {
67 unset($this->_mapperFields
[$value]);
69 $highlightedFieldsArray = array('membership_id', 'membership_start_date', 'membership_type_id');
70 foreach ($highlightedFieldsArray as $name) {
71 $highlightedFields[] = $name;
74 elseif ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP
) {
75 unset($this->_mapperFields
['membership_id']);
76 $highlightedFieldsArray = array(
77 'membership_contact_id',
79 'external_identifier',
80 'membership_start_date',
83 foreach ($highlightedFieldsArray as $name) {
84 $highlightedFields[] = $name;
89 $this->_mapperFields
['status_id'] = ts('Membership Status');
90 $this->_mapperFields
['membership_type_id'] = ts('Membership Type');
92 self
::$_contactType = $this->get('contactType');
93 $this->assign('highlightedFields', $highlightedFields);
97 * Build the form object.
101 public function buildQuickForm() {
102 //to save the current mappings
103 if (!$this->get('savedMapping')) {
104 $saveDetailsName = ts('Save this field mapping');
105 $this->applyFilter('saveMappingName', 'trim');
106 $this->add('text', 'saveMappingName', ts('Name'));
107 $this->add('text', 'saveMappingDesc', ts('Description'));
110 $savedMapping = $this->get('savedMapping');
112 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
114 $mappingName = $mappingName[1];
115 $mappingContactType = $mappingContactType[1];
116 $mappingLocation = $mappingLocation['1'] ??
NULL;
117 $mappingPhoneType = $mappingPhoneType['1'] ??
NULL;
118 $mappingRelation = $mappingRelation['1'] ??
NULL;
120 //mapping is to be loaded from database
122 $params = array('id' => $savedMapping);
124 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
126 $this->assign('loadedMapping', $mappingDetails->name
);
127 $this->set('loadedMapping', $savedMapping);
129 $getMappingName = new CRM_Core_DAO_Mapping();
130 $getMappingName->id
= $savedMapping;
131 $getMappingName->mapping_type
= 'Import Memberships';
132 $getMappingName->find();
133 while ($getMappingName->fetch()) {
134 $mapperName = $getMappingName->name
;
137 $this->assign('savedName', $mapperName);
139 $this->add('hidden', 'mappingId', $savedMapping);
141 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
142 $saveDetailsName = ts('Save as a new field mapping');
143 $this->add('text', 'saveMappingName', ts('Name'));
144 $this->add('text', 'saveMappingDesc', ts('Description'));
147 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
149 $this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this);
151 //-------- end of saved mapping stuff ---------
154 $mapperKeys = array_keys($this->_mapperFields
);
155 $hasHeaders = !empty($this->_columnHeaders
);
156 $headerPatterns = $this->get('headerPatterns');
157 $dataPatterns = $this->get('dataPatterns');
158 $hasLocationTypes = $this->get('fieldTypes');
160 /* Initialize all field usages to false */
162 foreach ($mapperKeys as $key) {
163 $this->_fieldUsed
[$key] = FALSE;
165 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
166 $sel1 = $this->_mapperFields
;
167 if (!$this->get('onDuplicate')) {
169 unset($sel1['membership_id']);
174 $js = "<script type='text/javascript'>\n";
175 $formName = 'document.forms.' . $this->_name
;
177 //used to warn for mismatch column count or mismatch mapping
180 for ($i = 0; $i < $this->_columnCount
; $i++
) {
181 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
183 if ($this->get('savedMapping')) {
184 if (isset($mappingName[$i])) {
185 if ($mappingName[$i] != ts('- do not import -')) {
187 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
189 //When locationType is not set
190 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
192 //When phoneType is not set
193 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
195 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
197 $defaults["mapper[$i]"] = array($mappingHeader[0]);
201 $defaults["mapper[$i]"] = [];
204 for ($k = 1; $k < 4; $k++
) {
205 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
210 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
211 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
214 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns));
217 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
220 //end of load mapping
223 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
225 // Infer the default from the skipped headers if we have them
226 $defaults["mapper[$i]"] = array(
227 $this->defaultFromHeader($this->_columnHeaders
[$i],
230 // $defaultLocationType->id
235 // Otherwise guess the default from the form of the data
236 $defaults["mapper[$i]"] = array(
237 $this->defaultFromData($dataPatterns, $i),
238 // $defaultLocationType->id
243 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ?
$sel3 : "", (isset($sel4)) ?
$sel4 : ""));
245 $js .= "</script>\n";
246 $this->assign('initHideBoxes', $js);
248 //set warning if mismatch in more than
249 if (isset($mappingName)) {
250 if (($this->_columnCount
!= count($mappingName))) {
254 if ($warning != 0 && $this->get('savedMapping')) {
255 $session = CRM_Core_Session
::singleton();
256 $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.'));
259 $session = CRM_Core_Session
::singleton();
260 $session->setStatus(NULL);
263 $this->setDefaults($defaults);
265 $this->addButtons(array(
268 'name' => ts('Previous'),
272 'name' => ts('Continue'),
273 'spacing' => ' ',
278 'name' => ts('Cancel'),
284 * Global validation rules for the form.
286 * @param array $fields
287 * Posted values of the form.
293 * list of errors to be posted back to the form
295 public static function formRule($fields, $files, $self) {
298 if (!array_key_exists('savedMapping', $fields)) {
300 foreach ($fields['mapper'] as $mapperPart) {
301 $importKeys[] = $mapperPart[0];
303 // FIXME: should use the schema titles, not redeclare them
304 $requiredFields = array(
305 'membership_contact_id' => ts('Contact ID'),
306 'membership_type_id' => ts('Membership Type'),
307 'membership_start_date' => ts('Membership Start Date'),
310 $contactTypeId = $self->get('contactType');
311 $contactTypes = array(
312 CRM_Import_Parser
::CONTACT_INDIVIDUAL
=> 'Individual',
313 CRM_Import_Parser
::CONTACT_HOUSEHOLD
=> 'Household',
314 CRM_Import_Parser
::CONTACT_ORGANIZATION
=> 'Organization',
317 'used' => 'Unsupervised',
318 'contact_type' => $contactTypes[$contactTypeId],
320 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
322 foreach ($importKeys as $key => $val) {
323 if (array_key_exists($val, $ruleFields)) {
324 $weightSum +
= $ruleFields[$val];
328 foreach ($ruleFields as $field => $weight) {
329 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
332 foreach ($requiredFields as $field => $title) {
333 if (!in_array($field, $importKeys)) {
334 if ($field == 'membership_contact_id') {
335 if ((($weightSum >= $threshold ||
in_array('external_identifier', $importKeys)) &&
336 $self->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_UPDATE
338 in_array('membership_id', $importKeys)
343 if (!isset($errors['_qf_default'])) {
344 $errors['_qf_default'] = '';
346 $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(
348 )) . ' ' . ts('(OR Membership ID if update mode.)') . '<br />';
352 if (!isset($errors['_qf_default'])) {
353 $errors['_qf_default'] = '';
355 $errors['_qf_default'] .= ts('Missing required field: %1', array(
363 if (!empty($fields['saveMapping'])) {
364 $nameField = $fields['saveMappingName'] ??
NULL;
365 if (empty($nameField)) {
366 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
369 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'))) {
370 $errors['saveMappingName'] = ts('Duplicate Import Membership Mapping Name');
375 if (!empty($errors)) {
376 if (!empty($errors['saveMappingName'])) {
378 $assignError = new CRM_Core_Page();
379 $assignError->assign('mappingDetailsError', $_flag);
388 * Process the mapped fields and map it into the uploaded file
389 * preview the file and extract some summary statistics
393 public function postProcess() {
394 $params = $this->controller
->exportValues('MapField');
395 //reload the mapfield if load mapping is pressed
396 if (!empty($params['savedMapping'])) {
397 $this->set('savedMapping', $params['savedMapping']);
398 $this->controller
->resetPage($this->_name
);
402 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
403 $seperator = $this->controller
->exportValue('DataSource', 'fieldSeparator');
404 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
408 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
409 $mapperKeysMain = [];
411 $mapperPhoneType = [];
413 for ($i = 0; $i < $this->_columnCount
; $i++
) {
414 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
415 $mapperKeysMain[$i] = $mapperKeys[$i][0];
417 if (!empty($mapperKeys[$i][1]) && is_numeric($mapperKeys[$i][1])) {
418 $mapperLocType[$i] = $mapperKeys[$i][1];
421 $mapperLocType[$i] = NULL;
424 if (!empty($mapperKeys[$i][2]) && (!is_numeric($mapperKeys[$i][2]))) {
425 $mapperPhoneType[$i] = $mapperKeys[$i][2];
428 $mapperPhoneType[$i] = NULL;
432 $this->set('mapper', $mapper);
434 // store mapping Id to display it in the preview page
435 if (!empty($params['mappingId'])) {
436 $this->set('loadMappingId', $params['mappingId']);
438 //Updating Mapping Records
439 if (!empty($params['updateMapping'])) {
440 $mappingFields = new CRM_Core_DAO_MappingField();
441 $mappingFields->mapping_id
= $params['mappingId'];
442 $mappingFields->find();
444 $mappingFieldsId = [];
445 while ($mappingFields->fetch()) {
446 if ($mappingFields->id
) {
447 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
451 for ($i = 0; $i < $this->_columnCount
; $i++
) {
452 $updateMappingFields = new CRM_Core_DAO_MappingField();
453 $updateMappingFields->id
= $mappingFieldsId[$i];
454 $updateMappingFields->mapping_id
= $params['mappingId'];
455 $updateMappingFields->column_number
= $i;
457 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
458 $id = $mapperKeyParts[0] ??
NULL;
459 $first = $mapperKeyParts[1] ??
NULL;
460 $second = $mapperKeyParts[2] ??
NULL;
461 $updateMappingFields->name
= $mapper[$i];
462 $updateMappingFields->save();
466 //Saving Mapping Details and Records
467 if (!empty($params['saveMapping'])) {
468 $mappingParams = array(
469 'name' => $params['saveMappingName'],
470 'description' => $params['saveMappingDesc'],
471 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'),
473 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
475 for ($i = 0; $i < $this->_columnCount
; $i++
) {
477 $saveMappingFields = new CRM_Core_DAO_MappingField();
478 $saveMappingFields->mapping_id
= $saveMapping->id
;
479 $saveMappingFields->column_number
= $i;
481 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
482 $id = $mapperKeyParts[0] ??
NULL;
483 $first = $mapperKeyParts[1] ??
NULL;
484 $second = $mapperKeyParts[2] ??
NULL;
485 $saveMappingFields->name
= $mapper[$i];
486 $saveMappingFields->save();
488 $this->set('savedMapping', $saveMappingFields->mapping_id
);
491 $parser = new CRM_Member_Import_Parser_Membership($mapperKeysMain, $mapperLocType, $mapperPhoneType);
492 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
493 CRM_Import_Parser
::MODE_PREVIEW
, $this->get('contactType')
495 // add all the necessary variables to the form