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 | /** | |
b6c94f42 | 19 | * This class previews the uploaded file and returns summary statistics. |
6a488035 | 20 | */ |
f532671f | 21 | class CRM_Activity_Import_Form_Preview extends CRM_Import_Form_Preview { |
6a488035 TO |
22 | |
23 | /** | |
fe482240 | 24 | * Set variables up before form is built. |
6a488035 TO |
25 | */ |
26 | public function preProcess() { | |
de7b9b56 | 27 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
6a488035 | 28 | |
7808aae6 | 29 | // Get the data from the session. |
353ffa53 TO |
30 | $dataValues = $this->get('dataValues'); |
31 | $mapper = $this->get('mapper'); | |
32 | $invalidRowCount = $this->get('invalidRowCount'); | |
6a488035 | 33 | $conflictRowCount = $this->get('conflictRowCount'); |
353ffa53 | 34 | $mismatchCount = $this->get('unMatchCount'); |
6a488035 | 35 | |
7808aae6 | 36 | // Get the mapping name displayed if the mappingId is set. |
6a488035 TO |
37 | $mappingId = $this->get('loadMappingId'); |
38 | if ($mappingId) { | |
39 | $mapDAO = new CRM_Core_DAO_Mapping(); | |
40 | $mapDAO->id = $mappingId; | |
41 | $mapDAO->find(TRUE); | |
42 | $this->assign('loadedMapping', $mappingId); | |
43 | $this->assign('savedName', $mapDAO->name); | |
44 | } | |
45 | ||
46 | if ($skipColumnHeader) { | |
47 | $this->assign('skipColumnHeader', $skipColumnHeader); | |
48 | $this->assign('rowDisplayCount', 3); | |
49 | } | |
50 | else { | |
51 | $this->assign('rowDisplayCount', 2); | |
52 | } | |
53 | ||
54 | if ($invalidRowCount) { | |
a05662ef | 55 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser'; |
6a488035 TO |
56 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
57 | } | |
58 | ||
59 | if ($conflictRowCount) { | |
a05662ef | 60 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Activity_Import_Parser'; |
6a488035 TO |
61 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
62 | } | |
63 | ||
64 | if ($mismatchCount) { | |
a05662ef | 65 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Activity_Import_Parser'; |
6a488035 TO |
66 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
67 | } | |
68 | ||
be2fb01f | 69 | $properties = [ |
6a488035 | 70 | 'mapper', |
353ffa53 TO |
71 | 'dataValues', |
72 | 'columnCount', | |
73 | 'totalRowCount', | |
74 | 'validRowCount', | |
75 | 'invalidRowCount', | |
76 | 'conflictRowCount', | |
6a488035 TO |
77 | 'downloadErrorRecordsUrl', |
78 | 'downloadConflictRecordsUrl', | |
79 | 'downloadMismatchRecordsUrl', | |
be2fb01f | 80 | ]; |
8cebffb2 | 81 | $this->setStatusUrl(); |
6a488035 TO |
82 | |
83 | foreach ($properties as $property) { | |
84 | $this->assign($property, $this->get($property)); | |
85 | } | |
86 | } | |
87 | ||
6a488035 | 88 | /** |
fe482240 | 89 | * Process the mapped fields and map it into the uploaded file. |
6a488035 | 90 | * |
b6c94f42 | 91 | * Preview the file and extract some summary statistics |
6a488035 TO |
92 | */ |
93 | public function postProcess() { | |
353ffa53 | 94 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); |
f0fed404 | 95 | $separator = $this->controller->exportValue('DataSource', 'fieldSeparator'); |
de7b9b56 | 96 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
353ffa53 | 97 | $invalidRowCount = $this->get('invalidRowCount'); |
6a488035 | 98 | $conflictRowCount = $this->get('conflictRowCount'); |
353ffa53 | 99 | $onDuplicate = $this->get('onDuplicate'); |
6a488035 | 100 | |
6a488035 | 101 | $mapper = $this->controller->exportValue('MapField', 'mapper'); |
be2fb01f CW |
102 | $mapperKeys = []; |
103 | $mapperLocType = []; | |
104 | $mapperPhoneType = []; | |
6a488035 TO |
105 | |
106 | foreach ($mapper as $key => $value) { | |
107 | $mapperKeys[$key] = $mapper[$key][0]; | |
108 | ||
a7488080 | 109 | if (!empty($mapper[$key][1]) && is_numeric($mapper[$key][1])) { |
6a488035 TO |
110 | $mapperLocType[$key] = $mapper[$key][1]; |
111 | } | |
112 | else { | |
113 | $mapperLocType[$key] = NULL; | |
114 | } | |
115 | ||
a7488080 | 116 | if (!empty($mapper[$key][2]) && (!is_numeric($mapper[$key][2]))) { |
6a488035 TO |
117 | $mapperPhoneType[$key] = $mapper[$key][2]; |
118 | } | |
119 | else { | |
120 | $mapperPhoneType[$key] = NULL; | |
121 | } | |
122 | } | |
123 | ||
124 | $parser = new CRM_Activity_Import_Parser_Activity($mapperKeys, $mapperLocType, $mapperPhoneType); | |
125 | ||
126 | $mapFields = $this->get('fields'); | |
127 | ||
128 | foreach ($mapper as $key => $value) { | |
be2fb01f | 129 | $header = []; |
6a488035 TO |
130 | if (isset($mapFields[$mapper[$key][0]])) { |
131 | $header[] = $mapFields[$mapper[$key][0]]; | |
132 | } | |
133 | $mapperFields[] = implode(' - ', $header); | |
134 | } | |
f0fed404 | 135 | $parser->run($fileName, $separator, |
6a488035 TO |
136 | $mapperFields, |
137 | $skipColumnHeader, | |
a05662ef | 138 | CRM_Import_Parser::MODE_IMPORT, |
8cebffb2 JP |
139 | $onDuplicate, |
140 | $this->get('statusID'), | |
141 | $this->get('totalRowCount') | |
6a488035 TO |
142 | ); |
143 | ||
144 | // add all the necessary variables to the form | |
a05662ef | 145 | $parser->set($this, CRM_Import_Parser::MODE_IMPORT); |
6a488035 | 146 | |
b44e3f84 | 147 | // check if there is any error occurred |
6a488035 TO |
148 | |
149 | $errorStack = CRM_Core_Error::singleton(); | |
150 | $errors = $errorStack->getErrors(); | |
be2fb01f | 151 | $errorMessage = []; |
6a488035 TO |
152 | |
153 | if (is_array($errors)) { | |
154 | foreach ($errors as $key => $value) { | |
155 | $errorMessage[] = $value['message']; | |
156 | } | |
157 | ||
158 | $errorFile = $fileName['name'] . '.error.log'; | |
159 | ||
160 | if ($fd = fopen($errorFile, 'w')) { | |
161 | fwrite($fd, implode('\n', $errorMessage)); | |
162 | } | |
163 | fclose($fd); | |
164 | ||
165 | $this->set('errorFile', $errorFile); | |
a05662ef | 166 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser'; |
6a488035 | 167 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
a05662ef | 168 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Activity_Import_Parser'; |
6a488035 | 169 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
a05662ef | 170 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Activity_Import_Parser'; |
6a488035 TO |
171 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
172 | } | |
173 | } | |
96025800 | 174 | |
6a488035 | 175 | } |