Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class previews the uploaded file and returns summary | |
20 | * statistics | |
21 | */ | |
f532671f | 22 | class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview { |
6a488035 TO |
23 | |
24 | /** | |
fe482240 | 25 | * Set variables up before form is built. |
6a488035 TO |
26 | * |
27 | * @return void | |
6a488035 TO |
28 | */ |
29 | public function preProcess() { | |
de7b9b56 | 30 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
6a488035 TO |
31 | |
32 | //get the data from the session | |
353ffa53 TO |
33 | $dataValues = $this->get('dataValues'); |
34 | $mapper = $this->get('mapper'); | |
35 | $invalidRowCount = $this->get('invalidRowCount'); | |
6a488035 | 36 | $conflictRowCount = $this->get('conflictRowCount'); |
353ffa53 | 37 | $mismatchCount = $this->get('unMatchCount'); |
6a488035 TO |
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) { | |
a05662ef | 58 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser'; |
6a488035 TO |
59 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
60 | } | |
61 | ||
62 | if ($conflictRowCount) { | |
a05662ef | 63 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Event_Import_Parser'; |
6a488035 TO |
64 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
65 | } | |
66 | ||
67 | if ($mismatchCount) { | |
a05662ef | 68 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Event_Import_Parser'; |
6a488035 TO |
69 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
70 | } | |
71 | ||
be2fb01f | 72 | $properties = [ |
6a488035 | 73 | 'mapper', |
353ffa53 TO |
74 | 'dataValues', |
75 | 'columnCount', | |
76 | 'totalRowCount', | |
77 | 'validRowCount', | |
78 | 'invalidRowCount', | |
79 | 'conflictRowCount', | |
6a488035 TO |
80 | 'downloadErrorRecordsUrl', |
81 | 'downloadConflictRecordsUrl', | |
82 | 'downloadMismatchRecordsUrl', | |
be2fb01f | 83 | ]; |
6a488035 TO |
84 | |
85 | foreach ($properties as $property) { | |
86 | $this->assign($property, $this->get($property)); | |
87 | } | |
88 | } | |
89 | ||
6a488035 TO |
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 | |
6a488035 TO |
95 | */ |
96 | public function postProcess() { | |
353ffa53 | 97 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); |
f0fed404 | 98 | $separator = $this->controller->exportValue('DataSource', 'fieldSeparator'); |
de7b9b56 | 99 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
353ffa53 | 100 | $invalidRowCount = $this->get('invalidRowCount'); |
6a488035 | 101 | $conflictRowCount = $this->get('conflictRowCount'); |
353ffa53 | 102 | $onDuplicate = $this->get('onDuplicate'); |
6a488035 | 103 | |
063338e5 | 104 | $mapper = $this->controller->exportValue('MapField', 'mapper'); |
be2fb01f | 105 | $mapperKeys = []; |
6a488035 TO |
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) { | |
be2fb01f | 116 | $header = []; |
6a488035 TO |
117 | if (isset($mapFields[$mapper[$key][0]])) { |
118 | $header[] = $mapFields[$mapper[$key][0]]; | |
119 | } | |
120 | $mapperFields[] = implode(' - ', $header); | |
121 | } | |
f0fed404 | 122 | $parser->run($fileName, $separator, |
6a488035 TO |
123 | $mapperFields, |
124 | $skipColumnHeader, | |
a05662ef | 125 | CRM_Import_Parser::MODE_IMPORT, |
6a488035 TO |
126 | $this->get('contactType'), |
127 | $onDuplicate | |
128 | ); | |
129 | ||
130 | // add all the necessary variables to the form | |
a05662ef | 131 | $parser->set($this, CRM_Import_Parser::MODE_IMPORT); |
6a488035 | 132 | |
b44e3f84 | 133 | // check if there is any error occurred |
6a488035 TO |
134 | |
135 | $errorStack = CRM_Core_Error::singleton(); | |
136 | $errors = $errorStack->getErrors(); | |
be2fb01f | 137 | $errorMessage = []; |
6a488035 TO |
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); | |
a05662ef | 152 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser'; |
6a488035 | 153 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
a05662ef | 154 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Event_Import_Parser'; |
6a488035 | 155 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
a05662ef | 156 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Event_Import_Parser'; |
6a488035 TO |
157 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
158 | } | |
159 | } | |
96025800 | 160 | |
6a488035 | 161 | } |