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
19 * This class gets the name of the file to upload
21 class CRM_Member_Import_Form_MapField
extends CRM_Import_Form_MapField
{
24 * Set variables up before form is built.
28 public function preProcess() {
29 $this->_mapperFields
= $this->get('fields');
30 asort($this->_mapperFields
);
33 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
34 $this->_onDuplicate
= $this->get('onDuplicate', $onDuplicate ??
"");
36 $highlightedFields = [];
37 if ($this->getSubmittedValue('skipColumnHeader')) {
38 $this->assign('skipColumnHeader', $skipColumnHeader);
39 $this->assign('rowDisplayCount', 3);
42 $this->assign('rowDisplayCount', 2);
45 //CRM-2219 removing other required fields since for updation only
46 //membership id is required.
47 if ($this->getSubmittedValue('onDuplicate') == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
48 $remove = array('membership_contact_id', 'email', 'first_name', 'last_name', 'external_identifier');
49 foreach ($remove as $value) {
50 unset($this->_mapperFields
[$value]);
52 $highlightedFieldsArray = array('membership_id', 'membership_start_date', 'membership_type_id');
53 foreach ($highlightedFieldsArray as $name) {
54 $highlightedFields[] = $name;
57 elseif ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP
) {
58 unset($this->_mapperFields
['membership_id']);
59 $highlightedFieldsArray = array(
60 'membership_contact_id',
62 'external_identifier',
63 'membership_start_date',
66 foreach ($highlightedFieldsArray as $name) {
67 $highlightedFields[] = $name;
72 $this->_mapperFields
['status_id'] = ts('Membership Status');
73 $this->_mapperFields
['membership_type_id'] = ts('Membership Type');
74 $this->assign('highlightedFields', $highlightedFields);
78 * Build the form object.
82 public function buildQuickForm() {
83 //to save the current mappings
84 if (!$this->get('savedMapping')) {
85 $saveDetailsName = ts('Save this field mapping');
86 $this->applyFilter('saveMappingName', 'trim');
87 $this->add('text', 'saveMappingName', ts('Name'));
88 $this->add('text', 'saveMappingDesc', ts('Description'));
91 $savedMapping = $this->get('savedMapping');
93 [$mappingName] = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
95 $mappingName = $mappingName[1];
97 //mapping is to be loaded from database
99 $this->set('loadedMapping', $savedMapping);
101 $getMappingName = new CRM_Core_DAO_Mapping();
102 $getMappingName->id
= $savedMapping;
103 $getMappingName->mapping_type
= 'Import Memberships';
104 $getMappingName->find();
105 while ($getMappingName->fetch()) {
106 $mapperName = $getMappingName->name
;
109 $this->assign('savedMappingName', $mapperName);
111 $this->add('hidden', 'mappingId', $savedMapping);
113 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
114 $saveDetailsName = ts('Save as a new field mapping');
115 $this->add('text', 'saveMappingName', ts('Name'));
116 $this->add('text', 'saveMappingDesc', ts('Description'));
119 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
121 $this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this);
123 //-------- end of saved mapping stuff ---------
126 $mapperKeys = array_keys($this->_mapperFields
);
127 $hasHeaders = !empty($this->_columnHeaders
);
128 $headerPatterns = $this->getHeaderPatterns();
129 $dataPatterns = $this->getDataPatterns();
131 /* Initialize all field usages to false */
133 foreach ($mapperKeys as $key) {
134 $this->_fieldUsed
[$key] = FALSE;
136 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
137 $sel1 = $this->_mapperFields
;
138 if (!$this->get('onDuplicate')) {
140 unset($sel1['membership_id']);
145 $js = "<script type='text/javascript'>\n";
146 $formName = 'document.forms.' . $this->_name
;
148 //used to warn for mismatch column count or mismatch mapping
151 for ($i = 0; $i < $this->_columnCount
; $i++
) {
152 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
154 if ($this->get('savedMapping')) {
155 if (isset($mappingName[$i])) {
156 if ($mappingName[$i] != ts('do_not_import')) {
157 //When locationType is not set
158 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
160 //When phoneType is not set
161 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
163 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
165 $defaults["mapper[$i]"] = [$mappingName[$i]];
169 $defaults["mapper[$i]"] = [];
172 for ($k = 1; $k < 4; $k++
) {
173 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
178 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
179 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
182 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns));
185 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
188 //end of load mapping
191 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
192 if ($this->getSubmittedValue('skipColumnHeader')) {
193 // Infer the default from the skipped headers if we have them
194 $defaults["mapper[$i]"] = array(
195 $this->defaultFromHeader($this->_columnHeaders
[$i],
198 // $defaultLocationType->id
203 // Otherwise guess the default from the form of the data
204 $defaults["mapper[$i]"] = array(
205 $this->defaultFromData($dataPatterns, $i),
206 // $defaultLocationType->id
211 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ?
$sel3 : "", (isset($sel4)) ?
$sel4 : ""));
213 $js .= "</script>\n";
214 $this->assign('initHideBoxes', $js);
216 //set warning if mismatch in more than
217 if (isset($mappingName)) {
218 if (($this->_columnCount
!= count($mappingName))) {
222 if ($warning != 0 && $this->get('savedMapping')) {
223 $session = CRM_Core_Session
::singleton();
224 $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.'));
227 $session = CRM_Core_Session
::singleton();
228 $session->setStatus(NULL);
231 $this->setDefaults($defaults);
233 $this->addButtons(array(
236 'name' => ts('Previous'),
240 'name' => ts('Continue'),
241 'spacing' => ' ',
246 'name' => ts('Cancel'),
252 * Global validation rules for the form.
254 * @param array $fields
255 * Posted values of the form.
261 * list of errors to be posted back to the form
263 public static function formRule($fields, $files, $self) {
267 foreach ($fields['mapper'] as $mapperPart) {
268 $importKeys[] = $mapperPart[0];
270 // FIXME: should use the schema titles, not redeclare them
271 $requiredFields = array(
272 'membership_contact_id' => ts('Contact ID'),
273 'membership_type_id' => ts('Membership Type'),
274 'membership_start_date' => ts('Membership Start Date'),
277 'used' => 'Unsupervised',
278 'contact_type' => $self->getContactType(),
280 list($ruleFields, $threshold) = CRM_Dedupe_BAO_DedupeRuleGroup
::dedupeRuleFieldsWeight($params);
282 foreach ($importKeys as $key => $val) {
283 if (array_key_exists($val, $ruleFields)) {
284 $weightSum +
= $ruleFields[$val];
288 foreach ($ruleFields as $field => $weight) {
289 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
292 foreach ($requiredFields as $field => $title) {
293 if (!in_array($field, $importKeys)) {
294 if ($field == 'membership_contact_id') {
295 if ((($weightSum >= $threshold ||
in_array('external_identifier', $importKeys)) &&
296 $self->_onDuplicate
!= CRM_Import_Parser
::DUPLICATE_UPDATE
298 in_array('membership_id', $importKeys)
303 if (!isset($errors['_qf_default'])) {
304 $errors['_qf_default'] = '';
306 $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(
308 )) . ' ' . ts('(OR Membership ID if update mode.)') . '<br />';
312 if (!isset($errors['_qf_default'])) {
313 $errors['_qf_default'] = '';
315 $errors['_qf_default'] .= ts('Missing required field: %1', array(
322 if (!empty($fields['saveMapping'])) {
323 $nameField = $fields['saveMappingName'] ??
NULL;
324 if (empty($nameField)) {
325 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
328 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'))) {
329 $errors['saveMappingName'] = ts('Duplicate Import Membership Mapping Name');
334 if (!empty($errors)) {
335 if (!empty($errors['saveMappingName'])) {
337 $assignError = new CRM_Core_Page();
338 $assignError->assign('mappingDetailsError', $_flag);
347 * Process the mapped fields and map it into the uploaded file
348 * preview the file and extract some summary statistics
352 public function postProcess() {
353 $params = $this->controller
->exportValues('MapField');
354 $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues());
356 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
357 $mapperKeysMain = [];
359 for ($i = 0; $i < $this->_columnCount
; $i++
) {
360 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
361 $mapperKeysMain[$i] = $mapperKeys[$i][0];
364 $this->set('mapper', $mapper);
366 // store mapping Id to display it in the preview page
367 if (!empty($params['mappingId'])) {
368 $this->set('loadMappingId', $params['mappingId']);
370 //Updating Mapping Records
371 if (!empty($params['updateMapping'])) {
372 for ($i = 0; $i < $this->_columnCount
; $i++
) {
373 $this->saveMappingField($params['mappingId'], $i, TRUE);
377 //Saving Mapping Details and Records
378 if (!empty($params['saveMapping'])) {
379 $mappingParams = array(
380 'name' => $params['saveMappingName'],
381 'description' => $params['saveMappingDesc'],
382 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'),
384 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
386 for ($i = 0; $i < $this->_columnCount
; $i++
) {
387 $this->saveMappingField($saveMapping->id
, $i);
389 $this->set('savedMapping', $saveMapping->id
);
392 $parser = new CRM_Member_Import_Parser_Membership($mapperKeysMain);
393 $parser->setUserJobID($this->getUserJobID());
394 $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'), $mapper, $this->getSubmittedValue('skipColumnHeader'),
395 CRM_Import_Parser
::MODE_PREVIEW
397 // add all the necessary variables to the form
402 * @return \CRM_Member_Import_Parser_Membership
404 protected function getParser(): CRM_Member_Import_Parser_Membership
{
405 if (!$this->parser
) {
406 $this->parser
= new CRM_Member_Import_Parser_Membership();
407 $this->parser
->setUserJobID($this->getUserJobID());
408 $this->parser
->init();
410 return $this->parser
;