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
{
51 * Seperator being used
54 protected $_seperator;
57 * Total number of lines in file
60 protected $_lineCount;
63 * Whether the file has a column header or not
67 protected $_haveColumnHeader;
70 * @param string $fileName
71 * @param string $seperator
73 * @param bool $skipColumnHeader
75 * @param int $contactType
76 * @param int $onDuplicate
77 * @param int $statusID
78 * @param int $totalRowCount
87 $skipColumnHeader = FALSE,
88 $mode = self
::MODE_PREVIEW
,
89 $contactType = self
::CONTACT_INDIVIDUAL
,
90 $onDuplicate = self
::DUPLICATE_SKIP
,
94 if (!is_array($fileName)) {
95 CRM_Core_Error
::fatal();
97 $fileName = $fileName['name'];
99 switch ($contactType) {
100 case self
::CONTACT_INDIVIDUAL
:
101 $this->_contactType
= 'Individual';
104 case self
::CONTACT_HOUSEHOLD
:
105 $this->_contactType
= 'Household';
108 case self
::CONTACT_ORGANIZATION
:
109 $this->_contactType
= 'Organization';
114 $this->_haveColumnHeader
= $skipColumnHeader;
116 $this->_seperator
= $seperator;
118 $fd = fopen($fileName, "r");
123 $this->_lineCount
= $this->_warningCount
= 0;
124 $this->_invalidRowCount
= $this->_validCount
= 0;
125 $this->_totalCount
= $this->_conflictCount
= 0;
128 $this->_warnings
= [];
129 $this->_conflicts
= [];
131 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
133 if ($mode == self
::MODE_MAPFIELD
) {
137 $this->_activeFieldCount
= count($this->_activeFields
);
140 $this->progressImport($statusID);
141 $startTimestamp = $currTimestamp = $prevTimestamp = time();
147 $values = fgetcsv($fd, 8192, $seperator);
152 self
::encloseScrub($values);
154 // skip column header if we're not in mapfield mode
155 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
156 $skipColumnHeader = FALSE;
160 /* trim whitespace around the values */
162 foreach ($values as $k => $v) {
163 $values[$k] = trim($v, " \t\r\n");
165 if (CRM_Utils_System
::isNull($values)) {
169 $this->_totalCount++
;
171 if ($mode == self
::MODE_MAPFIELD
) {
172 $returnCode = $this->mapField($values);
174 elseif ($mode == self
::MODE_PREVIEW
) {
175 $returnCode = $this->preview($values);
177 elseif ($mode == self
::MODE_SUMMARY
) {
178 $returnCode = $this->summary($values);
180 elseif ($mode == self
::MODE_IMPORT
) {
181 $returnCode = $this->import($onDuplicate, $values);
182 if ($statusID && (($this->_lineCount %
50) == 0)) {
183 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
187 $returnCode = self
::ERROR
;
190 // note that a line could be valid but still produce a warning
191 if ($returnCode & self
::VALID
) {
192 $this->_validCount++
;
193 if ($mode == self
::MODE_MAPFIELD
) {
194 $this->_rows
[] = $values;
195 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
199 if ($returnCode & self
::WARNING
) {
200 $this->_warningCount++
;
201 if ($this->_warningCount
< $this->_maxWarningCount
) {
202 $this->_warningCount
[] = $line;
206 if ($returnCode & self
::ERROR
) {
207 $this->_invalidRowCount++
;
208 $recordNumber = $this->_lineCount
;
209 array_unshift($values, $recordNumber);
210 $this->_errors
[] = $values;
213 if ($returnCode & self
::CONFLICT
) {
214 $this->_conflictCount++
;
215 $recordNumber = $this->_lineCount
;
216 array_unshift($values, $recordNumber);
217 $this->_conflicts
[] = $values;
220 if ($returnCode & self
::DUPLICATE
) {
221 if ($returnCode & self
::MULTIPLE_DUPE
) {
222 /* TODO: multi-dupes should be counted apart from singles
223 * on non-skip action */
225 $this->_duplicateCount++
;
226 $recordNumber = $this->_lineCount
;
227 array_unshift($values, $recordNumber);
228 $this->_duplicates
[] = $values;
229 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
230 $this->_validCount++
;
234 // we give the derived class a way of aborting the process
235 // note that the return code could be multiple code or'ed together
236 if ($returnCode & self
::STOP
) {
240 // if we are done processing the maxNumber of lines, break
241 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
248 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
249 $customHeaders = $mapper;
251 $customfields = CRM_Core_BAO_CustomField
::getFields('Membership');
252 foreach ($customHeaders as $key => $value) {
253 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
254 $customHeaders[$key] = $customfields[$id][0];
257 if ($this->_invalidRowCount
) {
258 // removed view url for invlaid contacts
259 $headers = array_merge([
263 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
265 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
267 if ($this->_conflictCount
) {
268 $headers = array_merge([
272 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
273 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
275 if ($this->_duplicateCount
) {
276 $headers = array_merge([
278 ts('View Membership URL'),
281 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
282 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
285 return $this->fini();
289 * Given a list of the importable field keys that the user has selected
290 * set the active fields array to this list
292 * @param array $fieldKeys mapped array of values
296 public function setActiveFields($fieldKeys) {
297 $this->_activeFieldCount
= count($fieldKeys);
298 foreach ($fieldKeys as $key) {
299 if (empty($this->_fields
[$key])) {
300 $this->_activeFields
[] = new CRM_Member_Import_Field('', ts('- do not import -'));
303 $this->_activeFields
[] = clone($this->_fields
[$key]);
309 * Format the field values for input to the api.
312 * (reference ) associative array of name/value pairs
314 public function &getActiveFieldParams() {
316 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
317 if (isset($this->_activeFields
[$i]->_value
)
318 && !isset($params[$this->_activeFields
[$i]->_name
])
319 && !isset($this->_activeFields
[$i]->_related
)
322 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
329 * @param string $name
332 * @param string $headerPattern
333 * @param string $dataPattern
335 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
337 $this->_fields
['doNotImport'] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
341 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
342 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
343 if (!array_key_exists($name, $tempField)) {
344 $this->_fields
[$name] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
347 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
348 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
355 * Store parser values.
357 * @param CRM_Core_Session $store
363 public function set($store, $mode = self
::MODE_SUMMARY
) {
364 $store->set('fileSize', $this->_fileSize
);
365 $store->set('lineCount', $this->_lineCount
);
366 $store->set('seperator', $this->_seperator
);
367 $store->set('fields', $this->getSelectValues());
368 $store->set('fieldTypes', $this->getSelectTypes());
370 $store->set('headerPatterns', $this->getHeaderPatterns());
371 $store->set('dataPatterns', $this->getDataPatterns());
372 $store->set('columnCount', $this->_activeFieldCount
);
374 $store->set('totalRowCount', $this->_totalCount
);
375 $store->set('validRowCount', $this->_validCount
);
376 $store->set('invalidRowCount', $this->_invalidRowCount
);
377 $store->set('conflictRowCount', $this->_conflictCount
);
379 switch ($this->_contactType
) {
381 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
385 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
389 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
392 if ($this->_invalidRowCount
) {
393 $store->set('errorsFileName', $this->_errorFileName
);
395 if ($this->_conflictCount
) {
396 $store->set('conflictsFileName', $this->_conflictFileName
);
398 if (isset($this->_rows
) && !empty($this->_rows
)) {
399 $store->set('dataValues', $this->_rows
);
402 if ($mode == self
::MODE_IMPORT
) {
403 $store->set('duplicateRowCount', $this->_duplicateCount
);
404 if ($this->_duplicateCount
) {
405 $store->set('duplicatesFileName', $this->_duplicateFileName
);
411 * Export data to a CSV file.
413 * @param string $fileName
414 * @param array $header
419 public static function exportCSV($fileName, $header, $data) {
421 $fd = fopen($fileName, 'w');
423 foreach ($header as $key => $value) {
424 $header[$key] = "\"$value\"";
426 $config = CRM_Core_Config
::singleton();
427 $output[] = implode($config->fieldSeparator
, $header);
429 foreach ($data as $datum) {
430 foreach ($datum as $key => $value) {
431 if (is_array($value)) {
432 foreach ($value[0] as $k1 => $v1) {
433 if ($k1 == 'location_type_id') {
440 $datum[$key] = "\"$value\"";
443 $output[] = implode($config->fieldSeparator
, $datum);
445 fwrite($fd, implode("\n", $output));