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