3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
37 * This class gets the name of the file to upload
39 class CRM_Event_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 asort($this->_mapperFields
);
50 unset($this->_mapperFields
['participant_is_test']);
51 $this->_columnCount
= $this->get('columnCount');
52 $this->assign('columnCount', $this->_columnCount
);
53 $this->_dataValues
= $this->get('dataValues');
54 $this->assign('dataValues', $this->_dataValues
);
56 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
57 $this->_onDuplicate
= $this->get('onDuplicate');
58 $highlightedFields = array();
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 if ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
70 $remove = array('participant_contact_id', 'email', 'first_name', 'last_name', 'external_identifier');
71 foreach ($remove as $value) {
72 unset($this->_mapperFields
[$value]);
74 $highlightedFieldsArray = array('participant_id', 'event_id', 'event_title', 'participant_status_id');
75 foreach ($highlightedFieldsArray as $name) {
76 $highlightedFields[] = $name;
79 elseif ($this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_SKIP ||
80 $this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_NOCHECK
82 unset($this->_mapperFields
['participant_id']);
83 $highlightedFieldsArray = array(
84 'participant_contact_id',
89 'external_identifier',
90 'participant_status_id',
92 foreach ($highlightedFieldsArray as $name) {
93 $highlightedFields[] = $name;
96 $this->assign('highlightedFields', $highlightedFields);
100 * Build the form object.
104 public function buildQuickForm() {
106 //to save the current mappings
107 if (!$this->get('savedMapping')) {
108 $saveDetailsName = ts('Save this field mapping');
109 $this->applyFilter('saveMappingName', 'trim');
110 $this->add('text', 'saveMappingName', ts('Name'));
111 $this->add('text', 'saveMappingDesc', ts('Description'));
114 $savedMapping = $this->get('savedMapping');
116 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping
::getMappingFields($savedMapping);
118 $mappingName = $mappingName[1];
119 $mappingContactType = $mappingContactType[1];
120 $mappingLocation = CRM_Utils_Array
::value('1', $mappingLocation);
121 $mappingPhoneType = CRM_Utils_Array
::value('1', $mappingPhoneType);
122 $mappingRelation = CRM_Utils_Array
::value('1', $mappingRelation);
124 //mapping is to be loaded from database
126 $params = array('id' => $savedMapping);
128 $mappingDetails = CRM_Core_BAO_Mapping
::retrieve($params, $temp);
130 $this->assign('loadedMapping', $mappingDetails->name
);
131 $this->set('loadedMapping', $savedMapping);
133 $getMappingName = new CRM_Core_DAO_Mapping();
134 $getMappingName->id
= $savedMapping;
135 $getMappingName->mapping_type
= 'Import Participants';
136 $getMappingName->find();
137 while ($getMappingName->fetch()) {
138 $mapperName = $getMappingName->name
;
141 $this->assign('savedName', $mapperName);
143 $this->add('hidden', 'mappingId', $savedMapping);
145 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
146 $saveDetailsName = ts('Save as a new field mapping');
147 $this->add('text', 'saveMappingName', ts('Name'));
148 $this->add('text', 'saveMappingDesc', ts('Description'));
151 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
153 $this->addFormRule(array('CRM_Event_Import_Form_MapField', 'formRule'), $this);
156 $mapperKeys = array_keys($this->_mapperFields
);
157 $hasHeaders = !empty($this->_columnHeaders
);
158 $headerPatterns = $this->get('headerPatterns');
159 $dataPatterns = $this->get('dataPatterns');
160 $hasLocationTypes = $this->get('fieldTypes');
161 /* Initialize all field usages to false */
163 foreach ($mapperKeys as $key) {
164 $this->_fieldUsed
[$key] = FALSE;
166 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
167 $sel1 = $this->_mapperFields
;
170 $js = "<script type='text/javascript'>\n";
171 $formName = 'document.forms.' . $this->_name
;
173 //used to warn for mismatch column count or mismatch mapping
175 for ($i = 0; $i < $this->_columnCount
; $i++
) {
176 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
178 if ($this->get('savedMapping')) {
179 if (isset($mappingName[$i])) {
180 if ($mappingName[$i] != ts('- do not import -')) {
182 $mappingHeader = array_keys($this->_mapperFields
, $mappingName[$i]);
184 if (!isset($locationId) ||
!$locationId) {
185 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
188 if (!isset($phoneType) ||
!$phoneType) {
189 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
192 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
193 $defaults["mapper[$i]"] = array(
195 (isset($locationId)) ?
$locationId : "",
196 (isset($phoneType)) ?
$phoneType : "",
201 $defaults["mapper[$i]"] = array();
204 for ($k = 1; $k < 4; $k++
) {
205 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
210 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
211 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
214 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders
[$i], $headerPatterns));
217 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
220 //end of load mapping
223 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
225 // Infer the default from the skipped headers if we have them
226 $defaults["mapper[$i]"] = array(
227 $this->defaultFromHeader($this->_columnHeaders
[$i],
230 // $defaultLocationType->id
235 // Otherwise guess the default from the form of the data
236 $defaults["mapper[$i]"] = array(
237 $this->defaultFromData($dataPatterns, $i),
238 // $defaultLocationType->id
243 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ?
$sel3 : "", (isset($sel4)) ?
$sel4 : ""));
245 $js .= "</script>\n";
246 $this->assign('initHideBoxes', $js);
248 //set warning if mismatch in more than
249 if (isset($mappingName)) {
250 if (($this->_columnCount
!= count($mappingName))) {
254 if ($warning != 0 && $this->get('savedMapping')) {
255 $session = CRM_Core_Session
::singleton();
256 $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.'));
259 $session = CRM_Core_Session
::singleton();
260 $session->setStatus(NULL);
263 $this->setDefaults($defaults);
265 $this->addButtons(array(
268 'name' => ts('Previous'),
272 'name' => ts('Continue'),
273 'spacing' => ' ',
278 'name' => ts('Cancel'),
285 * Global validation rules for the form.
287 * @param array $fields
288 * Posted values of the form.
294 * list of errors to be posted back to the form
296 public static function formRule($fields, $files, $self) {
298 // define so we avoid notices below
299 $errors['_qf_default'] = '';
300 $fieldMessage = NULL;
301 if (!array_key_exists('savedMapping', $fields)) {
302 $importKeys = array();
303 foreach ($fields['mapper'] as $mapperPart) {
304 $importKeys[] = $mapperPart[0];
306 // FIXME: should use the schema titles, not redeclare them
307 $requiredFields = array(
308 'participant_contact_id' => ts('Contact ID'),
309 'event_id' => ts('Event ID'),
312 $contactTypeId = $self->get('contactType');
313 $contactTypes = array(
314 CRM_Import_Parser
::CONTACT_INDIVIDUAL
=> 'Individual',
315 CRM_Import_Parser
::CONTACT_HOUSEHOLD
=> 'Household',
316 CRM_Import_Parser
::CONTACT_ORGANIZATION
=> 'Organization',
319 'used' => 'Unsupervised',
320 'contact_type' => $contactTypes[$contactTypeId],
322 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup
::dedupeRuleFieldsWeight($params);
324 foreach ($importKeys as $key => $val) {
325 if (array_key_exists($val, $ruleFields)) {
326 $weightSum +
= $ruleFields[$val];
329 foreach ($ruleFields as $field => $weight) {
330 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
333 foreach ($requiredFields as $field => $title) {
334 if (!in_array($field, $importKeys)) {
335 if ($field == 'participant_contact_id') {
336 if ($weightSum >= $threshold ||
in_array('external_identifier', $importKeys) ||
337 in_array('participant_id', $importKeys)
341 if ($self->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_UPDATE
) {
342 $errors['_qf_default'] .= ts('Missing required field: Provide Participant ID') . '<br />';
345 $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(
347 )) . ' ' . ts('Or Provide Contact ID or External ID.') . '<br />';
350 elseif (!in_array('event_title', $importKeys)) {
351 $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2',
352 array(1 => $title, 2 => 'Event Title')
359 if (!empty($fields['saveMapping'])) {
360 $nameField = CRM_Utils_Array
::value('saveMappingName', $fields);
361 if (empty($nameField)) {
362 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
365 if (CRM_Core_BAO_Mapping
::checkMapping($nameField, CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Participant'))) {
366 $errors['saveMappingName'] = ts('Duplicate Import Participant Mapping Name');
371 //display Error if loaded mapping is not selected
372 if (array_key_exists('loadMapping', $fields)) {
373 $getMapName = CRM_Utils_Array
::value('savedMapping', $fields);
374 if (empty($getMapName)) {
375 $errors['savedMapping'] = ts('Select saved mapping');
379 if (empty($errors['_qf_default'])) {
380 unset($errors['_qf_default']);
382 if (!empty($errors)) {
383 if (!empty($errors['saveMappingName'])) {
385 $assignError = new CRM_Core_Page();
386 $assignError->assign('mappingDetailsError', $_flag);
395 * Process the mapped fields and map it into the uploaded file
396 * preview the file and extract some summary statistics
400 public function postProcess() {
401 $params = $this->controller
->exportValues('MapField');
402 //reload the mapfield if load mapping is pressed
403 if (!empty($params['savedMapping'])) {
404 $this->set('savedMapping', $params['savedMapping']);
405 $this->controller
->resetPage($this->_name
);
409 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
410 $seperator = $this->controller
->exportValue('DataSource', 'fieldSeparator');
411 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
413 $mapperKeys = array();
415 $mapperKeys = $this->controller
->exportValue($this->_name
, 'mapper');
416 $mapperKeysMain = array();
418 for ($i = 0; $i < $this->_columnCount
; $i++
) {
419 $mapper[$i] = $this->_mapperFields
[$mapperKeys[$i][0]];
420 $mapperKeysMain[$i] = $mapperKeys[$i][0];
423 $this->set('mapper', $mapper);
425 // store mapping Id to display it in the preview page
426 $this->set('loadMappingId', CRM_Utils_Array
::value('mappingId', $params));
428 //Updating Mapping Records
429 if (!empty($params['updateMapping'])) {
431 $mappingFields = new CRM_Core_DAO_MappingField();
432 $mappingFields->mapping_id
= $params['mappingId'];
433 $mappingFields->find();
435 $mappingFieldsId = array();
436 while ($mappingFields->fetch()) {
437 if ($mappingFields->id
) {
438 $mappingFieldsId[$mappingFields->column_number
] = $mappingFields->id
;
442 for ($i = 0; $i < $this->_columnCount
; $i++
) {
443 $updateMappingFields = new CRM_Core_DAO_MappingField();
444 $updateMappingFields->id
= $mappingFieldsId[$i];
445 $updateMappingFields->mapping_id
= $params['mappingId'];
446 $updateMappingFields->column_number
= $i;
448 $explodedValues = explode('_', $mapperKeys[$i][0]);
449 $id = CRM_Utils_Array
::value(0, $explodedValues);
450 $first = CRM_Utils_Array
::value(1, $explodedValues);
451 $second = CRM_Utils_Array
::value(2, $explodedValues);
453 $updateMappingFields->name
= $mapper[$i];
454 $updateMappingFields->save();
458 //Saving Mapping Details and Records
459 if (!empty($params['saveMapping'])) {
460 $mappingParams = array(
461 'name' => $params['saveMappingName'],
462 'description' => $params['saveMappingDesc'],
463 'mapping_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Participant'),
465 $saveMapping = CRM_Core_BAO_Mapping
::add($mappingParams);
467 for ($i = 0; $i < $this->_columnCount
; $i++
) {
468 $saveMappingFields = new CRM_Core_DAO_MappingField();
469 $saveMappingFields->mapping_id
= $saveMapping->id
;
470 $saveMappingFields->column_number
= $i;
472 $explodedValues = explode('_', $mapperKeys[$i][0]);
473 $id = CRM_Utils_Array
::value(0, $explodedValues);
474 $first = CRM_Utils_Array
::value(1, $explodedValues);
475 $second = CRM_Utils_Array
::value(2, $explodedValues);
477 $saveMappingFields->name
= $mapper[$i];
478 $saveMappingFields->save();
480 $this->set('savedMapping', $saveMappingFields->mapping_id
);
483 $parser = new CRM_Event_Import_Parser_Participant($mapperKeysMain);
484 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
485 CRM_Import_Parser
::MODE_PREVIEW
, $this->get('contactType')
487 // add all the necessary variables to the form