3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class gets the name of the file to upload
39 class CRM_Event_Import_Form_MapField
extends CRM_Import_Form_MapField
{
45 * Function to set variables up before form is built
50 public function preProcess() {
51 $this->_mapperFields
= $this->get('fields');
52 asort($this->_mapperFields
);
53 unset($this->_mapperFields
['participant_is_test']);
54 $this->_columnCount
= $this->get('columnCount');
55 $this->assign('columnCount', $this->_columnCount
);
56 $this->_dataValues
= $this->get('dataValues');
57 $this->assign('dataValues', $this->_dataValues
);
59 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
60 $this->_onDuplicate
= $this->get('onDuplicate');
61 $highlightedFields = array();
62 if ($skipColumnHeader) {
63 $this->assign('skipColumnHeader', $skipColumnHeader);
64 $this->assign('rowDisplayCount', 3);
65 /* if we had a column header to skip, stash it for later */
67 $this->_columnHeaders
= $this->_dataValues
[0];
70 $this->assign('rowDisplayCount', 2);
72 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
73 $remove = array('participant_contact_id', 'email', 'first_name', 'last_name', 'external_identifier');
74 foreach ($remove as $value) {
75 unset($this->_mapperFields
[$value]);
77 $highlightedFieldsArray = array('participant_id', 'event_id', 'event_title', 'participant_status_id');
78 foreach ($highlightedFieldsArray as $name) {
79 $highlightedFields[] = $name;
82 elseif ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP ||
83 $this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_NOCHECK
85 unset($this->_mapperFields
['participant_id']);
86 $highlightedFieldsArray = array('participant_contact_id', 'event_id', 'email', 'first_name', 'last_name', 'external_identifier', 'participant_status_id');
87 foreach ($highlightedFieldsArray as $name) {
88 $highlightedFields[] = $name;
91 $this->assign('highlightedFields', $highlightedFields);
95 * Function to actually build the form
100 public function buildQuickForm() {
102 //to save the current mappings
103 if (!$this->get('savedMapping')) {
104 $saveDetailsName = ts('Save this field mapping');
105 $this->applyFilter('saveMappingName', 'trim');
106 $this->add('text', 'saveMappingName', ts('Name'));
107 $this->add('text', 'saveMappingDesc', ts('Description'));
110 $savedMapping = $this->get('savedMapping');
112 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
114 $mappingName = $mappingName[1];
115 $mappingContactType = $mappingContactType[1];
116 $mappingLocation = CRM_Utils_Array
::value('1', $mappingLocation);
117 $mappingPhoneType = CRM_Utils_Array
::value('1', $mappingPhoneType);
118 $mappingRelation = CRM_Utils_Array
::value('1', $mappingRelation);
120 //mapping is to be loaded from database
122 $params = array('id' => $savedMapping);
124 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
126 $this->assign('loadedMapping', $mappingDetails->name
);
127 $this->set('loadedMapping', $savedMapping);
129 $getMappingName = new CRM_Core_DAO_Mapping();
130 $getMappingName->id
= $savedMapping;
131 $getMappingName->mapping_type
= 'Import Participants';
132 $getMappingName->find();
133 while ($getMappingName->fetch()) {
134 $mapperName = $getMappingName->name
;
137 $this->assign('savedName', $mapperName);
139 $this->add('hidden', 'mappingId', $savedMapping);
141 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
142 $saveDetailsName = ts('Save as a new field mapping');
143 $this->add('text', 'saveMappingName', ts('Name'));
144 $this->add('text', 'saveMappingDesc', ts('Description'));
147 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
149 $this->addFormRule(array('CRM_Event_Import_Form_MapField', 'formRule'), $this);
152 $mapperKeys = array_keys($this->_mapperFields
);
153 $hasHeaders = !empty($this->_columnHeaders
);
154 $headerPatterns = $this->get('headerPatterns');
155 $dataPatterns = $this->get('dataPatterns');
156 $hasLocationTypes = $this->get('fieldTypes');
159 /* Initialize all field usages to false */
161 foreach ($mapperKeys as $key) {
162 $this->_fieldUsed
[$key] = FALSE;
164 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
165 $sel1 = $this->_mapperFields
;
168 $js = "<script type='text/javascript'>\n";
169 $formName = 'document.forms.' . $this->_name
;
171 //used to warn for mismatch column count or mismatch mapping
173 for ($i = 0; $i < $this->_columnCount
; $i++
) {
174 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
176 if ($this->get('savedMapping')) {
177 if (isset($mappingName[$i])) {
178 if ($mappingName[$i] != ts('- do not import -')) {
180 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
182 if (!isset($locationId) ||
!$locationId) {
183 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
186 if (!isset($phoneType) ||
!$phoneType) {
187 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
190 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
191 $defaults["mapper[$i]"] = array(
193 (isset($locationId)) ?
$locationId : "",
194 (isset($phoneType)) ?
$phoneType : "",
199 $defaults["mapper[$i]"] = array();
202 for ($k = 1; $k < 4; $k++
) {
203 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
208 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
209 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
212 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns));
215 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
218 //end of load mapping
221 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
223 // Infer the default from the skipped headers if we have them
224 $defaults["mapper[$i]"] = array(
225 $this->defaultFromHeader($this->_columnHeaders
[$i],
228 // $defaultLocationType->id
233 // Otherwise guess the default from the form of the data
234 $defaults["mapper[$i]"] = array(
235 $this->defaultFromData($dataPatterns, $i),
236 // $defaultLocationType->id
241 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ?
$sel3 : "", (isset($sel4)) ?
$sel4 : ""));
243 $js .= "</script>\n";
244 $this->assign('initHideBoxes', $js);
246 //set warning if mismatch in more than
247 if (isset($mappingName)) {
248 if (($this->_columnCount
!= count($mappingName))) {
252 if ($warning != 0 && $this->get('savedMapping')) {
253 $session = CRM_Core_Session
::singleton();
254 $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.'));
257 $session = CRM_Core_Session
::singleton();
258 $session->setStatus(NULL);
261 $this->setDefaults($defaults);
263 $this->addButtons(array(
266 'name' => ts('<< Previous'),
270 'name' => ts('Continue >>'),
271 'spacing' => ' ',
276 'name' => ts('Cancel'),
283 * global validation rules for the form
285 * @param array $fields posted values of the form
290 * @return array list of errors to be posted back to the form
294 static function formRule($fields, $files, $self) {
296 $fieldMessage = NULL;
297 if (!array_key_exists('savedMapping', $fields)) {
298 $importKeys = array();
299 foreach ($fields['mapper'] as $mapperPart) {
300 $importKeys[] = $mapperPart[0];
302 // FIXME: should use the schema titles, not redeclare them
303 $requiredFields = array(
304 'participant_contact_id' => ts('Contact ID'),
305 'event_id' => ts('Event ID'),
308 $contactTypeId = $self->get('contactType');
309 $contactTypes = array(
310 CRM_Import_Parser
::CONTACT_INDIVIDUAL
=> 'Individual',
311 CRM_Import_Parser
::CONTACT_HOUSEHOLD
=> 'Household',
312 CRM_Import_Parser
::CONTACT_ORGANIZATION
=> 'Organization',
315 'used' => 'Unsupervised',
316 'contact_type' => $contactTypes[$contactTypeId],
318 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
320 foreach ($importKeys as $key => $val) {
321 if (array_key_exists($val, $ruleFields)) {
322 $weightSum +
= $ruleFields[$val];
325 foreach ($ruleFields as $field => $weight) {
326 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
329 foreach ($requiredFields as $field => $title) {
330 if (!in_array($field, $importKeys)) {
331 if ($field == 'participant_contact_id') {
332 if ($weightSum >= $threshold ||
in_array('external_identifier', $importKeys) ||
333 in_array('participant_id', $importKeys)
337 if ($self->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
338 $errors['_qf_default'] .= ts('Missing required field: Provide Particiapnt ID') . '<br />';
341 $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $fieldMessage " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array(
342 1 => $threshold)) . ' ' . ts('Or Provide Contact ID or External ID.') . '<br />';
345 elseif (!in_array('event_title', $importKeys)) {
346 $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2',
347 array(1 => $title, 2 => 'Event Title')
354 if (!empty($fields['saveMapping'])) {
355 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
356 if (empty($nameField)) {
357 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
360 $mappingTypeId = CRM_Core_OptionGroup
::getValue('mapping_type', 'Import Participant', 'name');
361 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, $mappingTypeId)) {
362 $errors['saveMappingName'] = ts('Duplicate Import Participant Mapping Name');
367 //display Error if loaded mapping is not selected
368 if (array_key_exists('loadMapping', $fields)) {
369 $getMapName = CRM_Utils_Array
::value('savedMapping', $fields);
370 if (empty($getMapName)) {
371 $errors['savedMapping'] = ts('Select saved mapping');
375 if (!empty($errors)) {
376 if (!empty($errors['saveMappingName'])) {
378 $assignError = new CRM_Core_Page();
379 $assignError->assign('mappingDetailsError', $_flag);
388 * Process the mapped fields and map it into the uploaded file
389 * preview the file and extract some summary statistics
394 public function postProcess() {
395 $params = $this->controller
->exportValues('MapField');
396 //reload the mapfield if load mapping is pressed
397 if (!empty($params['savedMapping'])) {
398 $this->set('savedMapping', $params['savedMapping']);
399 $this->controller
->resetPage($this->_name
);
403 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
404 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
406 $config = CRM_Core_Config
::singleton();
407 $seperator = $config->fieldSeparator
;
409 $mapperKeys = array();
411 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
412 $mapperKeysMain = array();
414 for ($i = 0; $i < $this->_columnCount
; $i++
) {
415 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
416 $mapperKeysMain[$i] = $mapperKeys[$i][0];
419 $this->set('mapper', $mapper);
421 // store mapping Id to display it in the preview page
422 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
424 //Updating Mapping Records
425 if (!empty($params['updateMapping'])) {
427 $mappingFields = new CRM_Core_DAO_MappingField();
428 $mappingFields->mapping_id
= $params['mappingId'];
429 $mappingFields->find();
431 $mappingFieldsId = array();
432 while ($mappingFields->fetch()) {
433 if ($mappingFields->id
) {
434 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
438 for ($i = 0; $i < $this->_columnCount
; $i++
) {
439 $updateMappingFields = new CRM_Core_DAO_MappingField();
440 $updateMappingFields->id
= $mappingFieldsId[$i];
441 $updateMappingFields->mapping_id
= $params['mappingId'];
442 $updateMappingFields->column_number
= $i;
444 $explodedValues = explode('_', $mapperKeys[$i][0]);
445 $id = CRM_Utils_Array
::value(0, $explodedValues);
446 $first = CRM_Utils_Array
::value(1, $explodedValues);
447 $second = CRM_Utils_Array
::value(2, $explodedValues);
449 $updateMappingFields->name
= $mapper[$i];
450 $updateMappingFields->save();
454 //Saving Mapping Details and Records
455 if (!empty($params['saveMapping'])) {
456 $mappingParams = array(
457 'name' => $params['saveMappingName'],
458 'description' => $params['saveMappingDesc'],
459 'mapping_type_id' => CRM_Core_OptionGroup
::getValue('mapping_type',
460 'Import Participant',
464 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
466 for ($i = 0; $i < $this->_columnCount
; $i++
) {
467 $saveMappingFields = new CRM_Core_DAO_MappingField();
468 $saveMappingFields->mapping_id
= $saveMapping->id
;
469 $saveMappingFields->column_number
= $i;
471 $explodedValues = explode('_', $mapperKeys[$i][0]);
472 $id = CRM_Utils_Array
::value(0, $explodedValues);
473 $first = CRM_Utils_Array
::value(1, $explodedValues);
474 $second = CRM_Utils_Array
::value(2, $explodedValues);
476 $saveMappingFields->name
= $mapper[$i];
477 $saveMappingFields->save();
479 $this->set('savedMapping', $saveMappingFields->mapping_id
);
482 $parser = new CRM_Event_Import_Parser_Participant($mapperKeysMain);
483 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
484 CRM_Import_Parser
::MODE_PREVIEW
, $this->get('contactType')
486 // add all the necessary variables to the form