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