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