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