Merge pull request #3223 from lcdservices/CRM-14672
[civicrm-core.git] / CRM / Import / Parser.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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;
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 * @return int
213 */
214 function setActiveFieldValues($elements, &$erroneousField) {
215 $maxCount = count($elements) < $this->_activeFieldCount ? count($elements) : $this->_activeFieldCount;
216 for ($i = 0; $i < $maxCount; $i++) {
217 $this->_activeFields[$i]->setValue($elements[$i]);
218 }
219
220 // reset all the values that we did not have an equivalent import element
221 for (; $i < $this->_activeFieldCount; $i++) {
222 $this->_activeFields[$i]->resetValue();
223 }
224
225 // now validate the fields and return false if error
226 $valid = self::VALID;
227 for ($i = 0; $i < $this->_activeFieldCount; $i++) {
228 if (!$this->_activeFields[$i]->validate()) {
229 // no need to do any more validation
230 $erroneousField = $i;
231 $valid = self::ERROR;
232 break;
233 }
234 }
235 return $valid;
236 }
237
238 /**
239 * Format the field values for input to the api
240 *
241 * @return array (reference) associative array of name/value pairs
242 * @access public
243 */
244 function &getActiveFieldParams() {
245 $params = array();
246 for ($i = 0; $i < $this->_activeFieldCount; $i++) {
247 if (isset($this->_activeFields[$i]->_value)
248 && !isset($params[$this->_activeFields[$i]->_name])
249 && !isset($this->_activeFields[$i]->_related)
250 ) {
251
252 $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value;
253 }
254 }
255 return $params;
256 }
257
258 function getSelectValues() {
259 $values = array();
260 foreach ($this->_fields as $name => $field) {
261 $values[$name] = $field->_title;
262 }
263 return $values;
264 }
265
266 function getSelectTypes() {
267 $values = array();
268 foreach ($this->_fields as $name => $field) {
269 if (isset($field->_hasLocationType)) {
270 $values[$name] = $field->_hasLocationType;
271 }
272 }
273 return $values;
274 }
275
276 function getHeaderPatterns() {
277 $values = array();
278 foreach ($this->_fields as $name => $field) {
279 if (isset($field->_headerPattern)) {
280 $values[$name] = $field->_headerPattern;
281 }
282 }
283 return $values;
284 }
285
286 function getDataPatterns() {
287 $values = array();
288 foreach ($this->_fields as $name => $field) {
289 $values[$name] = $field->_dataPattern;
290 }
291 return $values;
292 }
293
294 /**
295 * Remove single-quote enclosures from a value array (row)
296 *
297 * @param array $values
298 * @param string $enclosure
299 *
300 * @return void
301 * @static
302 * @access public
303 */
304 static function encloseScrub(&$values, $enclosure = "'") {
305 if (empty($values)) {
306 return;
307 }
308
309 foreach ($values as $k => $v) {
310 $values[$k] = preg_replace("/^$enclosure(.*)$enclosure$/", '$1', $v);
311 }
312 }
313
314 /**
315 * setter function
316 *
317 * @param int $max
318 *
319 * @return void
320 * @access public
321 */
322 function setMaxLinesToProcess($max) {
323 $this->_maxLinesToProcess = $max;
324 }
325
326 /**
327 * Determines the file extension based on error code
328 *
329 * @var $type error code constant
330 * @return string
331 * @static
332 */
333 static function errorFileName($type) {
334 $fileName = NULL;
335 if (empty($type)) {
336 return $fileName;
337 }
338
339 $config = CRM_Core_Config::singleton();
340 $fileName = $config->uploadDir . "sqlImport";
341 switch ($type) {
342 case self::ERROR:
343 $fileName .= '.errors';
344 break;
345
346 case self::CONFLICT:
347 $fileName .= '.conflicts';
348 break;
349
350 case self::DUPLICATE:
351 $fileName .= '.duplicates';
352 break;
353
354 case self::NO_MATCH:
355 $fileName .= '.mismatch';
356 break;
357
358 case self::UNPARSED_ADDRESS_WARNING:
359 $fileName .= '.unparsedAddress';
360 break;
361 }
362
363 return $fileName;
364 }
365
366 /**
367 * Determines the file name based on error code
368 *
369 * @var $type error code constant
370 * @return string
371 * @static
372 */
373 static function saveFileName($type) {
374 $fileName = NULL;
375 if (empty($type)) {
376 return $fileName;
377 }
378 switch ($type) {
379 case self::ERROR:
380 $fileName = 'Import_Errors.csv';
381 break;
382
383 case self::CONFLICT:
384 $fileName = 'Import_Conflicts.csv';
385 break;
386
387 case self::DUPLICATE:
388 $fileName = 'Import_Duplicates.csv';
389 break;
390
391 case self::NO_MATCH:
392 $fileName = 'Import_Mismatch.csv';
393 break;
394
395 case self::UNPARSED_ADDRESS_WARNING:
396 $fileName = 'Import_Unparsed_Address.csv';
397 break;
398 }
399
400 return $fileName;
401 }
402
403 }