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