Merge pull request #4621 from atif-shaikh/CRM-15589
[civicrm-core.git] / CRM / Event / Import / Form / MapField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class gets the name of the file to upload
38 */
39 class CRM_Event_Import_Form_MapField extends CRM_Import_Form_MapField {
40
41
42
43
44 /**
45 * set variables up before form is built
46 *
47 * @return void
48 * @access public
49 */
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);
58
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 */
66
67 $this->_columnHeaders = $this->_dataValues[0];
68 }
69 else {
70 $this->assign('rowDisplayCount', 2);
71 }
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]);
76 }
77 $highlightedFieldsArray = array('participant_id', 'event_id', 'event_title', 'participant_status_id');
78 foreach ($highlightedFieldsArray as $name) {
79 $highlightedFields[] = $name;
80 }
81 }
82 elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP ||
83 $this->_onDuplicate == CRM_Import_Parser::DUPLICATE_NOCHECK
84 ) {
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;
89 }
90 }
91 $this->assign('highlightedFields', $highlightedFields);
92 }
93
94 /**
95 * Build the form object
96 *
97 * @return void
98 * @access public
99 */
100 public function buildQuickForm() {
101
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'));
108 }
109 else {
110 $savedMapping = $this->get('savedMapping');
111
112 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping);
113
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);
119
120 //mapping is to be loaded from database
121
122 $params = array('id' => $savedMapping);
123 $temp = array();
124 $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp);
125
126 $this->assign('loadedMapping', $mappingDetails->name);
127 $this->set('loadedMapping', $savedMapping);
128
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;
135 }
136
137 $this->assign('savedName', $mapperName);
138
139 $this->add('hidden', 'mappingId', $savedMapping);
140
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'));
145 }
146
147 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
148
149 $this->addFormRule(array('CRM_Event_Import_Form_MapField', 'formRule'), $this);
150
151 $defaults = array();
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');
157
158
159 /* Initialize all field usages to false */
160
161 foreach ($mapperKeys as $key) {
162 $this->_fieldUsed[$key] = FALSE;
163 }
164 $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
165 $sel1 = $this->_mapperFields;
166
167 $sel2[''] = NULL;
168 $js = "<script type='text/javascript'>\n";
169 $formName = 'document.forms.' . $this->_name;
170
171 //used to warn for mismatch column count or mismatch mapping
172 $warning = 0;
173 for ($i = 0; $i < $this->_columnCount; $i++) {
174 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
175 $jsSet = FALSE;
176 if ($this->get('savedMapping')) {
177 if (isset($mappingName[$i])) {
178 if ($mappingName[$i] != ts('- do not import -')) {
179
180 $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]);
181
182 if (!isset($locationId) || !$locationId) {
183 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
184 }
185
186 if (!isset($phoneType) || !$phoneType) {
187 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
188 }
189
190 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
191 $defaults["mapper[$i]"] = array(
192 $mappingHeader[0],
193 (isset($locationId)) ? $locationId : "",
194 (isset($phoneType)) ? $phoneType : "",
195 );
196 $jsSet = TRUE;
197 }
198 else {
199 $defaults["mapper[$i]"] = array();
200 }
201 if (!$jsSet) {
202 for ($k = 1; $k < 4; $k++) {
203 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
204 }
205 }
206 }
207 else {
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";
210
211 if ($hasHeaders) {
212 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns));
213 }
214 else {
215 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
216 }
217 }
218 //end of load mapping
219 }
220 else {
221 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
222 if ($hasHeaders) {
223 // Infer the default from the skipped headers if we have them
224 $defaults["mapper[$i]"] = array(
225 $this->defaultFromHeader($this->_columnHeaders[$i],
226 $headerPatterns
227 ),
228 // $defaultLocationType->id
229 0,
230 );
231 }
232 else {
233 // Otherwise guess the default from the form of the data
234 $defaults["mapper[$i]"] = array(
235 $this->defaultFromData($dataPatterns, $i),
236 // $defaultLocationType->id
237 0,
238 );
239 }
240 }
241 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ? $sel3 : "", (isset($sel4)) ? $sel4 : ""));
242 }
243 $js .= "</script>\n";
244 $this->assign('initHideBoxes', $js);
245
246 //set warning if mismatch in more than
247 if (isset($mappingName)) {
248 if (($this->_columnCount != count($mappingName))) {
249 $warning++;
250 }
251 }
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.'));
255 }
256 else {
257 $session = CRM_Core_Session::singleton();
258 $session->setStatus(NULL);
259 }
260
261 $this->setDefaults($defaults);
262
263 $this->addButtons(array(
264 array(
265 'type' => 'back',
266 'name' => ts('<< Previous'),
267 ),
268 array(
269 'type' => 'next',
270 'name' => ts('Continue >>'),
271 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
272 'isDefault' => TRUE,
273 ),
274 array(
275 'type' => 'cancel',
276 'name' => ts('Cancel'),
277 ),
278 )
279 );
280 }
281
282 /**
283 * global validation rules for the form
284 *
285 * @param array $fields posted values of the form
286 *
287 * @param $files
288 * @param $self
289 *
290 * @return array list of errors to be posted back to the form
291 * @static
292 * @access public
293 */
294 static function formRule($fields, $files, $self) {
295 $errors = array();
296 $fieldMessage = NULL;
297 if (!array_key_exists('savedMapping', $fields)) {
298 $importKeys = array();
299 foreach ($fields['mapper'] as $mapperPart) {
300 $importKeys[] = $mapperPart[0];
301 }
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'),
306 );
307
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',
313 );
314 $params = array(
315 'used' => 'Unsupervised',
316 'contact_type' => $contactTypes[$contactTypeId],
317 );
318 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params);
319 $weightSum = 0;
320 foreach ($importKeys as $key => $val) {
321 if (array_key_exists($val, $ruleFields)) {
322 $weightSum += $ruleFields[$val];
323 }
324 }
325 foreach ($ruleFields as $field => $weight) {
326 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
327 }
328
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)
334 ) {
335 continue;
336 }
337 if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
338 $errors['_qf_default'] .= ts('Missing required field: Provide Participant ID') . '<br />';
339 }
340 else {
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 />';
343 }
344 }
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')
348 ) . '<br />';
349 }
350 }
351 }
352 }
353
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');
358 }
359 else {
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');
363 }
364 }
365 }
366
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');
372 }
373 }
374
375 if (!empty($errors)) {
376 if (!empty($errors['saveMappingName'])) {
377 $_flag = 1;
378 $assignError = new CRM_Core_Page();
379 $assignError->assign('mappingDetailsError', $_flag);
380 }
381 return $errors;
382 }
383
384 return TRUE;
385 }
386
387 /**
388 * Process the mapped fields and map it into the uploaded file
389 * preview the file and extract some summary statistics
390 *
391 * @return void
392 * @access public
393 */
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);
400 return;
401 }
402
403 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
404 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
405
406 $config = CRM_Core_Config::singleton();
407 $seperator = $config->fieldSeparator;
408
409 $mapperKeys = array();
410 $mapper = array();
411 $mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
412 $mapperKeysMain = array();
413
414 for ($i = 0; $i < $this->_columnCount; $i++) {
415 $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]];
416 $mapperKeysMain[$i] = $mapperKeys[$i][0];
417 }
418
419 $this->set('mapper', $mapper);
420
421 // store mapping Id to display it in the preview page
422 $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params));
423
424 //Updating Mapping Records
425 if (!empty($params['updateMapping'])) {
426
427 $mappingFields = new CRM_Core_DAO_MappingField();
428 $mappingFields->mapping_id = $params['mappingId'];
429 $mappingFields->find();
430
431 $mappingFieldsId = array();
432 while ($mappingFields->fetch()) {
433 if ($mappingFields->id) {
434 $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id;
435 }
436 }
437
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;
443
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);
448
449 $updateMappingFields->name = $mapper[$i];
450 $updateMappingFields->save();
451 }
452 }
453
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',
461 'name'
462 ),
463 );
464 $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
465
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;
470
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);
475
476 $saveMappingFields->name = $mapper[$i];
477 $saveMappingFields->save();
478 }
479 $this->set('savedMapping', $saveMappingFields->mapping_id);
480 }
481
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')
485 );
486 // add all the necessary variables to the form
487 $parser->set($this);
488 }
489 }
490