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