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