Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-07-31-15-53-16
[civicrm-core.git] / CRM / Contact / Import / Parser.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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
ec3811b1 37abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser {
6a488035
TO
38
39 protected $_tableName;
40
41 /**#@+
42 * @access protected
43 * @var integer
44 */
45
46 /**
47 * total number of lines in file
48 */
49 protected $_rowCount;
50
6a488035
TO
51 /**
52 * running total number of un matched Conact
53 */
54 protected $_unMatchCount;
55
56 /**
57 * array of unmatched lines
58 */
59 protected $_unMatch;
60
6a488035
TO
61 /**
62 * total number of contacts with unparsed addresses
63 */
64 protected $_unparsedAddressCount;
65
6a488035
TO
66 /**
67 * filename of mismatch data
68 *
69 * @var string
70 */
71 protected $_misMatchFilemName;
72
73 protected $_primaryKeyName;
74 protected $_statusFieldName;
75
6a488035
TO
76 /**
77 * on duplicate
78 *
79 * @var int
80 */
81 public $_onDuplicate;
82
83 /**
84 * dedupe rule group id to use if set
85 *
86 * @var int
87 */
03e04002 88 public $_dedupeRuleGroupID = NULL;
6a488035 89
6a488035
TO
90 function run($tableName,
91 &$mapper,
52892e8b
CW
92 $mode = self::MODE_PREVIEW,
93 $contactType = self::CONTACT_INDIVIDUAL,
94 $primaryKeyName = '_id',
95 $statusFieldName = '_status',
96 $onDuplicate = self::DUPLICATE_SKIP,
97 $statusID = NULL,
98 $totalRowCount = NULL,
99 $doGeocodeAddress = FALSE,
100 $timeout = CRM_Contact_Import_Parser::DEFAULT_TIMEOUT,
101 $contactSubType = NULL,
6a488035
TO
102 $dedupeRuleGroupID = NULL
103 ) {
104
105 // TODO: Make the timeout actually work
106 $this->_onDuplicate = $onDuplicate;
107 $this->_dedupeRuleGroupID = $dedupeRuleGroupID;
108
109 switch ($contactType) {
a05662ef 110 case CRM_Import_Parser::CONTACT_INDIVIDUAL:
6a488035
TO
111 $this->_contactType = 'Individual';
112 break;
113
a05662ef 114 case CRM_Import_Parser::CONTACT_HOUSEHOLD:
6a488035
TO
115 $this->_contactType = 'Household';
116 break;
117
a05662ef 118 case CRM_Import_Parser::CONTACT_ORGANIZATION:
6a488035
TO
119 $this->_contactType = 'Organization';
120 }
121
122 $this->_contactSubType = $contactSubType;
123
124 $this->init();
125
126 $this->_rowCount = $this->_warningCount = 0;
127 $this->_invalidRowCount = $this->_validCount = 0;
128 $this->_totalCount = $this->_conflictCount = 0;
129
130 $this->_errors = array();
131 $this->_warnings = array();
132 $this->_conflicts = array();
133 $this->_unparsedAddresses = array();
134
135 $status = '';
136
137 $this->_tableName = $tableName;
138 $this->_primaryKeyName = $primaryKeyName;
139 $this->_statusFieldName = $statusFieldName;
140
141 if ($mode == self::MODE_MAPFIELD) {
142 $this->_rows = array();
143 }
144 else {
145 $this->_activeFieldCount = count($this->_activeFields);
146 }
147
148 if ($mode == self::MODE_IMPORT) {
149 //get the key of email field
150 foreach ($mapper as $key => $value) {
151 if (strtolower($value) == 'email') {
152 $emailKey = $key;
153 break;
154 }
155 }
156 }
157
158 if ($statusID) {
159 $skip = 50;
160 // $skip = 1;
52892e8b 161 $config = CRM_Core_Config::singleton();
6a488035 162 $statusFile = "{$config->uploadDir}status_{$statusID}.txt";
52892e8b 163 $status = "<div class='description'>&nbsp; " . ts('No processing status reported yet.') . "</div>";
6a488035
TO
164
165 //do not force the browser to display the save dialog, CRM-7640
166 $contents = json_encode(array(0, $status));
167
168 file_put_contents($statusFile, $contents);
169
170 $startTimestamp = $currTimestamp = $prevTimestamp = time();
171 }
172
173 // get the contents of the temp. import table
174 $query = "SELECT * FROM $tableName";
175 if ($mode == self::MODE_IMPORT) {
176 $query .= " WHERE $statusFieldName = 'NEW'";
177 }
52892e8b
CW
178 $dao = new CRM_Core_DAO();
179 $db = $dao->getDatabaseConnection();
6a488035
TO
180 $result = $db->query($query);
181
182 while ($values = $result->fetchRow(DB_FETCHMODE_ORDERED)) {
183 $this->_rowCount++;
184
185 /* trim whitespace around the values */
186
187 $empty = TRUE;
188 foreach ($values as $k => $v) {
189 $values[$k] = trim($v, " \t\r\n");
190 }
191 if (CRM_Utils_System::isNull($values)) {
192 continue;
193 }
194
195 $this->_totalCount++;
196
197 if ($mode == self::MODE_MAPFIELD) {
198 $returnCode = $this->mapField($values);
199 }
200 elseif ($mode == self::MODE_PREVIEW) {
201 $returnCode = $this->preview($values);
202 }
203 elseif ($mode == self::MODE_SUMMARY) {
204 $returnCode = $this->summary($values);
205 }
206 elseif ($mode == self::MODE_IMPORT) {
207 //print "Running parser in import mode<br/>\n";
208 $returnCode = $this->import($onDuplicate, $values, $doGeocodeAddress);
209 if ($statusID && (($this->_rowCount % $skip) == 0)) {
210 $currTimestamp = time();
52892e8b
CW
211 $totalTime = ($currTimestamp - $startTimestamp);
212 $time = ($currTimestamp - $prevTimestamp);
213 $recordsLeft = $totalRowCount - $this->_rowCount;
6a488035
TO
214 if ($recordsLeft < 0) {
215 $recordsLeft = 0;
216 }
217 $estimatedTime = ($recordsLeft / $skip) * $time;
52892e8b 218 $estMinutes = floor($estimatedTime / 60);
6a488035
TO
219 $timeFormatted = '';
220 if ($estMinutes > 1) {
221 $timeFormatted = $estMinutes . ' ' . ts('minutes') . ' ';
222 $estimatedTime = $estimatedTime - ($estMinutes * 60);
223 }
224 $timeFormatted .= round($estimatedTime) . ' ' . ts('seconds');
225 $processedPercent = (int )(($this->_rowCount * 100) / $totalRowCount);
226 $statusMsg = ts('%1 of %2 records - %3 remaining',
227 array(1 => $this->_rowCount, 2 => $totalRowCount, 3 => $timeFormatted)
228 );
229 $status = "
230<div class=\"description\">
231&nbsp; <strong>{$statusMsg}</strong>
232</div>
233";
234
235 $contents = json_encode (array($processedPercent, $status));
236
237 file_put_contents($statusFile, $contents);
238
239 $prevTimestamp = $currTimestamp;
240 }
241 // sleep(1);
242 }
243 else {
244 $returnCode = self::ERROR;
245 }
246
247 // note that a line could be valid but still produce a warning
248 if ($returnCode & self::VALID) {
249 $this->_validCount++;
250 if ($mode == self::MODE_MAPFIELD) {
251 $this->_rows[] = $values;
252 $this->_activeFieldCount = max($this->_activeFieldCount, count($values));
253 }
254 }
255
256 if ($returnCode & self::WARNING) {
257 $this->_warningCount++;
258 if ($this->_warningCount < $this->_maxWarningCount) {
259 $this->_warningCount[] = $line;
260 }
261 }
262
263 if ($returnCode & self::ERROR) {
264 $this->_invalidRowCount++;
265 if ($this->_invalidRowCount < $this->_maxErrorCount) {
266 array_unshift($values, $this->_rowCount);
267 $this->_errors[] = $values;
268 }
269 }
270
271 if ($returnCode & self::CONFLICT) {
272 $this->_conflictCount++;
273 array_unshift($values, $this->_rowCount);
274 $this->_conflicts[] = $values;
275 }
276
277 if ($returnCode & self::NO_MATCH) {
278 $this->_unMatchCount++;
279 array_unshift($values, $this->_rowCount);
280 $this->_unMatch[] = $values;
281 }
282
283 if ($returnCode & self::DUPLICATE) {
284 if ($returnCode & self::MULTIPLE_DUPE) {
285 /* TODO: multi-dupes should be counted apart from singles
286 * on non-skip action */
287 }
288 $this->_duplicateCount++;
289 array_unshift($values, $this->_rowCount);
290 $this->_duplicates[] = $values;
291 if ($onDuplicate != self::DUPLICATE_SKIP) {
292 $this->_validCount++;
293 }
294 }
295
296 if ($returnCode & self::UNPARSED_ADDRESS_WARNING) {
297 $this->_unparsedAddressCount++;
298 array_unshift($values, $this->_rowCount);
299 $this->_unparsedAddresses[] = $values;
300 }
301 // we give the derived class a way of aborting the process
302 // note that the return code could be multiple code or'ed together
303 if ($returnCode & self::STOP) {
304 break;
305 }
306
307 // if we are done processing the maxNumber of lines, break
308 if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) {
309 break;
310 }
311
312 // clean up memory from dao's
313 CRM_Core_DAO::freeResult();
314
315 // see if we've hit our timeout yet
316 /* if ( $the_thing_with_the_stuff ) {
317 do_something( );
318 } */
319 }
320
321
322 if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) {
323 $customHeaders = $mapper;
324
325 $customfields = CRM_Core_BAO_CustomField::getFields($this->_contactType);
326 foreach ($customHeaders as $key => $value) {
327 if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) {
328 $customHeaders[$key] = $customfields[$id][0];
329 }
330 }
331
332 if ($this->_invalidRowCount) {
333 // removed view url for invlaid contacts
334 $headers = array_merge(array(ts('Line Number'),
335 ts('Reason'),
336 ),
337 $customHeaders
338 );
339 $this->_errorFileName = self::errorFileName(self::ERROR);
340 self::exportCSV($this->_errorFileName, $headers, $this->_errors);
341 }
342 if ($this->_conflictCount) {
343 $headers = array_merge(array(ts('Line Number'),
344 ts('Reason'),
345 ),
346 $customHeaders
347 );
348 $this->_conflictFileName = self::errorFileName(self::CONFLICT);
349 self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts);
350 }
351 if ($this->_duplicateCount) {
352 $headers = array_merge(array(ts('Line Number'),
353 ts('View Contact URL'),
354 ),
355 $customHeaders
356 );
357
358 $this->_duplicateFileName = self::errorFileName(self::DUPLICATE);
359 self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates);
360 }
361 if ($this->_unMatchCount) {
362 $headers = array_merge(array(ts('Line Number'),
363 ts('Reason'),
364 ),
365 $customHeaders
366 );
367
368 $this->_misMatchFilemName = self::errorFileName(self::NO_MATCH);
369 self::exportCSV($this->_misMatchFilemName, $headers, $this->_unMatch);
370 }
371 if ($this->_unparsedAddressCount) {
372 $headers = array_merge(array(ts('Line Number'),
373 ts('Contact Edit URL'),
374 ),
375 $customHeaders
376 );
377 $this->_errorFileName = self::errorFileName(self::UNPARSED_ADDRESS_WARNING);
378 self::exportCSV($this->_errorFileName, $headers, $this->_unparsedAddresses);
379 }
380 }
381 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
382 return $this->fini();
383 }
384
6a488035
TO
385 /**
386 * Given a list of the importable field keys that the user has selected
387 * set the active fields array to this list
388 *
389 * @param array mapped array of values
390 *
391 * @return void
392 * @access public
393 */
394 function setActiveFields($fieldKeys) {
395 $this->_activeFieldCount = count($fieldKeys);
396 foreach ($fieldKeys as $key) {
397 if (empty($this->_fields[$key])) {
719a6fec 398 $this->_activeFields[] = new CRM_Contact_Import_Field('', ts('- do not import -'));
6a488035
TO
399 }
400 else {
401 $this->_activeFields[] = clone($this->_fields[$key]);
402 }
403 }
404 }
405
6a488035
TO
406 function setActiveFieldLocationTypes($elements) {
407 for ($i = 0; $i < count($elements); $i++) {
408 $this->_activeFields[$i]->_hasLocationType = $elements[$i];
409 }
410 }
411
412 function setActiveFieldPhoneTypes($elements) {
413 for ($i = 0; $i < count($elements); $i++) {
414 $this->_activeFields[$i]->_phoneType = $elements[$i];
415 }
416 }
417
418 function setActiveFieldWebsiteTypes($elements) {
419 for ($i = 0; $i < count($elements); $i++) {
420 $this->_activeFields[$i]->_websiteType = $elements[$i];
421 }
422 }
423
424 /**
425 * Function to set IM Service Provider type fields
426 *
427 * @param array $elements IM service provider type ids
428 *
429 * @return void
430 * @access public
431 */
432 function setActiveFieldImProviders($elements) {
433 for ($i = 0; $i < count($elements); $i++) {
434 $this->_activeFields[$i]->_imProvider = $elements[$i];
435 }
436 }
437
438 function setActiveFieldRelated($elements) {
439 for ($i = 0; $i < count($elements); $i++) {
440 $this->_activeFields[$i]->_related = $elements[$i];
441 }
442 }
443
444 function setActiveFieldRelatedContactType($elements) {
445 for ($i = 0; $i < count($elements); $i++) {
446 $this->_activeFields[$i]->_relatedContactType = $elements[$i];
447 }
448 }
449
450 function setActiveFieldRelatedContactDetails($elements) {
451 for ($i = 0; $i < count($elements); $i++) {
452 $this->_activeFields[$i]->_relatedContactDetails = $elements[$i];
453 }
454 }
455
456 function setActiveFieldRelatedContactLocType($elements) {
457 for ($i = 0; $i < count($elements); $i++) {
458 $this->_activeFields[$i]->_relatedContactLocType = $elements[$i];
459 }
460 }
461
462 function setActiveFieldRelatedContactPhoneType($elements) {
463 for ($i = 0; $i < count($elements); $i++) {
464 $this->_activeFields[$i]->_relatedContactPhoneType = $elements[$i];
465 }
466 }
467
468 function setActiveFieldRelatedContactWebsiteType($elements) {
469 for ($i = 0; $i < count($elements); $i++) {
470 $this->_activeFields[$i]->_relatedContactWebsiteType = $elements[$i];
471 }
472 }
473
474 /**
475 * Function to set IM Service Provider type fields for related contacts
476 *
477 * @param array $elements IM service provider type ids of related contact
478 *
479 * @return void
480 * @access public
481 */
482 function setActiveFieldRelatedContactImProvider($elements) {
483 for ($i = 0; $i < count($elements); $i++) {
484 $this->_activeFields[$i]->_relatedContactImProvider = $elements[$i];
485 }
486 }
487
488 /**
489 * function to format the field values for input to the api
490 *
491 * @return array (reference ) associative array of name/value pairs
492 * @access public
493 */
494 function &getActiveFieldParams() {
495 $params = array();
496
497 //CRM_Core_Error::debug( 'Count', $this->_activeFieldCount );
498 for ($i = 0; $i < $this->_activeFieldCount; $i++) {
499 if ($this->_activeFields[$i]->_name == 'do_not_import') {
500 continue;
501 }
502
503 if (isset($this->_activeFields[$i]->_value)) {
504 if (isset($this->_activeFields[$i]->_hasLocationType)) {
505 if (!isset($params[$this->_activeFields[$i]->_name])) {
506 $params[$this->_activeFields[$i]->_name] = array();
507 }
508
509 $value = array(
510 $this->_activeFields[$i]->_name =>
511 $this->_activeFields[$i]->_value,
512 'location_type_id' =>
513 $this->_activeFields[$i]->_hasLocationType,
514 );
515
516 if (isset($this->_activeFields[$i]->_phoneType)) {
517 $value['phone_type_id'] = $this->_activeFields[$i]->_phoneType;
518 }
519
520 // get IM service Provider type id
521 if (isset($this->_activeFields[$i]->_imProvider)) {
522 $value['provider_id'] = $this->_activeFields[$i]->_imProvider;
523 }
524
525 $params[$this->_activeFields[$i]->_name][] = $value;
526 }
527 elseif (isset($this->_activeFields[$i]->_websiteType)) {
528 $value = array(
529 $this->_activeFields[$i]->_name => $this->_activeFields[$i]->_value,
530 'website_type_id' => $this->_activeFields[$i]->_websiteType,
531 );
532
533 $params[$this->_activeFields[$i]->_name][] = $value;
534 }
535
536 if (!isset($params[$this->_activeFields[$i]->_name])) {
537 if (!isset($this->_activeFields[$i]->_related)) {
538 $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value;
539 }
540 }
541
542 //minor fix for CRM-4062
543 if (isset($this->_activeFields[$i]->_related)) {
544 if (!isset($params[$this->_activeFields[$i]->_related])) {
545 $params[$this->_activeFields[$i]->_related] = array();
546 }
547
548 if (!isset($params[$this->_activeFields[$i]->_related]['contact_type']) && !empty($this->_activeFields[$i]->_relatedContactType)) {
549 $params[$this->_activeFields[$i]->_related]['contact_type'] = $this->_activeFields[$i]->_relatedContactType;
550 }
551
552 if (isset($this->_activeFields[$i]->_relatedContactLocType) && !empty($this->_activeFields[$i]->_value)) {
553 if (!is_array($params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails])) {
554 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails] = array();
555 }
556 $value = array(
557 $this->_activeFields[$i]->_relatedContactDetails => $this->_activeFields[$i]->_value,
558 'location_type_id' => $this->_activeFields[$i]->_relatedContactLocType,
559 );
560
561 if (isset($this->_activeFields[$i]->_relatedContactPhoneType)) {
562 $value['phone_type_id'] = $this->_activeFields[$i]->_relatedContactPhoneType;
563 }
564
565 // get IM service Provider type id for related contact
566 if (isset($this->_activeFields[$i]->_relatedContactImProvider)) {
567 $value['provider_id'] = $this->_activeFields[$i]->_relatedContactImProvider;
568 }
569
570 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails][] = $value;
571 }
572 elseif (isset($this->_activeFields[$i]->_relatedContactWebsiteType)) {
573 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails][] = array(
574 'url' => $this->_activeFields[$i]->_value,
575 'website_type_id' => $this->_activeFields[$i]->_relatedContactWebsiteType,
576 );
577 }
578 else {
579 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails] = $this->_activeFields[$i]->_value;
580 }
581 }
582 }
583 }
584
585 return $params;
586 }
587
6a488035
TO
588 function getColumnPatterns() {
589 $values = array();
590 foreach ($this->_fields as $name => $field) {
591 $values[$name] = $field->_columnPattern;
592 }
593 return $values;
594 }
595
6a488035
TO
596 function addField($name, $title, $type = CRM_Utils_Type::T_INT,
597 $headerPattern = '//', $dataPattern = '//',
598 $hasLocationType = FALSE
599 ) {
719a6fec 600 $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
6a488035 601 if (empty($name)) {
719a6fec 602 $this->_fields['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
6a488035
TO
603 }
604 }
605
6a488035
TO
606 /**
607 * Store parser values
608 *
609 * @param CRM_Core_Session $store
610 *
611 * @return void
612 * @access public
613 */
614 function set($store, $mode = self::MODE_SUMMARY) {
615 $store->set('rowCount', $this->_rowCount);
616 $store->set('fields', $this->getSelectValues());
617 $store->set('fieldTypes', $this->getSelectTypes());
618
619 $store->set('columnPatterns', $this->getColumnPatterns());
620 $store->set('dataPatterns', $this->getDataPatterns());
621 $store->set('columnCount', $this->_activeFieldCount);
622
623 $store->set('totalRowCount', $this->_totalCount);
624 $store->set('validRowCount', $this->_validCount);
625 $store->set('invalidRowCount', $this->_invalidRowCount);
626 $store->set('conflictRowCount', $this->_conflictCount);
627 $store->set('unMatchCount', $this->_unMatchCount);
628
629 switch ($this->_contactType) {
630 case 'Individual':
a05662ef 631 $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
6a488035
TO
632 break;
633
634 case 'Household':
a05662ef 635 $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
6a488035
TO
636 break;
637
638 case 'Organization':
a05662ef 639 $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
6a488035
TO
640 }
641
642 if ($this->_invalidRowCount) {
643 $store->set('errorsFileName', $this->_errorFileName);
644 }
645 if ($this->_conflictCount) {
646 $store->set('conflictsFileName', $this->_conflictFileName);
647 }
648 if (isset($this->_rows) && !empty($this->_rows)) {
649 $store->set('dataValues', $this->_rows);
650 }
651
652 if ($this->_unMatchCount) {
653 $store->set('mismatchFileName', $this->_misMatchFilemName);
654 }
655
656 if ($mode == self::MODE_IMPORT) {
657 $store->set('duplicateRowCount', $this->_duplicateCount);
658 $store->set('unparsedAddressCount', $this->_unparsedAddressCount);
659 if ($this->_duplicateCount) {
660 $store->set('duplicatesFileName', $this->_duplicateFileName);
661 }
662 if ($this->_unparsedAddressCount) {
663 $store->set('errorsFileName', $this->_errorFileName);
664 }
665 }
666 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
667 }
668
669 /**
670 * Export data to a CSV file
671 *
672 * @param string $filename
673 * @param array $header
674 * @param data $data
675 *
676 * @return void
677 * @access public
678 */
679 static function exportCSV($fileName, $header, $data) {
680
681 if (file_exists($fileName) && !is_writable($fileName)) {
682 CRM_Core_Error::movedSiteError($fileName);
683 }
684 //hack to remove '_status', '_statusMsg' and '_id' from error file
685 $errorValues = array();
686 $dbRecordStatus = array('IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW');
687 foreach ($data as $rowCount => $rowValues) {
688 $count = 0;
689 foreach ($rowValues as $key => $val) {
690 if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) {
691 break;
692 }
693 $errorValues[$rowCount][$key] = $val;
694 $count++;
695 }
696 }
697 $data = $errorValues;
698
699 $output = array();
700 $fd = fopen($fileName, 'w');
701
702 foreach ($header as $key => $value) {
703 $header[$key] = "\"$value\"";
704 }
705 $config = CRM_Core_Config::singleton();
706 $output[] = implode($config->fieldSeparator, $header);
707
708 foreach ($data as $datum) {
709 foreach ($datum as $key => $value) {
710 $datum[$key] = "\"$value\"";
711 }
712 $output[] = implode($config->fieldSeparator, $datum);
713 }
714 fwrite($fd, implode("\n", $output));
715 fclose($fd);
716 }
717
718 /**
719 * Update the record with PK $id in the import database table
720 *
721 * @param int $id
722 * @param array $params
723 *
724 * @return void
725 * @access public
726 */
727 public function updateImportRecord($id, &$params) {
728 $statusFieldName = $this->_statusFieldName;
729 $primaryKeyName = $this->_primaryKeyName;
730
731 if ($statusFieldName && $primaryKeyName) {
732 $dao = new CRM_Core_DAO();
733 $db = $dao->getDatabaseConnection();
734
735 $query = "UPDATE $this->_tableName
52892e8b 736 SET $statusFieldName = ?,
6a488035 737 ${statusFieldName}Msg = ?
52892e8b 738 WHERE $primaryKeyName = ?";
6a488035
TO
739 $args = array(
740 $params[$statusFieldName],
741 CRM_Utils_Array::value("${statusFieldName}Msg", $params),
742 $id,
743 );
744
745 //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>";
746
747 $db->query($query, $args);
748 }
749 }
750
6a488035
TO
751}
752