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