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