3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
35 abstract class CRM_Member_Import_Parser
extends CRM_Import_Parser
{
46 * Seperator being used
49 protected $_seperator;
52 * Total number of lines in file
55 protected $_lineCount;
58 * Whether the file has a column header or not
62 protected $_haveColumnHeader;
65 * @param string $fileName
66 * @param string $seperator
68 * @param bool $skipColumnHeader
70 * @param int $contactType
71 * @param int $onDuplicate
72 * @param int $statusID
73 * @param int $totalRowCount
82 $skipColumnHeader = FALSE,
83 $mode = self
::MODE_PREVIEW
,
84 $contactType = self
::CONTACT_INDIVIDUAL
,
85 $onDuplicate = self
::DUPLICATE_SKIP
,
89 if (!is_array($fileName)) {
90 CRM_Core_Error
::fatal();
92 $fileName = $fileName['name'];
94 switch ($contactType) {
95 case self
::CONTACT_INDIVIDUAL
:
96 $this->_contactType
= 'Individual';
99 case self
::CONTACT_HOUSEHOLD
:
100 $this->_contactType
= 'Household';
103 case self
::CONTACT_ORGANIZATION
:
104 $this->_contactType
= 'Organization';
109 $this->_haveColumnHeader
= $skipColumnHeader;
111 $this->_seperator
= $seperator;
113 $fd = fopen($fileName, "r");
118 $this->_lineCount
= $this->_warningCount
= 0;
119 $this->_invalidRowCount
= $this->_validCount
= 0;
120 $this->_totalCount
= $this->_conflictCount
= 0;
123 $this->_warnings
= [];
124 $this->_conflicts
= [];
126 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
128 if ($mode == self
::MODE_MAPFIELD
) {
132 $this->_activeFieldCount
= count($this->_activeFields
);
135 $this->progressImport($statusID);
136 $startTimestamp = $currTimestamp = $prevTimestamp = time();
142 $values = fgetcsv($fd, 8192, $seperator);
147 self
::encloseScrub($values);
149 // skip column header if we're not in mapfield mode
150 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
151 $skipColumnHeader = FALSE;
155 /* trim whitespace around the values */
157 foreach ($values as $k => $v) {
158 $values[$k] = trim($v, " \t\r\n");
160 if (CRM_Utils_System
::isNull($values)) {
164 $this->_totalCount++
;
166 if ($mode == self
::MODE_MAPFIELD
) {
167 $returnCode = $this->mapField($values);
169 elseif ($mode == self
::MODE_PREVIEW
) {
170 $returnCode = $this->preview($values);
172 elseif ($mode == self
::MODE_SUMMARY
) {
173 $returnCode = $this->summary($values);
175 elseif ($mode == self
::MODE_IMPORT
) {
176 $returnCode = $this->import($onDuplicate, $values);
177 if ($statusID && (($this->_lineCount %
50) == 0)) {
178 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
182 $returnCode = self
::ERROR
;
185 // note that a line could be valid but still produce a warning
186 if ($returnCode & self
::VALID
) {
187 $this->_validCount++
;
188 if ($mode == self
::MODE_MAPFIELD
) {
189 $this->_rows
[] = $values;
190 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
194 if ($returnCode & self
::WARNING
) {
195 $this->_warningCount++
;
196 if ($this->_warningCount
< $this->_maxWarningCount
) {
197 $this->_warningCount
[] = $line;
201 if ($returnCode & self
::ERROR
) {
202 $this->_invalidRowCount++
;
203 $recordNumber = $this->_lineCount
;
204 array_unshift($values, $recordNumber);
205 $this->_errors
[] = $values;
208 if ($returnCode & self
::CONFLICT
) {
209 $this->_conflictCount++
;
210 $recordNumber = $this->_lineCount
;
211 array_unshift($values, $recordNumber);
212 $this->_conflicts
[] = $values;
215 if ($returnCode & self
::DUPLICATE
) {
216 if ($returnCode & self
::MULTIPLE_DUPE
) {
217 /* TODO: multi-dupes should be counted apart from singles
218 * on non-skip action */
220 $this->_duplicateCount++
;
221 $recordNumber = $this->_lineCount
;
222 array_unshift($values, $recordNumber);
223 $this->_duplicates
[] = $values;
224 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
225 $this->_validCount++
;
229 // we give the derived class a way of aborting the process
230 // note that the return code could be multiple code or'ed together
231 if ($returnCode & self
::STOP
) {
235 // if we are done processing the maxNumber of lines, break
236 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
243 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
244 $customHeaders = $mapper;
246 $customfields = CRM_Core_BAO_CustomField
::getFields('Membership');
247 foreach ($customHeaders as $key => $value) {
248 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
249 $customHeaders[$key] = $customfields[$id][0];
252 if ($this->_invalidRowCount
) {
253 // removed view url for invlaid contacts
254 $headers = array_merge([
258 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
260 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
262 if ($this->_conflictCount
) {
263 $headers = array_merge([
267 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
268 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
270 if ($this->_duplicateCount
) {
271 $headers = array_merge([
273 ts('View Membership URL'),
276 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
277 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
280 return $this->fini();
284 * Given a list of the importable field keys that the user has selected
285 * set the active fields array to this list
287 * @param array $fieldKeys mapped array of values
291 public function setActiveFields($fieldKeys) {
292 $this->_activeFieldCount
= count($fieldKeys);
293 foreach ($fieldKeys as $key) {
294 if (empty($this->_fields
[$key])) {
295 $this->_activeFields
[] = new CRM_Member_Import_Field('', ts('- do not import -'));
298 $this->_activeFields
[] = clone($this->_fields
[$key]);
304 * Format the field values for input to the api.
307 * (reference ) associative array of name/value pairs
309 public function &getActiveFieldParams() {
311 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
312 if (isset($this->_activeFields
[$i]->_value
)
313 && !isset($params[$this->_activeFields
[$i]->_name
])
314 && !isset($this->_activeFields
[$i]->_related
)
317 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
324 * @param string $name
327 * @param string $headerPattern
328 * @param string $dataPattern
330 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
332 $this->_fields
['doNotImport'] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
336 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
337 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
338 if (!array_key_exists($name, $tempField)) {
339 $this->_fields
[$name] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
342 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
343 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
350 * Store parser values.
352 * @param CRM_Core_Session $store
358 public function set($store, $mode = self
::MODE_SUMMARY
) {
359 $store->set('fileSize', $this->_fileSize
);
360 $store->set('lineCount', $this->_lineCount
);
361 $store->set('seperator', $this->_seperator
);
362 $store->set('fields', $this->getSelectValues());
363 $store->set('fieldTypes', $this->getSelectTypes());
365 $store->set('headerPatterns', $this->getHeaderPatterns());
366 $store->set('dataPatterns', $this->getDataPatterns());
367 $store->set('columnCount', $this->_activeFieldCount
);
369 $store->set('totalRowCount', $this->_totalCount
);
370 $store->set('validRowCount', $this->_validCount
);
371 $store->set('invalidRowCount', $this->_invalidRowCount
);
372 $store->set('conflictRowCount', $this->_conflictCount
);
374 switch ($this->_contactType
) {
376 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
380 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
384 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
387 if ($this->_invalidRowCount
) {
388 $store->set('errorsFileName', $this->_errorFileName
);
390 if ($this->_conflictCount
) {
391 $store->set('conflictsFileName', $this->_conflictFileName
);
393 if (isset($this->_rows
) && !empty($this->_rows
)) {
394 $store->set('dataValues', $this->_rows
);
397 if ($mode == self
::MODE_IMPORT
) {
398 $store->set('duplicateRowCount', $this->_duplicateCount
);
399 if ($this->_duplicateCount
) {
400 $store->set('duplicatesFileName', $this->_duplicateFileName
);
406 * Export data to a CSV file.
408 * @param string $fileName
409 * @param array $header
414 public static function exportCSV($fileName, $header, $data) {
416 $fd = fopen($fileName, 'w');
418 foreach ($header as $key => $value) {
419 $header[$key] = "\"$value\"";
421 $config = CRM_Core_Config
::singleton();
422 $output[] = implode($config->fieldSeparator
, $header);
424 foreach ($data as $datum) {
425 foreach ($datum as $key => $value) {
426 if (is_array($value)) {
427 foreach ($value[0] as $k1 => $v1) {
428 if ($k1 == 'location_type_id') {
435 $datum[$key] = "\"$value\"";
438 $output[] = implode($config->fieldSeparator
, $datum);
440 fwrite($fd, implode("\n", $output));