Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.6 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2015 | | |
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-2015 | |
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 | * Set variables up before form is built. | |
44 | * | |
45 | * @return void | |
46 | */ | |
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); | |
55 | ||
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 */ | |
63 | ||
64 | $this->_columnHeaders = $this->_dataValues[0]; | |
65 | } | |
66 | else { | |
67 | $this->assign('rowDisplayCount', 2); | |
68 | } | |
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]); | |
73 | } | |
74 | $highlightedFieldsArray = array('participant_id', 'event_id', 'event_title', 'participant_status_id'); | |
75 | foreach ($highlightedFieldsArray as $name) { | |
76 | $highlightedFields[] = $name; | |
77 | } | |
78 | } | |
79 | elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP || | |
80 | $this->_onDuplicate == CRM_Import_Parser::DUPLICATE_NOCHECK | |
81 | ) { | |
82 | unset($this->_mapperFields['participant_id']); | |
83 | $highlightedFieldsArray = array( | |
84 | 'participant_contact_id', | |
85 | 'event_id', | |
86 | 'email', | |
87 | 'first_name', | |
88 | 'last_name', | |
89 | 'external_identifier', | |
90 | 'participant_status_id', | |
91 | ); | |
92 | foreach ($highlightedFieldsArray as $name) { | |
93 | $highlightedFields[] = $name; | |
94 | } | |
95 | } | |
96 | $this->assign('highlightedFields', $highlightedFields); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Build the form object. | |
101 | * | |
102 | * @return void | |
103 | */ | |
104 | public function buildQuickForm() { | |
105 | ||
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')); | |
112 | } | |
113 | else { | |
114 | $savedMapping = $this->get('savedMapping'); | |
115 | ||
116 | list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping); | |
117 | ||
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); | |
123 | ||
124 | //mapping is to be loaded from database | |
125 | ||
126 | $params = array('id' => $savedMapping); | |
127 | $temp = array(); | |
128 | $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp); | |
129 | ||
130 | $this->assign('loadedMapping', $mappingDetails->name); | |
131 | $this->set('loadedMapping', $savedMapping); | |
132 | ||
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; | |
139 | } | |
140 | ||
141 | $this->assign('savedName', $mapperName); | |
142 | ||
143 | $this->add('hidden', 'mappingId', $savedMapping); | |
144 | ||
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')); | |
149 | } | |
150 | ||
151 | $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)")); | |
152 | ||
153 | $this->addFormRule(array('CRM_Event_Import_Form_MapField', 'formRule'), $this); | |
154 | ||
155 | $defaults = array(); | |
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 */ | |
162 | ||
163 | foreach ($mapperKeys as $key) { | |
164 | $this->_fieldUsed[$key] = FALSE; | |
165 | } | |
166 | $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); | |
167 | $sel1 = $this->_mapperFields; | |
168 | ||
169 | $sel2[''] = NULL; | |
170 | $js = "<script type='text/javascript'>\n"; | |
171 | $formName = 'document.forms.' . $this->_name; | |
172 | ||
173 | //used to warn for mismatch column count or mismatch mapping | |
174 | $warning = 0; | |
175 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
176 | $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL); | |
177 | $jsSet = FALSE; | |
178 | if ($this->get('savedMapping')) { | |
179 | if (isset($mappingName[$i])) { | |
180 | if ($mappingName[$i] != ts('- do not import -')) { | |
181 | ||
182 | $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]); | |
183 | ||
184 | if (!isset($locationId) || !$locationId) { | |
185 | $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n"; | |
186 | } | |
187 | ||
188 | if (!isset($phoneType) || !$phoneType) { | |
189 | $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n"; | |
190 | } | |
191 | ||
192 | $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n"; | |
193 | $defaults["mapper[$i]"] = array( | |
194 | $mappingHeader[0], | |
195 | (isset($locationId)) ? $locationId : "", | |
196 | (isset($phoneType)) ? $phoneType : "", | |
197 | ); | |
198 | $jsSet = TRUE; | |
199 | } | |
200 | else { | |
201 | $defaults["mapper[$i]"] = array(); | |
202 | } | |
203 | if (!$jsSet) { | |
204 | for ($k = 1; $k < 4; $k++) { | |
205 | $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n"; | |
206 | } | |
207 | } | |
208 | } | |
209 | else { | |
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"; | |
212 | ||
213 | if ($hasHeaders) { | |
214 | $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns)); | |
215 | } | |
216 | else { | |
217 | $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i)); | |
218 | } | |
219 | } | |
220 | //end of load mapping | |
221 | } | |
222 | else { | |
223 | $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n"; | |
224 | if ($hasHeaders) { | |
225 | // Infer the default from the skipped headers if we have them | |
226 | $defaults["mapper[$i]"] = array( | |
227 | $this->defaultFromHeader($this->_columnHeaders[$i], | |
228 | $headerPatterns | |
229 | ), | |
230 | // $defaultLocationType->id | |
231 | 0, | |
232 | ); | |
233 | } | |
234 | else { | |
235 | // Otherwise guess the default from the form of the data | |
236 | $defaults["mapper[$i]"] = array( | |
237 | $this->defaultFromData($dataPatterns, $i), | |
238 | // $defaultLocationType->id | |
239 | 0, | |
240 | ); | |
241 | } | |
242 | } | |
243 | $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ? $sel3 : "", (isset($sel4)) ? $sel4 : "")); | |
244 | } | |
245 | $js .= "</script>\n"; | |
246 | $this->assign('initHideBoxes', $js); | |
247 | ||
248 | //set warning if mismatch in more than | |
249 | if (isset($mappingName)) { | |
250 | if (($this->_columnCount != count($mappingName))) { | |
251 | $warning++; | |
252 | } | |
253 | } | |
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.')); | |
257 | } | |
258 | else { | |
259 | $session = CRM_Core_Session::singleton(); | |
260 | $session->setStatus(NULL); | |
261 | } | |
262 | ||
263 | $this->setDefaults($defaults); | |
264 | ||
265 | $this->addButtons(array( | |
266 | array( | |
267 | 'type' => 'back', | |
268 | 'name' => ts('Previous'), | |
269 | ), | |
270 | array( | |
271 | 'type' => 'next', | |
272 | 'name' => ts('Continue'), | |
273 | 'spacing' => ' ', | |
274 | 'isDefault' => TRUE, | |
275 | ), | |
276 | array( | |
277 | 'type' => 'cancel', | |
278 | 'name' => ts('Cancel'), | |
279 | ), | |
280 | ) | |
281 | ); | |
282 | } | |
283 | ||
284 | /** | |
285 | * Global validation rules for the form. | |
286 | * | |
287 | * @param array $fields | |
288 | * Posted values of the form. | |
289 | * | |
290 | * @param $files | |
291 | * @param $self | |
292 | * | |
293 | * @return array | |
294 | * list of errors to be posted back to the form | |
295 | */ | |
296 | public static function formRule($fields, $files, $self) { | |
297 | $errors = array(); | |
298 | $fieldMessage = NULL; | |
299 | if (!array_key_exists('savedMapping', $fields)) { | |
300 | $importKeys = array(); | |
301 | foreach ($fields['mapper'] as $mapperPart) { | |
302 | $importKeys[] = $mapperPart[0]; | |
303 | } | |
304 | // FIXME: should use the schema titles, not redeclare them | |
305 | $requiredFields = array( | |
306 | 'participant_contact_id' => ts('Contact ID'), | |
307 | 'event_id' => ts('Event ID'), | |
308 | ); | |
309 | ||
310 | $contactTypeId = $self->get('contactType'); | |
311 | $contactTypes = array( | |
312 | CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', | |
313 | CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', | |
314 | CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization', | |
315 | ); | |
316 | $params = array( | |
317 | 'used' => 'Unsupervised', | |
318 | 'contact_type' => $contactTypes[$contactTypeId], | |
319 | ); | |
320 | list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params); | |
321 | $weightSum = 0; | |
322 | foreach ($importKeys as $key => $val) { | |
323 | if (array_key_exists($val, $ruleFields)) { | |
324 | $weightSum += $ruleFields[$val]; | |
325 | } | |
326 | } | |
327 | foreach ($ruleFields as $field => $weight) { | |
328 | $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; | |
329 | } | |
330 | ||
331 | foreach ($requiredFields as $field => $title) { | |
332 | if (!in_array($field, $importKeys)) { | |
333 | if ($field == 'participant_contact_id') { | |
334 | if ($weightSum >= $threshold || in_array('external_identifier', $importKeys) || | |
335 | in_array('participant_id', $importKeys) | |
336 | ) { | |
337 | continue; | |
338 | } | |
339 | if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { | |
340 | $errors['_qf_default'] .= ts('Missing required field: Provide Participant ID') . '<br />'; | |
341 | } | |
342 | else { | |
343 | $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( | |
344 | 1 => $threshold, | |
345 | )) . ' ' . ts('Or Provide Contact ID or External ID.') . '<br />'; | |
346 | } | |
347 | } | |
348 | elseif (!in_array('event_title', $importKeys)) { | |
349 | $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2', | |
350 | array(1 => $title, 2 => 'Event Title') | |
351 | ) . '<br />'; | |
352 | } | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | if (!empty($fields['saveMapping'])) { | |
358 | $nameField = CRM_Utils_Array::value('saveMappingName', $fields); | |
359 | if (empty($nameField)) { | |
360 | $errors['saveMappingName'] = ts('Name is required to save Import Mapping'); | |
361 | } | |
362 | else { | |
363 | $mappingTypeId = CRM_Core_OptionGroup::getValue('mapping_type', 'Import Participant', 'name'); | |
364 | if (CRM_Core_BAO_Mapping::checkMapping($nameField, $mappingTypeId)) { | |
365 | $errors['saveMappingName'] = ts('Duplicate Import Participant Mapping Name'); | |
366 | } | |
367 | } | |
368 | } | |
369 | ||
370 | //display Error if loaded mapping is not selected | |
371 | if (array_key_exists('loadMapping', $fields)) { | |
372 | $getMapName = CRM_Utils_Array::value('savedMapping', $fields); | |
373 | if (empty($getMapName)) { | |
374 | $errors['savedMapping'] = ts('Select saved mapping'); | |
375 | } | |
376 | } | |
377 | ||
378 | if (!empty($errors)) { | |
379 | if (!empty($errors['saveMappingName'])) { | |
380 | $_flag = 1; | |
381 | $assignError = new CRM_Core_Page(); | |
382 | $assignError->assign('mappingDetailsError', $_flag); | |
383 | } | |
384 | return $errors; | |
385 | } | |
386 | ||
387 | return TRUE; | |
388 | } | |
389 | ||
390 | /** | |
391 | * Process the mapped fields and map it into the uploaded file | |
392 | * preview the file and extract some summary statistics | |
393 | * | |
394 | * @return void | |
395 | */ | |
396 | public function postProcess() { | |
397 | $params = $this->controller->exportValues('MapField'); | |
398 | //reload the mapfield if load mapping is pressed | |
399 | if (!empty($params['savedMapping'])) { | |
400 | $this->set('savedMapping', $params['savedMapping']); | |
401 | $this->controller->resetPage($this->_name); | |
402 | return; | |
403 | } | |
404 | ||
405 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); | |
406 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); | |
407 | ||
408 | $config = CRM_Core_Config::singleton(); | |
409 | $seperator = $config->fieldSeparator; | |
410 | ||
411 | $mapperKeys = array(); | |
412 | $mapper = array(); | |
413 | $mapperKeys = $this->controller->exportValue($this->_name, 'mapper'); | |
414 | $mapperKeysMain = array(); | |
415 | ||
416 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
417 | $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]]; | |
418 | $mapperKeysMain[$i] = $mapperKeys[$i][0]; | |
419 | } | |
420 | ||
421 | $this->set('mapper', $mapper); | |
422 | ||
423 | // store mapping Id to display it in the preview page | |
424 | $this->set('loadMappingId', CRM_Utils_Array::value('mappingId', $params)); | |
425 | ||
426 | //Updating Mapping Records | |
427 | if (!empty($params['updateMapping'])) { | |
428 | ||
429 | $mappingFields = new CRM_Core_DAO_MappingField(); | |
430 | $mappingFields->mapping_id = $params['mappingId']; | |
431 | $mappingFields->find(); | |
432 | ||
433 | $mappingFieldsId = array(); | |
434 | while ($mappingFields->fetch()) { | |
435 | if ($mappingFields->id) { | |
436 | $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id; | |
437 | } | |
438 | } | |
439 | ||
440 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
441 | $updateMappingFields = new CRM_Core_DAO_MappingField(); | |
442 | $updateMappingFields->id = $mappingFieldsId[$i]; | |
443 | $updateMappingFields->mapping_id = $params['mappingId']; | |
444 | $updateMappingFields->column_number = $i; | |
445 | ||
446 | $explodedValues = explode('_', $mapperKeys[$i][0]); | |
447 | $id = CRM_Utils_Array::value(0, $explodedValues); | |
448 | $first = CRM_Utils_Array::value(1, $explodedValues); | |
449 | $second = CRM_Utils_Array::value(2, $explodedValues); | |
450 | ||
451 | $updateMappingFields->name = $mapper[$i]; | |
452 | $updateMappingFields->save(); | |
453 | } | |
454 | } | |
455 | ||
456 | //Saving Mapping Details and Records | |
457 | if (!empty($params['saveMapping'])) { | |
458 | $mappingParams = array( | |
459 | 'name' => $params['saveMappingName'], | |
460 | 'description' => $params['saveMappingDesc'], | |
461 | 'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type', | |
462 | 'Import Participant', | |
463 | 'name' | |
464 | ), | |
465 | ); | |
466 | $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams); | |
467 | ||
468 | for ($i = 0; $i < $this->_columnCount; $i++) { | |
469 | $saveMappingFields = new CRM_Core_DAO_MappingField(); | |
470 | $saveMappingFields->mapping_id = $saveMapping->id; | |
471 | $saveMappingFields->column_number = $i; | |
472 | ||
473 | $explodedValues = explode('_', $mapperKeys[$i][0]); | |
474 | $id = CRM_Utils_Array::value(0, $explodedValues); | |
475 | $first = CRM_Utils_Array::value(1, $explodedValues); | |
476 | $second = CRM_Utils_Array::value(2, $explodedValues); | |
477 | ||
478 | $saveMappingFields->name = $mapper[$i]; | |
479 | $saveMappingFields->save(); | |
480 | } | |
481 | $this->set('savedMapping', $saveMappingFields->mapping_id); | |
482 | } | |
483 | ||
484 | $parser = new CRM_Event_Import_Parser_Participant($mapperKeysMain); | |
485 | $parser->run($fileName, $seperator, $mapper, $skipColumnHeader, | |
486 | CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType') | |
487 | ); | |
488 | // add all the necessary variables to the form | |
489 | $parser->set($this); | |
490 | } | |
491 | ||
492 | } |