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