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