3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class gets the name of the file to upload
39 class CRM_Activity_Import_Form_MapField
extends CRM_Core_Form
{
42 * cache of preview data values
47 protected $_dataValues;
55 protected $_mapperFields;
63 protected $_loadedMappingId;
66 * number of columns in import file
71 protected $_columnCount;
74 * column headers, if we have them
79 protected $_columnHeaders;
82 * an array of booleans to keep track of whether a field has been used in
83 * form building already.
88 protected $_fieldUsed;
91 * Attempt to match header labels with our mapper fields
99 public function defaultFromHeader($header, &$patterns) {
100 foreach ($patterns as $key => $re) {
101 // Skip empty key/patterns
102 if (!$key ||
!$re ||
strlen("$re") < 5) {
106 // Scan through the headerPatterns defined in the schema for a match
107 if (preg_match($re, $header)) {
108 $this->_fieldUsed
[$key] = TRUE;
116 * Guess at the field names given the data and patterns from the schema
124 public function defaultFromData(&$patterns, $index) {
127 $n = count($this->_dataValues
);
129 foreach ($patterns as $key => $re) {
130 // Skip empty key/patterns
131 if (!$key ||
!$re ||
strlen("$re") < 5) {
135 // if ($this->_fieldUsed[$key])
138 /* Take a vote over the preview data set */
141 for ($i = 0; $i < $n; $i++
) {
142 if (preg_match($re, $this->_dataValues
[$i][$index])) {
147 if ($hits > $bestHits) {
154 $this->_fieldUsed
[$best] = TRUE;
160 * Function to set variables up before form is built
165 public function preProcess() {
166 $this->_mapperFields
= $this->get('fields');
167 unset($this->_mapperFields
['id']);
168 asort($this->_mapperFields
);
170 $this->_columnCount
= $this->get('columnCount');
171 $this->assign('columnCount', $this->_columnCount
);
172 $this->_dataValues
= $this->get('dataValues');
173 $this->assign('dataValues', $this->_dataValues
);
175 $skipColumnHeader = $this->controller
->exportValue('UploadFile', 'skipColumnHeader');
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 */
182 $this->_columnHeaders
= $this->_dataValues
[0];
185 $this->assign('rowDisplayCount', 2);
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;
192 $this->assign('highlightedFields', $highlightedFields);
196 * Function to actually build the form
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'));
210 $savedMapping = $this->get('savedMapping');
211 //mapping is to be loaded from database
213 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
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);
222 $this->assign('loadedMapping', $savedMapping);
223 $this->set('loadedMapping', $savedMapping);
225 $params = array('id' => $savedMapping);
227 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
229 $this->assign('savedName', $mappingDetails->name
);
231 $this->add('hidden', 'mappingId', $savedMapping);
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'));
239 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
241 $this->addFormRule(array('CRM_Activity_Import_Form_MapField', 'formRule'));
243 //-------- end of saved mapping stuff ---------
246 $mapperKeys = array_keys($this->_mapperFields
);
248 $hasHeaders = !empty($this->_columnHeaders
);
249 $headerPatterns = $this->get('headerPatterns');
250 $dataPatterns = $this->get('dataPatterns');
251 $hasLocationTypes = $this->get('fieldTypes');
254 /* Initialize all field usages to false */
256 foreach ($mapperKeys as $key) {
257 $this->_fieldUsed
[$key] = FALSE;
259 $this->_location_types
= CRM_Core_PseudoConstant
::locationType();
260 $sel1 = $this->_mapperFields
;
264 $js = "<script type='text/javascript'>\n";
265 $formName = 'document.forms.' . $this->_name
;
267 //used to warn for mismatch column count or mismatch mapping
271 for ($i = 0; $i < $this->_columnCount
; $i++
) {
272 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
274 if ($this->get('savedMapping')) {
275 if (isset($mappingName[$i])) {
276 if ($mappingName[$i] != ts('- do not import -')) {
278 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
280 if (!isset($locationId) ||
!$locationId) {
281 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
284 if (!isset($phoneType) ||
!$phoneType) {
285 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
288 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
289 $defaults["mapper[$i]"] = array(
291 (isset($locationId)) ?
$locationId : "",
292 (isset($phoneType)) ?
$phoneType : "",
297 $defaults["mapper[$i]"] = array();
300 for ($k = 1; $k < 4; $k++
) {
301 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
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";
310 $defaults["mapper[$i]"] = array(
311 $this->defaultFromHeader($this->_columnHeaders
[$i],
316 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
319 //end of load mapping
322 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
324 // Infer the default from the skipped headers if we have them
325 $defaults["mapper[$i]"] = array(
326 $this->defaultFromHeader($this->_columnHeaders
[$i],
329 // $defaultLocationType->id
334 // Otherwise guess the default from the form of the data
335 $defaults["mapper[$i]"] = array(
336 $this->defaultFromData($dataPatterns, $i),
337 // $defaultLocationType->id
343 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ?
$sel3 : "", (isset($sel4)) ?
$sel4 : ""));
345 $js .= "</script>\n";
346 $this->assign('initHideBoxes', $js);
348 //set warning if mismatch in more than
349 if (isset($mappingName)) {
350 if (($this->_columnCount
!= count($mappingName))) {
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.'));
359 $session = CRM_Core_Session
::singleton();
360 $session->setStatus(NULL);
363 $this->setDefaults($defaults);
365 $this->addButtons(array(
368 'name' => ts('<< Previous'),
372 'name' => ts('Continue >>'),
373 'spacing' => ' ',
378 'name' => ts('Cancel'),
385 * global validation rules for the form
387 * @param array $fields posted values of the form
389 * @return array list of errors to be posted back to the form
393 static function formRule($fields) {
395 // define so we avoid notices below
396 $errors['_qf_default'] = '';
398 $fieldMessage = NULL;
399 if (!array_key_exists('savedMapping', $fields)) {
400 $importKeys = array();
401 foreach ($fields['mapper'] as $mapperPart) {
402 $importKeys[] = $mapperPart[0];
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'),
413 'used' => 'Unsupervised',
414 'contact_type' => 'Individual',
416 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
418 foreach ($importKeys as $key => $val) {
419 if (array_key_exists($val, $ruleFields)) {
420 $weightSum +
= $ruleFields[$val];
423 foreach ($ruleFields as $field => $weight) {
424 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
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)) {
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))
439 elseif ($field == 'activity_type_id') {
440 if (in_array('activity_label', $importKeys)) {
444 $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2',
447 2 => 'Activity Type Label'
452 $errors['_qf_default'] .= ts('Missing required field: %1', array(1 => $title)) . '<br />';
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');
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');
471 if (empty($errors['_qf_default'])) {
472 unset($errors['_qf_default']);
474 if (!empty($errors)) {
475 if (!empty($errors['saveMappingName'])) {
477 $assignError = new CRM_Core_Page();
478 $assignError->assign('mappingDetailsError', $_flag);
487 * Process the mapped fields and map it into the uploaded file
488 * preview the file and extract some summary statistics
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
);
502 $fileName = $this->controller
->exportValue('UploadFile', 'uploadFile');
503 $skipColumnHeader = $this->controller
->exportValue('UploadFile', 'skipColumnHeader');
505 $config = CRM_Core_Config
::singleton();
506 $seperator = $config->fieldSeparator
;
508 $mapperKeys = array();
510 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
511 $mapperKeysMain = array();
512 $mapperLocType = array();
513 $mapperPhoneType = array();
515 for ($i = 0; $i < $this->_columnCount
; $i++
) {
516 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
517 $mapperKeysMain[$i] = $mapperKeys[$i][0];
519 if ((CRM_Utils_Array
::value(1, $mapperKeys[$i])) && (is_numeric($mapperKeys[$i][1]))) {
520 $mapperLocType[$i] = $mapperKeys[$i][1];
523 $mapperLocType[$i] = NULL;
526 if ((CRM_Utils_Array
::value(2, $mapperKeys[$i])) && (!is_numeric($mapperKeys[$i][2]))) {
527 $mapperPhoneType[$i] = $mapperKeys[$i][2];
530 $mapperPhoneType[$i] = NULL;
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']);
540 //Updating Mapping Records
541 if (CRM_Utils_Array
::value('updateMapping', $params)) {
543 $mappingFields = new CRM_Core_DAO_MappingField();
544 $mappingFields->mapping_id
= $params['mappingId'];
545 $mappingFields->find();
547 $mappingFieldsId = array();
548 while ($mappingFields->fetch()) {
549 if ($mappingFields->id
) {
550 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
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;
560 $updateMappingFields->name
= $mapper[$i];
561 $updateMappingFields->save();
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',
575 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
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;
582 $saveMappingFields->name
= $mapper[$i];
583 $saveMappingFields->save();
585 $this->set('savedMapping', $saveMappingFields->mapping_id
);
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
594 // add all the necessary variables to the form
599 * Return a descriptive name for the page, used in wizard header
604 public function getTitle() {
605 return ts('Match Fields');