3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
37 * This class gets the name of the file to upload
39 class CRM_Activity_Import_Form_MapField
extends CRM_Import_Form_MapField
{
43 * Set variables up before form is built.
47 public function preProcess() {
48 $this->_mapperFields
= $this->get('fields');
49 unset($this->_mapperFields
['id']);
50 asort($this->_mapperFields
);
52 $this->_columnCount
= $this->get('columnCount');
53 $this->assign('columnCount', $this->_columnCount
);
54 $this->_dataValues
= $this->get('dataValues');
55 $this->assign('dataValues', $this->_dataValues
);
57 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
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 */
64 $this->_columnHeaders
= $this->_dataValues
[0];
67 $this->assign('rowDisplayCount', 2);
69 $highlightedFields = array();
70 $requiredFields = array(
77 foreach ($requiredFields as $val) {
78 $highlightedFields[] = $val;
80 $this->assign('highlightedFields', $highlightedFields);
84 * Build the form object.
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'));
97 $savedMapping = $this->get('savedMapping');
98 //mapping is to be loaded from database
100 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
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);
109 $this->assign('loadedMapping', $savedMapping);
110 $this->set('loadedMapping', $savedMapping);
112 $params = array('id' => $savedMapping);
114 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
116 $this->assign('savedName', $mappingDetails->name
);
118 $this->add('hidden', 'mappingId', $savedMapping);
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'));
126 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
128 $this->addFormRule(array('CRM_Activity_Import_Form_MapField', 'formRule'));
130 //-------- end of saved mapping stuff ---------
133 $mapperKeys = array_keys($this->_mapperFields
);
135 $hasHeaders = !empty($this->_columnHeaders
);
136 $headerPatterns = $this->get('headerPatterns');
137 $dataPatterns = $this->get('dataPatterns');
138 $hasLocationTypes = $this->get('fieldTypes');
140 /* Initialize all field usages to false */
142 foreach ($mapperKeys as $key) {
143 $this->_fieldUsed
[$key] = FALSE;
145 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
146 $sel1 = $this->_mapperFields
;
150 $js = "<script type='text/javascript'>\n";
151 $formName = 'document.forms.' . $this->_name
;
153 //used to warn for mismatch column count or mismatch mapping
156 for ($i = 0; $i < $this->_columnCount
; $i++
) {
157 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
159 if ($this->get('savedMapping')) {
160 if (isset($mappingName[$i])) {
161 if ($mappingName[$i] != ts('- do not import -')) {
163 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
165 if (!isset($locationId) ||
!$locationId) {
166 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
169 if (!isset($phoneType) ||
!$phoneType) {
170 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
173 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
174 $defaults["mapper[$i]"] = array(
176 (isset($locationId)) ?
$locationId : "",
177 (isset($phoneType)) ?
$phoneType : "",
182 $defaults["mapper[$i]"] = array();
185 for ($k = 1; $k < 4; $k++
) {
186 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
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";
195 $defaults["mapper[$i]"] = array(
196 $this->defaultFromHeader($this->_columnHeaders
[$i],
202 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
205 //end of load mapping
208 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
210 // Infer the default from the skipped headers if we have them
211 $defaults["mapper[$i]"] = array(
212 $this->defaultFromHeader($this->_columnHeaders
[$i],
215 // $defaultLocationType->id
220 // Otherwise guess the default from the form of the data
221 $defaults["mapper[$i]"] = array(
222 $this->defaultFromData($dataPatterns, $i),
223 // $defaultLocationType->id
229 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ?
$sel3 : "", (isset($sel4)) ?
$sel4 : ""));
231 $js .= "</script>\n";
232 $this->assign('initHideBoxes', $js);
234 //set warning if mismatch in more than
235 if (isset($mappingName)) {
236 if (($this->_columnCount
!= count($mappingName))) {
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.'));
245 $session = CRM_Core_Session
::singleton();
246 $session->setStatus(NULL);
249 $this->setDefaults($defaults);
251 $this->addButtons(array(
254 'name' => ts('Previous'),
258 'name' => ts('Continue'),
259 'spacing' => ' ',
264 'name' => ts('Cancel'),
271 * Global validation rules for the form.
273 * @param array $fields
274 * Posted values of the form.
277 * list of errors to be posted back to the form
279 public static function formRule($fields) {
281 // define so we avoid notices below
282 $errors['_qf_default'] = '';
284 $fieldMessage = NULL;
285 if (!array_key_exists('savedMapping', $fields)) {
286 $importKeys = array();
287 foreach ($fields['mapper'] as $mapperPart) {
288 $importKeys[] = $mapperPart[0];
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'),
299 'used' => 'Unsupervised',
300 'contact_type' => 'Individual',
302 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
304 foreach ($importKeys as $key => $val) {
305 if (array_key_exists($val, $ruleFields)) {
306 $weightSum +
= $ruleFields[$val];
309 foreach ($ruleFields as $field => $weight) {
310 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
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)) {
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))
325 elseif ($field == 'activity_type_id') {
326 if (in_array('activity_label', $importKeys)) {
330 $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2',
333 2 => 'Activity Type Label',
338 $errors['_qf_default'] .= ts('Missing required field: %1', array(1 => $title)) . '<br />';
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');
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');
357 if (empty($errors['_qf_default'])) {
358 unset($errors['_qf_default']);
360 if (!empty($errors)) {
361 if (!empty($errors['saveMappingName'])) {
363 $assignError = new CRM_Core_Page();
364 $assignError->assign('mappingDetailsError', $_flag);
373 * Process the mapped fields and map it into the uploaded file.
374 * preview the file and extract some summary statistics
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
);
387 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
388 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
390 $config = CRM_Core_Config
::singleton();
391 $seperator = $config->fieldSeparator
;
393 $mapperKeys = array();
395 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
396 $mapperKeysMain = array();
397 $mapperLocType = array();
398 $mapperPhoneType = array();
400 for ($i = 0; $i < $this->_columnCount
; $i++
) {
401 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
402 $mapperKeysMain[$i] = $mapperKeys[$i][0];
404 if ((CRM_Utils_Array
::value(1, $mapperKeys[$i])) && (is_numeric($mapperKeys[$i][1]))) {
405 $mapperLocType[$i] = $mapperKeys[$i][1];
408 $mapperLocType[$i] = NULL;
411 if ((CRM_Utils_Array
::value(2, $mapperKeys[$i])) && (!is_numeric($mapperKeys[$i][2]))) {
412 $mapperPhoneType[$i] = $mapperKeys[$i][2];
415 $mapperPhoneType[$i] = NULL;
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']);
425 //Updating Mapping Records
426 if (!empty($params['updateMapping'])) {
428 $mappingFields = new CRM_Core_DAO_MappingField();
429 $mappingFields->mapping_id
= $params['mappingId'];
430 $mappingFields->find();
432 $mappingFieldsId = array();
433 while ($mappingFields->fetch()) {
434 if ($mappingFields->id
) {
435 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
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;
445 $updateMappingFields->name
= $mapper[$i];
446 $updateMappingFields->save();
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',
460 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
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;
467 $saveMappingFields->name
= $mapper[$i];
468 $saveMappingFields->save();
470 $this->set('savedMapping', $saveMappingFields->mapping_id
);
473 $parser = new CRM_Activity_Import_Parser_Activity($mapperKeysMain, $mapperLocType, $mapperPhoneType);
474 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
475 CRM_Import_Parser
::MODE_PREVIEW
478 // add all the necessary variables to the form