Merge pull request #4009 from eileenmcnaughton/CRM-15210
[civicrm-core.git] / CRM / Import / Parser.php
CommitLineData
ec3811b1
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
ec3811b1
CW
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36
37abstract class CRM_Import_Parser {
38 /**
39 * Settings
40 */
41 CONST MAX_ERRORS = 250, MAX_WARNINGS = 25, DEFAULT_TIMEOUT = 30;
42
43 /**
44 * Return codes
45 */
46 CONST VALID = 1, WARNING = 2, ERROR = 4, CONFLICT = 8, STOP = 16, DUPLICATE = 32, MULTIPLE_DUPE = 64, NO_MATCH = 128, UNPARSED_ADDRESS_WARNING = 256;
47
48 /**
49 * Parser modes
50 */
51 CONST MODE_MAPFIELD = 1, MODE_PREVIEW = 2, MODE_SUMMARY = 4, MODE_IMPORT = 8;
52
53 /**
54 * Codes for duplicate record handling
55 */
56 CONST DUPLICATE_SKIP = 1, DUPLICATE_REPLACE = 2, DUPLICATE_UPDATE = 4, DUPLICATE_FILL = 8, DUPLICATE_NOCHECK = 16;
57
58 /**
59 * Contact types
60 */
61 CONST CONTACT_INDIVIDUAL = 1, CONTACT_HOUSEHOLD = 2, CONTACT_ORGANIZATION = 4;
69a4c20a
CW
62
63
64 /**
65 * total number of non empty lines
66 */
67 protected $_totalCount;
68
69 /**
70 * running total number of valid lines
71 */
72 protected $_validCount;
73
74 /**
75 * running total number of invalid rows
76 */
77 protected $_invalidRowCount;
78
79 /**
80 * maximum number of non-empty/comment lines to process
81 *
82 * @var int
83 */
84 protected $_maxLinesToProcess;
85
86 /**
87 * maximum number of invalid rows to store
88 */
89 protected $_maxErrorCount;
90
91 /**
92 * array of error lines, bounded by MAX_ERROR
93 */
94 protected $_errors;
95
96 /**
97 * total number of conflict lines
98 */
99 protected $_conflictCount;
100
101 /**
102 * array of conflict lines
103 */
104 protected $_conflicts;
105
106 /**
107 * total number of duplicate (from database) lines
108 */
109 protected $_duplicateCount;
110
111 /**
112 * array of duplicate lines
113 */
114 protected $_duplicates;
115
116 /**
117 * running total number of warnings
118 */
119 protected $_warningCount;
120
121 /**
122 * maximum number of warnings to store
123 */
124 protected $_maxWarningCount = self::MAX_WARNINGS;
125
126 /**
127 * array of warning lines, bounded by MAX_WARNING
128 */
129 protected $_warnings;
130
131 /**
132 * array of all the fields that could potentially be part
133 * of this import process
134 * @var array
135 */
136 protected $_fields;
137
138 /**
139 * array of the fields that are actually part of the import process
140 * the position in the array also dictates their position in the import
141 * file
142 * @var array
143 */
144 protected $_activeFields;
145
146 /**
147 * cache the count of active fields
148 *
149 * @var int
150 */
151 protected $_activeFieldCount;
152
153 /**
154 * cache of preview rows
155 *
156 * @var array
157 */
158 protected $_rows;
159
160 /**
161 * filename of error data
162 *
163 * @var string
164 */
165 protected $_errorFileName;
166
167 /**
168 * filename of conflict data
169 *
170 * @var string
171 */
172 protected $_conflictFileName;
173
174 /**
175 * filename of duplicate data
176 *
177 * @var string
178 */
179 protected $_duplicateFileName;
180
181 /**
182 * contact type
183 *
184 * @var int
185 */
186 public $_contactType;
187
188 /**
189 * Class constructor
190 */
191 function __construct() {
192 $this->_maxLinesToProcess = 0;
193 $this->_maxErrorCount = self::MAX_ERRORS;
194 }
195
196 /**
197 * Abstract function definitions
198 */
199 abstract function init();
200 abstract function fini();
201 abstract function mapField(&$values);
202 abstract function preview(&$values);
203 abstract function summary(&$values);
204 abstract function import($onDuplicate, &$values);
205
206 /**
207 * Set and validate field values
208 *
209 * @param $elements: array
210 * @param $erroneousField: reference
211 */
212 function setActiveFieldValues($elements, &$erroneousField) {
213 $maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount;
214 for ($i = 0; $i < $maxCount; $i++) {
215 $this->_activeFields[$i]->setValue($elements[$i]);
216 }
217
218 // reset all the values that we did not have an equivalent import element
219 for (; $i < $this->_activeFieldCount; $i++) {
220 $this->_activeFields[$i]->resetValue();
221 }
222
223 // now validate the fields and return false if error
224 $valid = self::VALID;
225 for ($i = 0; $i < $this->_activeFieldCount; $i++) {
226 if (!$this->_activeFields[$i]->validate()) {
227 // no need to do any more validation
228 $erroneousField = $i;
229 $valid = self::ERROR;
230 break;
231 }
232 }
233 return $valid;
234 }
235
236 /**
237 * Format the field values for input to the api
238 *
239 * @return array (reference) associative array of name/value pairs
240 * @access public
241 */
242 function &getActiveFieldParams() {
243 $params = array();
244 for ($i = 0; $i < $this->_activeFieldCount; $i++) {
245 if (isset($this->_activeFields[$i]->_value)
246 && !isset($params[$this->_activeFields[$i]->_name])
247 && !isset($this->_activeFields[$i]->_related)
248 ) {
249
250 $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value;
251 }
252 }
253 return $params;
254 }
255
256 function getSelectValues() {
257 $values = array();
258 foreach ($this->_fields as $name => $field) {
259 $values[$name] = $field->_title;
260 }
261 return $values;
262 }
263
264 function getSelectTypes() {
265 $values = array();
266 foreach ($this->_fields as $name => $field) {
267 if (isset($field->_hasLocationType)) {
268 $values[$name] = $field->_hasLocationType;
269 }
270 }
271 return $values;
272 }
273
274 function getHeaderPatterns() {
275 $values = array();
276 foreach ($this->_fields as $name => $field) {
277 if (isset($field->_headerPattern)) {
278 $values[$name] = $field->_headerPattern;
279 }
280 }
281 return $values;
282 }
283
284 function getDataPatterns() {
285 $values = array();
286 foreach ($this->_fields as $name => $field) {
287 $values[$name] = $field->_dataPattern;
288 }
289 return $values;
290 }
291
292 /**
293 * Remove single-quote enclosures from a value array (row)
294 *
295 * @param array $values
296 * @param string $enclosure
297 *
298 * @return void
299 * @static
300 * @access public
301 */
302 static function encloseScrub(&$values, $enclosure = "'") {
303 if (empty($values)) {
304 return;
305 }
306
307 foreach ($values as $k => $v) {
308 $values[$k] = preg_replace("/^$enclosure(.*)$enclosure$/", '$1', $v);
309 }
310 }
311
312 /**
313 * setter function
314 *
315 * @param int $max
316 *
317 * @return void
318 * @access public
319 */
320 function setMaxLinesToProcess($max) {
321 $this->_maxLinesToProcess = $max;
322 }
323
324 /**
325 * Determines the file extension based on error code
326 *
327 * @var $type error code constant
328 * @return string
329 * @static
330 */
331 static function errorFileName($type) {
332 $fileName = NULL;
333 if (empty($type)) {
334 return $fileName;
335 }
336
337 $config = CRM_Core_Config::singleton();
338 $fileName = $config->uploadDir . "sqlImport";
339 switch ($type) {
340 case self::ERROR:
341 $fileName .= '.errors';
342 break;
343
344 case self::CONFLICT:
345 $fileName .= '.conflicts';
346 break;
347
348 case self::DUPLICATE:
349 $fileName .= '.duplicates';
350 break;
351
352 case self::NO_MATCH:
353 $fileName .= '.mismatch';
354 break;
355
356 case self::UNPARSED_ADDRESS_WARNING:
357 $fileName .= '.unparsedAddress';
358 break;
359 }
360
361 return $fileName;
362 }
363
364 /**
365 * Determines the file name based on error code
366 *
367 * @var $type error code constant
368 * @return string
369 * @static
370 */
371 static function saveFileName($type) {
372 $fileName = NULL;
373 if (empty($type)) {
374 return $fileName;
375 }
376 switch ($type) {
377 case self::ERROR:
378 $fileName = 'Import_Errors.csv';
379 break;
380
381 case self::CONFLICT:
382 $fileName = 'Import_Conflicts.csv';
383 break;
384
385 case self::DUPLICATE:
386 $fileName = 'Import_Duplicates.csv';
387 break;
388
389 case self::NO_MATCH:
390 $fileName = 'Import_Mismatch.csv';
391 break;
392
393 case self::UNPARSED_ADDRESS_WARNING:
394 $fileName = 'Import_Unparsed_Address.csv';
395 break;
396 }
397
398 return $fileName;
399 }
400
ec3811b1 401}