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