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