Merge pull request #11735 from mukeshcompucorp/CRM-21814-add-proper-container-to...
[civicrm-core.git] / CRM / Member / Import / Form / MapField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 * $Id$
33 *
34 */
35
36 /**
37 * This class gets the name of the file to upload
38 */
39 class CRM_Member_Import_Form_MapField extends CRM_Import_Form_MapField {
40
41
42 /**
43 * store contactType.
44 *
45 * @var int
46 */
47 static $_contactType = NULL;
48
49
50 /**
51 * Set variables up before form is built.
52 *
53 * @return void
54 */
55 public function preProcess() {
56 $this->_mapperFields = $this->get('fields');
57 asort($this->_mapperFields);
58
59 $this->_columnCount = $this->get('columnCount');
60 $this->assign('columnCount', $this->_columnCount);
61 $this->_dataValues = $this->get('dataValues');
62 $this->assign('dataValues', $this->_dataValues);
63
64 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
65 $this->_onDuplicate = $this->get('onDuplicate', isset($onDuplicate) ? $onDuplicate : "");
66
67 $highlightedFields = array();
68 if ($skipColumnHeader) {
69 $this->assign('skipColumnHeader', $skipColumnHeader);
70 $this->assign('rowDisplayCount', 3);
71 /* if we had a column header to skip, stash it for later */
72
73 $this->_columnHeaders = $this->_dataValues[0];
74 }
75 else {
76 $this->assign('rowDisplayCount', 2);
77 }
78
79 //CRM-2219 removing other required fields since for updation only
80 //membership id is required.
81 if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
82 $remove = array('membership_contact_id', 'email', 'first_name', 'last_name', 'external_identifier');
83 foreach ($remove as $value) {
84 unset($this->_mapperFields[$value]);
85 }
86 $highlightedFieldsArray = array('membership_id', 'membership_start_date', 'membership_type_id');
87 foreach ($highlightedFieldsArray as $name) {
88 $highlightedFields[] = $name;
89 }
90 }
91 elseif ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) {
92 unset($this->_mapperFields['membership_id']);
93 $highlightedFieldsArray = array(
94 'membership_contact_id',
95 'email',
96 'external_identifier',
97 'membership_start_date',
98 'membership_type_id',
99 );
100 foreach ($highlightedFieldsArray as $name) {
101 $highlightedFields[] = $name;
102 }
103 }
104
105 // modify field title
106 $this->_mapperFields['status_id'] = ts('Membership Status');
107 $this->_mapperFields['membership_type_id'] = ts('Membership Type');
108
109 self::$_contactType = $this->get('contactType');
110 $this->assign('highlightedFields', $highlightedFields);
111 }
112
113 /**
114 * Build the form object.
115 *
116 * @return void
117 */
118 public function buildQuickForm() {
119 //to save the current mappings
120 if (!$this->get('savedMapping')) {
121 $saveDetailsName = ts('Save this field mapping');
122 $this->applyFilter('saveMappingName', 'trim');
123 $this->add('text', 'saveMappingName', ts('Name'));
124 $this->add('text', 'saveMappingDesc', ts('Description'));
125 }
126 else {
127 $savedMapping = $this->get('savedMapping');
128
129 list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingRelation) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping);
130
131 $mappingName = $mappingName[1];
132 $mappingContactType = $mappingContactType[1];
133 $mappingLocation = CRM_Utils_Array::value('1', $mappingLocation);
134 $mappingPhoneType = CRM_Utils_Array::value('1', $mappingPhoneType);
135 $mappingRelation = CRM_Utils_Array::value('1', $mappingRelation);
136
137 //mapping is to be loaded from database
138
139 $params = array('id' => $savedMapping);
140 $temp = array();
141 $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp);
142
143 $this->assign('loadedMapping', $mappingDetails->name);
144 $this->set('loadedMapping', $savedMapping);
145
146 $getMappingName = new CRM_Core_DAO_Mapping();
147 $getMappingName->id = $savedMapping;
148 $getMappingName->mapping_type = 'Import Memberships';
149 $getMappingName->find();
150 while ($getMappingName->fetch()) {
151 $mapperName = $getMappingName->name;
152 }
153
154 $this->assign('savedName', $mapperName);
155
156 $this->add('hidden', 'mappingId', $savedMapping);
157
158 $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL);
159 $saveDetailsName = ts('Save as a new field mapping');
160 $this->add('text', 'saveMappingName', ts('Name'));
161 $this->add('text', 'saveMappingDesc', ts('Description'));
162 }
163
164 $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)"));
165
166 $this->addFormRule(array('CRM_Member_Import_Form_MapField', 'formRule'), $this);
167
168 //-------- end of saved mapping stuff ---------
169
170 $defaults = array();
171 $mapperKeys = array_keys($this->_mapperFields);
172 $hasHeaders = !empty($this->_columnHeaders);
173 $headerPatterns = $this->get('headerPatterns');
174 $dataPatterns = $this->get('dataPatterns');
175 $hasLocationTypes = $this->get('fieldTypes');
176
177 /* Initialize all field usages to false */
178
179 foreach ($mapperKeys as $key) {
180 $this->_fieldUsed[$key] = FALSE;
181 }
182 $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
183 $sel1 = $this->_mapperFields;
184 if (!$this->get('onDuplicate')) {
185 unset($sel1['id']);
186 unset($sel1['membership_id']);
187 }
188
189 $sel2[''] = NULL;
190
191 $js = "<script type='text/javascript'>\n";
192 $formName = 'document.forms.' . $this->_name;
193
194 //used to warn for mismatch column count or mismatch mapping
195 $warning = 0;
196
197 for ($i = 0; $i < $this->_columnCount; $i++) {
198 $sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', array(1 => $i)), NULL);
199 $jsSet = FALSE;
200 if ($this->get('savedMapping')) {
201 if (isset($mappingName[$i])) {
202 if ($mappingName[$i] != ts('- do not import -')) {
203
204 $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]);
205
206 //When locationType is not set
207 $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
208
209 //When phoneType is not set
210 $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
211
212 $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
213
214 $defaults["mapper[$i]"] = array($mappingHeader[0]);
215 $jsSet = TRUE;
216 }
217 else {
218 $defaults["mapper[$i]"] = array();
219 }
220 if (!$jsSet) {
221 for ($k = 1; $k < 4; $k++) {
222 $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n";
223 }
224 }
225 }
226 else {
227 // this load section to help mapping if we ran out of saved columns when doing Load Mapping
228 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
229
230 if ($hasHeaders) {
231 $defaults["mapper[$i]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns));
232 }
233 else {
234 $defaults["mapper[$i]"] = array($this->defaultFromData($dataPatterns, $i));
235 }
236 }
237 //end of load mapping
238 }
239 else {
240 $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_" . $i . "_');\n";
241 if ($hasHeaders) {
242 // Infer the default from the skipped headers if we have them
243 $defaults["mapper[$i]"] = array(
244 $this->defaultFromHeader($this->_columnHeaders[$i],
245 $headerPatterns
246 ),
247 // $defaultLocationType->id
248 0,
249 );
250 }
251 else {
252 // Otherwise guess the default from the form of the data
253 $defaults["mapper[$i]"] = array(
254 $this->defaultFromData($dataPatterns, $i),
255 // $defaultLocationType->id
256 0,
257 );
258 }
259 }
260 $sel->setOptions(array($sel1, $sel2, (isset($sel3)) ? $sel3 : "", (isset($sel4)) ? $sel4 : ""));
261 }
262 $js .= "</script>\n";
263 $this->assign('initHideBoxes', $js);
264
265 //set warning if mismatch in more than
266 if (isset($mappingName)) {
267 if (($this->_columnCount != count($mappingName))) {
268 $warning++;
269 }
270 }
271 if ($warning != 0 && $this->get('savedMapping')) {
272 $session = CRM_Core_Session::singleton();
273 $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.'));
274 }
275 else {
276 $session = CRM_Core_Session::singleton();
277 $session->setStatus(NULL);
278 }
279
280 $this->setDefaults($defaults);
281
282 $this->addButtons(array(
283 array(
284 'type' => 'back',
285 'name' => ts('Previous'),
286 ),
287 array(
288 'type' => 'next',
289 'name' => ts('Continue'),
290 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
291 'isDefault' => TRUE,
292 ),
293 array(
294 'type' => 'cancel',
295 'name' => ts('Cancel'),
296 ),
297 ));
298 }
299
300 /**
301 * Global validation rules for the form.
302 *
303 * @param array $fields
304 * Posted values of the form.
305 *
306 * @param $files
307 * @param $self
308 *
309 * @return array
310 * list of errors to be posted back to the form
311 */
312 public static function formRule($fields, $files, $self) {
313 $errors = array();
314
315 if (!array_key_exists('savedMapping', $fields)) {
316 $importKeys = array();
317 foreach ($fields['mapper'] as $mapperPart) {
318 $importKeys[] = $mapperPart[0];
319 }
320 // FIXME: should use the schema titles, not redeclare them
321 $requiredFields = array(
322 'membership_contact_id' => ts('Contact ID'),
323 'membership_type_id' => ts('Membership Type'),
324 'membership_start_date' => ts('Membership Start Date'),
325 );
326
327 $contactTypeId = $self->get('contactType');
328 $contactTypes = array(
329 CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual',
330 CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household',
331 CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization',
332 );
333 $params = array(
334 'used' => 'Unsupervised',
335 'contact_type' => $contactTypes[$contactTypeId],
336 );
337 list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params);
338 $weightSum = 0;
339 foreach ($importKeys as $key => $val) {
340 if (array_key_exists($val, $ruleFields)) {
341 $weightSum += $ruleFields[$val];
342 }
343 }
344 $fieldMessage = '';
345 foreach ($ruleFields as $field => $weight) {
346 $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
347 }
348
349 foreach ($requiredFields as $field => $title) {
350 if (!in_array($field, $importKeys)) {
351 if ($field == 'membership_contact_id') {
352 if ((($weightSum >= $threshold || in_array('external_identifier', $importKeys)) &&
353 $self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE
354 ) ||
355 in_array('membership_id', $importKeys)
356 ) {
357 continue;
358 }
359 else {
360 if (!isset($errors['_qf_default'])) {
361 $errors['_qf_default'] = '';
362 }
363 $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(
364 1 => $threshold,
365 )) . ' ' . ts('(OR Membership ID if update mode.)') . '<br />';
366 }
367 }
368 else {
369 if (!isset($errors['_qf_default'])) {
370 $errors['_qf_default'] = '';
371 }
372 $errors['_qf_default'] .= ts('Missing required field: %1', array(
373 1 => $title,
374 )) . '<br />';
375 }
376 }
377 }
378 }
379
380 if (!empty($fields['saveMapping'])) {
381 $nameField = CRM_Utils_Array::value('saveMappingName', $fields);
382 if (empty($nameField)) {
383 $errors['saveMappingName'] = ts('Name is required to save Import Mapping');
384 }
385 else {
386 if (CRM_Core_BAO_Mapping::checkMapping($nameField, CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'))) {
387 $errors['saveMappingName'] = ts('Duplicate Import Membership Mapping Name');
388 }
389 }
390 }
391
392 if (!empty($errors)) {
393 if (!empty($errors['saveMappingName'])) {
394 $_flag = 1;
395 $assignError = new CRM_Core_Page();
396 $assignError->assign('mappingDetailsError', $_flag);
397 }
398 return $errors;
399 }
400
401 return TRUE;
402 }
403
404 /**
405 * Process the mapped fields and map it into the uploaded file
406 * preview the file and extract some summary statistics
407 *
408 * @return void
409 */
410 public function postProcess() {
411 $params = $this->controller->exportValues('MapField');
412 //reload the mapfield if load mapping is pressed
413 if (!empty($params['savedMapping'])) {
414 $this->set('savedMapping', $params['savedMapping']);
415 $this->controller->resetPage($this->_name);
416 return;
417 }
418
419 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
420 $seperator = $this->controller->exportValue('DataSource', 'fieldSeparator');
421 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
422
423 $mapperKeys = array();
424 $mapper = array();
425 $mapperKeys = $this->controller->exportValue($this->_name, 'mapper');
426 $mapperKeysMain = array();
427 $mapperLocType = array();
428 $mapperPhoneType = array();
429
430 for ($i = 0; $i < $this->_columnCount; $i++) {
431 $mapper[$i] = $this->_mapperFields[$mapperKeys[$i][0]];
432 $mapperKeysMain[$i] = $mapperKeys[$i][0];
433
434 if (!empty($mapperKeys[$i][1]) && is_numeric($mapperKeys[$i][1])) {
435 $mapperLocType[$i] = $mapperKeys[$i][1];
436 }
437 else {
438 $mapperLocType[$i] = NULL;
439 }
440
441 if (!empty($mapperKeys[$i][2]) && (!is_numeric($mapperKeys[$i][2]))) {
442 $mapperPhoneType[$i] = $mapperKeys[$i][2];
443 }
444 else {
445 $mapperPhoneType[$i] = NULL;
446 }
447 }
448
449 $this->set('mapper', $mapper);
450
451 // store mapping Id to display it in the preview page
452 if (!empty($params['mappingId'])) {
453 $this->set('loadMappingId', $params['mappingId']);
454 }
455 //Updating Mapping Records
456 if (!empty($params['updateMapping'])) {
457 $mappingFields = new CRM_Core_DAO_MappingField();
458 $mappingFields->mapping_id = $params['mappingId'];
459 $mappingFields->find();
460
461 $mappingFieldsId = array();
462 while ($mappingFields->fetch()) {
463 if ($mappingFields->id) {
464 $mappingFieldsId[$mappingFields->column_number] = $mappingFields->id;
465 }
466 }
467
468 for ($i = 0; $i < $this->_columnCount; $i++) {
469 $updateMappingFields = new CRM_Core_DAO_MappingField();
470 $updateMappingFields->id = $mappingFieldsId[$i];
471 $updateMappingFields->mapping_id = $params['mappingId'];
472 $updateMappingFields->column_number = $i;
473
474 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
475 $id = isset($mapperKeyParts[0]) ? $mapperKeyParts[0] : NULL;
476 $first = isset($mapperKeyParts[1]) ? $mapperKeyParts[1] : NULL;
477 $second = isset($mapperKeyParts[2]) ? $mapperKeyParts[2] : NULL;
478 $updateMappingFields->name = $mapper[$i];
479 $updateMappingFields->save();
480 }
481 }
482
483 //Saving Mapping Details and Records
484 if (!empty($params['saveMapping'])) {
485 $mappingParams = array(
486 'name' => $params['saveMappingName'],
487 'description' => $params['saveMappingDesc'],
488 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Import Membership'),
489 );
490 $saveMapping = CRM_Core_BAO_Mapping::add($mappingParams);
491
492 for ($i = 0; $i < $this->_columnCount; $i++) {
493
494 $saveMappingFields = new CRM_Core_DAO_MappingField();
495 $saveMappingFields->mapping_id = $saveMapping->id;
496 $saveMappingFields->column_number = $i;
497
498 $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3);
499 $id = isset($mapperKeyParts[0]) ? $mapperKeyParts[0] : NULL;
500 $first = isset($mapperKeyParts[1]) ? $mapperKeyParts[1] : NULL;
501 $second = isset($mapperKeyParts[2]) ? $mapperKeyParts[2] : NULL;
502 $saveMappingFields->name = $mapper[$i];
503 $saveMappingFields->save();
504 }
505 $this->set('savedMapping', $saveMappingFields->mapping_id);
506 }
507
508 $parser = new CRM_Member_Import_Parser_Membership($mapperKeysMain, $mapperLocType, $mapperPhoneType);
509 $parser->run($fileName, $seperator, $mapper, $skipColumnHeader,
510 CRM_Import_Parser::MODE_PREVIEW, $this->get('contactType')
511 );
512 // add all the necessary variables to the form
513 $parser->set($this);
514 }
515
516 }