Merge pull request #18544 from magnolia61/participant_status_notification_default
[civicrm-core.git] / CRM / Member / Import / Form / MapField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class gets the name of the file to upload
20 */
21 class CRM_Member_Import_Form_MapField extends CRM_Import_Form_MapField {
22
23
24 /**
25 * store contactType.
26 *
27 * @var int
28 */
29 public static $_contactType = NULL;
30
31 /**
32 * Set variables up before form is built.
33 *
34 * @return void
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
45 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
46 $this->_onDuplicate = $this->get('onDuplicate', $onDuplicate ?? "");
47
48 $highlightedFields = [];
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.
62 if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
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 }
72 elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) {
73 unset($this->_mapperFields['membership_id']);
74 $highlightedFieldsArray = array(
75 'membership_contact_id',
76 'email',
77 'external_identifier',
78 'membership_start_date',
79 'membership_type_id',
80 );
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 /**
95 * Build the form object.
96 *
97 * @return void
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
112 $mappingName = $mappingName[1];
113 $mappingContactType = $mappingContactType[1];
114 $mappingLocation = $mappingLocation['1'] ?? NULL;
115 $mappingPhoneType = $mappingPhoneType['1'] ?? NULL;
116 $mappingRelation = $mappingRelation['1'] ?? NULL;
117
118 //mapping is to be loaded from database
119
120 $params = array('id' => $savedMapping);
121 $temp = [];
122 $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp);
123
124 $this->assign('loadedMapping', $mappingDetails->name);
125 $this->set('loadedMapping', $savedMapping);
126
127 $getMappingName = new CRM_Core_DAO_Mapping();
128 $getMappingName->id = $savedMapping;
129 $getMappingName->mapping_type = 'Import Memberships';
130 $getMappingName->find();
131 while ($getMappingName->fetch()) {
132 $mapperName = $getMappingName->name;
133 }
134
135 $this->assign('savedName', $mapperName);
136
137 $this->add('hidden', 'mappingId', $savedMapping);
138
139 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
140 $saveDetailsName = ts('Save as a new field mapping');
141 $this->add('text', 'saveMappingName', ts('Name'));
142 $this->add('text', 'saveMappingDesc', ts('Description'));
143 }
144
145 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
146
147 $this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this);
148
149 //-------- end of saved mapping stuff ---------
150
151 $defaults = [];
152 $mapperKeys = array_keys($this->_mapperFields);
153 $hasHeaders = !empty($this->_columnHeaders);
154 $headerPatterns = $this->get('headerPatterns');
155 $dataPatterns = $this->get('dataPatterns');
156 $hasLocationTypes = $this->get('fieldTypes');
157
158 /* Initialize all field usages to false */
159
160 foreach ($mapperKeys as $key) {
161 $this->_fieldUsed[$key] = FALSE;
162 }
163 $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
164 $sel1 = $this->_mapperFields;
165 if (!$this->get('onDuplicate')) {
166 unset($sel1['id']);
167 unset($sel1['membership_id']);
168 }
169
170 $sel2[''] = NULL;
171
172 $js = "<script type='text/javascript'>\n";
173 $formName = 'document.forms.' . $this->_name;
174
175 //used to warn for mismatch column count or mismatch mapping
176 $warning = 0;
177
178 for ($i = 0; $i < $this->_columnCount; $i++) {
179 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
180 $jsSet = FALSE;
181 if ($this->get('savedMapping')) {
182 if (isset($mappingName[$i])) {
183 if ($mappingName[$i] != ts('- do not import -')) {
184
185 $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]);
186
187 //When locationType is not set
188 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
189
190 //When phoneType is not set
191 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
192
193 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
194
195 $defaults["mapper[$i]"] = array($mappingHeader[0]);
196 $jsSet = TRUE;
197 }
198 else {
199 $defaults["mapper[$i]"] = [];
200 }
201 if (!$jsSet) {
202 for ($k = 1; $k < 4; $k++) {
203 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
204 }
205 }
206 }
207 else {
208 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
209 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
210
211 if ($hasHeaders) {
212 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns));
213 }
214 else {
215 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
216 }
217 }
218 //end of load mapping
219 }
220 else {
221 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
222 if ($hasHeaders) {
223 // Infer the default from the skipped headers if we have them
224 $defaults["mapper[$i]"] = array(
225 $this->defaultFromHeader($this->_columnHeaders[$i],
226 $headerPatterns
227 ),
228 // $defaultLocationType->id
229 0,
230 );
231 }
232 else {
233 // Otherwise guess the default from the form of the data
234 $defaults["mapper[$i]"] = array(
235 $this->defaultFromData($dataPatterns, $i),
236 // $defaultLocationType->id
237 0,
238 );
239 }
240 }
241 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ? $sel3 : "", (isset($sel4)) ? $sel4 : ""));
242 }
243 $js .= "</script>\n";
244 $this->assign('initHideBoxes', $js);
245
246 //set warning if mismatch in more than
247 if (isset($mappingName)) {
248 if (($this->_columnCount != count($mappingName))) {
249 $warning++;
250 }
251 }
252 if ($warning != 0 && $this->get('savedMapping')) {
253 $session = CRM_Core_Session::singleton();
254 $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.'));
255 }
256 else {
257 $session = CRM_Core_Session::singleton();
258 $session->setStatus(NULL);
259 }
260
261 $this->setDefaults($defaults);
262
263 $this->addButtons(array(
264 array(
265 'type' => 'back',
266 'name' => ts('Previous'),
267 ),
268 array(
269 'type' => 'next',
270 'name' => ts('Continue'),
271 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
272 'isDefault' => TRUE,
273 ),
274 array(
275 'type' => 'cancel',
276 'name' => ts('Cancel'),
277 ),
278 ));
279 }
280
281 /**
282 * Global validation rules for the form.
283 *
284 * @param array $fields
285 * Posted values of the form.
286 *
287 * @param $files
288 * @param $self
289 *
290 * @return array
291 * list of errors to be posted back to the form
292 */
293 public static function formRule($fields, $files, $self) {
294 $errors = [];
295
296 if (!array_key_exists('savedMapping', $fields)) {
297 $importKeys = [];
298 foreach ($fields['mapper'] as $mapperPart) {
299 $importKeys[] = $mapperPart[0];
300 }
301 // FIXME: should use the schema titles, not redeclare them
302 $requiredFields = array(
303 'membership_contact_id' => ts('Contact ID'),
304 'membership_type_id' => ts('Membership Type'),
305 'membership_start_date' => ts('Membership Start Date'),
306 );
307
308 $contactTypeId = $self->get('contactType');
309 $contactTypes = array(
310 CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual',
311 CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household',
312 CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization',
313 );
314 $params = array(
315 'used' => 'Unsupervised',
316 'contact_type' => $contactTypes[$contactTypeId],
317 );
318 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params);
319 $weightSum = 0;
320 foreach ($importKeys as $key => $val) {
321 if (array_key_exists($val, $ruleFields)) {
322 $weightSum += $ruleFields[$val];
323 }
324 }
325 $fieldMessage = '';
326 foreach ($ruleFields as $field => $weight) {
327 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
328 }
329
330 foreach ($requiredFields as $field => $title) {
331 if (!in_array($field, $importKeys)) {
332 if ($field == 'membership_contact_id') {
333 if ((($weightSum >= $threshold || in_array('external_identifier', $importKeys)) &&
334 $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE
335 ) ||
336 in_array('membership_id', $importKeys)
337 ) {
338 continue;
339 }
340 else {
341 if (!isset($errors['_qf_default'])) {
342 $errors['_qf_default'] = '';
343 }
344 $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(
345 1 => $threshold,
346 )) . ' ' . ts('(OR Membership ID if update mode.)') . '<br />';
347 }
348 }
349 else {
350 if (!isset($errors['_qf_default'])) {
351 $errors['_qf_default'] = '';
352 }
353 $errors['_qf_default'] .= ts('Missing required field: %1', array(
354 1 => $title,
355 )) . '<br />';
356 }
357 }
358 }
359 }
360
361 if (!empty($fields['saveMapping'])) {
362 $nameField = $fields['saveMappingName'] ?? NULL;
363 if (empty($nameField)) {
364 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
365 }
366 else {
367 if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'))) {
368 $errors['saveMappingName'] = ts('Duplicate Import Membership Mapping Name');
369 }
370 }
371 }
372
373 if (!empty($errors)) {
374 if (!empty($errors['saveMappingName'])) {
375 $_flag = 1;
376 $assignError = new CRM_Core_Page();
377 $assignError->assign('mappingDetailsError', $_flag);
378 }
379 return $errors;
380 }
381
382 return TRUE;
383 }
384
385 /**
386 * Process the mapped fields and map it into the uploaded file
387 * preview the file and extract some summary statistics
388 *
389 * @return void
390 */
391 public function postProcess() {
392 $params = $this->controller->exportValues('MapField');
393 //reload the mapfield if load mapping is pressed
394 if (!empty($params['savedMapping'])) {
395 $this->set('savedMapping', $params['savedMapping']);
396 $this->controller->resetPage($this->_name);
397 return;
398 }
399
400 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
401 $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
402 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
403
404 $mapperKeys = [];
405 $mapper = [];
406 $mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
407 $mapperKeysMain = [];
408 $mapperLocType = [];
409 $mapperPhoneType = [];
410
411 for ($i = 0; $i < $this->_columnCount; $i++) {
412 $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]];
413 $mapperKeysMain[$i] = $mapperKeys[$i][0];
414
415 if (!empty($mapperKeys[$i][1]) && is_numeric($mapperKeys[$i][1])) {
416 $mapperLocType[$i] = $mapperKeys[$i][1];
417 }
418 else {
419 $mapperLocType[$i] = NULL;
420 }
421
422 if (!empty($mapperKeys[$i][2]) && (!is_numeric($mapperKeys[$i][2]))) {
423 $mapperPhoneType[$i] = $mapperKeys[$i][2];
424 }
425 else {
426 $mapperPhoneType[$i] = NULL;
427 }
428 }
429
430 $this->set('mapper', $mapper);
431
432 // store mapping Id to display it in the preview page
433 if (!empty($params['mappingId'])) {
434 $this->set('loadMappingId', $params['mappingId']);
435 }
436 //Updating Mapping Records
437 if (!empty($params['updateMapping'])) {
438 $mappingFields = new CRM_Core_DAO_MappingField();
439 $mappingFields->mapping_id = $params['mappingId'];
440 $mappingFields->find();
441
442 $mappingFieldsId = [];
443 while ($mappingFields->fetch()) {
444 if ($mappingFields->id) {
445 $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id;
446 }
447 }
448
449 for ($i = 0; $i < $this->_columnCount; $i++) {
450 $updateMappingFields = new CRM_Core_DAO_MappingField();
451 $updateMappingFields->id = $mappingFieldsId[$i];
452 $updateMappingFields->mapping_id = $params['mappingId'];
453 $updateMappingFields->column_number = $i;
454
455 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
456 $id = $mapperKeyParts[0] ?? NULL;
457 $first = $mapperKeyParts[1] ?? NULL;
458 $second = $mapperKeyParts[2] ?? NULL;
459 $updateMappingFields->name = $mapper[$i];
460 $updateMappingFields->save();
461 }
462 }
463
464 //Saving Mapping Details and Records
465 if (!empty($params['saveMapping'])) {
466 $mappingParams = array(
467 'name' => $params['saveMappingName'],
468 'description' => $params['saveMappingDesc'],
469 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'),
470 );
471 $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
472
473 for ($i = 0; $i < $this->_columnCount; $i++) {
474
475 $saveMappingFields = new CRM_Core_DAO_MappingField();
476 $saveMappingFields->mapping_id = $saveMapping->id;
477 $saveMappingFields->column_number = $i;
478
479 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
480 $id = $mapperKeyParts[0] ?? NULL;
481 $first = $mapperKeyParts[1] ?? NULL;
482 $second = $mapperKeyParts[2] ?? NULL;
483 $saveMappingFields->name = $mapper[$i];
484 $saveMappingFields->save();
485 }
486 $this->set('savedMapping', $saveMappingFields->mapping_id);
487 }
488
489 $parser = new CRM_Member_Import_Parser_Membership($mapperKeysMain, $mapperLocType, $mapperPhoneType);
490 $parser->run($fileName, $separator, $mapper, $skipColumnHeader,
491 CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType')
492 );
493 // add all the necessary variables to the form
494 $parser->set($this);
495 }
496
497 }