Merge pull request #23428 from eileenmcnaughton/import_field_keys
[civicrm-core.git] / CRM / Event / Import / Form / Preview.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class previews the uploaded file and returns summary
20 * statistics
21 */
22 class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview {
23
24 /**
25 * Set variables up before form is built.
26 *
27 * @return void
28 */
29 public function preProcess() {
30 parent::preProcess();
31
32 //get the data from the session
33 $dataValues = $this->get('dataValues');
34 $mapper = $this->get('mapper');
35 $invalidRowCount = $this->get('invalidRowCount');
36
37 //get the mapping name displayed if the mappingId is set
38 $mappingId = $this->get('loadMappingId');
39 if ($mappingId) {
40 $mapDAO = new CRM_Core_DAO_Mapping();
41 $mapDAO->id = $mappingId;
42 $mapDAO->find(TRUE);
43 }
44 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
45
46 if ($invalidRowCount) {
47 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser_Participant';
48 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
49 }
50
51 $properties = [
52 'mapper',
53 'dataValues',
54 'columnCount',
55 'totalRowCount',
56 'validRowCount',
57 'invalidRowCount',
58 'downloadErrorRecordsUrl',
59 ];
60
61 foreach ($properties as $property) {
62 $this->assign($property, $this->get($property));
63 }
64 }
65
66 /**
67 * Process the mapped fields and map it into the uploaded file
68 * preview the file and extract some summary statistics
69 *
70 * @return void
71 */
72 public function postProcess() {
73 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
74 $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
75 $invalidRowCount = $this->get('invalidRowCount');
76 $onDuplicate = $this->get('onDuplicate');
77
78 $mapper = $this->controller->exportValue('MapField', 'mapper');
79 $mapperKeys = [];
80
81 foreach ($mapper as $key => $value) {
82 $mapperKeys[$key] = $mapper[$key][0];
83 }
84
85 $parser = new CRM_Event_Import_Parser_Participant($mapperKeys);
86
87 $mapFields = $this->get('fields');
88
89 foreach ($mapper as $key => $value) {
90 $header = [];
91 if (isset($mapFields[$mapper[$key][0]])) {
92 $header[] = $mapFields[$mapper[$key][0]];
93 }
94 $mapperFields[] = implode(' - ', $header);
95 }
96 $parser->run($fileName, $separator,
97 $mapperFields,
98 $this->getSubmittedValue('skipColumnHeader'),
99 CRM_Import_Parser::MODE_IMPORT,
100 $this->get('contactType'),
101 $onDuplicate
102 );
103
104 // add all the necessary variables to the form
105 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
106
107 // check if there is any error occurred
108
109 $errorStack = CRM_Core_Error::singleton();
110 $errors = $errorStack->getErrors();
111 $errorMessage = [];
112
113 if (is_array($errors)) {
114 foreach ($errors as $key => $value) {
115 $errorMessage[] = $value['message'];
116 }
117
118 $errorFile = $fileName['name'] . '.error.log';
119
120 if ($fd = fopen($errorFile, 'w')) {
121 fwrite($fd, implode('\n', $errorMessage));
122 }
123 fclose($fd);
124
125 $this->set('errorFile', $errorFile);
126 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser_Participant';
127 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
128 }
129 }
130
131 }