3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 abstract class CRM_Member_Import_Parser
extends CRM_Import_Parser
{
52 * seperator being used
54 protected $_seperator;
57 * total number of lines in file
59 protected $_lineCount;
62 * whether the file has a column header or not
66 protected $_haveColumnHeader;
68 function run($fileName,
71 $skipColumnHeader = FALSE,
72 $mode = self
::MODE_PREVIEW
,
73 $contactType = self
::CONTACT_INDIVIDUAL
,
74 $onDuplicate = self
::DUPLICATE_SKIP
76 if (!is_array($fileName)) {
77 CRM_Core_Error
::fatal();
79 $fileName = $fileName['name'];
81 switch ($contactType) {
82 case self
::CONTACT_INDIVIDUAL
:
83 $this->_contactType
= 'Individual';
86 case self
::CONTACT_HOUSEHOLD
:
87 $this->_contactType
= 'Household';
90 case self
::CONTACT_ORGANIZATION
:
91 $this->_contactType
= 'Organization';
96 $this->_haveColumnHeader
= $skipColumnHeader;
98 $this->_seperator
= $seperator;
100 $fd = fopen($fileName, "r");
105 $this->_lineCount
= $this->_warningCount
= 0;
106 $this->_invalidRowCount
= $this->_validCount
= 0;
107 $this->_totalCount
= $this->_conflictCount
= 0;
109 $this->_errors
= array();
110 $this->_warnings
= array();
111 $this->_conflicts
= array();
113 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
115 if ($mode == self
::MODE_MAPFIELD
) {
116 $this->_rows
= array();
119 $this->_activeFieldCount
= count($this->_activeFields
);
125 $values = fgetcsv($fd, 8192, $seperator);
130 self
::encloseScrub($values);
132 // skip column header if we're not in mapfield mode
133 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
134 $skipColumnHeader = FALSE;
138 /* 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);
163 $returnCode = self
::ERROR
;
166 // note that a line could be valid but still produce a warning
167 if ($returnCode & self
::VALID
) {
168 $this->_validCount++
;
169 if ($mode == self
::MODE_MAPFIELD
) {
170 $this->_rows
[] = $values;
171 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
175 if ($returnCode & self
::WARNING
) {
176 $this->_warningCount++
;
177 if ($this->_warningCount
< $this->_maxWarningCount
) {
178 $this->_warningCount
[] = $line;
182 if ($returnCode & self
::ERROR
) {
183 $this->_invalidRowCount++
;
184 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
185 $recordNumber = $this->_lineCount
;
186 array_unshift($values, $recordNumber);
187 $this->_errors
[] = $values;
191 if ($returnCode & self
::CONFLICT
) {
192 $this->_conflictCount++
;
193 $recordNumber = $this->_lineCount
;
194 array_unshift($values, $recordNumber);
195 $this->_conflicts
[] = $values;
198 if ($returnCode & self
::DUPLICATE
) {
199 if ($returnCode & self
::MULTIPLE_DUPE
) {
200 /* TODO: multi-dupes should be counted apart from singles
201 * on non-skip action */
203 $this->_duplicateCount++
;
204 $recordNumber = $this->_lineCount
;
205 array_unshift($values, $recordNumber);
206 $this->_duplicates
[] = $values;
207 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
208 $this->_validCount++
;
212 // we give the derived class a way of aborting the process
213 // note that the return code could be multiple code or'ed together
214 if ($returnCode & self
::STOP
) {
218 // if we are done processing the maxNumber of lines, break
219 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
226 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
227 $customHeaders = $mapper;
229 $customfields = CRM_Core_BAO_CustomField
::getFields('Membership');
230 foreach ($customHeaders as $key => $value) {
231 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
232 $customHeaders[$key] = $customfields[$id][0];
235 if ($this->_invalidRowCount
) {
236 // removed view url for invlaid contacts
237 $headers = array_merge(array(ts('Line Number'),
242 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
244 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
246 if ($this->_conflictCount
) {
247 $headers = array_merge(array(ts('Line Number'),
252 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
253 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
255 if ($this->_duplicateCount
) {
256 $headers = array_merge(array(ts('Line Number'),
257 ts('View Membership URL'),
262 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
263 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
266 return $this->fini();
270 * Given a list of the importable field keys that the user has selected
271 * set the active fields array to this list
273 * @param array mapped array of values
278 function setActiveFields($fieldKeys) {
279 $this->_activeFieldCount
= count($fieldKeys);
280 foreach ($fieldKeys as $key) {
281 if (empty($this->_fields
[$key])) {
282 $this->_activeFields
[] = new CRM_Member_Import_Field('', ts('- do not import -'));
285 $this->_activeFields
[] = clone($this->_fields
[$key]);
291 * function to format the field values for input to the api
293 * @return array (reference ) associative array of name/value pairs
296 function &getActiveFieldParams() {
298 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
299 if (isset($this->_activeFields
[$i]->_value
)
300 && !isset($params[$this->_activeFields
[$i]->_name
])
301 && !isset($this->_activeFields
[$i]->_related
)
304 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
310 function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
312 $this->_fields
['doNotImport'] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
316 //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null );
317 $tempField = CRM_Contact_BAO_Contact
::importableFields('All', NULL);
318 if (!array_key_exists($name, $tempField)) {
319 $this->_fields
[$name] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
322 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern,
323 CRM_Utils_Array
::value('hasLocationType', $tempField[$name])
330 * Store parser values
332 * @param CRM_Core_Session $store
337 function set($store, $mode = self
::MODE_SUMMARY
) {
338 $store->set('fileSize', $this->_fileSize
);
339 $store->set('lineCount', $this->_lineCount
);
340 $store->set('seperator', $this->_seperator
);
341 $store->set('fields', $this->getSelectValues());
342 $store->set('fieldTypes', $this->getSelectTypes());
344 $store->set('headerPatterns', $this->getHeaderPatterns());
345 $store->set('dataPatterns', $this->getDataPatterns());
346 $store->set('columnCount', $this->_activeFieldCount
);
348 $store->set('totalRowCount', $this->_totalCount
);
349 $store->set('validRowCount', $this->_validCount
);
350 $store->set('invalidRowCount', $this->_invalidRowCount
);
351 $store->set('conflictRowCount', $this->_conflictCount
);
353 switch ($this->_contactType
) {
355 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
359 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
363 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
366 if ($this->_invalidRowCount
) {
367 $store->set('errorsFileName', $this->_errorFileName
);
369 if ($this->_conflictCount
) {
370 $store->set('conflictsFileName', $this->_conflictFileName
);
372 if (isset($this->_rows
) && !empty($this->_rows
)) {
373 $store->set('dataValues', $this->_rows
);
376 if ($mode == self
::MODE_IMPORT
) {
377 $store->set('duplicateRowCount', $this->_duplicateCount
);
378 if ($this->_duplicateCount
) {
379 $store->set('duplicatesFileName', $this->_duplicateFileName
);
385 * Export data to a CSV file
387 * @param string $filename
388 * @param array $header
394 static function exportCSV($fileName, $header, $data) {
396 $fd = fopen($fileName, 'w');
398 foreach ($header as $key => $value) {
399 $header[$key] = "\"$value\"";
401 $config = CRM_Core_Config
::singleton();
402 $output[] = implode($config->fieldSeparator
, $header);
404 foreach ($data as $datum) {
405 foreach ($datum as $key => $value) {
406 if (is_array($value)) {
407 foreach ($value[0] as $k1 => $v1) {
408 if ($k1 == 'location_type_id') {
415 $datum[$key] = "\"$value\"";
418 $output[] = implode($config->fieldSeparator
, $datum);
420 fwrite($fd, implode("\n", $output));