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