Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class gets the name of the file to upload | |
20 | */ | |
b26295b8 | 21 | class CRM_Member_Import_Form_MapField extends CRM_Import_Form_MapField { |
6a488035 | 22 | |
6a488035 | 23 | |
6a488035 | 24 | /** |
fe482240 | 25 | * store contactType. |
6a488035 TO |
26 | * |
27 | * @var int | |
6a488035 | 28 | */ |
971e129b | 29 | public static $_contactType = NULL; |
6a488035 TO |
30 | |
31 | /** | |
fe482240 | 32 | * Set variables up before form is built. |
6a488035 TO |
33 | * |
34 | * @return void | |
6a488035 TO |
35 | */ |
36 | public function preProcess() { | |
37 | $this->_mapperFields = $this->get('fields'); | |
38 | asort($this->_mapperFields); | |
39 | ||
40 | $this->_columnCount = $this->get('columnCount'); | |
41 | $this->assign('columnCount', $this->_columnCount); | |
42 | $this->_dataValues = $this->get('dataValues'); | |
43 | $this->assign('dataValues', $this->_dataValues); | |
44 | ||
de7b9b56 | 45 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
2e1f50d6 | 46 | $this->_onDuplicate = $this->get('onDuplicate', $onDuplicate ?? ""); |
6a488035 | 47 | |
affcc9d2 | 48 | $highlightedFields = []; |
6a488035 TO |
49 | if ($skipColumnHeader) { |
50 | $this->assign('skipColumnHeader', $skipColumnHeader); | |
51 | $this->assign('rowDisplayCount', 3); | |
52 | /* if we had a column header to skip, stash it for later */ | |
53 | ||
54 | $this->_columnHeaders = $this->_dataValues[0]; | |
55 | } | |
56 | else { | |
57 | $this->assign('rowDisplayCount', 2); | |
58 | } | |
59 | ||
60 | //CRM-2219 removing other required fields since for updation only | |
61 | //membership id is required. | |
a05662ef | 62 | if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { |
6a488035 TO |
63 | $remove = array('membership_contact_id', 'email', 'first_name', 'last_name', 'external_identifier'); |
64 | foreach ($remove as $value) { | |
65 | unset($this->_mapperFields[$value]); | |
66 | } | |
67 | $highlightedFieldsArray = array('membership_id', 'membership_start_date', 'membership_type_id'); | |
68 | foreach ($highlightedFieldsArray as $name) { | |
69 | $highlightedFields[] = $name; | |
70 | } | |
71 | } | |
a05662ef | 72 | elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { |
6a488035 | 73 | unset($this->_mapperFields['membership_id']); |
353ffa53 TO |
74 | $highlightedFieldsArray = array( |
75 | 'membership_contact_id', | |
76 | 'email', | |
77 | 'external_identifier', | |
78 | 'membership_start_date', | |
317fceb4 | 79 | 'membership_type_id', |
353ffa53 | 80 | ); |
6a488035 TO |
81 | foreach ($highlightedFieldsArray as $name) { |
82 | $highlightedFields[] = $name; | |
83 | } | |
84 | } | |
85 | ||
86 | // modify field title | |
87 | $this->_mapperFields['status_id'] = ts('Membership Status'); | |
88 | $this->_mapperFields['membership_type_id'] = ts('Membership Type'); | |
89 | ||
90 | self::$_contactType = $this->get('contactType'); | |
91 | $this->assign('highlightedFields', $highlightedFields); | |
92 | } | |
93 | ||
94 | /** | |
fe482240 | 95 | * Build the form object. |
6a488035 TO |
96 | * |
97 | * @return void | |
6a488035 TO |
98 | */ |
99 | public function buildQuickForm() { | |
100 | //to save the current mappings | |
101 | if (!$this->get('savedMapping')) { | |
102 | $saveDetailsName = ts('Save this field mapping'); | |
103 | $this->applyFilter('saveMappingName', 'trim'); | |
104 | $this->add('text', 'saveMappingName', ts('Name')); | |
105 | $this->add('text', 'saveMappingDesc', ts('Description')); | |
106 | } | |
107 | else { | |
108 | $savedMapping = $this->get('savedMapping'); | |
109 | ||
07c7b62c | 110 | list($mappingName) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping); |
6a488035 | 111 | |
353ffa53 | 112 | $mappingName = $mappingName[1]; |
6a488035 TO |
113 | |
114 | //mapping is to be loaded from database | |
115 | ||
6a488035 TO |
116 | $this->set('loadedMapping', $savedMapping); |
117 | ||
118 | $getMappingName = new CRM_Core_DAO_Mapping(); | |
119 | $getMappingName->id = $savedMapping; | |
120 | $getMappingName->mapping_type = 'Import Memberships'; | |
121 | $getMappingName->find(); | |
122 | while ($getMappingName->fetch()) { | |
123 | $mapperName = $getMappingName->name; | |
124 | } | |
125 | ||
262b7f26 | 126 | $this->assign('savedMappingName', $mapperName); |
6a488035 TO |
127 | |
128 | $this->add('hidden', 'mappingId', $savedMapping); | |
129 | ||
130 | $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); | |
131 | $saveDetailsName = ts('Save as a new field mapping'); | |
132 | $this->add('text', 'saveMappingName', ts('Name')); | |
133 | $this->add('text', 'saveMappingDesc', ts('Description')); | |
134 | } | |
135 | ||
136 | $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)")); | |
137 | ||
138 | $this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this); | |
139 | ||
140 | //-------- end of saved mapping stuff --------- | |
141 | ||
affcc9d2 | 142 | $defaults = []; |
353ffa53 TO |
143 | $mapperKeys = array_keys($this->_mapperFields); |
144 | $hasHeaders = !empty($this->_columnHeaders); | |
145 | $headerPatterns = $this->get('headerPatterns'); | |
146 | $dataPatterns = $this->get('dataPatterns'); | |
6a488035 | 147 | |
6a488035 TO |
148 | /* Initialize all field usages to false */ |
149 | ||
150 | foreach ($mapperKeys as $key) { | |
151 | $this->_fieldUsed[$key] = FALSE; | |
152 | } | |
b2b0530a | 153 | $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
154 | $sel1 = $this->_mapperFields; |
155 | if (!$this->get('onDuplicate')) { | |
156 | unset($sel1['id']); | |
157 | unset($sel1['membership_id']); | |
158 | } | |
159 | ||
160 | $sel2[''] = NULL; | |
161 | ||
162 | $js = "<script type='text/javascript'>\n"; | |
163 | $formName = 'document.forms.' . $this->_name; | |
164 | ||
165 | //used to warn for mismatch column count or mismatch mapping | |
166 | $warning = 0; | |
167 | ||
168 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
169 | $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL); | |
170 | $jsSet = FALSE; | |
171 | if ($this->get('savedMapping')) { | |
172 | if (isset($mappingName[$i])) { | |
173 | if ($mappingName[$i] != ts('- do not import -')) { | |
174 | ||
175 | $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]); | |
176 | ||
177 | //When locationType is not set | |
178 | $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n"; | |
179 | ||
180 | //When phoneType is not set | |
181 | $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n"; | |
182 | ||
183 | $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n"; | |
184 | ||
185 | $defaults["mapper[$i]"] = array($mappingHeader[0]); | |
186 | $jsSet = TRUE; | |
187 | } | |
188 | else { | |
affcc9d2 | 189 | $defaults["mapper[$i]"] = []; |
6a488035 TO |
190 | } |
191 | if (!$jsSet) { | |
192 | for ($k = 1; $k < 4; $k++) { | |
193 | $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n"; | |
194 | } | |
195 | } | |
196 | } | |
197 | else { | |
198 | // this load section to help mapping if we ran out of saved columns when doing Load Mapping | |
199 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n"; | |
200 | ||
201 | if ($hasHeaders) { | |
202 | $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns)); | |
203 | } | |
204 | else { | |
205 | $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i)); | |
206 | } | |
207 | } | |
208 | //end of load mapping | |
209 | } | |
210 | else { | |
211 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n"; | |
212 | if ($hasHeaders) { | |
213 | // Infer the default from the skipped headers if we have them | |
214 | $defaults["mapper[$i]"] = array( | |
215 | $this->defaultFromHeader($this->_columnHeaders[$i], | |
216 | $headerPatterns | |
217 | ), | |
218 | // $defaultLocationType->id | |
219 | 0, | |
220 | ); | |
221 | } | |
222 | else { | |
223 | // Otherwise guess the default from the form of the data | |
224 | $defaults["mapper[$i]"] = array( | |
225 | $this->defaultFromData($dataPatterns, $i), | |
226 | // $defaultLocationType->id | |
227 | 0, | |
228 | ); | |
229 | } | |
230 | } | |
231 | $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ? $sel3 : "", (isset($sel4)) ? $sel4 : "")); | |
232 | } | |
233 | $js .= "</script>\n"; | |
234 | $this->assign('initHideBoxes', $js); | |
235 | ||
236 | //set warning if mismatch in more than | |
237 | if (isset($mappingName)) { | |
238 | if (($this->_columnCount != count($mappingName))) { | |
239 | $warning++; | |
240 | } | |
241 | } | |
242 | if ($warning != 0 && $this->get('savedMapping')) { | |
243 | $session = CRM_Core_Session::singleton(); | |
244 | $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.')); | |
245 | } | |
246 | else { | |
247 | $session = CRM_Core_Session::singleton(); | |
248 | $session->setStatus(NULL); | |
249 | } | |
250 | ||
251 | $this->setDefaults($defaults); | |
252 | ||
253 | $this->addButtons(array( | |
c5c263ca AH |
254 | array( |
255 | 'type' => 'back', | |
256 | 'name' => ts('Previous'), | |
257 | ), | |
258 | array( | |
259 | 'type' => 'next', | |
260 | 'name' => ts('Continue'), | |
261 | 'spacing' => ' ', | |
262 | 'isDefault' => TRUE, | |
263 | ), | |
264 | array( | |
265 | 'type' => 'cancel', | |
266 | 'name' => ts('Cancel'), | |
267 | ), | |
268 | )); | |
6a488035 TO |
269 | } |
270 | ||
271 | /** | |
fe482240 | 272 | * Global validation rules for the form. |
6a488035 | 273 | * |
b2363ea8 TO |
274 | * @param array $fields |
275 | * Posted values of the form. | |
6a488035 | 276 | * |
fd31fa4c | 277 | * @param $files |
e8cf95b4 | 278 | * @param self $self |
fd31fa4c | 279 | * |
a6c01b45 CW |
280 | * @return array |
281 | * list of errors to be posted back to the form | |
6a488035 | 282 | */ |
00be9182 | 283 | public static function formRule($fields, $files, $self) { |
affcc9d2 | 284 | $errors = []; |
6a488035 TO |
285 | |
286 | if (!array_key_exists('savedMapping', $fields)) { | |
affcc9d2 | 287 | $importKeys = []; |
6a488035 TO |
288 | foreach ($fields['mapper'] as $mapperPart) { |
289 | $importKeys[] = $mapperPart[0]; | |
290 | } | |
291 | // FIXME: should use the schema titles, not redeclare them | |
292 | $requiredFields = array( | |
293 | 'membership_contact_id' => ts('Contact ID'), | |
294 | 'membership_type_id' => ts('Membership Type'), | |
295 | 'membership_start_date' => ts('Membership Start Date'), | |
296 | ); | |
297 | ||
298 | $contactTypeId = $self->get('contactType'); | |
299 | $contactTypes = array( | |
a05662ef CW |
300 | CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', |
301 | CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', | |
302 | CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization', | |
6a488035 TO |
303 | ); |
304 | $params = array( | |
353ffa53 | 305 | 'used' => 'Unsupervised', |
6a488035 TO |
306 | 'contact_type' => $contactTypes[$contactTypeId], |
307 | ); | |
61194d45 | 308 | list($ruleFields, $threshold) = CRM_Dedupe_BAO_DedupeRuleGroup::dedupeRuleFieldsWeight($params); |
6a488035 TO |
309 | $weightSum = 0; |
310 | foreach ($importKeys as $key => $val) { | |
311 | if (array_key_exists($val, $ruleFields)) { | |
312 | $weightSum += $ruleFields[$val]; | |
313 | } | |
314 | } | |
315 | $fieldMessage = ''; | |
316 | foreach ($ruleFields as $field => $weight) { | |
317 | $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; | |
318 | } | |
319 | ||
320 | foreach ($requiredFields as $field => $title) { | |
321 | if (!in_array($field, $importKeys)) { | |
322 | if ($field == 'membership_contact_id') { | |
323 | if ((($weightSum >= $threshold || in_array('external_identifier', $importKeys)) && | |
a05662ef | 324 | $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE |
6a488035 TO |
325 | ) || |
326 | in_array('membership_id', $importKeys) | |
327 | ) { | |
328 | continue; | |
329 | } | |
330 | else { | |
331 | if (!isset($errors['_qf_default'])) { | |
332 | $errors['_qf_default'] = ''; | |
333 | } | |
334 | $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( | |
317fceb4 | 335 | 1 => $threshold, |
c5c263ca | 336 | )) . ' ' . ts('(OR Membership ID if update mode.)') . '<br />'; |
6a488035 TO |
337 | } |
338 | } | |
339 | else { | |
340 | if (!isset($errors['_qf_default'])) { | |
341 | $errors['_qf_default'] = ''; | |
342 | } | |
343 | $errors['_qf_default'] .= ts('Missing required field: %1', array( | |
317fceb4 | 344 | 1 => $title, |
c5c263ca | 345 | )) . '<br />'; |
6a488035 TO |
346 | } |
347 | } | |
348 | } | |
349 | } | |
350 | ||
a7488080 | 351 | if (!empty($fields['saveMapping'])) { |
9c1bc317 | 352 | $nameField = $fields['saveMappingName'] ?? NULL; |
6a488035 TO |
353 | if (empty($nameField)) { |
354 | $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); | |
355 | } | |
356 | else { | |
95f52e3b | 357 | if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'))) { |
6a488035 TO |
358 | $errors['saveMappingName'] = ts('Duplicate Import Membership Mapping Name'); |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | if (!empty($errors)) { | |
364 | if (!empty($errors['saveMappingName'])) { | |
365 | $_flag = 1; | |
366 | $assignError = new CRM_Core_Page(); | |
367 | $assignError->assign('mappingDetailsError', $_flag); | |
368 | } | |
369 | return $errors; | |
370 | } | |
371 | ||
372 | return TRUE; | |
373 | } | |
374 | ||
375 | /** | |
376 | * Process the mapped fields and map it into the uploaded file | |
377 | * preview the file and extract some summary statistics | |
378 | * | |
379 | * @return void | |
6a488035 TO |
380 | */ |
381 | public function postProcess() { | |
382 | $params = $this->controller->exportValues('MapField'); | |
383 | //reload the mapfield if load mapping is pressed | |
384 | if (!empty($params['savedMapping'])) { | |
385 | $this->set('savedMapping', $params['savedMapping']); | |
386 | $this->controller->resetPage($this->_name); | |
387 | return; | |
388 | } | |
9b78b60c | 389 | $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues()); |
6a488035 | 390 | |
affcc9d2 | 391 | $mapper = []; |
353ffa53 | 392 | $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); |
affcc9d2 | 393 | $mapperKeysMain = []; |
6a488035 TO |
394 | |
395 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
396 | $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]]; | |
397 | $mapperKeysMain[$i] = $mapperKeys[$i][0]; | |
6a488035 TO |
398 | } |
399 | ||
400 | $this->set('mapper', $mapper); | |
401 | ||
402 | // store mapping Id to display it in the preview page | |
a7488080 | 403 | if (!empty($params['mappingId'])) { |
6a488035 TO |
404 | $this->set('loadMappingId', $params['mappingId']); |
405 | } | |
406 | //Updating Mapping Records | |
a7488080 | 407 | if (!empty($params['updateMapping'])) { |
6a488035 TO |
408 | $mappingFields = new CRM_Core_DAO_MappingField(); |
409 | $mappingFields->mapping_id = $params['mappingId']; | |
410 | $mappingFields->find(); | |
411 | ||
affcc9d2 | 412 | $mappingFieldsId = []; |
6a488035 TO |
413 | while ($mappingFields->fetch()) { |
414 | if ($mappingFields->id) { | |
415 | $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id; | |
416 | } | |
417 | } | |
418 | ||
419 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
420 | $updateMappingFields = new CRM_Core_DAO_MappingField(); | |
421 | $updateMappingFields->id = $mappingFieldsId[$i]; | |
422 | $updateMappingFields->mapping_id = $params['mappingId']; | |
423 | $updateMappingFields->column_number = $i; | |
6a488035 TO |
424 | $updateMappingFields->name = $mapper[$i]; |
425 | $updateMappingFields->save(); | |
426 | } | |
427 | } | |
428 | ||
429 | //Saving Mapping Details and Records | |
a7488080 | 430 | if (!empty($params['saveMapping'])) { |
6a488035 TO |
431 | $mappingParams = array( |
432 | 'name' => $params['saveMappingName'], | |
433 | 'description' => $params['saveMappingDesc'], | |
95f52e3b | 434 | 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'), |
6a488035 TO |
435 | ); |
436 | $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); | |
437 | ||
438 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
439 | ||
440 | $saveMappingFields = new CRM_Core_DAO_MappingField(); | |
441 | $saveMappingFields->mapping_id = $saveMapping->id; | |
442 | $saveMappingFields->column_number = $i; | |
6a488035 TO |
443 | $saveMappingFields->name = $mapper[$i]; |
444 | $saveMappingFields->save(); | |
445 | } | |
446 | $this->set('savedMapping', $saveMappingFields->mapping_id); | |
447 | } | |
448 | ||
8bbb06d1 | 449 | $parser = new CRM_Member_Import_Parser_Membership($mapperKeysMain); |
9b78b60c | 450 | $parser->setUserJobID($this->getUserJobID()); |
5e8faabc | 451 | $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'), $mapper, $this->getSubmittedValue('skipColumnHeader'), |
a05662ef | 452 | CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType') |
6a488035 TO |
453 | ); |
454 | // add all the necessary variables to the form | |
455 | $parser->set($this); | |
456 | } | |
96025800 | 457 | |
83a1c234 EM |
458 | /** |
459 | * @return \CRM_Member_Import_Parser_Membership | |
460 | */ | |
461 | protected function getParser(): CRM_Member_Import_Parser_Membership { | |
462 | if (!$this->parser) { | |
463 | $this->parser = new CRM_Member_Import_Parser_Membership(); | |
464 | $this->parser->setUserJobID($this->getUserJobID()); | |
465 | $this->parser->init(); | |
466 | } | |
467 | return $this->parser; | |
468 | } | |
469 | ||
6a488035 | 470 | } |