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