3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 abstract class CRM_Member_Import_Parser
extends CRM_Import_Parser
{
30 * Seperator being used
33 protected $_seperator;
36 * Total number of lines in file
39 protected $_lineCount;
42 * Whether the file has a column header or not
46 protected $_haveColumnHeader;
49 * @param string $fileName
50 * @param string $seperator
52 * @param bool $skipColumnHeader
54 * @param int $contactType
55 * @param int $onDuplicate
56 * @param int $statusID
57 * @param int $totalRowCount
66 $skipColumnHeader = FALSE,
67 $mode = self
::MODE_PREVIEW
,
68 $contactType = self
::CONTACT_INDIVIDUAL
,
69 $onDuplicate = self
::DUPLICATE_SKIP
,
73 if (!is_array($fileName)) {
74 CRM_Core_Error
::fatal();
76 $fileName = $fileName['name'];
78 switch ($contactType) {
79 case self
::CONTACT_INDIVIDUAL
:
80 $this->_contactType
= 'Individual';
83 case self
::CONTACT_HOUSEHOLD
:
84 $this->_contactType
= 'Household';
87 case self
::CONTACT_ORGANIZATION
:
88 $this->_contactType
= 'Organization';
93 $this->_haveColumnHeader
= $skipColumnHeader;
95 $this->_seperator
= $seperator;
97 $fd = fopen($fileName, "r");
102 $this->_lineCount
= $this->_warningCount
= 0;
103 $this->_invalidRowCount
= $this->_validCount
= 0;
104 $this->_totalCount
= $this->_conflictCount
= 0;
107 $this->_warnings
= [];
108 $this->_conflicts
= [];
110 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
112 if ($mode == self
::MODE_MAPFIELD
) {
116 $this->_activeFieldCount
= count($this->_activeFields
);
119 $this->progressImport($statusID);
120 $startTimestamp = $currTimestamp = $prevTimestamp = time();
126 $values = fgetcsv($fd, 8192, $seperator);
131 self
::encloseScrub($values);
133 // skip column header if we're not in mapfield mode
134 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
135 $skipColumnHeader = FALSE;
139 /* trim whitespace around the values */
141 foreach ($values as $k => $v) {
142 $values[$k] = trim($v, " \t\r\n");
144 if (CRM_Utils_System
::isNull($values)) {
148 $this->_totalCount++
;
150 if ($mode == self
::MODE_MAPFIELD
) {
151 $returnCode = $this->mapField($values);
153 elseif ($mode == self
::MODE_PREVIEW
) {
154 $returnCode = $this->preview($values);
156 elseif ($mode == self
::MODE_SUMMARY
) {
157 $returnCode = $this->summary($values);
159 elseif ($mode == self
::MODE_IMPORT
) {
160 $returnCode = $this->import($onDuplicate, $values);
161 if ($statusID && (($this->_lineCount %
50) == 0)) {
162 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
166 $returnCode = self
::ERROR
;
169 // note that a line could be valid but still produce a warning
170 if ($returnCode & self
::VALID
) {
171 $this->_validCount++
;
172 if ($mode == self
::MODE_MAPFIELD
) {
173 $this->_rows
[] = $values;
174 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
178 if ($returnCode & self
::WARNING
) {
179 $this->_warningCount++
;
180 if ($this->_warningCount
< $this->_maxWarningCount
) {
181 $this->_warningCount
[] = $line;
185 if ($returnCode & self
::ERROR
) {
186 $this->_invalidRowCount++
;
187 $recordNumber = $this->_lineCount
;
188 array_unshift($values, $recordNumber);
189 $this->_errors
[] = $values;
192 if ($returnCode & self
::CONFLICT
) {
193 $this->_conflictCount++
;
194 $recordNumber = $this->_lineCount
;
195 array_unshift($values, $recordNumber);
196 $this->_conflicts
[] = $values;
199 if ($returnCode & self
::DUPLICATE
) {
200 if ($returnCode & self
::MULTIPLE_DUPE
) {
201 /* TODO: multi-dupes should be counted apart from singles
202 * on non-skip action */
204 $this->_duplicateCount++
;
205 $recordNumber = $this->_lineCount
;
206 array_unshift($values, $recordNumber);
207 $this->_duplicates
[] = $values;
208 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
209 $this->_validCount++
;
213 // we give the derived class a way of aborting the process
214 // note that the return code could be multiple code or'ed together
215 if ($returnCode & self
::STOP
) {
219 // if we are done processing the maxNumber of lines, break
220 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
227 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
228 $customHeaders = $mapper;
230 $customfields = CRM_Core_BAO_CustomField
::getFields('Membership');
231 foreach ($customHeaders as $key => $value) {
232 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
233 $customHeaders[$key] = $customfields[$id][0];
236 if ($this->_invalidRowCount
) {
237 // removed view url for invlaid contacts
238 $headers = array_merge([
242 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
244 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
246 if ($this->_conflictCount
) {
247 $headers = array_merge([
251 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
252 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
254 if ($this->_duplicateCount
) {
255 $headers = array_merge([
257 ts('View Membership URL'),
260 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
261 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
264 return $this->fini();
268 * Given a list of the importable field keys that the user has selected
269 * set the active fields array to this list
271 * @param array $fieldKeys mapped array of values
275 public function setActiveFields($fieldKeys) {
276 $this->_activeFieldCount
= count($fieldKeys);
277 foreach ($fieldKeys as $key) {
278 if (empty($this->_fields
[$key])) {
279 $this->_activeFields
[] = new CRM_Member_Import_Field('', ts('- do not import -'));
282 $this->_activeFields
[] = clone($this->_fields
[$key]);
288 * Format the field values for input to the api.
291 * (reference ) associative array of name/value pairs
293 public function &getActiveFieldParams() {
295 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
296 if (isset($this->_activeFields
[$i]->_value
)
297 && !isset($params[$this->_activeFields
[$i]->_name
])
298 && !isset($this->_activeFields
[$i]->_related
)
301 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
308 * @param string $name
311 * @param string $headerPattern
312 * @param string $dataPattern
314 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
316 $this->_fields
['doNotImport'] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
320 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
321 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
322 if (!array_key_exists($name, $tempField)) {
323 $this->_fields
[$name] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
326 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
327 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
334 * Store parser values.
336 * @param CRM_Core_Session $store
342 public function set($store, $mode = self
::MODE_SUMMARY
) {
343 $store->set('fileSize', $this->_fileSize
);
344 $store->set('lineCount', $this->_lineCount
);
345 $store->set('seperator', $this->_seperator
);
346 $store->set('fields', $this->getSelectValues());
347 $store->set('fieldTypes', $this->getSelectTypes());
349 $store->set('headerPatterns', $this->getHeaderPatterns());
350 $store->set('dataPatterns', $this->getDataPatterns());
351 $store->set('columnCount', $this->_activeFieldCount
);
353 $store->set('totalRowCount', $this->_totalCount
);
354 $store->set('validRowCount', $this->_validCount
);
355 $store->set('invalidRowCount', $this->_invalidRowCount
);
356 $store->set('conflictRowCount', $this->_conflictCount
);
358 switch ($this->_contactType
) {
360 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
364 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
368 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
371 if ($this->_invalidRowCount
) {
372 $store->set('errorsFileName', $this->_errorFileName
);
374 if ($this->_conflictCount
) {
375 $store->set('conflictsFileName', $this->_conflictFileName
);
377 if (isset($this->_rows
) && !empty($this->_rows
)) {
378 $store->set('dataValues', $this->_rows
);
381 if ($mode == self
::MODE_IMPORT
) {
382 $store->set('duplicateRowCount', $this->_duplicateCount
);
383 if ($this->_duplicateCount
) {
384 $store->set('duplicatesFileName', $this->_duplicateFileName
);
390 * Export data to a CSV file.
392 * @param string $fileName
393 * @param array $header
398 public static function exportCSV($fileName, $header, $data) {
400 $fd = fopen($fileName, 'w');
402 foreach ($header as $key => $value) {
403 $header[$key] = "\"$value\"";
405 $config = CRM_Core_Config
::singleton();
406 $output[] = implode($config->fieldSeparator
, $header);
408 foreach ($data as $datum) {
409 foreach ($datum as $key => $value) {
410 if (is_array($value)) {
411 foreach ($value[0] as $k1 => $v1) {
412 if ($k1 == 'location_type_id') {
419 $datum[$key] = "\"$value\"";
422 $output[] = implode($config->fieldSeparator
, $datum);
424 fwrite($fd, implode("\n", $output));