3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
35 abstract class CRM_Member_Import_Parser
extends CRM_Import_Parser
{
49 * Seperator being used
51 protected $_seperator;
54 * Total number of lines in file
56 protected $_lineCount;
59 * Whether the file has a column header or not
63 protected $_haveColumnHeader;
66 * @param string $fileName
67 * @param string $seperator
69 * @param bool $skipColumnHeader
71 * @param int $contactType
72 * @param int $onDuplicate
81 $skipColumnHeader = FALSE,
82 $mode = self
::MODE_PREVIEW
,
83 $contactType = self
::CONTACT_INDIVIDUAL
,
84 $onDuplicate = self
::DUPLICATE_SKIP
,
88 if (!is_array($fileName)) {
89 CRM_Core_Error
::fatal();
91 $fileName = $fileName['name'];
93 switch ($contactType) {
94 case self
::CONTACT_INDIVIDUAL
:
95 $this->_contactType
= 'Individual';
98 case self
::CONTACT_HOUSEHOLD
:
99 $this->_contactType
= 'Household';
102 case self
::CONTACT_ORGANIZATION
:
103 $this->_contactType
= 'Organization';
108 $this->_haveColumnHeader
= $skipColumnHeader;
110 $this->_seperator
= $seperator;
112 $fd = fopen($fileName, "r");
117 $this->_lineCount
= $this->_warningCount
= 0;
118 $this->_invalidRowCount
= $this->_validCount
= 0;
119 $this->_totalCount
= $this->_conflictCount
= 0;
121 $this->_errors
= array();
122 $this->_warnings
= array();
123 $this->_conflicts
= array();
125 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
127 if ($mode == self
::MODE_MAPFIELD
) {
128 $this->_rows
= array();
131 $this->_activeFieldCount
= count($this->_activeFields
);
134 $this->progressImport($statusID);
135 $startTimestamp = $currTimestamp = $prevTimestamp = time();
141 $values = fgetcsv($fd, 8192, $seperator);
146 self
::encloseScrub($values);
148 // skip column header if we're not in mapfield mode
149 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
150 $skipColumnHeader = FALSE;
154 /* trim whitespace around the values */
156 foreach ($values as $k => $v) {
157 $values[$k] = trim($v, " \t\r\n");
159 if (CRM_Utils_System
::isNull($values)) {
163 $this->_totalCount++
;
165 if ($mode == self
::MODE_MAPFIELD
) {
166 $returnCode = $this->mapField($values);
168 elseif ($mode == self
::MODE_PREVIEW
) {
169 $returnCode = $this->preview($values);
171 elseif ($mode == self
::MODE_SUMMARY
) {
172 $returnCode = $this->summary($values);
174 elseif ($mode == self
::MODE_IMPORT
) {
175 $returnCode = $this->import($onDuplicate, $values);
176 if ($statusID && (($this->_lineCount %
50) == 0)) {
177 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
181 $returnCode = self
::ERROR
;
184 // note that a line could be valid but still produce a warning
185 if ($returnCode & self
::VALID
) {
186 $this->_validCount++
;
187 if ($mode == self
::MODE_MAPFIELD
) {
188 $this->_rows
[] = $values;
189 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
193 if ($returnCode & self
::WARNING
) {
194 $this->_warningCount++
;
195 if ($this->_warningCount
< $this->_maxWarningCount
) {
196 $this->_warningCount
[] = $line;
200 if ($returnCode & self
::ERROR
) {
201 $this->_invalidRowCount++
;
202 $recordNumber = $this->_lineCount
;
203 array_unshift($values, $recordNumber);
204 $this->_errors
[] = $values;
207 if ($returnCode & self
::CONFLICT
) {
208 $this->_conflictCount++
;
209 $recordNumber = $this->_lineCount
;
210 array_unshift($values, $recordNumber);
211 $this->_conflicts
[] = $values;
214 if ($returnCode & self
::DUPLICATE
) {
215 if ($returnCode & self
::MULTIPLE_DUPE
) {
216 /* TODO: multi-dupes should be counted apart from singles
217 * on non-skip action */
219 $this->_duplicateCount++
;
220 $recordNumber = $this->_lineCount
;
221 array_unshift($values, $recordNumber);
222 $this->_duplicates
[] = $values;
223 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
224 $this->_validCount++
;
228 // we give the derived class a way of aborting the process
229 // note that the return code could be multiple code or'ed together
230 if ($returnCode & self
::STOP
) {
234 // if we are done processing the maxNumber of lines, break
235 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
242 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
243 $customHeaders = $mapper;
245 $customfields = CRM_Core_BAO_CustomField
::getFields('Membership');
246 foreach ($customHeaders as $key => $value) {
247 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
248 $customHeaders[$key] = $customfields[$id][0];
251 if ($this->_invalidRowCount
) {
252 // removed view url for invlaid contacts
253 $headers = array_merge(array(
257 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
259 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
261 if ($this->_conflictCount
) {
262 $headers = array_merge(array(
266 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
267 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
269 if ($this->_duplicateCount
) {
270 $headers = array_merge(array(
272 ts('View Membership URL'),
275 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
276 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
279 return $this->fini();
283 * Given a list of the importable field keys that the user has selected
284 * set the active fields array to this list
286 * @param array $fieldKeys mapped array of values
290 public function setActiveFields($fieldKeys) {
291 $this->_activeFieldCount
= count($fieldKeys);
292 foreach ($fieldKeys as $key) {
293 if (empty($this->_fields
[$key])) {
294 $this->_activeFields
[] = new CRM_Member_Import_Field('', ts('- do not import -'));
297 $this->_activeFields
[] = clone($this->_fields
[$key]);
303 * Format the field values for input to the api.
306 * (reference ) associative array of name/value pairs
308 public function &getActiveFieldParams() {
310 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
311 if (isset($this->_activeFields
[$i]->_value
)
312 && !isset($params[$this->_activeFields
[$i]->_name
])
313 && !isset($this->_activeFields
[$i]->_related
)
316 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
323 * @param string $name
326 * @param string $headerPattern
327 * @param string $dataPattern
329 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
331 $this->_fields
['doNotImport'] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
335 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
336 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
337 if (!array_key_exists($name, $tempField)) {
338 $this->_fields
[$name] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
341 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
342 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
349 * Store parser values.
351 * @param CRM_Core_Session $store
357 public function set($store, $mode = self
::MODE_SUMMARY
) {
358 $store->set('fileSize', $this->_fileSize
);
359 $store->set('lineCount', $this->_lineCount
);
360 $store->set('seperator', $this->_seperator
);
361 $store->set('fields', $this->getSelectValues());
362 $store->set('fieldTypes', $this->getSelectTypes());
364 $store->set('headerPatterns', $this->getHeaderPatterns());
365 $store->set('dataPatterns', $this->getDataPatterns());
366 $store->set('columnCount', $this->_activeFieldCount
);
368 $store->set('totalRowCount', $this->_totalCount
);
369 $store->set('validRowCount', $this->_validCount
);
370 $store->set('invalidRowCount', $this->_invalidRowCount
);
371 $store->set('conflictRowCount', $this->_conflictCount
);
373 switch ($this->_contactType
) {
375 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
379 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
383 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
386 if ($this->_invalidRowCount
) {
387 $store->set('errorsFileName', $this->_errorFileName
);
389 if ($this->_conflictCount
) {
390 $store->set('conflictsFileName', $this->_conflictFileName
);
392 if (isset($this->_rows
) && !empty($this->_rows
)) {
393 $store->set('dataValues', $this->_rows
);
396 if ($mode == self
::MODE_IMPORT
) {
397 $store->set('duplicateRowCount', $this->_duplicateCount
);
398 if ($this->_duplicateCount
) {
399 $store->set('duplicatesFileName', $this->_duplicateFileName
);
405 * Export data to a CSV file.
407 * @param string $fileName
408 * @param array $header
413 public static function exportCSV($fileName, $header, $data) {
415 $fd = fopen($fileName, 'w');
417 foreach ($header as $key => $value) {
418 $header[$key] = "\"$value\"";
420 $config = CRM_Core_Config
::singleton();
421 $output[] = implode($config->fieldSeparator
, $header);
423 foreach ($data as $datum) {
424 foreach ($datum as $key => $value) {
425 if (is_array($value)) {
426 foreach ($value[0] as $k1 => $v1) {
427 if ($k1 == 'location_type_id') {
434 $datum[$key] = "\"$value\"";
437 $output[] = implode($config->fieldSeparator
, $datum);
439 fwrite($fd, implode("\n", $output));