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