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 | 11 | |
ec3811b1 CW |
12 | /** |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
ec3811b1 CW |
16 | * $Id$ |
17 | * | |
18 | */ | |
ec3811b1 | 19 | abstract class CRM_Event_Import_Parser extends CRM_Import_Parser { |
6a488035 TO |
20 | |
21 | protected $_fileName; | |
22 | ||
90b461f1 | 23 | /** |
9f266042 | 24 | * Imported file size. |
25 | * | |
90b461f1 | 26 | * @var int |
6a488035 TO |
27 | */ |
28 | protected $_fileSize; | |
29 | ||
30 | /** | |
9f266042 | 31 | * Separator being used. |
32 | * | |
90b461f1 | 33 | * @var string |
6a488035 TO |
34 | */ |
35 | protected $_seperator; | |
36 | ||
37 | /** | |
9f266042 | 38 | * Total number of lines in file. |
39 | * | |
90b461f1 | 40 | * @var int |
6a488035 TO |
41 | */ |
42 | protected $_lineCount; | |
43 | ||
6a488035 | 44 | /** |
100fef9d | 45 | * Whether the file has a column header or not |
6a488035 | 46 | * |
d51c6add | 47 | * @var bool |
6a488035 TO |
48 | */ |
49 | protected $_haveColumnHeader; | |
50 | ||
0cf587a7 | 51 | /** |
100fef9d | 52 | * @param string $fileName |
0cf587a7 EM |
53 | * @param string $seperator |
54 | * @param $mapper | |
55 | * @param bool $skipColumnHeader | |
56 | * @param int $mode | |
57 | * @param int $contactType | |
58 | * @param int $onDuplicate | |
59 | * | |
60 | * @return mixed | |
61 | * @throws Exception | |
62 | */ | |
8d7a9d07 | 63 | public function run( |
ddca8f33 | 64 | $fileName, |
6a488035 TO |
65 | $seperator = ',', |
66 | &$mapper, | |
67 | $skipColumnHeader = FALSE, | |
52892e8b CW |
68 | $mode = self::MODE_PREVIEW, |
69 | $contactType = self::CONTACT_INDIVIDUAL, | |
70 | $onDuplicate = self::DUPLICATE_SKIP | |
6a488035 TO |
71 | ) { |
72 | if (!is_array($fileName)) { | |
79e11805 | 73 | throw new CRM_Core_Exception('Unable to determine import file'); |
6a488035 TO |
74 | } |
75 | $fileName = $fileName['name']; | |
76 | ||
77 | switch ($contactType) { | |
78 | case self::CONTACT_INDIVIDUAL: | |
79 | $this->_contactType = 'Individual'; | |
80 | break; | |
81 | ||
82 | case self::CONTACT_HOUSEHOLD: | |
83 | $this->_contactType = 'Household'; | |
84 | break; | |
85 | ||
86 | case self::CONTACT_ORGANIZATION: | |
87 | $this->_contactType = 'Organization'; | |
88 | } | |
89 | ||
90 | $this->init(); | |
91 | ||
92 | $this->_haveColumnHeader = $skipColumnHeader; | |
93 | ||
94 | $this->_seperator = $seperator; | |
95 | ||
96 | $fd = fopen($fileName, "r"); | |
97 | if (!$fd) { | |
98 | return FALSE; | |
99 | } | |
100 | ||
101 | $this->_lineCount = $this->_warningCount = 0; | |
102 | $this->_invalidRowCount = $this->_validCount = 0; | |
103 | $this->_totalCount = $this->_conflictCount = 0; | |
104 | ||
be2fb01f CW |
105 | $this->_errors = []; |
106 | $this->_warnings = []; | |
107 | $this->_conflicts = []; | |
6a488035 TO |
108 | |
109 | $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2); | |
110 | ||
111 | if ($mode == self::MODE_MAPFIELD) { | |
be2fb01f | 112 | $this->_rows = []; |
6a488035 TO |
113 | } |
114 | else { | |
115 | $this->_activeFieldCount = count($this->_activeFields); | |
116 | } | |
117 | ||
118 | while (!feof($fd)) { | |
119 | $this->_lineCount++; | |
120 | ||
121 | $values = fgetcsv($fd, 8192, $seperator); | |
122 | if (!$values) { | |
123 | continue; | |
124 | } | |
125 | ||
126 | self::encloseScrub($values); | |
127 | ||
128 | // skip column header if we're not in mapfield mode | |
129 | if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) { | |
130 | $skipColumnHeader = FALSE; | |
131 | continue; | |
132 | } | |
133 | ||
134 | /* trim whitespace around the values */ | |
135 | ||
136 | $empty = TRUE; | |
137 | foreach ($values as $k => $v) { | |
138 | $values[$k] = trim($v, " \t\r\n"); | |
139 | } | |
140 | ||
141 | if (CRM_Utils_System::isNull($values)) { | |
142 | continue; | |
143 | } | |
144 | ||
145 | $this->_totalCount++; | |
146 | ||
147 | if ($mode == self::MODE_MAPFIELD) { | |
148 | $returnCode = $this->mapField($values); | |
149 | } | |
150 | elseif ($mode == self::MODE_PREVIEW) { | |
151 | $returnCode = $this->preview($values); | |
152 | } | |
153 | elseif ($mode == self::MODE_SUMMARY) { | |
154 | $returnCode = $this->summary($values); | |
155 | } | |
156 | elseif ($mode == self::MODE_IMPORT) { | |
157 | $returnCode = $this->import($onDuplicate, $values); | |
158 | } | |
159 | else { | |
160 | $returnCode = self::ERROR; | |
161 | } | |
162 | ||
163 | // note that a line could be valid but still produce a warning | |
164 | if ($returnCode & self::VALID) { | |
165 | $this->_validCount++; | |
166 | if ($mode == self::MODE_MAPFIELD) { | |
167 | $this->_rows[] = $values; | |
168 | $this->_activeFieldCount = max($this->_activeFieldCount, count($values)); | |
169 | } | |
170 | } | |
171 | ||
172 | if ($returnCode & self::WARNING) { | |
173 | $this->_warningCount++; | |
174 | if ($this->_warningCount < $this->_maxWarningCount) { | |
175 | $this->_warningCount[] = $line; | |
176 | } | |
177 | } | |
178 | ||
179 | if ($returnCode & self::ERROR) { | |
180 | $this->_invalidRowCount++; | |
ca2057ea SM |
181 | $recordNumber = $this->_lineCount; |
182 | if ($this->_haveColumnHeader) { | |
183 | $recordNumber--; | |
6a488035 | 184 | } |
ca2057ea SM |
185 | array_unshift($values, $recordNumber); |
186 | $this->_errors[] = $values; | |
6a488035 TO |
187 | } |
188 | ||
189 | if ($returnCode & self::CONFLICT) { | |
190 | $this->_conflictCount++; | |
191 | $recordNumber = $this->_lineCount; | |
192 | if ($this->_haveColumnHeader) { | |
193 | $recordNumber--; | |
194 | } | |
195 | array_unshift($values, $recordNumber); | |
196 | $this->_conflicts[] = $values; | |
197 | } | |
198 | ||
199 | if ($returnCode & self::DUPLICATE) { | |
200 | if ($returnCode & self::MULTIPLE_DUPE) { | |
201 | /* TODO: multi-dupes should be counted apart from singles | |
e70a7fc0 | 202 | * on non-skip action */ |
6a488035 TO |
203 | } |
204 | $this->_duplicateCount++; | |
205 | $recordNumber = $this->_lineCount; | |
206 | if ($this->_haveColumnHeader) { | |
207 | $recordNumber--; | |
208 | } | |
209 | array_unshift($values, $recordNumber); | |
210 | $this->_duplicates[] = $values; | |
211 | if ($onDuplicate != self::DUPLICATE_SKIP) { | |
212 | $this->_validCount++; | |
213 | } | |
214 | } | |
215 | ||
216 | // we give the derived class a way of aborting the process | |
217 | // note that the return code could be multiple code or'ed together | |
218 | if ($returnCode & self::STOP) { | |
219 | break; | |
220 | } | |
221 | ||
222 | // if we are done processing the maxNumber of lines, break | |
223 | if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) { | |
224 | break; | |
225 | } | |
226 | } | |
227 | ||
228 | fclose($fd); | |
229 | ||
230 | if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) { | |
231 | $customHeaders = $mapper; | |
232 | ||
233 | $customfields = CRM_Core_BAO_CustomField::getFields('Participant'); | |
234 | foreach ($customHeaders as $key => $value) { | |
235 | if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) { | |
236 | $customHeaders[$key] = $customfields[$id][0]; | |
237 | } | |
238 | } | |
239 | ||
240 | if ($this->_invalidRowCount) { | |
241 | // removed view url for invlaid contacts | |
be2fb01f | 242 | $headers = array_merge([ |
90b461f1 SL |
243 | ts('Line Number'), |
244 | ts('Reason'), | |
245 | ], $customHeaders); | |
6a488035 TO |
246 | $this->_errorFileName = self::errorFileName(self::ERROR); |
247 | self::exportCSV($this->_errorFileName, $headers, $this->_errors); | |
248 | } | |
249 | if ($this->_conflictCount) { | |
be2fb01f | 250 | $headers = array_merge([ |
90b461f1 SL |
251 | ts('Line Number'), |
252 | ts('Reason'), | |
253 | ], $customHeaders); | |
6a488035 TO |
254 | $this->_conflictFileName = self::errorFileName(self::CONFLICT); |
255 | self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts); | |
256 | } | |
257 | if ($this->_duplicateCount) { | |
be2fb01f | 258 | $headers = array_merge([ |
90b461f1 SL |
259 | ts('Line Number'), |
260 | ts('View Participant URL'), | |
261 | ], $customHeaders); | |
6a488035 TO |
262 | |
263 | $this->_duplicateFileName = self::errorFileName(self::DUPLICATE); | |
264 | self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates); | |
265 | } | |
266 | } | |
267 | return $this->fini(); | |
268 | } | |
269 | ||
6a488035 TO |
270 | /** |
271 | * Given a list of the importable field keys that the user has selected | |
272 | * set the active fields array to this list | |
273 | * | |
8d7a9d07 | 274 | * @param $fieldKeys array mapped array of values |
6a488035 TO |
275 | * |
276 | * @return void | |
6a488035 | 277 | */ |
00be9182 | 278 | public function setActiveFields($fieldKeys) { |
6a488035 TO |
279 | $this->_activeFieldCount = count($fieldKeys); |
280 | foreach ($fieldKeys as $key) { | |
281 | if (empty($this->_fields[$key])) { | |
282 | $this->_activeFields[] = new CRM_Event_Import_Field('', ts('- do not import -')); | |
283 | } | |
284 | else { | |
285 | $this->_activeFields[] = clone($this->_fields[$key]); | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
6a488035 | 290 | /** |
fe482240 | 291 | * Format the field values for input to the api. |
6a488035 | 292 | * |
a6c01b45 CW |
293 | * @return array |
294 | * (reference ) associative array of name/value pairs | |
6a488035 | 295 | */ |
00be9182 | 296 | public function &getActiveFieldParams() { |
be2fb01f | 297 | $params = []; |
6a488035 TO |
298 | for ($i = 0; $i < $this->_activeFieldCount; $i++) { |
299 | if (isset($this->_activeFields[$i]->_value) | |
300 | && !isset($params[$this->_activeFields[$i]->_name]) | |
301 | && !isset($this->_activeFields[$i]->_related) | |
302 | ) { | |
303 | ||
304 | $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value; | |
305 | } | |
306 | } | |
307 | return $params; | |
308 | } | |
309 | ||
0cf587a7 | 310 | /** |
100fef9d | 311 | * @param string $name |
0cf587a7 EM |
312 | * @param $title |
313 | * @param int $type | |
314 | * @param string $headerPattern | |
315 | * @param string $dataPattern | |
316 | */ | |
00be9182 | 317 | public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { |
6a488035 TO |
318 | if (empty($name)) { |
319 | $this->_fields['doNotImport'] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
320 | } | |
321 | else { | |
322 | ||
323 | //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null ); | |
324 | $tempField = CRM_Contact_BAO_Contact::importableFields('All', NULL); | |
325 | if (!array_key_exists($name, $tempField)) { | |
326 | $this->_fields[$name] = new CRM_Event_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
327 | } | |
328 | else { | |
719a6fec | 329 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, |
6a488035 TO |
330 | CRM_Utils_Array::value('hasLocationType', $tempField[$name]) |
331 | ); | |
332 | } | |
333 | } | |
334 | } | |
335 | ||
6a488035 | 336 | /** |
fe482240 | 337 | * Store parser values. |
6a488035 TO |
338 | * |
339 | * @param CRM_Core_Session $store | |
340 | * | |
dd244018 EM |
341 | * @param int $mode |
342 | * | |
6a488035 | 343 | * @return void |
6a488035 | 344 | */ |
00be9182 | 345 | public function set($store, $mode = self::MODE_SUMMARY) { |
6a488035 TO |
346 | $store->set('fileSize', $this->_fileSize); |
347 | $store->set('lineCount', $this->_lineCount); | |
348 | $store->set('seperator', $this->_seperator); | |
349 | $store->set('fields', $this->getSelectValues()); | |
350 | $store->set('fieldTypes', $this->getSelectTypes()); | |
351 | ||
352 | $store->set('headerPatterns', $this->getHeaderPatterns()); | |
353 | $store->set('dataPatterns', $this->getDataPatterns()); | |
354 | $store->set('columnCount', $this->_activeFieldCount); | |
355 | ||
356 | $store->set('totalRowCount', $this->_totalCount); | |
357 | $store->set('validRowCount', $this->_validCount); | |
358 | $store->set('invalidRowCount', $this->_invalidRowCount); | |
359 | $store->set('conflictRowCount', $this->_conflictCount); | |
360 | ||
361 | switch ($this->_contactType) { | |
362 | case 'Individual': | |
a05662ef | 363 | $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); |
6a488035 TO |
364 | break; |
365 | ||
366 | case 'Household': | |
a05662ef | 367 | $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD); |
6a488035 TO |
368 | break; |
369 | ||
370 | case 'Organization': | |
a05662ef | 371 | $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION); |
6a488035 TO |
372 | } |
373 | ||
374 | if ($this->_invalidRowCount) { | |
375 | $store->set('errorsFileName', $this->_errorFileName); | |
376 | } | |
377 | if ($this->_conflictCount) { | |
378 | $store->set('conflictsFileName', $this->_conflictFileName); | |
379 | } | |
380 | if (isset($this->_rows) && !empty($this->_rows)) { | |
381 | $store->set('dataValues', $this->_rows); | |
382 | } | |
383 | ||
384 | if ($mode == self::MODE_IMPORT) { | |
385 | $store->set('duplicateRowCount', $this->_duplicateCount); | |
386 | if ($this->_duplicateCount) { | |
387 | $store->set('duplicatesFileName', $this->_duplicateFileName); | |
388 | } | |
389 | } | |
390 | } | |
391 | ||
392 | /** | |
fe482240 | 393 | * Export data to a CSV file. |
6a488035 | 394 | * |
c490a46a | 395 | * @param string $fileName |
6a488035 | 396 | * @param array $header |
c490a46a | 397 | * @param array $data |
6a488035 TO |
398 | * |
399 | * @return void | |
6a488035 | 400 | */ |
00be9182 | 401 | public static function exportCSV($fileName, $header, $data) { |
be2fb01f | 402 | $output = []; |
6a488035 TO |
403 | $fd = fopen($fileName, 'w'); |
404 | ||
405 | foreach ($header as $key => $value) { | |
406 | $header[$key] = "\"$value\""; | |
407 | } | |
408 | $config = CRM_Core_Config::singleton(); | |
409 | $output[] = implode($config->fieldSeparator, $header); | |
410 | ||
411 | foreach ($data as $datum) { | |
412 | foreach ($datum as $key => $value) { | |
413 | if (is_array($value)) { | |
414 | foreach ($value[0] as $k1 => $v1) { | |
415 | if ($k1 == 'location_type_id') { | |
416 | continue; | |
417 | } | |
418 | $datum[$k1] = $v1; | |
419 | } | |
420 | } | |
421 | else { | |
422 | $datum[$key] = "\"$value\""; | |
423 | } | |
424 | } | |
425 | $output[] = implode($config->fieldSeparator, $datum); | |
426 | } | |
427 | fwrite($fd, implode("\n", $output)); | |
428 | fclose($fd); | |
429 | } | |
430 | ||
6a488035 | 431 | } |