Merge pull request #3196 from JoeMurray/master
[civicrm-core.git] / CRM / Custom / Import / Parser.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36
37 abstract class CRM_Custom_Import_Parser extends CRM_Contact_Import_Parser {
38
39 protected $_fileName;
40
41 /**#@+
42 * @access protected
43 * @var integer
44 */
45
46 /**
47 * imported file size
48 */
49 protected $_fileSize;
50
51 /**
52 * separator being used
53 */
54 protected $_separator;
55
56 /**
57 * total number of lines in file
58 */
59 protected $_lineCount;
60
61 /**
62 * whether the file has a column header or not
63 *
64 * @var boolean
65 */
66 protected $_haveColumnHeader;
67
68 function run($fileName,
69 $separator = ',',
70 &$mapper,
71 $skipColumnHeader = FALSE,
72 $mode = self::MODE_PREVIEW,
73 $contactType = self::CONTACT_INDIVIDUAL,
74 $onDuplicate = self::DUPLICATE_SKIP
75 ) {
76 if (!is_array($fileName)) {
77 CRM_Core_Error::fatal();
78 }
79 $fileName = $fileName['name'];
80
81 switch ($contactType) {
82 case CRM_Import_Parser::CONTACT_INDIVIDUAL:
83 $this->_contactType = 'Individual';
84 break;
85
86 case CRM_Import_Parser::CONTACT_HOUSEHOLD:
87 $this->_contactType = 'Household';
88 break;
89
90 case CRM_Import_Parser::CONTACT_ORGANIZATION:
91 $this->_contactType = 'Organization';
92 }
93 $this->init();
94
95 $this->_haveColumnHeader = $skipColumnHeader;
96
97 $this->_separator = $separator;
98
99 $fd = fopen($fileName, "r");
100 if (!$fd) {
101 return FALSE;
102 }
103
104 $this->_lineCount = $this->_warningCount = 0;
105 $this->_invalidRowCount = $this->_validCount = 0;
106 $this->_totalCount = $this->_conflictCount = 0;
107
108 $this->_errors = array();
109 $this->_warnings = array();
110 $this->_conflicts = array();
111
112 $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2);
113
114 if ($mode == self::MODE_MAPFIELD) {
115 $this->_rows = array();
116 }
117 else {
118 $this->_activeFieldCount = count($this->_activeFields);
119 }
120
121 while (!feof($fd)) {
122 $this->_lineCount++;
123
124 $values = fgetcsv($fd, 8192, $separator);
125 if (!$values) {
126 continue;
127 }
128
129 self::encloseScrub($values);
130
131 // skip column header if we're not in mapfield mode
132 if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) {
133 $skipColumnHeader = FALSE;
134 continue;
135 }
136
137 /* trim whitespace around the values */
138
139 $empty = TRUE;
140 foreach ($values as $k => $v) {
141 $values[$k] = trim($v, " \t\r\n");
142 }
143
144 if (CRM_Utils_System::isNull($values)) {
145 continue;
146 }
147
148 $this->_totalCount++;
149
150 if ($mode == self::MODE_MAPFIELD) {
151 $returnCode = $this->mapField($values);
152 }
153 elseif ($mode == self::MODE_PREVIEW) {
154 $returnCode = $this->preview($values);
155 }
156 elseif ($mode == self::MODE_SUMMARY) {
157 $returnCode = $this->summary($values);
158 }
159 elseif ($mode == self::MODE_IMPORT) {
160 $returnCode = $this->import($onDuplicate, $values);
161 }
162 else {
163 $returnCode = self::ERROR;
164 }
165
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));
172 }
173 }
174
175 if ($returnCode & self::WARNING) {
176 $this->_warningCount++;
177 if ($this->_warningCount < $this->_maxWarningCount) {
178 $this->_warningCount[] = $line;
179 }
180 }
181
182 if ($returnCode & self::ERROR) {
183 $this->_invalidRowCount++;
184 if ($this->_invalidRowCount < $this->_maxErrorCount) {
185 $recordNumber = $this->_lineCount;
186 if ($this->_haveColumnHeader) {
187 $recordNumber--;
188 }
189 array_unshift($values, $recordNumber);
190 $this->_errors[] = $values;
191 }
192 }
193
194 if ($returnCode & self::CONFLICT) {
195 $this->_conflictCount++;
196 $recordNumber = $this->_lineCount;
197 if ($this->_haveColumnHeader) {
198 $recordNumber--;
199 }
200 array_unshift($values, $recordNumber);
201 $this->_conflicts[] = $values;
202 }
203
204 if ($returnCode & self::DUPLICATE) {
205 if ($returnCode & self::MULTIPLE_DUPE) {
206 /* TODO: multi-dupes should be counted apart from singles
207 * on non-skip action */
208 }
209 $this->_duplicateCount++;
210 $recordNumber = $this->_lineCount;
211 if ($this->_haveColumnHeader) {
212 $recordNumber--;
213 }
214 array_unshift($values, $recordNumber);
215 $this->_duplicates[] = $values;
216 if ($onDuplicate != self::DUPLICATE_SKIP) {
217 $this->_validCount++;
218 }
219 }
220
221 // we give the derived class a way of aborting the process
222 // note that the return code could be multiple code or'ed together
223 if ($returnCode & self::STOP) {
224 break;
225 }
226
227 // if we are done processing the maxNumber of lines, break
228 if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) {
229 break;
230 }
231 }
232
233 fclose($fd);
234
235
236 if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) {
237 $customHeaders = $mapper;
238
239 $customfields = CRM_Core_BAO_CustomField::getFields('Activity');
240 foreach ($customHeaders as $key => $value) {
241 if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) {
242 $customHeaders[$key] = $customfields[$id][0];
243 }
244 }
245 if ($this->_invalidRowCount) {
246 // removed view url for invlaid contacts
247 $headers = array_merge(array(ts('Line Number'),
248 ts('Reason'),
249 ),
250 $customHeaders
251 );
252 $this->_errorFileName = self::errorFileName(self::ERROR);
253 self::exportCSV($this->_errorFileName, $headers, $this->_errors);
254 }
255 if ($this->_conflictCount) {
256 $headers = array_merge(array(ts('Line Number'),
257 ts('Reason'),
258 ),
259 $customHeaders
260 );
261 $this->_conflictFileName = self::errorFileName(self::CONFLICT);
262 self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts);
263 }
264 if ($this->_duplicateCount) {
265 $headers = array_merge(array(ts('Line Number'),
266 ts('View Activity History URL'),
267 ),
268 $customHeaders
269 );
270
271 $this->_duplicateFileName = self::errorFileName(self::DUPLICATE);
272 self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates);
273 }
274 }
275 return $this->fini();
276 }
277
278 /**
279 * Given a list of the importable field keys that the user has selected
280 * set the active fields array to this list
281 *
282 * @param array mapped array of values
283 *
284 * @return void
285 * @access public
286 */
287 function setActiveFields($fieldKeys) {
288 $this->_activeFieldCount = count($fieldKeys);
289 foreach ($fieldKeys as $key) {
290 if (empty($this->_fields[$key])) {
291 $this->_activeFields[] = new CRM_Custom_Import_Field('', ts('- do not import -'));
292 }
293 else {
294 $this->_activeFields[] = clone($this->_fields[$key]);
295 }
296 }
297 }
298
299 /**
300 * function to format the field values for input to the api
301 *
302 * @return array (reference ) associative array of name/value pairs
303 * @access public
304 */
305 function &getActiveFieldParams() {
306 $params = array();
307 for ($i = 0; $i < $this->_activeFieldCount; $i++) {
308 if (isset($this->_activeFields[$i]->_value)
309 && !isset($params[$this->_activeFields[$i]->_name])
310 && !isset($this->_activeFields[$i]->_related)
311 ) {
312 $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value;
313 }
314 }
315 return $params;
316 }
317
318 /**
319 * Store parser values
320 *
321 * @param CRM_Core_Session $store
322 *
323 * @return void
324 * @access public
325 */
326 function set($store, $mode = self::MODE_SUMMARY) {
327 $store->set('fileSize', $this->_fileSize);
328 $store->set('lineCount', $this->_lineCount);
329 $store->set('seperator', $this->_separator);
330 $store->set('fields', $this->getSelectValues());
331 $store->set('fieldTypes', $this->getSelectTypes());
332
333 $store->set('headerPatterns', $this->getHeaderPatterns());
334 $store->set('dataPatterns', $this->getDataPatterns());
335 $store->set('columnCount', $this->_activeFieldCount);
336 $store->set('_entity', $this->_entity);
337 $store->set('totalRowCount', $this->_totalCount);
338 $store->set('validRowCount', $this->_validCount);
339 $store->set('invalidRowCount', $this->_invalidRowCount);
340 $store->set('conflictRowCount', $this->_conflictCount);
341
342 switch ($this->_contactType) {
343 case 'Individual':
344 $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
345 break;
346
347 case 'Household':
348 $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD);
349 break;
350
351 case 'Organization':
352 $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION);
353 }
354
355 if ($this->_invalidRowCount) {
356 $store->set('errorsFileName', $this->_errorFileName);
357 }
358 if ($this->_conflictCount) {
359 $store->set('conflictsFileName', $this->_conflictFileName);
360 }
361 if (isset($this->_rows) && !empty($this->_rows)) {
362 $store->set('dataValues', $this->_rows);
363 }
364
365 if ($mode == self::MODE_IMPORT) {
366 $store->set('duplicateRowCount', $this->_duplicateCount);
367 if ($this->_duplicateCount) {
368 $store->set('duplicatesFileName', $this->_duplicateFileName);
369 }
370 }
371 }
372 }
373