Commit | Line | Data |
---|---|---|
ec3811b1 CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
ec3811b1 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
ec3811b1 CW |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
ec3811b1 CW |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | ||
37 | abstract 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 | /** | |
100fef9d | 65 | * Total number of non empty lines |
69a4c20a CW |
66 | */ |
67 | protected $_totalCount; | |
68 | ||
69 | /** | |
100fef9d | 70 | * Running total number of valid lines |
69a4c20a CW |
71 | */ |
72 | protected $_validCount; | |
73 | ||
74 | /** | |
100fef9d | 75 | * Running total number of invalid rows |
69a4c20a CW |
76 | */ |
77 | protected $_invalidRowCount; | |
78 | ||
79 | /** | |
100fef9d | 80 | * Maximum number of non-empty/comment lines to process |
69a4c20a CW |
81 | * |
82 | * @var int | |
83 | */ | |
84 | protected $_maxLinesToProcess; | |
85 | ||
86 | /** | |
100fef9d | 87 | * Maximum number of invalid rows to store |
69a4c20a CW |
88 | */ |
89 | protected $_maxErrorCount; | |
90 | ||
91 | /** | |
100fef9d | 92 | * Array of error lines, bounded by MAX_ERROR |
69a4c20a CW |
93 | */ |
94 | protected $_errors; | |
95 | ||
96 | /** | |
100fef9d | 97 | * Total number of conflict lines |
69a4c20a CW |
98 | */ |
99 | protected $_conflictCount; | |
100 | ||
101 | /** | |
100fef9d | 102 | * Array of conflict lines |
69a4c20a CW |
103 | */ |
104 | protected $_conflicts; | |
105 | ||
106 | /** | |
100fef9d | 107 | * Total number of duplicate (from database) lines |
69a4c20a CW |
108 | */ |
109 | protected $_duplicateCount; | |
110 | ||
111 | /** | |
100fef9d | 112 | * Array of duplicate lines |
69a4c20a CW |
113 | */ |
114 | protected $_duplicates; | |
115 | ||
116 | /** | |
100fef9d | 117 | * Running total number of warnings |
69a4c20a CW |
118 | */ |
119 | protected $_warningCount; | |
120 | ||
121 | /** | |
100fef9d | 122 | * Maximum number of warnings to store |
69a4c20a CW |
123 | */ |
124 | protected $_maxWarningCount = self::MAX_WARNINGS; | |
125 | ||
126 | /** | |
100fef9d | 127 | * Array of warning lines, bounded by MAX_WARNING |
69a4c20a CW |
128 | */ |
129 | protected $_warnings; | |
130 | ||
131 | /** | |
100fef9d | 132 | * Array of all the fields that could potentially be part |
69a4c20a CW |
133 | * of this import process |
134 | * @var array | |
135 | */ | |
136 | protected $_fields; | |
137 | ||
138 | /** | |
100fef9d | 139 | * Array of the fields that are actually part of the import process |
69a4c20a CW |
140 | * the position in the array also dictates their position in the import |
141 | * file | |
142 | * @var array | |
143 | */ | |
144 | protected $_activeFields; | |
145 | ||
146 | /** | |
100fef9d | 147 | * Cache the count of active fields |
69a4c20a CW |
148 | * |
149 | * @var int | |
150 | */ | |
151 | protected $_activeFieldCount; | |
152 | ||
153 | /** | |
100fef9d | 154 | * Cache of preview rows |
69a4c20a CW |
155 | * |
156 | * @var array | |
157 | */ | |
158 | protected $_rows; | |
159 | ||
160 | /** | |
100fef9d | 161 | * Filename of error data |
69a4c20a CW |
162 | * |
163 | * @var string | |
164 | */ | |
165 | protected $_errorFileName; | |
166 | ||
167 | /** | |
100fef9d | 168 | * Filename of conflict data |
69a4c20a CW |
169 | * |
170 | * @var string | |
171 | */ | |
172 | protected $_conflictFileName; | |
173 | ||
174 | /** | |
100fef9d | 175 | * Filename of duplicate data |
69a4c20a CW |
176 | * |
177 | * @var string | |
178 | */ | |
179 | protected $_duplicateFileName; | |
180 | ||
181 | /** | |
100fef9d | 182 | * Contact type |
69a4c20a CW |
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(); | |
e0ef6999 EM |
200 | |
201 | /** | |
202 | * @return mixed | |
203 | */ | |
69a4c20a | 204 | abstract function fini(); |
e0ef6999 EM |
205 | |
206 | /** | |
207 | * @param $values | |
208 | * | |
209 | * @return mixed | |
210 | */ | |
69a4c20a | 211 | abstract function mapField(&$values); |
e0ef6999 EM |
212 | |
213 | /** | |
214 | * @param $values | |
215 | * | |
216 | * @return mixed | |
217 | */ | |
69a4c20a | 218 | abstract function preview(&$values); |
e0ef6999 EM |
219 | |
220 | /** | |
221 | * @param $values | |
222 | * | |
223 | * @return mixed | |
224 | */ | |
69a4c20a | 225 | abstract function summary(&$values); |
e0ef6999 EM |
226 | |
227 | /** | |
228 | * @param $onDuplicate | |
229 | * @param $values | |
230 | * | |
231 | * @return mixed | |
232 | */ | |
69a4c20a CW |
233 | abstract function import($onDuplicate, &$values); |
234 | ||
235 | /** | |
236 | * Set and validate field values | |
237 | * | |
77b97be7 EM |
238 | * @param $elements : array |
239 | * @param $erroneousField : reference | |
240 | * | |
241 | * @return int | |
69a4c20a CW |
242 | */ |
243 | function setActiveFieldValues($elements, &$erroneousField) { | |
244 | $maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount; | |
245 | for ($i = 0; $i < $maxCount; $i++) { | |
246 | $this->_activeFields[$i]->setValue($elements[$i]); | |
247 | } | |
248 | ||
249 | // reset all the values that we did not have an equivalent import element | |
250 | for (; $i < $this->_activeFieldCount; $i++) { | |
251 | $this->_activeFields[$i]->resetValue(); | |
252 | } | |
253 | ||
254 | // now validate the fields and return false if error | |
255 | $valid = self::VALID; | |
256 | for ($i = 0; $i < $this->_activeFieldCount; $i++) { | |
257 | if (!$this->_activeFields[$i]->validate()) { | |
258 | // no need to do any more validation | |
259 | $erroneousField = $i; | |
260 | $valid = self::ERROR; | |
261 | break; | |
262 | } | |
263 | } | |
264 | return $valid; | |
265 | } | |
266 | ||
267 | /** | |
268 | * Format the field values for input to the api | |
269 | * | |
270 | * @return array (reference) associative array of name/value pairs | |
271 | * @access public | |
272 | */ | |
273 | function &getActiveFieldParams() { | |
274 | $params = array(); | |
275 | for ($i = 0; $i < $this->_activeFieldCount; $i++) { | |
276 | if (isset($this->_activeFields[$i]->_value) | |
277 | && !isset($params[$this->_activeFields[$i]->_name]) | |
278 | && !isset($this->_activeFields[$i]->_related) | |
279 | ) { | |
280 | ||
281 | $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value; | |
282 | } | |
283 | } | |
284 | return $params; | |
285 | } | |
286 | ||
e0ef6999 EM |
287 | /** |
288 | * @return array | |
289 | */ | |
69a4c20a CW |
290 | function getSelectValues() { |
291 | $values = array(); | |
292 | foreach ($this->_fields as $name => $field) { | |
293 | $values[$name] = $field->_title; | |
294 | } | |
295 | return $values; | |
296 | } | |
297 | ||
e0ef6999 EM |
298 | /** |
299 | * @return array | |
300 | */ | |
69a4c20a CW |
301 | function getSelectTypes() { |
302 | $values = array(); | |
303 | foreach ($this->_fields as $name => $field) { | |
304 | if (isset($field->_hasLocationType)) { | |
305 | $values[$name] = $field->_hasLocationType; | |
306 | } | |
307 | } | |
308 | return $values; | |
309 | } | |
310 | ||
e0ef6999 EM |
311 | /** |
312 | * @return array | |
313 | */ | |
69a4c20a CW |
314 | function getHeaderPatterns() { |
315 | $values = array(); | |
316 | foreach ($this->_fields as $name => $field) { | |
317 | if (isset($field->_headerPattern)) { | |
318 | $values[$name] = $field->_headerPattern; | |
319 | } | |
320 | } | |
321 | return $values; | |
322 | } | |
323 | ||
e0ef6999 EM |
324 | /** |
325 | * @return array | |
326 | */ | |
69a4c20a CW |
327 | function getDataPatterns() { |
328 | $values = array(); | |
329 | foreach ($this->_fields as $name => $field) { | |
330 | $values[$name] = $field->_dataPattern; | |
331 | } | |
332 | return $values; | |
333 | } | |
334 | ||
335 | /** | |
336 | * Remove single-quote enclosures from a value array (row) | |
337 | * | |
338 | * @param array $values | |
339 | * @param string $enclosure | |
340 | * | |
341 | * @return void | |
342 | * @static | |
343 | * @access public | |
344 | */ | |
345 | static function encloseScrub(&$values, $enclosure = "'") { | |
346 | if (empty($values)) { | |
347 | return; | |
348 | } | |
349 | ||
350 | foreach ($values as $k => $v) { | |
351 | $values[$k] = preg_replace("/^$enclosure(.*)$enclosure$/", '$1', $v); | |
352 | } | |
353 | } | |
354 | ||
355 | /** | |
100fef9d | 356 | * Setter function |
69a4c20a CW |
357 | * |
358 | * @param int $max | |
359 | * | |
360 | * @return void | |
361 | * @access public | |
362 | */ | |
363 | function setMaxLinesToProcess($max) { | |
364 | $this->_maxLinesToProcess = $max; | |
365 | } | |
366 | ||
367 | /** | |
368 | * Determines the file extension based on error code | |
369 | * | |
370 | * @var $type error code constant | |
371 | * @return string | |
372 | * @static | |
373 | */ | |
374 | static function errorFileName($type) { | |
375 | $fileName = NULL; | |
376 | if (empty($type)) { | |
377 | return $fileName; | |
378 | } | |
379 | ||
380 | $config = CRM_Core_Config::singleton(); | |
381 | $fileName = $config->uploadDir . "sqlImport"; | |
382 | switch ($type) { | |
383 | case self::ERROR: | |
384 | $fileName .= '.errors'; | |
385 | break; | |
386 | ||
387 | case self::CONFLICT: | |
388 | $fileName .= '.conflicts'; | |
389 | break; | |
390 | ||
391 | case self::DUPLICATE: | |
392 | $fileName .= '.duplicates'; | |
393 | break; | |
394 | ||
395 | case self::NO_MATCH: | |
396 | $fileName .= '.mismatch'; | |
397 | break; | |
398 | ||
399 | case self::UNPARSED_ADDRESS_WARNING: | |
400 | $fileName .= '.unparsedAddress'; | |
401 | break; | |
402 | } | |
403 | ||
404 | return $fileName; | |
405 | } | |
406 | ||
407 | /** | |
408 | * Determines the file name based on error code | |
409 | * | |
410 | * @var $type error code constant | |
411 | * @return string | |
412 | * @static | |
413 | */ | |
414 | static function saveFileName($type) { | |
415 | $fileName = NULL; | |
416 | if (empty($type)) { | |
417 | return $fileName; | |
418 | } | |
419 | switch ($type) { | |
420 | case self::ERROR: | |
421 | $fileName = 'Import_Errors.csv'; | |
422 | break; | |
423 | ||
424 | case self::CONFLICT: | |
425 | $fileName = 'Import_Conflicts.csv'; | |
426 | break; | |
427 | ||
428 | case self::DUPLICATE: | |
429 | $fileName = 'Import_Duplicates.csv'; | |
430 | break; | |
431 | ||
432 | case self::NO_MATCH: | |
433 | $fileName = 'Import_Mismatch.csv'; | |
434 | break; | |
435 | ||
436 | case self::UNPARSED_ADDRESS_WARNING: | |
437 | $fileName = 'Import_Unparsed_Address.csv'; | |
438 | break; | |
439 | } | |
440 | ||
441 | return $fileName; | |
442 | } | |
443 | ||
ec3811b1 | 444 | } |