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