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 | ||
110 | list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping); | |
111 | ||
353ffa53 | 112 | $mappingName = $mappingName[1]; |
6a488035 | 113 | $mappingContactType = $mappingContactType[1]; |
9c1bc317 CW |
114 | $mappingLocation = $mappingLocation['1'] ?? NULL; |
115 | $mappingPhoneType = $mappingPhoneType['1'] ?? NULL; | |
116 | $mappingRelation = $mappingRelation['1'] ?? NULL; | |
6a488035 TO |
117 | |
118 | //mapping is to be loaded from database | |
119 | ||
6a488035 TO |
120 | $this->set('loadedMapping', $savedMapping); |
121 | ||
122 | $getMappingName = new CRM_Core_DAO_Mapping(); | |
123 | $getMappingName->id = $savedMapping; | |
124 | $getMappingName->mapping_type = 'Import Memberships'; | |
125 | $getMappingName->find(); | |
126 | while ($getMappingName->fetch()) { | |
127 | $mapperName = $getMappingName->name; | |
128 | } | |
129 | ||
262b7f26 | 130 | $this->assign('savedMappingName', $mapperName); |
6a488035 TO |
131 | |
132 | $this->add('hidden', 'mappingId', $savedMapping); | |
133 | ||
134 | $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); | |
135 | $saveDetailsName = ts('Save as a new field mapping'); | |
136 | $this->add('text', 'saveMappingName', ts('Name')); | |
137 | $this->add('text', 'saveMappingDesc', ts('Description')); | |
138 | } | |
139 | ||
140 | $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)")); | |
141 | ||
142 | $this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this); | |
143 | ||
144 | //-------- end of saved mapping stuff --------- | |
145 | ||
affcc9d2 | 146 | $defaults = []; |
353ffa53 TO |
147 | $mapperKeys = array_keys($this->_mapperFields); |
148 | $hasHeaders = !empty($this->_columnHeaders); | |
149 | $headerPatterns = $this->get('headerPatterns'); | |
150 | $dataPatterns = $this->get('dataPatterns'); | |
6a488035 TO |
151 | $hasLocationTypes = $this->get('fieldTypes'); |
152 | ||
6a488035 TO |
153 | /* Initialize all field usages to false */ |
154 | ||
155 | foreach ($mapperKeys as $key) { | |
156 | $this->_fieldUsed[$key] = FALSE; | |
157 | } | |
b2b0530a | 158 | $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
159 | $sel1 = $this->_mapperFields; |
160 | if (!$this->get('onDuplicate')) { | |
161 | unset($sel1['id']); | |
162 | unset($sel1['membership_id']); | |
163 | } | |
164 | ||
165 | $sel2[''] = NULL; | |
166 | ||
167 | $js = "<script type='text/javascript'>\n"; | |
168 | $formName = 'document.forms.' . $this->_name; | |
169 | ||
170 | //used to warn for mismatch column count or mismatch mapping | |
171 | $warning = 0; | |
172 | ||
173 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
174 | $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL); | |
175 | $jsSet = FALSE; | |
176 | if ($this->get('savedMapping')) { | |
177 | if (isset($mappingName[$i])) { | |
178 | if ($mappingName[$i] != ts('- do not import -')) { | |
179 | ||
180 | $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]); | |
181 | ||
182 | //When locationType is not set | |
183 | $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n"; | |
184 | ||
185 | //When phoneType is not set | |
186 | $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n"; | |
187 | ||
188 | $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n"; | |
189 | ||
190 | $defaults["mapper[$i]"] = array($mappingHeader[0]); | |
191 | $jsSet = TRUE; | |
192 | } | |
193 | else { | |
affcc9d2 | 194 | $defaults["mapper[$i]"] = []; |
6a488035 TO |
195 | } |
196 | if (!$jsSet) { | |
197 | for ($k = 1; $k < 4; $k++) { | |
198 | $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n"; | |
199 | } | |
200 | } | |
201 | } | |
202 | else { | |
203 | // this load section to help mapping if we ran out of saved columns when doing Load Mapping | |
204 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n"; | |
205 | ||
206 | if ($hasHeaders) { | |
207 | $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns)); | |
208 | } | |
209 | else { | |
210 | $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i)); | |
211 | } | |
212 | } | |
213 | //end of load mapping | |
214 | } | |
215 | else { | |
216 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n"; | |
217 | if ($hasHeaders) { | |
218 | // Infer the default from the skipped headers if we have them | |
219 | $defaults["mapper[$i]"] = array( | |
220 | $this->defaultFromHeader($this->_columnHeaders[$i], | |
221 | $headerPatterns | |
222 | ), | |
223 | // $defaultLocationType->id | |
224 | 0, | |
225 | ); | |
226 | } | |
227 | else { | |
228 | // Otherwise guess the default from the form of the data | |
229 | $defaults["mapper[$i]"] = array( | |
230 | $this->defaultFromData($dataPatterns, $i), | |
231 | // $defaultLocationType->id | |
232 | 0, | |
233 | ); | |
234 | } | |
235 | } | |
236 | $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ? $sel3 : "", (isset($sel4)) ? $sel4 : "")); | |
237 | } | |
238 | $js .= "</script>\n"; | |
239 | $this->assign('initHideBoxes', $js); | |
240 | ||
241 | //set warning if mismatch in more than | |
242 | if (isset($mappingName)) { | |
243 | if (($this->_columnCount != count($mappingName))) { | |
244 | $warning++; | |
245 | } | |
246 | } | |
247 | if ($warning != 0 && $this->get('savedMapping')) { | |
248 | $session = CRM_Core_Session::singleton(); | |
249 | $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.')); | |
250 | } | |
251 | else { | |
252 | $session = CRM_Core_Session::singleton(); | |
253 | $session->setStatus(NULL); | |
254 | } | |
255 | ||
256 | $this->setDefaults($defaults); | |
257 | ||
258 | $this->addButtons(array( | |
c5c263ca AH |
259 | array( |
260 | 'type' => 'back', | |
261 | 'name' => ts('Previous'), | |
262 | ), | |
263 | array( | |
264 | 'type' => 'next', | |
265 | 'name' => ts('Continue'), | |
266 | 'spacing' => ' ', | |
267 | 'isDefault' => TRUE, | |
268 | ), | |
269 | array( | |
270 | 'type' => 'cancel', | |
271 | 'name' => ts('Cancel'), | |
272 | ), | |
273 | )); | |
6a488035 TO |
274 | } |
275 | ||
276 | /** | |
fe482240 | 277 | * Global validation rules for the form. |
6a488035 | 278 | * |
b2363ea8 TO |
279 | * @param array $fields |
280 | * Posted values of the form. | |
6a488035 | 281 | * |
fd31fa4c | 282 | * @param $files |
e8cf95b4 | 283 | * @param self $self |
fd31fa4c | 284 | * |
a6c01b45 CW |
285 | * @return array |
286 | * list of errors to be posted back to the form | |
6a488035 | 287 | */ |
00be9182 | 288 | public static function formRule($fields, $files, $self) { |
affcc9d2 | 289 | $errors = []; |
6a488035 TO |
290 | |
291 | if (!array_key_exists('savedMapping', $fields)) { | |
affcc9d2 | 292 | $importKeys = []; |
6a488035 TO |
293 | foreach ($fields['mapper'] as $mapperPart) { |
294 | $importKeys[] = $mapperPart[0]; | |
295 | } | |
296 | // FIXME: should use the schema titles, not redeclare them | |
297 | $requiredFields = array( | |
298 | 'membership_contact_id' => ts('Contact ID'), | |
299 | 'membership_type_id' => ts('Membership Type'), | |
300 | 'membership_start_date' => ts('Membership Start Date'), | |
301 | ); | |
302 | ||
303 | $contactTypeId = $self->get('contactType'); | |
304 | $contactTypes = array( | |
a05662ef CW |
305 | CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', |
306 | CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', | |
307 | CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization', | |
6a488035 TO |
308 | ); |
309 | $params = array( | |
353ffa53 | 310 | 'used' => 'Unsupervised', |
6a488035 TO |
311 | 'contact_type' => $contactTypes[$contactTypeId], |
312 | ); | |
61194d45 | 313 | list($ruleFields, $threshold) = CRM_Dedupe_BAO_DedupeRuleGroup::dedupeRuleFieldsWeight($params); |
6a488035 TO |
314 | $weightSum = 0; |
315 | foreach ($importKeys as $key => $val) { | |
316 | if (array_key_exists($val, $ruleFields)) { | |
317 | $weightSum += $ruleFields[$val]; | |
318 | } | |
319 | } | |
320 | $fieldMessage = ''; | |
321 | foreach ($ruleFields as $field => $weight) { | |
322 | $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; | |
323 | } | |
324 | ||
325 | foreach ($requiredFields as $field => $title) { | |
326 | if (!in_array($field, $importKeys)) { | |
327 | if ($field == 'membership_contact_id') { | |
328 | if ((($weightSum >= $threshold || in_array('external_identifier', $importKeys)) && | |
a05662ef | 329 | $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE |
6a488035 TO |
330 | ) || |
331 | in_array('membership_id', $importKeys) | |
332 | ) { | |
333 | continue; | |
334 | } | |
335 | else { | |
336 | if (!isset($errors['_qf_default'])) { | |
337 | $errors['_qf_default'] = ''; | |
338 | } | |
339 | $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 | 340 | 1 => $threshold, |
c5c263ca | 341 | )) . ' ' . ts('(OR Membership ID if update mode.)') . '<br />'; |
6a488035 TO |
342 | } |
343 | } | |
344 | else { | |
345 | if (!isset($errors['_qf_default'])) { | |
346 | $errors['_qf_default'] = ''; | |
347 | } | |
348 | $errors['_qf_default'] .= ts('Missing required field: %1', array( | |
317fceb4 | 349 | 1 => $title, |
c5c263ca | 350 | )) . '<br />'; |
6a488035 TO |
351 | } |
352 | } | |
353 | } | |
354 | } | |
355 | ||
a7488080 | 356 | if (!empty($fields['saveMapping'])) { |
9c1bc317 | 357 | $nameField = $fields['saveMappingName'] ?? NULL; |
6a488035 TO |
358 | if (empty($nameField)) { |
359 | $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); | |
360 | } | |
361 | else { | |
95f52e3b | 362 | if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'))) { |
6a488035 TO |
363 | $errors['saveMappingName'] = ts('Duplicate Import Membership Mapping Name'); |
364 | } | |
365 | } | |
366 | } | |
367 | ||
368 | if (!empty($errors)) { | |
369 | if (!empty($errors['saveMappingName'])) { | |
370 | $_flag = 1; | |
371 | $assignError = new CRM_Core_Page(); | |
372 | $assignError->assign('mappingDetailsError', $_flag); | |
373 | } | |
374 | return $errors; | |
375 | } | |
376 | ||
377 | return TRUE; | |
378 | } | |
379 | ||
380 | /** | |
381 | * Process the mapped fields and map it into the uploaded file | |
382 | * preview the file and extract some summary statistics | |
383 | * | |
384 | * @return void | |
6a488035 TO |
385 | */ |
386 | public function postProcess() { | |
387 | $params = $this->controller->exportValues('MapField'); | |
388 | //reload the mapfield if load mapping is pressed | |
389 | if (!empty($params['savedMapping'])) { | |
390 | $this->set('savedMapping', $params['savedMapping']); | |
391 | $this->controller->resetPage($this->_name); | |
392 | return; | |
393 | } | |
9b78b60c | 394 | $this->updateUserJobMetadata('submitted_values', $this->getSubmittedValues()); |
6a488035 | 395 | |
affcc9d2 | 396 | $mapper = []; |
353ffa53 | 397 | $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); |
affcc9d2 | 398 | $mapperKeysMain = []; |
6a488035 TO |
399 | |
400 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
401 | $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]]; | |
402 | $mapperKeysMain[$i] = $mapperKeys[$i][0]; | |
6a488035 TO |
403 | } |
404 | ||
405 | $this->set('mapper', $mapper); | |
406 | ||
407 | // store mapping Id to display it in the preview page | |
a7488080 | 408 | if (!empty($params['mappingId'])) { |
6a488035 TO |
409 | $this->set('loadMappingId', $params['mappingId']); |
410 | } | |
411 | //Updating Mapping Records | |
a7488080 | 412 | if (!empty($params['updateMapping'])) { |
6a488035 TO |
413 | $mappingFields = new CRM_Core_DAO_MappingField(); |
414 | $mappingFields->mapping_id = $params['mappingId']; | |
415 | $mappingFields->find(); | |
416 | ||
affcc9d2 | 417 | $mappingFieldsId = []; |
6a488035 TO |
418 | while ($mappingFields->fetch()) { |
419 | if ($mappingFields->id) { | |
420 | $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id; | |
421 | } | |
422 | } | |
423 | ||
424 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
425 | $updateMappingFields = new CRM_Core_DAO_MappingField(); | |
426 | $updateMappingFields->id = $mappingFieldsId[$i]; | |
427 | $updateMappingFields->mapping_id = $params['mappingId']; | |
428 | $updateMappingFields->column_number = $i; | |
6a488035 TO |
429 | $updateMappingFields->name = $mapper[$i]; |
430 | $updateMappingFields->save(); | |
431 | } | |
432 | } | |
433 | ||
434 | //Saving Mapping Details and Records | |
a7488080 | 435 | if (!empty($params['saveMapping'])) { |
6a488035 TO |
436 | $mappingParams = array( |
437 | 'name' => $params['saveMappingName'], | |
438 | 'description' => $params['saveMappingDesc'], | |
95f52e3b | 439 | 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'), |
6a488035 TO |
440 | ); |
441 | $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); | |
442 | ||
443 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
444 | ||
445 | $saveMappingFields = new CRM_Core_DAO_MappingField(); | |
446 | $saveMappingFields->mapping_id = $saveMapping->id; | |
447 | $saveMappingFields->column_number = $i; | |
6a488035 TO |
448 | $saveMappingFields->name = $mapper[$i]; |
449 | $saveMappingFields->save(); | |
450 | } | |
451 | $this->set('savedMapping', $saveMappingFields->mapping_id); | |
452 | } | |
453 | ||
8bbb06d1 | 454 | $parser = new CRM_Member_Import_Parser_Membership($mapperKeysMain); |
9b78b60c | 455 | $parser->setUserJobID($this->getUserJobID()); |
5e8faabc | 456 | $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'), $mapper, $this->getSubmittedValue('skipColumnHeader'), |
a05662ef | 457 | CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType') |
6a488035 TO |
458 | ); |
459 | // add all the necessary variables to the form | |
460 | $parser->set($this); | |
461 | } | |
96025800 | 462 | |
6a488035 | 463 | } |