3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 abstract class CRM_Activity_Import_Parser
extends CRM_Import_Parser
{
49 * Separator 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 $onDuplicate
80 $skipColumnHeader = FALSE,
81 $mode = self
::MODE_PREVIEW
,
82 $onDuplicate = self
::DUPLICATE_SKIP
84 if (!is_array($fileName)) {
85 CRM_Core_Error
::fatal();
87 $fileName = $fileName['name'];
91 $this->_haveColumnHeader
= $skipColumnHeader;
93 $this->_seperator
= $seperator;
95 $fd = fopen($fileName, "r");
100 $this->_lineCount
= $this->_warningCount
= 0;
101 $this->_invalidRowCount
= $this->_validCount
= 0;
102 $this->_totalCount
= $this->_conflictCount
= 0;
104 $this->_errors
= array();
105 $this->_warnings
= array();
106 $this->_conflicts
= array();
108 $this->_fileSize
= number_format(filesize($fileName) / 1024.0, 2);
110 if ($mode == self
::MODE_MAPFIELD
) {
111 $this->_rows
= array();
114 $this->_activeFieldCount
= count($this->_activeFields
);
120 $values = fgetcsv($fd, 8192, $seperator);
125 self
::encloseScrub($values);
127 // skip column header if we're not in mapfield mode
128 if ($mode != self
::MODE_MAPFIELD
&& $skipColumnHeader) {
129 $skipColumnHeader = FALSE;
133 /* trim whitespace around the values */
136 foreach ($values as $k => $v) {
137 $values[$k] = trim($v, " \t\r\n");
140 if (CRM_Utils_System
::isNull($values)) {
144 $this->_totalCount++
;
146 if ($mode == self
::MODE_MAPFIELD
) {
147 $returnCode = $this->mapField($values);
149 elseif ($mode == self
::MODE_PREVIEW
) {
150 $returnCode = $this->preview($values);
152 elseif ($mode == self
::MODE_SUMMARY
) {
153 $returnCode = $this->summary($values);
155 elseif ($mode == self
::MODE_IMPORT
) {
156 $returnCode = $this->import($onDuplicate, $values);
159 $returnCode = self
::ERROR
;
162 // note that a line could be valid but still produce a warning
163 if ($returnCode & self
::VALID
) {
164 $this->_validCount++
;
165 if ($mode == self
::MODE_MAPFIELD
) {
166 $this->_rows
[] = $values;
167 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
171 if ($returnCode & self
::WARNING
) {
172 $this->_warningCount++
;
173 if ($this->_warningCount
< $this->_maxWarningCount
) {
174 $this->_warningCount
[] = $line;
178 if ($returnCode & self
::ERROR
) {
179 $this->_invalidRowCount++
;
180 if ($this->_invalidRowCount
< $this->_maxErrorCount
) {
181 $recordNumber = $this->_lineCount
;
182 if ($this->_haveColumnHeader
) {
185 array_unshift($values, $recordNumber);
186 $this->_errors
[] = $values;
190 if ($returnCode & self
::CONFLICT
) {
191 $this->_conflictCount++
;
192 $recordNumber = $this->_lineCount
;
193 if ($this->_haveColumnHeader
) {
196 array_unshift($values, $recordNumber);
197 $this->_conflicts
[] = $values;
200 if ($returnCode & self
::DUPLICATE
) {
201 if ($returnCode & self
::MULTIPLE_DUPE
) {
202 /* TODO: multi-dupes should be counted apart from singles
203 * on non-skip action */
205 $this->_duplicateCount++
;
206 $recordNumber = $this->_lineCount
;
207 if ($this->_haveColumnHeader
) {
210 array_unshift($values, $recordNumber);
211 $this->_duplicates
[] = $values;
212 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
213 $this->_validCount++
;
217 // we give the derived class a way of aborting the process
218 // note that the return code could be multiple code or'ed together
219 if ($returnCode & self
::STOP
) {
223 // if we are done processing the maxNumber of lines, break
224 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
231 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
232 $customHeaders = $mapper;
234 $customfields = CRM_Core_BAO_CustomField
::getFields('Activity');
235 foreach ($customHeaders as $key => $value) {
236 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
237 $customHeaders[$key] = $customfields[$id][0];
240 if ($this->_invalidRowCount
) {
241 // removed view url for invlaid contacts
242 $headers = array_merge(array(
248 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
249 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
251 if ($this->_conflictCount
) {
252 $headers = array_merge(array(
258 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
259 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
261 if ($this->_duplicateCount
) {
262 $headers = array_merge(array(
264 ts('View Activity History URL'),
269 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
270 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
273 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
274 return $this->fini();
278 * Given a list of the importable field keys that the user has selected
279 * set the active fields array to this list
281 * @param array $fieldKeys
285 public function setActiveFields($fieldKeys) {
286 $this->_activeFieldCount
= count($fieldKeys);
287 foreach ($fieldKeys as $key) {
288 if (empty($this->_fields
[$key])) {
289 $this->_activeFields
[] = new CRM_Activity_Import_Field('', ts('- do not import -'));
292 $this->_activeFields
[] = clone($this->_fields
[$key]);
298 * Format the field values for input to the api.
301 * (reference ) associative array of name/value pairs
303 public function &getActiveFieldParams() {
305 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
306 if (isset($this->_activeFields
[$i]->_value
)
307 && !isset($params[$this->_activeFields
[$i]->_name
])
308 && !isset($this->_activeFields
[$i]->_related
)
311 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
318 * @param string $name
321 * @param string $headerPattern
322 * @param string $dataPattern
324 public function addField($name, $title, $type = CRM_Utils_Type
::T_INT
, $headerPattern = '//', $dataPattern = '//') {
326 $this->_fields
['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
330 $tempField = CRM_Contact_BAO_Contact
::importableFields('Individual', NULL);
331 if (!array_key_exists($name, $tempField)) {
332 $this->_fields
[$name] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern);
335 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array
::value('hasLocationType', $tempField[$name]));
341 * Store parser values.
343 * @param CRM_Core_Session $store
349 public function set($store, $mode = self
::MODE_SUMMARY
) {
350 $store->set('fileSize', $this->_fileSize
);
351 $store->set('lineCount', $this->_lineCount
);
352 $store->set('seperator', $this->_seperator
);
353 $store->set('fields', $this->getSelectValues());
354 $store->set('fieldTypes', $this->getSelectTypes());
356 $store->set('headerPatterns', $this->getHeaderPatterns());
357 $store->set('dataPatterns', $this->getDataPatterns());
358 $store->set('columnCount', $this->_activeFieldCount
);
360 $store->set('totalRowCount', $this->_totalCount
);
361 $store->set('validRowCount', $this->_validCount
);
362 $store->set('invalidRowCount', $this->_invalidRowCount
);
363 $store->set('conflictRowCount', $this->_conflictCount
);
365 if ($this->_invalidRowCount
) {
366 $store->set('errorsFileName', $this->_errorFileName
);
368 if ($this->_conflictCount
) {
369 $store->set('conflictsFileName', $this->_conflictFileName
);
371 if (isset($this->_rows
) && !empty($this->_rows
)) {
372 $store->set('dataValues', $this->_rows
);
375 if ($mode == self
::MODE_IMPORT
) {
376 $store->set('duplicateRowCount', $this->_duplicateCount
);
377 if ($this->_duplicateCount
) {
378 $store->set('duplicatesFileName', $this->_duplicateFileName
);
381 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
385 * Export data to a CSV file.
387 * @param string $fileName
388 * @param array $header
393 public static function exportCSV($fileName, $header, $data) {
395 $fd = fopen($fileName, 'w');
397 foreach ($header as $key => $value) {
398 $header[$key] = "\"$value\"";
400 $config = CRM_Core_Config
::singleton();
401 $output[] = implode($config->fieldSeparator
, $header);
403 foreach ($data as $datum) {
404 foreach ($datum as $key => $value) {
405 $datum[$key] = "\"$value\"";
407 $output[] = implode($config->fieldSeparator
, $datum);
409 fwrite($fd, implode("\n", $output));