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