Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
ec3811b1 | 35 | abstract class CRM_Activity_Import_Parser extends CRM_Import_Parser { |
6a488035 TO |
36 | |
37 | protected $_fileName; | |
38 | ||
39 | /**#@+ | |
6a488035 TO |
40 | * @var integer |
41 | */ | |
42 | ||
43 | /** | |
fe482240 | 44 | * Imported file size. |
6a488035 TO |
45 | */ |
46 | protected $_fileSize; | |
47 | ||
48 | /** | |
2e2605fe | 49 | * Separator being used. |
6a488035 TO |
50 | */ |
51 | protected $_seperator; | |
52 | ||
53 | /** | |
fe482240 | 54 | * Total number of lines in file. |
6a488035 TO |
55 | */ |
56 | protected $_lineCount; | |
57 | ||
6a488035 | 58 | /** |
fe482240 | 59 | * Whether the file has a column header or not. |
6a488035 TO |
60 | * |
61 | * @var boolean | |
62 | */ | |
63 | protected $_haveColumnHeader; | |
f813f78e | 64 | |
ffd93213 | 65 | /** |
100fef9d | 66 | * @param string $fileName |
ffd93213 EM |
67 | * @param string $seperator |
68 | * @param $mapper | |
69 | * @param bool $skipColumnHeader | |
70 | * @param int $mode | |
71 | * @param int $onDuplicate | |
72 | * | |
73 | * @return mixed | |
74 | * @throws Exception | |
75 | */ | |
2da40d21 | 76 | public function run( |
9d5494f7 | 77 | $fileName, |
6a488035 TO |
78 | $seperator = ',', |
79 | &$mapper, | |
80 | $skipColumnHeader = FALSE, | |
52892e8b CW |
81 | $mode = self::MODE_PREVIEW, |
82 | $onDuplicate = self::DUPLICATE_SKIP | |
6a488035 TO |
83 | ) { |
84 | if (!is_array($fileName)) { | |
85 | CRM_Core_Error::fatal(); | |
86 | } | |
87 | $fileName = $fileName['name']; | |
88 | ||
89 | $this->init(); | |
90 | ||
91 | $this->_haveColumnHeader = $skipColumnHeader; | |
92 | ||
93 | $this->_seperator = $seperator; | |
94 | ||
95 | $fd = fopen($fileName, "r"); | |
96 | if (!$fd) { | |
97 | return FALSE; | |
98 | } | |
99 | ||
100 | $this->_lineCount = $this->_warningCount = 0; | |
101 | $this->_invalidRowCount = $this->_validCount = 0; | |
102 | $this->_totalCount = $this->_conflictCount = 0; | |
103 | ||
52892e8b CW |
104 | $this->_errors = array(); |
105 | $this->_warnings = array(); | |
6a488035 TO |
106 | $this->_conflicts = array(); |
107 | ||
108 | $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2); | |
109 | ||
110 | if ($mode == self::MODE_MAPFIELD) { | |
111 | $this->_rows = array(); | |
112 | } | |
113 | else { | |
114 | $this->_activeFieldCount = count($this->_activeFields); | |
115 | } | |
116 | ||
117 | while (!feof($fd)) { | |
118 | $this->_lineCount++; | |
119 | ||
120 | $values = fgetcsv($fd, 8192, $seperator); | |
121 | if (!$values) { | |
122 | continue; | |
123 | } | |
124 | ||
125 | self::encloseScrub($values); | |
126 | ||
127 | // skip column header if we're not in mapfield mode | |
128 | if ($mode != self::MODE_MAPFIELD && $skipColumnHeader) { | |
129 | $skipColumnHeader = FALSE; | |
130 | continue; | |
131 | } | |
132 | ||
133 | /* trim whitespace around the values */ | |
134 | ||
135 | $empty = TRUE; | |
136 | foreach ($values as $k => $v) { | |
137 | $values[$k] = trim($v, " \t\r\n"); | |
138 | } | |
139 | ||
140 | if (CRM_Utils_System::isNull($values)) { | |
141 | continue; | |
142 | } | |
143 | ||
144 | $this->_totalCount++; | |
145 | ||
146 | if ($mode == self::MODE_MAPFIELD) { | |
147 | $returnCode = $this->mapField($values); | |
148 | } | |
149 | elseif ($mode == self::MODE_PREVIEW) { | |
150 | $returnCode = $this->preview($values); | |
151 | } | |
152 | elseif ($mode == self::MODE_SUMMARY) { | |
153 | $returnCode = $this->summary($values); | |
154 | } | |
155 | elseif ($mode == self::MODE_IMPORT) { | |
156 | $returnCode = $this->import($onDuplicate, $values); | |
157 | } | |
158 | else { | |
159 | $returnCode = self::ERROR; | |
160 | } | |
161 | ||
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)); | |
168 | } | |
169 | } | |
170 | ||
171 | if ($returnCode & self::WARNING) { | |
172 | $this->_warningCount++; | |
173 | if ($this->_warningCount < $this->_maxWarningCount) { | |
174 | $this->_warningCount[] = $line; | |
175 | } | |
176 | } | |
177 | ||
178 | if ($returnCode & self::ERROR) { | |
179 | $this->_invalidRowCount++; | |
180 | if ($this->_invalidRowCount < $this->_maxErrorCount) { | |
181 | $recordNumber = $this->_lineCount; | |
182 | if ($this->_haveColumnHeader) { | |
183 | $recordNumber--; | |
184 | } | |
185 | array_unshift($values, $recordNumber); | |
186 | $this->_errors[] = $values; | |
187 | } | |
188 | } | |
189 | ||
190 | if ($returnCode & self::CONFLICT) { | |
191 | $this->_conflictCount++; | |
192 | $recordNumber = $this->_lineCount; | |
193 | if ($this->_haveColumnHeader) { | |
194 | $recordNumber--; | |
195 | } | |
196 | array_unshift($values, $recordNumber); | |
197 | $this->_conflicts[] = $values; | |
198 | } | |
199 | ||
200 | if ($returnCode & self::DUPLICATE) { | |
201 | if ($returnCode & self::MULTIPLE_DUPE) { | |
202 | /* TODO: multi-dupes should be counted apart from singles | |
e70a7fc0 | 203 | * on non-skip action */ |
6a488035 TO |
204 | } |
205 | $this->_duplicateCount++; | |
206 | $recordNumber = $this->_lineCount; | |
207 | if ($this->_haveColumnHeader) { | |
208 | $recordNumber--; | |
209 | } | |
210 | array_unshift($values, $recordNumber); | |
211 | $this->_duplicates[] = $values; | |
212 | if ($onDuplicate != self::DUPLICATE_SKIP) { | |
213 | $this->_validCount++; | |
214 | } | |
215 | } | |
216 | ||
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) { | |
220 | break; | |
221 | } | |
222 | ||
223 | // if we are done processing the maxNumber of lines, break | |
224 | if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) { | |
225 | break; | |
226 | } | |
227 | } | |
228 | ||
229 | fclose($fd); | |
230 | ||
6a488035 TO |
231 | if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) { |
232 | $customHeaders = $mapper; | |
233 | ||
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]; | |
238 | } | |
239 | } | |
240 | if ($this->_invalidRowCount) { | |
241 | // removed view url for invlaid contacts | |
353ffa53 TO |
242 | $headers = array_merge(array( |
243 | ts('Line Number'), | |
6a488035 TO |
244 | ts('Reason'), |
245 | ), | |
246 | $customHeaders | |
247 | ); | |
248 | $this->_errorFileName = self::errorFileName(self::ERROR); | |
249 | self::exportCSV($this->_errorFileName, $headers, $this->_errors); | |
250 | } | |
251 | if ($this->_conflictCount) { | |
353ffa53 TO |
252 | $headers = array_merge(array( |
253 | ts('Line Number'), | |
6a488035 TO |
254 | ts('Reason'), |
255 | ), | |
256 | $customHeaders | |
257 | ); | |
258 | $this->_conflictFileName = self::errorFileName(self::CONFLICT); | |
259 | self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts); | |
260 | } | |
261 | if ($this->_duplicateCount) { | |
353ffa53 TO |
262 | $headers = array_merge(array( |
263 | ts('Line Number'), | |
6a488035 TO |
264 | ts('View Activity History URL'), |
265 | ), | |
266 | $customHeaders | |
267 | ); | |
268 | ||
269 | $this->_duplicateFileName = self::errorFileName(self::DUPLICATE); | |
270 | self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates); | |
271 | } | |
272 | } | |
273 | //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount"; | |
274 | return $this->fini(); | |
275 | } | |
276 | ||
6a488035 TO |
277 | /** |
278 | * Given a list of the importable field keys that the user has selected | |
279 | * set the active fields array to this list | |
280 | * | |
6c552737 | 281 | * @param array $fieldKeys |
6a488035 TO |
282 | * |
283 | * @return void | |
6a488035 | 284 | */ |
00be9182 | 285 | public function setActiveFields($fieldKeys) { |
6a488035 TO |
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 -')); | |
290 | } | |
291 | else { | |
292 | $this->_activeFields[] = clone($this->_fields[$key]); | |
293 | } | |
294 | } | |
295 | } | |
296 | ||
6a488035 | 297 | /** |
fe482240 | 298 | * Format the field values for input to the api. |
6a488035 | 299 | * |
a6c01b45 CW |
300 | * @return array |
301 | * (reference ) associative array of name/value pairs | |
6a488035 | 302 | */ |
00be9182 | 303 | public function &getActiveFieldParams() { |
6a488035 TO |
304 | $params = array(); |
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) | |
309 | ) { | |
310 | ||
311 | $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value; | |
312 | } | |
313 | } | |
314 | return $params; | |
315 | } | |
316 | ||
ffd93213 | 317 | /** |
100fef9d | 318 | * @param string $name |
ffd93213 EM |
319 | * @param $title |
320 | * @param int $type | |
321 | * @param string $headerPattern | |
322 | * @param string $dataPattern | |
323 | */ | |
00be9182 | 324 | public function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { |
6a488035 TO |
325 | if (empty($name)) { |
326 | $this->_fields['doNotImport'] = new CRM_Activity_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
327 | } | |
328 | else { | |
329 | ||
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); | |
333 | } | |
334 | else { | |
719a6fec | 335 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, CRM_Utils_Array::value('hasLocationType', $tempField[$name])); |
6a488035 TO |
336 | } |
337 | } | |
338 | } | |
339 | ||
6a488035 | 340 | /** |
fe482240 | 341 | * Store parser values. |
6a488035 TO |
342 | * |
343 | * @param CRM_Core_Session $store | |
344 | * | |
fd31fa4c EM |
345 | * @param int $mode |
346 | * | |
6a488035 | 347 | * @return void |
6a488035 | 348 | */ |
00be9182 | 349 | public function set($store, $mode = self::MODE_SUMMARY) { |
6a488035 TO |
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()); | |
355 | ||
356 | $store->set('headerPatterns', $this->getHeaderPatterns()); | |
357 | $store->set('dataPatterns', $this->getDataPatterns()); | |
358 | $store->set('columnCount', $this->_activeFieldCount); | |
359 | ||
360 | $store->set('totalRowCount', $this->_totalCount); | |
361 | $store->set('validRowCount', $this->_validCount); | |
362 | $store->set('invalidRowCount', $this->_invalidRowCount); | |
363 | $store->set('conflictRowCount', $this->_conflictCount); | |
364 | ||
365 | if ($this->_invalidRowCount) { | |
366 | $store->set('errorsFileName', $this->_errorFileName); | |
367 | } | |
368 | if ($this->_conflictCount) { | |
369 | $store->set('conflictsFileName', $this->_conflictFileName); | |
370 | } | |
371 | if (isset($this->_rows) && !empty($this->_rows)) { | |
372 | $store->set('dataValues', $this->_rows); | |
373 | } | |
374 | ||
375 | if ($mode == self::MODE_IMPORT) { | |
376 | $store->set('duplicateRowCount', $this->_duplicateCount); | |
377 | if ($this->_duplicateCount) { | |
378 | $store->set('duplicatesFileName', $this->_duplicateFileName); | |
379 | } | |
380 | } | |
381 | //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount"; | |
382 | } | |
383 | ||
384 | /** | |
fe482240 | 385 | * Export data to a CSV file. |
6a488035 | 386 | * |
c490a46a | 387 | * @param string $fileName |
6a488035 | 388 | * @param array $header |
c490a46a | 389 | * @param array $data |
6a488035 TO |
390 | * |
391 | * @return void | |
6a488035 | 392 | */ |
00be9182 | 393 | public static function exportCSV($fileName, $header, $data) { |
6a488035 TO |
394 | $output = array(); |
395 | $fd = fopen($fileName, 'w'); | |
396 | ||
397 | foreach ($header as $key => $value) { | |
398 | $header[$key] = "\"$value\""; | |
399 | } | |
400 | $config = CRM_Core_Config::singleton(); | |
401 | $output[] = implode($config->fieldSeparator, $header); | |
402 | ||
403 | foreach ($data as $datum) { | |
404 | foreach ($datum as $key => $value) { | |
405 | $datum[$key] = "\"$value\""; | |
406 | } | |
407 | $output[] = implode($config->fieldSeparator, $datum); | |
408 | } | |
409 | fwrite($fd, implode("\n", $output)); | |
410 | fclose($fd); | |
411 | } | |
412 | ||
6a488035 | 413 | } |