Merge pull request #22977 from eileenmcnaughton/issetit
[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 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
31
32 //get the data from the session
33 $dataValues = $this->get('dataValues');
34 $mapper = $this->get('mapper');
35 $invalidRowCount = $this->get('invalidRowCount');
36 $conflictRowCount = $this->get('conflictRowCount');
37 $mismatchCount = $this->get('unMatchCount');
38
39 //get the mapping name displayed if the mappingId is set
40 $mappingId = $this->get('loadMappingId');
41 if ($mappingId) {
42 $mapDAO = new CRM_Core_DAO_Mapping();
43 $mapDAO->id = $mappingId;
44 $mapDAO->find(TRUE);
45 }
46 $this->assign('savedMappingName', $mappingId ? $mapDAO->name : NULL);
47
48 if ($skipColumnHeader) {
49 $this->assign('skipColumnHeader', $skipColumnHeader);
50 $this->assign('rowDisplayCount', 3);
51 }
52 else {
53 $this->assign('rowDisplayCount', 2);
54 }
55
56 if ($invalidRowCount) {
57 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser';
58 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
59 }
60
61 if ($conflictRowCount) {
62 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Event_Import_Parser';
63 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
64 }
65
66 if ($mismatchCount) {
67 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Event_Import_Parser';
68 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
69 }
70
71 $properties = [
72 'mapper',
73 'dataValues',
74 'columnCount',
75 'totalRowCount',
76 'validRowCount',
77 'invalidRowCount',
78 'conflictRowCount',
79 'downloadErrorRecordsUrl',
80 'downloadConflictRecordsUrl',
81 'downloadMismatchRecordsUrl',
82 ];
83
84 foreach ($properties as $property) {
85 $this->assign($property, $this->get($property));
86 }
87 }
88
89 /**
90 * Process the mapped fields and map it into the uploaded file
91 * preview the file and extract some summary statistics
92 *
93 * @return void
94 */
95 public function postProcess() {
96 $fileName = $this->controller->exportValue('DataSource', 'uploadFile');
97 $separator = $this->controller->exportValue('DataSource', 'fieldSeparator');
98 $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader');
99 $invalidRowCount = $this->get('invalidRowCount');
100 $conflictRowCount = $this->get('conflictRowCount');
101 $onDuplicate = $this->get('onDuplicate');
102
103 $mapper = $this->controller->exportValue('MapField', 'mapper');
104 $mapperKeys = [];
105
106 foreach ($mapper as $key => $value) {
107 $mapperKeys[$key] = $mapper[$key][0];
108 }
109
110 $parser = new CRM_Event_Import_Parser_Participant($mapperKeys);
111
112 $mapFields = $this->get('fields');
113
114 foreach ($mapper as $key => $value) {
115 $header = [];
116 if (isset($mapFields[$mapper[$key][0]])) {
117 $header[] = $mapFields[$mapper[$key][0]];
118 }
119 $mapperFields[] = implode(' - ', $header);
120 }
121 $parser->run($fileName, $separator,
122 $mapperFields,
123 $skipColumnHeader,
124 CRM_Import_Parser::MODE_IMPORT,
125 $this->get('contactType'),
126 $onDuplicate
127 );
128
129 // add all the necessary variables to the form
130 $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
131
132 // check if there is any error occurred
133
134 $errorStack = CRM_Core_Error::singleton();
135 $errors = $errorStack->getErrors();
136 $errorMessage = [];
137
138 if (is_array($errors)) {
139 foreach ($errors as $key => $value) {
140 $errorMessage[] = $value['message'];
141 }
142
143 $errorFile = $fileName['name'] . '.error.log';
144
145 if ($fd = fopen($errorFile, 'w')) {
146 fwrite($fd, implode('\n', $errorMessage));
147 }
148 fclose($fd);
149
150 $this->set('errorFile', $errorFile);
151 $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser';
152 $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
153 $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Event_Import_Parser';
154 $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
155 $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Event_Import_Parser';
156 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
157 }
158 }
159
160 }