3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 abstract class CRM_Contact_Import_Parser
extends CRM_Import_Parser
{
39 protected $_tableName;
47 * total number of lines in file
52 * total number of non empty lines
54 protected $_totalCount;
57 * running total number of valid lines
59 protected $_validCount;
62 * running total number of invalid rows
64 protected $_invalidRowCount;
67 * maximum number of invalid rows to store
69 protected $_maxErrorCount;
72 * array of error lines, bounded by MAX_ERROR
77 * total number of conflict lines
79 protected $_conflictCount;
82 * array of conflict lines
84 protected $_conflicts;
87 * total number of duplicate (from database) lines
89 protected $_duplicateCount;
92 * array of duplicate lines
94 protected $_duplicates;
97 * running total number of warnings
99 protected $_warningCount;
102 * running total number of un matched Conact
104 protected $_unMatchCount;
107 * array of unmatched lines
112 * maximum number of warnings to store
114 protected $_maxWarningCount = self
::MAX_WARNINGS
;
117 * total number of contacts with unparsed addresses
119 protected $_unparsedAddressCount;
122 * array of warning lines, bounded by MAX_WARNING
124 protected $_warnings;
127 * array of all the fields that could potentially be part
128 * of this import process
134 * array of the fields that are actually part of the import process
135 * the position in the array also dictates their position in the import
139 protected $_activeFields;
142 * cache the count of active fields
146 protected $_activeFieldCount;
149 * maximum number of non-empty/comment lines to process
153 protected $_maxLinesToProcess;
156 * cache of preview rows
163 * filename of error data
167 protected $_errorFileName;
170 * filename of conflict data
174 protected $_conflictFileName;
177 * filename of duplicate data
181 protected $_duplicateFileName;
184 * filename of mismatch data
188 protected $_misMatchFilemName;
190 protected $_primaryKeyName;
191 protected $_statusFieldName;
199 public $_contactType;
206 public $_onDuplicate;
209 * dedupe rule group id to use if set
213 public $_dedupeRuleGroupID = NULL;
215 function __construct() {
216 $this->_maxLinesToProcess
= 0;
217 $this->_maxErrorCount
= self
::MAX_ERRORS
;
220 abstract function init();
222 function run($tableName,
224 $mode = self
::MODE_PREVIEW
,
225 $contactType = self
::CONTACT_INDIVIDUAL
,
226 $primaryKeyName = '_id',
227 $statusFieldName = '_status',
228 $onDuplicate = self
::DUPLICATE_SKIP
,
230 $totalRowCount = NULL,
231 $doGeocodeAddress = FALSE,
232 $timeout = CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
233 $contactSubType = NULL,
234 $dedupeRuleGroupID = NULL
237 // TODO: Make the timeout actually work
238 $this->_onDuplicate
= $onDuplicate;
239 $this->_dedupeRuleGroupID
= $dedupeRuleGroupID;
241 switch ($contactType) {
242 case CRM_Contact_Import_Parser
::CONTACT_INDIVIDUAL
:
243 $this->_contactType
= 'Individual';
246 case CRM_Contact_Import_Parser
::CONTACT_HOUSEHOLD
:
247 $this->_contactType
= 'Household';
250 case CRM_Contact_Import_Parser
::CONTACT_ORGANIZATION
:
251 $this->_contactType
= 'Organization';
254 $this->_contactSubType
= $contactSubType;
258 $this->_rowCount
= $this->_warningCount
= 0;
259 $this->_invalidRowCount
= $this->_validCount
= 0;
260 $this->_totalCount
= $this->_conflictCount
= 0;
262 $this->_errors
= array();
263 $this->_warnings
= array();
264 $this->_conflicts
= array();
265 $this->_unparsedAddresses
= array();
269 $this->_tableName
= $tableName;
270 $this->_primaryKeyName
= $primaryKeyName;
271 $this->_statusFieldName
= $statusFieldName;
273 if ($mode == self
::MODE_MAPFIELD
) {
274 $this->_rows
= array();
277 $this->_activeFieldCount
= count($this->_activeFields
);
280 if ($mode == self
::MODE_IMPORT
) {
281 //get the key of email field
282 foreach ($mapper as $key => $value) {
283 if (strtolower($value) == 'email') {
293 $config = CRM_Core_Config
::singleton();
294 $statusFile = "{$config->uploadDir}status_{$statusID}.txt";
295 $status = "<div class='description'> " . ts('No processing status reported yet.') . "</div>";
297 //do not force the browser to display the save dialog, CRM-7640
298 $contents = json_encode(array(0, $status));
300 file_put_contents($statusFile, $contents);
302 $startTimestamp = $currTimestamp = $prevTimestamp = time();
305 // get the contents of the temp. import table
306 $query = "SELECT * FROM $tableName";
307 if ($mode == self
::MODE_IMPORT
) {
308 $query .= " WHERE $statusFieldName = 'NEW'";
310 $dao = new CRM_Core_DAO();
311 $db = $dao->getDatabaseConnection();
312 $result = $db->query($query);
314 while ($values = $result->fetchRow(DB_FETCHMODE_ORDERED
)) {
317 /* trim whitespace around the values */
320 foreach ($values as $k => $v) {
321 $values[$k] = trim($v, " \t\r\n");
323 if (CRM_Utils_System
::isNull($values)) {
327 $this->_totalCount++
;
329 if ($mode == self
::MODE_MAPFIELD
) {
330 $returnCode = $this->mapField($values);
332 elseif ($mode == self
::MODE_PREVIEW
) {
333 $returnCode = $this->preview($values);
335 elseif ($mode == self
::MODE_SUMMARY
) {
336 $returnCode = $this->summary($values);
338 elseif ($mode == self
::MODE_IMPORT
) {
339 //print "Running parser in import mode<br/>\n";
340 $returnCode = $this->import($onDuplicate, $values, $doGeocodeAddress);
341 if ($statusID && (($this->_rowCount %
$skip) == 0)) {
342 $currTimestamp = time();
343 $totalTime = ($currTimestamp - $startTimestamp);
344 $time = ($currTimestamp - $prevTimestamp);
345 $recordsLeft = $totalRowCount - $this->_rowCount
;
346 if ($recordsLeft < 0) {
349 $estimatedTime = ($recordsLeft / $skip) * $time;
350 $estMinutes = floor($estimatedTime / 60);
352 if ($estMinutes > 1) {
353 $timeFormatted = $estMinutes . ' ' . ts('minutes') . ' ';
354 $estimatedTime = $estimatedTime - ($estMinutes * 60);
356 $timeFormatted .= round($estimatedTime) . ' ' . ts('seconds');
357 $processedPercent = (int )(($this->_rowCount
* 100) / $totalRowCount);
358 $statusMsg = ts('%1 of %2 records - %3 remaining',
359 array(1 => $this->_rowCount
, 2 => $totalRowCount, 3 => $timeFormatted)
362 <div class=\"description\">
363 <strong>{$statusMsg}</strong>
367 $contents = json_encode (array($processedPercent, $status));
369 file_put_contents($statusFile, $contents);
371 $prevTimestamp = $currTimestamp;
376 $returnCode = self
::ERROR
;
379 // note that a line could be valid but still produce a warning
380 if ($returnCode & self
::VALID
) {
381 $this->_validCount++
;
382 if ($mode == self
::MODE_MAPFIELD
) {
383 $this->_rows
[] = $values;
384 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
388 if ($returnCode & self
::WARNING
) {
389 $this->_warningCount++
;
390 if ($this->_warningCount
< $this->_maxWarningCount
) {
391 $this->_warningCount
[] = $line;
395 if ($returnCode & self
::ERROR
) {
396 $this->_invalidRowCount++
;
397 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
398 array_unshift($values, $this->_rowCount
);
399 $this->_errors
[] = $values;
403 if ($returnCode & self
::CONFLICT
) {
404 $this->_conflictCount++
;
405 array_unshift($values, $this->_rowCount
);
406 $this->_conflicts
[] = $values;
409 if ($returnCode & self
::NO_MATCH
) {
410 $this->_unMatchCount++
;
411 array_unshift($values, $this->_rowCount
);
412 $this->_unMatch
[] = $values;
415 if ($returnCode & self
::DUPLICATE
) {
416 if ($returnCode & self
::MULTIPLE_DUPE
) {
417 /* TODO: multi-dupes should be counted apart from singles
418 * on non-skip action */
420 $this->_duplicateCount++
;
421 array_unshift($values, $this->_rowCount
);
422 $this->_duplicates
[] = $values;
423 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
424 $this->_validCount++
;
428 if ($returnCode & self
::UNPARSED_ADDRESS_WARNING
) {
429 $this->_unparsedAddressCount++
;
430 array_unshift($values, $this->_rowCount
);
431 $this->_unparsedAddresses
[] = $values;
433 // we give the derived class a way of aborting the process
434 // note that the return code could be multiple code or'ed together
435 if ($returnCode & self
::STOP
) {
439 // if we are done processing the maxNumber of lines, break
440 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
444 // clean up memory from dao's
445 CRM_Core_DAO
::freeResult();
447 // see if we've hit our timeout yet
448 /* if ( $the_thing_with_the_stuff ) {
454 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
455 $customHeaders = $mapper;
457 $customfields = CRM_Core_BAO_CustomField
::getFields($this->_contactType
);
458 foreach ($customHeaders as $key => $value) {
459 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
460 $customHeaders[$key] = $customfields[$id][0];
464 if ($this->_invalidRowCount
) {
465 // removed view url for invlaid contacts
466 $headers = array_merge(array(ts('Line Number'),
471 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
472 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
474 if ($this->_conflictCount
) {
475 $headers = array_merge(array(ts('Line Number'),
480 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
481 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
483 if ($this->_duplicateCount
) {
484 $headers = array_merge(array(ts('Line Number'),
485 ts('View Contact URL'),
490 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
491 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
493 if ($this->_unMatchCount
) {
494 $headers = array_merge(array(ts('Line Number'),
500 $this->_misMatchFilemName
= self
::errorFileName(self
::NO_MATCH
);
501 self
::exportCSV($this->_misMatchFilemName
, $headers, $this->_unMatch
);
503 if ($this->_unparsedAddressCount
) {
504 $headers = array_merge(array(ts('Line Number'),
505 ts('Contact Edit URL'),
509 $this->_errorFileName
= self
::errorFileName(self
::UNPARSED_ADDRESS_WARNING
);
510 self
::exportCSV($this->_errorFileName
, $headers, $this->_unparsedAddresses
);
513 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
514 return $this->fini();
517 abstract function mapField(&$values);
518 abstract function preview(&$values);
519 abstract function summary(&$values);
520 abstract function import($onDuplicate, &$values);
522 abstract function fini();
525 * Given a list of the importable field keys that the user has selected
526 * set the active fields array to this list
528 * @param array mapped array of values
533 function setActiveFields($fieldKeys) {
534 $this->_activeFieldCount
= count($fieldKeys);
535 foreach ($fieldKeys as $key) {
536 if (empty($this->_fields
[$key])) {
537 $this->_activeFields
[] = new CRM_Contact_Import_Field('', ts('- do not import -'));
540 $this->_activeFields
[] = clone($this->_fields
[$key]);
545 function setActiveFieldValues($elements) {
546 $maxCount = count($elements) < $this->_activeFieldCount ?
count($elements) : $this->_activeFieldCount
;
547 for ($i = 0; $i < $maxCount; $i++
) {
548 $this->_activeFields
[$i]->setValue($elements[$i]);
551 // reset all the values that we did not have an equivalent import element
552 for (; $i < $this->_activeFieldCount
; $i++
) {
553 $this->_activeFields
[$i]->resetValue();
556 // now validate the fields and return false if error
557 $valid = self
::VALID
;
558 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
559 if (!$this->_activeFields
[$i]->validate()) {
560 // no need to do any more validation
561 $valid = self
::ERROR
;
568 function setActiveFieldLocationTypes($elements) {
569 for ($i = 0; $i < count($elements); $i++
) {
570 $this->_activeFields
[$i]->_hasLocationType
= $elements[$i];
574 function setActiveFieldPhoneTypes($elements) {
575 for ($i = 0; $i < count($elements); $i++
) {
576 $this->_activeFields
[$i]->_phoneType
= $elements[$i];
580 function setActiveFieldWebsiteTypes($elements) {
581 for ($i = 0; $i < count($elements); $i++
) {
582 $this->_activeFields
[$i]->_websiteType
= $elements[$i];
587 * Function to set IM Service Provider type fields
589 * @param array $elements IM service provider type ids
594 function setActiveFieldImProviders($elements) {
595 for ($i = 0; $i < count($elements); $i++
) {
596 $this->_activeFields
[$i]->_imProvider
= $elements[$i];
600 function setActiveFieldRelated($elements) {
601 for ($i = 0; $i < count($elements); $i++
) {
602 $this->_activeFields
[$i]->_related
= $elements[$i];
606 function setActiveFieldRelatedContactType($elements) {
607 for ($i = 0; $i < count($elements); $i++
) {
608 $this->_activeFields
[$i]->_relatedContactType
= $elements[$i];
612 function setActiveFieldRelatedContactDetails($elements) {
613 for ($i = 0; $i < count($elements); $i++
) {
614 $this->_activeFields
[$i]->_relatedContactDetails
= $elements[$i];
618 function setActiveFieldRelatedContactLocType($elements) {
619 for ($i = 0; $i < count($elements); $i++
) {
620 $this->_activeFields
[$i]->_relatedContactLocType
= $elements[$i];
624 function setActiveFieldRelatedContactPhoneType($elements) {
625 for ($i = 0; $i < count($elements); $i++
) {
626 $this->_activeFields
[$i]->_relatedContactPhoneType
= $elements[$i];
630 function setActiveFieldRelatedContactWebsiteType($elements) {
631 for ($i = 0; $i < count($elements); $i++
) {
632 $this->_activeFields
[$i]->_relatedContactWebsiteType
= $elements[$i];
637 * Function to set IM Service Provider type fields for related contacts
639 * @param array $elements IM service provider type ids of related contact
644 function setActiveFieldRelatedContactImProvider($elements) {
645 for ($i = 0; $i < count($elements); $i++
) {
646 $this->_activeFields
[$i]->_relatedContactImProvider
= $elements[$i];
651 * function to format the field values for input to the api
653 * @return array (reference ) associative array of name/value pairs
656 function &getActiveFieldParams() {
659 //CRM_Core_Error::debug( 'Count', $this->_activeFieldCount );
660 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
661 if ($this->_activeFields
[$i]->_name
== 'do_not_import') {
665 if (isset($this->_activeFields
[$i]->_value
)) {
666 if (isset($this->_activeFields
[$i]->_hasLocationType
)) {
667 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
668 $params[$this->_activeFields
[$i]->_name
] = array();
672 $this->_activeFields
[$i]->_name
=>
673 $this->_activeFields
[$i]->_value
,
674 'location_type_id' =>
675 $this->_activeFields
[$i]->_hasLocationType
,
678 if (isset($this->_activeFields
[$i]->_phoneType
)) {
679 $value['phone_type_id'] = $this->_activeFields
[$i]->_phoneType
;
682 // get IM service Provider type id
683 if (isset($this->_activeFields
[$i]->_imProvider
)) {
684 $value['provider_id'] = $this->_activeFields
[$i]->_imProvider
;
687 $params[$this->_activeFields
[$i]->_name
][] = $value;
689 elseif (isset($this->_activeFields
[$i]->_websiteType
)) {
691 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
692 'website_type_id' => $this->_activeFields
[$i]->_websiteType
,
695 $params[$this->_activeFields
[$i]->_name
][] = $value;
698 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
699 if (!isset($this->_activeFields
[$i]->_related
)) {
700 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
704 //minor fix for CRM-4062
705 if (isset($this->_activeFields
[$i]->_related
)) {
706 if (!isset($params[$this->_activeFields
[$i]->_related
])) {
707 $params[$this->_activeFields
[$i]->_related
] = array();
710 if (!isset($params[$this->_activeFields
[$i]->_related
]['contact_type']) && !empty($this->_activeFields
[$i]->_relatedContactType
)) {
711 $params[$this->_activeFields
[$i]->_related
]['contact_type'] = $this->_activeFields
[$i]->_relatedContactType
;
714 if (isset($this->_activeFields
[$i]->_relatedContactLocType
) && !empty($this->_activeFields
[$i]->_value
)) {
715 if (!is_array($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
])) {
716 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = array();
719 $this->_activeFields
[$i]->_relatedContactDetails
=> $this->_activeFields
[$i]->_value
,
720 'location_type_id' => $this->_activeFields
[$i]->_relatedContactLocType
,
723 if (isset($this->_activeFields
[$i]->_relatedContactPhoneType
)) {
724 $value['phone_type_id'] = $this->_activeFields
[$i]->_relatedContactPhoneType
;
727 // get IM service Provider type id for related contact
728 if (isset($this->_activeFields
[$i]->_relatedContactImProvider
)) {
729 $value['provider_id'] = $this->_activeFields
[$i]->_relatedContactImProvider
;
732 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = $value;
734 elseif (isset($this->_activeFields
[$i]->_relatedContactWebsiteType
)) {
735 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = array(
736 'url' => $this->_activeFields
[$i]->_value
,
737 'website_type_id' => $this->_activeFields
[$i]->_relatedContactWebsiteType
,
741 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = $this->_activeFields
[$i]->_value
;
750 function getSelectValues() {
752 foreach ($this->_fields
as $name => $field) {
753 $values[$name] = $field->_title
;
758 function getSelectTypes() {
760 foreach ($this->_fields
as $name => $field) {
761 $values[$name] = $field->_hasLocationType
;
766 function getColumnPatterns() {
768 foreach ($this->_fields
as $name => $field) {
769 $values[$name] = $field->_columnPattern
;
774 function getDataPatterns() {
776 foreach ($this->_fields
as $name => $field) {
777 $values[$name] = $field->_dataPattern
;
782 function addField($name, $title, $type = CRM_Utils_Type
::T_INT
,
783 $headerPattern = '//', $dataPattern = '//',
784 $hasLocationType = FALSE
786 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
788 $this->_fields
['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
800 function setMaxLinesToProcess($max) {
801 $this->_maxLinesToProcess
= $max;
805 * Store parser values
807 * @param CRM_Core_Session $store
812 function set($store, $mode = self
::MODE_SUMMARY
) {
813 $store->set('rowCount', $this->_rowCount
);
814 $store->set('fields', $this->getSelectValues());
815 $store->set('fieldTypes', $this->getSelectTypes());
817 $store->set('columnPatterns', $this->getColumnPatterns());
818 $store->set('dataPatterns', $this->getDataPatterns());
819 $store->set('columnCount', $this->_activeFieldCount
);
821 $store->set('totalRowCount', $this->_totalCount
);
822 $store->set('validRowCount', $this->_validCount
);
823 $store->set('invalidRowCount', $this->_invalidRowCount
);
824 $store->set('conflictRowCount', $this->_conflictCount
);
825 $store->set('unMatchCount', $this->_unMatchCount
);
827 switch ($this->_contactType
) {
829 $store->set('contactType', CRM_Contact_Import_Parser
::CONTACT_INDIVIDUAL
);
833 $store->set('contactType', CRM_Contact_Import_Parser
::CONTACT_HOUSEHOLD
);
837 $store->set('contactType', CRM_Contact_Import_Parser
::CONTACT_ORGANIZATION
);
840 if ($this->_invalidRowCount
) {
841 $store->set('errorsFileName', $this->_errorFileName
);
843 if ($this->_conflictCount
) {
844 $store->set('conflictsFileName', $this->_conflictFileName
);
846 if (isset($this->_rows
) && !empty($this->_rows
)) {
847 $store->set('dataValues', $this->_rows
);
850 if ($this->_unMatchCount
) {
851 $store->set('mismatchFileName', $this->_misMatchFilemName
);
854 if ($mode == self
::MODE_IMPORT
) {
855 $store->set('duplicateRowCount', $this->_duplicateCount
);
856 $store->set('unparsedAddressCount', $this->_unparsedAddressCount
);
857 if ($this->_duplicateCount
) {
858 $store->set('duplicatesFileName', $this->_duplicateFileName
);
860 if ($this->_unparsedAddressCount
) {
861 $store->set('errorsFileName', $this->_errorFileName
);
864 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
868 * Export data to a CSV file
870 * @param string $filename
871 * @param array $header
877 static function exportCSV($fileName, $header, $data) {
879 if (file_exists($fileName) && !is_writable($fileName)) {
880 CRM_Core_Error
::movedSiteError($fileName);
882 //hack to remove '_status', '_statusMsg' and '_id' from error file
883 $errorValues = array();
884 $dbRecordStatus = array('IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW');
885 foreach ($data as $rowCount => $rowValues) {
887 foreach ($rowValues as $key => $val) {
888 if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) {
891 $errorValues[$rowCount][$key] = $val;
895 $data = $errorValues;
898 $fd = fopen($fileName, 'w');
900 foreach ($header as $key => $value) {
901 $header[$key] = "\"$value\"";
903 $config = CRM_Core_Config
::singleton();
904 $output[] = implode($config->fieldSeparator
, $header);
906 foreach ($data as $datum) {
907 foreach ($datum as $key => $value) {
908 $datum[$key] = "\"$value\"";
910 $output[] = implode($config->fieldSeparator
, $datum);
912 fwrite($fd, implode("\n", $output));
917 * Update the record with PK $id in the import database table
920 * @param array $params
925 public function updateImportRecord($id, &$params) {
926 $statusFieldName = $this->_statusFieldName
;
927 $primaryKeyName = $this->_primaryKeyName
;
929 if ($statusFieldName && $primaryKeyName) {
930 $dao = new CRM_Core_DAO();
931 $db = $dao->getDatabaseConnection();
933 $query = "UPDATE $this->_tableName
934 SET $statusFieldName = ?,
935 ${statusFieldName}Msg = ?
936 WHERE $primaryKeyName = ?";
938 $params[$statusFieldName],
939 CRM_Utils_Array
::value("${statusFieldName}Msg", $params),
943 //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>";
945 $db->query($query, $args);
949 function errorFileName($type) {
955 $config = CRM_Core_Config
::singleton();
956 $fileName = $config->uploadDir
. "sqlImport";
958 case CRM_Contact_Import_Parser
::ERROR
:
959 $fileName .= '.errors';
962 case CRM_Contact_Import_Parser
::CONFLICT
:
963 $fileName .= '.conflicts';
966 case CRM_Contact_Import_Parser
::DUPLICATE
:
967 $fileName .= '.duplicates';
970 case CRM_Contact_Import_Parser
::NO_MATCH
:
971 $fileName .= '.mismatch';
974 case CRM_Contact_Import_Parser
::UNPARSED_ADDRESS_WARNING
:
975 $fileName .= '.unparsedAddress';
982 function saveFileName($type) {
988 case CRM_Contact_Import_Parser
::ERROR
:
989 $fileName = 'Import_Errors.csv';
992 case CRM_Contact_Import_Parser
::CONFLICT
:
993 $fileName = 'Import_Conflicts.csv';
996 case CRM_Contact_Import_Parser
::DUPLICATE
:
997 $fileName = 'Import_Duplicates.csv';
1000 case CRM_Contact_Import_Parser
::NO_MATCH
:
1001 $fileName = 'Import_Mismatch.csv';
1004 case CRM_Contact_Import_Parser
::UNPARSED_ADDRESS_WARNING
:
1005 $fileName = 'Import_Unparsed_Address.csv';