Merge pull request #567 from dlobo/CRM-12452
[civicrm-core.git] / CRM / Activity / Import / Form / MapField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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_Core_Form {
40
41 /**
42 * cache of preview data values
43 *
44 * @var array
45 * @access protected
46 */
47 protected $_dataValues;
48
49 /**
50 * mapper fields
51 *
52 * @var array
53 * @access protected
54 */
55 protected $_mapperFields;
56
57 /**
58 * loaded mapping ID
59 *
60 * @var int
61 * @access protected
62 */
63 protected $_loadedMappingId;
64
65 /**
66 * number of columns in import file
67 *
68 * @var int
69 * @access protected
70 */
71 protected $_columnCount;
72
73 /**
74 * column headers, if we have them
75 *
76 * @var array
77 * @access protected
78 */
79 protected $_columnHeaders;
80
81 /**
82 * an array of booleans to keep track of whether a field has been used in
83 * form building already.
84 *
85 * @var array
86 * @access protected
87 */
88 protected $_fieldUsed;
89
90 /**
91 * Attempt to match header labels with our mapper fields
92 *
93 * @param header
94 * @param mapperFields
95 *
96 * @return string
97 * @access public
98 */
99 public function defaultFromHeader($header, &$patterns) {
100 foreach ($patterns as $key => $re) {
101 // Skip empty key/patterns
102 if (!$key || !$re || strlen("$re") < 5) {
103 continue;
104 }
105
106 // Scan through the headerPatterns defined in the schema for a match
107 if (preg_match($re, $header)) {
108 $this->_fieldUsed[$key] = TRUE;
109 return $key;
110 }
111 }
112 return '';
113 }
114
115 /**
116 * Guess at the field names given the data and patterns from the schema
117 *
118 * @param patterns
119 * @param index
120 *
121 * @return string
122 * @access public
123 */
124 public function defaultFromData(&$patterns, $index) {
125 $best = '';
126 $bestHits = 0;
127 $n = count($this->_dataValues);
128
129 foreach ($patterns as $key => $re) {
130 // Skip empty key/patterns
131 if (!$key || !$re || strlen("$re") < 5) {
132 continue;
133 }
134
135 // if ($this->_fieldUsed[$key])
136 // continue;
137
138 /* Take a vote over the preview data set */
139
140 $hits = 0;
141 for ($i = 0; $i < $n; $i++) {
142 if (preg_match($re, $this->_dataValues[$i][$index])) {
143 $hits++;
144 }
145 }
146
147 if ($hits > $bestHits) {
148 $bestHits = $hits;
149 $best = $key;
150 }
151 }
152
153 if ($best != '') {
154 $this->_fieldUsed[$best] = TRUE;
155 }
156 return $best;
157 }
158
159 /**
160 * Function to set variables up before form is built
161 *
162 * @return void
163 * @access public
164 */
165 public function preProcess() {
166 $this->_mapperFields = $this->get('fields');
167 unset($this->_mapperFields['id']);
168 asort($this->_mapperFields);
169
170 $this->_columnCount = $this->get('columnCount');
171 $this->assign('columnCount', $this->_columnCount);
172 $this->_dataValues = $this->get('dataValues');
173 $this->assign('dataValues', $this->_dataValues);
174
175 $skipColumnHeader = $this->controller->exportValue('UploadFile', 'skipColumnHeader');
176
177 if ($skipColumnHeader) {
178 $this->assign('skipColumnHeader', $skipColumnHeader);
179 $this->assign('rowDisplayCount', 3);
180 /* if we had a column header to skip, stash it for later */
181
182 $this->_columnHeaders = $this->_dataValues[0];
183 }
184 else {
185 $this->assign('rowDisplayCount', 2);
186 }
187 $highlightedFields = array();
188 $requiredFields = array('activity_date_time', 'activity_type_id', 'activity_label', 'target_contact_id', 'activity_subject');
189 foreach ($requiredFields as $val) {
190 $highlightedFields[] = $val;
191 }
192 $this->assign('highlightedFields', $highlightedFields);
193 }
194
195 /**
196 * Function to actually build the form
197 *
198 * @return void
199 * @access public
200 */
201 public function buildQuickForm() {
202 //to save the current mappings
203 if (!$this->get('savedMapping')) {
204 $saveDetailsName = ts('Save this field mapping');
205 $this->applyFilter('saveMappingName', 'trim');
206 $this->add('text', 'saveMappingName', ts('Name'));
207 $this->add('text', 'saveMappingDesc', ts('Description'));
208 }
209 else {
210 $savedMapping = $this->get('savedMapping');
211 //mapping is to be loaded from database
212
213 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping);
214
215 //get loaded Mapping Fields
216 $mappingName = CRM_Utils_Array::value('1', $mappingName);
217 $mappingContactType = CRM_Utils_Array::value('1', $mappingContactType);
218 $mappingLocation = CRM_Utils_Array::value('1', $mappingLocation);
219 $mappingPhoneType = CRM_Utils_Array::value('1', $mappingPhoneType);
220 $mappingRelation = CRM_Utils_Array::value('1', $mappingRelation);
221
222 $this->assign('loadedMapping', $savedMapping);
223 $this->set('loadedMapping', $savedMapping);
224
225 $params = array('id' => $savedMapping);
226 $temp = array();
227 $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp);
228
229 $this->assign('savedName', $mappingDetails->name);
230
231 $this->add('hidden', 'mappingId', $savedMapping);
232
233 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
234 $saveDetailsName = ts('Save as a new field mapping');
235 $this->add('text', 'saveMappingName', ts('Name'));
236 $this->add('text', 'saveMappingDesc', ts('Description'));
237 }
238
239 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
240
241 $this->addFormRule(array('CRM_Activity_Import_Form_MapField', 'formRule'));
242
243 //-------- end of saved mapping stuff ---------
244
245 $defaults = array();
246 $mapperKeys = array_keys($this->_mapperFields);
247
248 $hasHeaders = !empty($this->_columnHeaders);
249 $headerPatterns = $this->get('headerPatterns');
250 $dataPatterns = $this->get('dataPatterns');
251 $hasLocationTypes = $this->get('fieldTypes');
252
253
254 /* Initialize all field usages to false */
255
256 foreach ($mapperKeys as $key) {
257 $this->_fieldUsed[$key] = FALSE;
258 }
259 $this->_location_types = CRM_Core_PseudoConstant::locationType();
260 $sel1 = $this->_mapperFields;
261
262 $sel2[''] = NULL;
263
264 $js = "<script type='text/javascript'>\n";
265 $formName = 'document.forms.' . $this->_name;
266
267 //used to warn for mismatch column count or mismatch mapping
268 $warning = 0;
269
270
271 for ($i = 0; $i < $this->_columnCount; $i++) {
272 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
273 $jsSet = FALSE;
274 if ($this->get('savedMapping')) {
275 if (isset($mappingName[$i])) {
276 if ($mappingName[$i] != ts('- do not import -')) {
277
278 $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]);
279
280 if (!isset($locationId) || !$locationId) {
281 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
282 }
283
284 if (!isset($phoneType) || !$phoneType) {
285 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
286 }
287
288 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
289 $defaults["mapper[$i]"] = array(
290 $mappingHeader[0],
291 (isset($locationId)) ? $locationId : "",
292 (isset($phoneType)) ? $phoneType : "",
293 );
294 $jsSet = TRUE;
295 }
296 else {
297 $defaults["mapper[$i]"] = array();
298 }
299 if (!$jsSet) {
300 for ($k = 1; $k < 4; $k++) {
301 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
302 }
303 }
304 }
305 else {
306 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
307 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
308
309 if ($hasHeaders) {
310 $defaults["mapper[$i]"] = array(
311 $this->defaultFromHeader($this->_columnHeaders[$i],
312 $headerPatterns
313 ));
314 }
315 else {
316 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
317 }
318 }
319 //end of load mapping
320 }
321 else {
322 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
323 if ($hasHeaders) {
324 // Infer the default from the skipped headers if we have them
325 $defaults["mapper[$i]"] = array(
326 $this->defaultFromHeader($this->_columnHeaders[$i],
327 $headerPatterns
328 ),
329 // $defaultLocationType->id
330 0,
331 );
332 }
333 else {
334 // Otherwise guess the default from the form of the data
335 $defaults["mapper[$i]"] = array(
336 $this->defaultFromData($dataPatterns, $i),
337 // $defaultLocationType->id
338 0,
339 );
340 }
341 }
342
343 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ? $sel3 : "", (isset($sel4)) ? $sel4 : ""));
344 }
345 $js .= "</script>\n";
346 $this->assign('initHideBoxes', $js);
347
348 //set warning if mismatch in more than
349 if (isset($mappingName)) {
350 if (($this->_columnCount != count($mappingName))) {
351 $warning++;
352 }
353 }
354 if ($warning != 0 && $this->get('savedMapping')) {
355 $session = CRM_Core_Session::singleton();
356 $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.'));
357 }
358 else {
359 $session = CRM_Core_Session::singleton();
360 $session->setStatus(NULL);
361 }
362
363 $this->setDefaults($defaults);
364
365 $this->addButtons(array(
366 array(
367 'type' => 'back',
368 'name' => ts('<< Previous'),
369 ),
370 array(
371 'type' => 'next',
372 'name' => ts('Continue >>'),
373 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
374 'isDefault' => TRUE,
375 ),
376 array(
377 'type' => 'cancel',
378 'name' => ts('Cancel'),
379 ),
380 )
381 );
382 }
383
384 /**
385 * global validation rules for the form
386 *
387 * @param array $fields posted values of the form
388 *
389 * @return array list of errors to be posted back to the form
390 * @static
391 * @access public
392 */
393 static function formRule($fields) {
394 $errors = array();
395 // define so we avoid notices below
396 $errors['_qf_default'] = '';
397
398 $fieldMessage = NULL;
399 if (!array_key_exists('savedMapping', $fields)) {
400 $importKeys = array();
401 foreach ($fields['mapper'] as $mapperPart) {
402 $importKeys[] = $mapperPart[0];
403 }
404 // FIXME: should use the schema titles, not redeclare them
405 $requiredFields = array(
406 'target_contact_id' => ts('Contact ID'),
407 'activity_date_time' => ts('Activity Date'),
408 'activity_subject' => ts('Activity Subject'),
409 'activity_type_id' => ts('Activity Type Id'),
410 );
411
412 $params = array(
413 'used' => 'Unsupervised',
414 'contact_type' => 'Individual',
415 );
416 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params);
417 $weightSum = 0;
418 foreach ($importKeys as $key => $val) {
419 if (array_key_exists($val, $ruleFields)) {
420 $weightSum += $ruleFields[$val];
421 }
422 }
423 foreach ($ruleFields as $field => $weight) {
424 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
425 }
426 foreach ($requiredFields as $field => $title) {
427 if (!in_array($field, $importKeys)) {
428 if ($field == 'target_contact_id') {
429 if ($weightSum >= $threshold || in_array('external_identifier', $importKeys)) {
430 continue;
431 }
432 else {
433 $errors['_qf_default'] .= ts('Missing required contact matching fields.')
434 . $fieldMessage . ' '
435 . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(1 => $threshold))
436 . '<br />';
437 }
438 }
439 elseif ($field == 'activity_type_id') {
440 if (in_array('activity_label', $importKeys)) {
441 continue;
442 }
443 else {
444 $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2',
445 array(
446 1 => $title,
447 2 => 'Activity Type Label'
448 )) . '<br />';
449 }
450 }
451 else {
452 $errors['_qf_default'] .= ts('Missing required field: %1', array(1 => $title)) . '<br />';
453 }
454 }
455 }
456 }
457
458 if (CRM_Utils_Array::value('saveMapping', $fields)) {
459 $nameField = CRM_Utils_Array::value('saveMappingName', $fields);
460 if (empty($nameField)) {
461 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
462 }
463 else {
464 $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Activity', 'name');
465 if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) {
466 $errors['saveMappingName'] = ts('Duplicate Import Mapping Name');
467 }
468 }
469 }
470
471 if (empty($errors['_qf_default'])) {
472 unset($errors['_qf_default']);
473 }
474 if (!empty($errors)) {
475 if (!empty($errors['saveMappingName'])) {
476 $_flag = 1;
477 $assignError = new CRM_Core_Page();
478 $assignError->assign('mappingDetailsError', $_flag);
479 }
480 return $errors;
481 }
482
483 return TRUE;
484 }
485
486 /**
487 * Process the mapped fields and map it into the uploaded file
488 * preview the file and extract some summary statistics
489 *
490 * @return void
491 * @access public
492 */
493 public function postProcess() {
494 $params = $this->controller->exportValues('MapField');
495 //reload the mapfield if load mapping is pressed
496 if (!empty($params['savedMapping'])) {
497 $this->set('savedMapping', $params['savedMapping']);
498 $this->controller->resetPage($this->_name);
499 return;
500 }
501
502 $fileName = $this->controller->exportValue('UploadFile', 'uploadFile');
503 $skipColumnHeader = $this->controller->exportValue('UploadFile', 'skipColumnHeader');
504
505 $config = CRM_Core_Config::singleton();
506 $seperator = $config->fieldSeparator;
507
508 $mapperKeys = array();
509 $mapper = array();
510 $mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
511 $mapperKeysMain = array();
512 $mapperLocType = array();
513 $mapperPhoneType = array();
514
515 for ($i = 0; $i < $this->_columnCount; $i++) {
516 $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]];
517 $mapperKeysMain[$i] = $mapperKeys[$i][0];
518
519 if ((CRM_Utils_Array::value(1, $mapperKeys[$i])) && (is_numeric($mapperKeys[$i][1]))) {
520 $mapperLocType[$i] = $mapperKeys[$i][1];
521 }
522 else {
523 $mapperLocType[$i] = NULL;
524 }
525
526 if ((CRM_Utils_Array::value(2, $mapperKeys[$i])) && (!is_numeric($mapperKeys[$i][2]))) {
527 $mapperPhoneType[$i] = $mapperKeys[$i][2];
528 }
529 else {
530 $mapperPhoneType[$i] = NULL;
531 }
532 }
533
534 $this->set('mapper', $mapper);
535 // store mapping Id to display it in the preview page
536 if (CRM_Utils_Array::value('mappingId', $params)) {
537 $this->set('loadMappingId', $params['mappingId']);
538 }
539
540 //Updating Mapping Records
541 if (CRM_Utils_Array::value('updateMapping', $params)) {
542
543 $mappingFields = new CRM_Core_DAO_MappingField();
544 $mappingFields->mapping_id = $params['mappingId'];
545 $mappingFields->find();
546
547 $mappingFieldsId = array();
548 while ($mappingFields->fetch()) {
549 if ($mappingFields->id) {
550 $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id;
551 }
552 }
553
554 for ($i = 0; $i < $this->_columnCount; $i++) {
555 $updateMappingFields = new CRM_Core_DAO_MappingField();
556 $updateMappingFields->id = $mappingFieldsId[$i];
557 $updateMappingFields->mapping_id = $params['mappingId'];
558 $updateMappingFields->column_number = $i;
559
560 $updateMappingFields->name = $mapper[$i];
561 $updateMappingFields->save();
562 }
563 }
564
565 //Saving Mapping Details and Records
566 if (CRM_Utils_Array::value('saveMapping', $params)) {
567 $mappingParams = array(
568 'name' => $params['saveMappingName'],
569 'description' => $params['saveMappingDesc'],
570 'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type',
571 'Import Activity',
572 'name'
573 ),
574 );
575 $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
576
577 for ($i = 0; $i < $this->_columnCount; $i++) {
578 $saveMappingFields = new CRM_Core_DAO_MappingField();
579 $saveMappingFields->mapping_id = $saveMapping->id;
580 $saveMappingFields->column_number = $i;
581
582 $saveMappingFields->name = $mapper[$i];
583 $saveMappingFields->save();
584 }
585 $this->set('savedMapping', $saveMappingFields->mapping_id);
586 }
587
588
589 $parser = new CRM_Activity_Import_Parser_Activity($mapperKeysMain, $mapperLocType, $mapperPhoneType);
590 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
591 CRM_Activity_Import_Parser::MODE_PREVIEW
592 );
593
594 // add all the necessary variables to the form
595 $parser->set($this);
596 }
597
598 /**
599 * Return a descriptive name for the page, used in wizard header
600 *
601 * @return string
602 * @access public
603 */
604 public function getTitle() {
605 return ts('Match Fields');
606 }
607 }
608