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