commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Contact / Import / Parser.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 public 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 $fieldKeys
409 * Mapped array of values.
410 */
411 public 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 public 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 public 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 public 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
457 * IM service provider type ids.
458 *
459 * @return void
460 */
461 public 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 public 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 public 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 public 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 public 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 public 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 public 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
525 * IM service provider type ids of related contact.
526 *
527 * @return void
528 */
529 public 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
539 * (reference ) associative array of name/value pairs
540 */
541 public 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 => $this->_activeFields[$i]->_value,
557 'location_type_id' => $this->_activeFields[$i]->_hasLocationType,
558 );
559
560 if (isset($this->_activeFields[$i]->_phoneType)) {
561 $value['phone_type_id'] = $this->_activeFields[$i]->_phoneType;
562 }
563
564 // get IM service Provider type id
565 if (isset($this->_activeFields[$i]->_imProvider)) {
566 $value['provider_id'] = $this->_activeFields[$i]->_imProvider;
567 }
568
569 $params[$this->_activeFields[$i]->_name][] = $value;
570 }
571 elseif (isset($this->_activeFields[$i]->_websiteType)) {
572 $value = array(
573 $this->_activeFields[$i]->_name => $this->_activeFields[$i]->_value,
574 'website_type_id' => $this->_activeFields[$i]->_websiteType,
575 );
576
577 $params[$this->_activeFields[$i]->_name][] = $value;
578 }
579
580 if (!isset($params[$this->_activeFields[$i]->_name])) {
581 if (!isset($this->_activeFields[$i]->_related)) {
582 $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value;
583 }
584 }
585
586 //minor fix for CRM-4062
587 if (isset($this->_activeFields[$i]->_related)) {
588 if (!isset($params[$this->_activeFields[$i]->_related])) {
589 $params[$this->_activeFields[$i]->_related] = array();
590 }
591
592 if (!isset($params[$this->_activeFields[$i]->_related]['contact_type']) && !empty($this->_activeFields[$i]->_relatedContactType)) {
593 $params[$this->_activeFields[$i]->_related]['contact_type'] = $this->_activeFields[$i]->_relatedContactType;
594 }
595
596 if (isset($this->_activeFields[$i]->_relatedContactLocType) && !empty($this->_activeFields[$i]->_value)) {
597 if (!empty($params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails]) &&
598 !is_array($params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails])
599 ) {
600 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails] = array();
601 }
602 $value = array(
603 $this->_activeFields[$i]->_relatedContactDetails => $this->_activeFields[$i]->_value,
604 'location_type_id' => $this->_activeFields[$i]->_relatedContactLocType,
605 );
606
607 if (isset($this->_activeFields[$i]->_relatedContactPhoneType)) {
608 $value['phone_type_id'] = $this->_activeFields[$i]->_relatedContactPhoneType;
609 }
610
611 // get IM service Provider type id for related contact
612 if (isset($this->_activeFields[$i]->_relatedContactImProvider)) {
613 $value['provider_id'] = $this->_activeFields[$i]->_relatedContactImProvider;
614 }
615
616 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails][] = $value;
617 }
618 elseif (isset($this->_activeFields[$i]->_relatedContactWebsiteType)) {
619 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails][] = array(
620 'url' => $this->_activeFields[$i]->_value,
621 'website_type_id' => $this->_activeFields[$i]->_relatedContactWebsiteType,
622 );
623 }
624 else {
625 $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails] = $this->_activeFields[$i]->_value;
626 }
627 }
628 }
629 }
630
631 return $params;
632 }
633
634 /**
635 * @return array
636 */
637 public function getColumnPatterns() {
638 $values = array();
639 foreach ($this->_fields as $name => $field) {
640 $values[$name] = $field->_columnPattern;
641 }
642 return $values;
643 }
644
645 /**
646 * @param string $name
647 * @param $title
648 * @param int $type
649 * @param string $headerPattern
650 * @param string $dataPattern
651 * @param bool $hasLocationType
652 */
653 public function addField(
654 $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 */
673 public function set($store, $mode = self::MODE_SUMMARY) {
674 $store->set('rowCount', $this->_rowCount);
675 $store->set('fields', $this->getSelectValues());
676 $store->set('fieldTypes', $this->getSelectTypes());
677
678 $store->set('columnPatterns', $this->getColumnPatterns());
679 $store->set('dataPatterns', $this->getDataPatterns());
680 $store->set('columnCount', $this->_activeFieldCount);
681
682 $store->set('totalRowCount', $this->_totalCount);
683 $store->set('validRowCount', $this->_validCount);
684 $store->set('invalidRowCount', $this->_invalidRowCount);
685 $store->set('conflictRowCount', $this->_conflictCount);
686 $store->set('unMatchCount', $this->_unMatchCount);
687
688 switch ($this->_contactType) {
689 case 'Individual':
690 $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
691 break;
692
693 case 'Household':
694 $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
695 break;
696
697 case 'Organization':
698 $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
699 }
700
701 if ($this->_invalidRowCount) {
702 $store->set('errorsFileName', $this->_errorFileName);
703 }
704 if ($this->_conflictCount) {
705 $store->set('conflictsFileName', $this->_conflictFileName);
706 }
707 if (isset($this->_rows) && !empty($this->_rows)) {
708 $store->set('dataValues', $this->_rows);
709 }
710
711 if ($this->_unMatchCount) {
712 $store->set('mismatchFileName', $this->_misMatchFilemName);
713 }
714
715 if ($mode == self::MODE_IMPORT) {
716 $store->set('duplicateRowCount', $this->_duplicateCount);
717 $store->set('unparsedAddressCount', $this->_unparsedAddressCount);
718 if ($this->_duplicateCount) {
719 $store->set('duplicatesFileName', $this->_duplicateFileName);
720 }
721 if ($this->_unparsedAddressCount) {
722 $store->set('errorsFileName', $this->_errorFileName);
723 }
724 }
725 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
726 }
727
728 /**
729 * Export data to a CSV file.
730 *
731 * @param string $fileName
732 * @param array $header
733 * @param array $data
734 *
735 * @return void
736 */
737 public static function exportCSV($fileName, $header, $data) {
738
739 if (file_exists($fileName) && !is_writable($fileName)) {
740 CRM_Core_Error::movedSiteError($fileName);
741 }
742 //hack to remove '_status', '_statusMsg' and '_id' from error file
743 $errorValues = array();
744 $dbRecordStatus = array('IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW');
745 foreach ($data as $rowCount => $rowValues) {
746 $count = 0;
747 foreach ($rowValues as $key => $val) {
748 if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) {
749 break;
750 }
751 $errorValues[$rowCount][$key] = $val;
752 $count++;
753 }
754 }
755 $data = $errorValues;
756
757 $output = array();
758 $fd = fopen($fileName, 'w');
759
760 foreach ($header as $key => $value) {
761 $header[$key] = "\"$value\"";
762 }
763 $config = CRM_Core_Config::singleton();
764 $output[] = implode($config->fieldSeparator, $header);
765
766 foreach ($data as $datum) {
767 foreach ($datum as $key => $value) {
768 $datum[$key] = "\"$value\"";
769 }
770 $output[] = implode($config->fieldSeparator, $datum);
771 }
772 fwrite($fd, implode("\n", $output));
773 fclose($fd);
774 }
775
776 /**
777 * Update the record with PK $id in the import database table
778 *
779 * @param int $id
780 * @param array $params
781 *
782 * @return void
783 */
784 public function updateImportRecord($id, &$params) {
785 $statusFieldName = $this->_statusFieldName;
786 $primaryKeyName = $this->_primaryKeyName;
787
788 if ($statusFieldName && $primaryKeyName) {
789 $dao = new CRM_Core_DAO();
790 $db = $dao->getDatabaseConnection();
791
792 $query = "UPDATE $this->_tableName
793 SET $statusFieldName = ?,
794 ${statusFieldName}Msg = ?
795 WHERE $primaryKeyName = ?";
796 $args = array(
797 $params[$statusFieldName],
798 CRM_Utils_Array::value("${statusFieldName}Msg", $params),
799 $id,
800 );
801
802 //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>";
803
804 $db->query($query, $args);
805 }
806 }
807
808 }