Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | ||
ec3811b1 | 37 | abstract class CRM_Member_Import_Parser extends CRM_Import_Parser { |
6a488035 TO |
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 | * seperator being used | |
53 | */ | |
54 | protected $_seperator; | |
55 | ||
56 | /** | |
57 | * total number of lines in file | |
58 | */ | |
59 | protected $_lineCount; | |
60 | ||
6a488035 TO |
61 | /** |
62 | * whether the file has a column header or not | |
63 | * | |
64 | * @var boolean | |
65 | */ | |
66 | protected $_haveColumnHeader; | |
67 | ||
6a488035 TO |
68 | function run($fileName, |
69 | $seperator = ',', | |
70 | &$mapper, | |
71 | $skipColumnHeader = FALSE, | |
52892e8b CW |
72 | $mode = self::MODE_PREVIEW, |
73 | $contactType = self::CONTACT_INDIVIDUAL, | |
74 | $onDuplicate = self::DUPLICATE_SKIP | |
6a488035 TO |
75 | ) { |
76 | if (!is_array($fileName)) { | |
77 | CRM_Core_Error::fatal(); | |
78 | } | |
79 | $fileName = $fileName['name']; | |
80 | ||
81 | switch ($contactType) { | |
82 | case self::CONTACT_INDIVIDUAL: | |
83 | $this->_contactType = 'Individual'; | |
84 | break; | |
85 | ||
86 | case self::CONTACT_HOUSEHOLD: | |
87 | $this->_contactType = 'Household'; | |
88 | break; | |
89 | ||
90 | case self::CONTACT_ORGANIZATION: | |
91 | $this->_contactType = 'Organization'; | |
92 | } | |
93 | ||
94 | $this->init(); | |
95 | ||
96 | $this->_haveColumnHeader = $skipColumnHeader; | |
97 | ||
98 | $this->_seperator = $seperator; | |
99 | ||
100 | $fd = fopen($fileName, "r"); | |
101 | if (!$fd) { | |
102 | return FALSE; | |
103 | } | |
104 | ||
105 | $this->_lineCount = $this->_warningCount = 0; | |
106 | $this->_invalidRowCount = $this->_validCount = 0; | |
107 | $this->_totalCount = $this->_conflictCount = 0; | |
108 | ||
52892e8b CW |
109 | $this->_errors = array(); |
110 | $this->_warnings = array(); | |
6a488035 TO |
111 | $this->_conflicts = array(); |
112 | ||
113 | $this->_fileSize = number_format(filesize($fileName) / 1024.0, 2); | |
114 | ||
115 | if ($mode == self::MODE_MAPFIELD) { | |
116 | $this->_rows = array(); | |
117 | } | |
118 | else { | |
119 | $this->_activeFieldCount = count($this->_activeFields); | |
120 | } | |
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 | ||
138 | /* trim whitespace around the values */ | |
139 | ||
140 | $empty = TRUE; | |
141 | foreach ($values as $k => $v) { | |
142 | $values[$k] = trim($v, " \t\r\n"); | |
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 | array_unshift($values, $recordNumber); | |
187 | $this->_errors[] = $values; | |
188 | } | |
189 | } | |
190 | ||
191 | if ($returnCode & self::CONFLICT) { | |
192 | $this->_conflictCount++; | |
193 | $recordNumber = $this->_lineCount; | |
194 | array_unshift($values, $recordNumber); | |
195 | $this->_conflicts[] = $values; | |
196 | } | |
197 | ||
198 | if ($returnCode & self::DUPLICATE) { | |
199 | if ($returnCode & self::MULTIPLE_DUPE) { | |
200 | /* TODO: multi-dupes should be counted apart from singles | |
201 | * on non-skip action */ | |
202 | } | |
203 | $this->_duplicateCount++; | |
204 | $recordNumber = $this->_lineCount; | |
205 | array_unshift($values, $recordNumber); | |
206 | $this->_duplicates[] = $values; | |
207 | if ($onDuplicate != self::DUPLICATE_SKIP) { | |
208 | $this->_validCount++; | |
209 | } | |
210 | } | |
211 | ||
212 | // we give the derived class a way of aborting the process | |
213 | // note that the return code could be multiple code or'ed together | |
214 | if ($returnCode & self::STOP) { | |
215 | break; | |
216 | } | |
217 | ||
218 | // if we are done processing the maxNumber of lines, break | |
219 | if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) { | |
220 | break; | |
221 | } | |
222 | } | |
223 | ||
224 | fclose($fd); | |
225 | ||
226 | if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) { | |
227 | $customHeaders = $mapper; | |
228 | ||
229 | $customfields = CRM_Core_BAO_CustomField::getFields('Membership'); | |
230 | foreach ($customHeaders as $key => $value) { | |
231 | if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) { | |
232 | $customHeaders[$key] = $customfields[$id][0]; | |
233 | } | |
234 | } | |
235 | if ($this->_invalidRowCount) { | |
236 | // removed view url for invlaid contacts | |
237 | $headers = array_merge(array(ts('Line Number'), | |
238 | ts('Reason'), | |
239 | ), | |
240 | $customHeaders | |
241 | ); | |
242 | $this->_errorFileName = self::errorFileName(self::ERROR); | |
243 | ||
244 | self::exportCSV($this->_errorFileName, $headers, $this->_errors); | |
245 | } | |
246 | if ($this->_conflictCount) { | |
247 | $headers = array_merge(array(ts('Line Number'), | |
248 | ts('Reason'), | |
249 | ), | |
250 | $customHeaders | |
251 | ); | |
252 | $this->_conflictFileName = self::errorFileName(self::CONFLICT); | |
253 | self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts); | |
254 | } | |
255 | if ($this->_duplicateCount) { | |
256 | $headers = array_merge(array(ts('Line Number'), | |
257 | ts('View Membership URL'), | |
258 | ), | |
259 | $customHeaders | |
260 | ); | |
261 | ||
262 | $this->_duplicateFileName = self::errorFileName(self::DUPLICATE); | |
263 | self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates); | |
264 | } | |
265 | } | |
266 | return $this->fini(); | |
267 | } | |
268 | ||
6a488035 TO |
269 | /** |
270 | * Given a list of the importable field keys that the user has selected | |
271 | * set the active fields array to this list | |
272 | * | |
273 | * @param array mapped array of values | |
274 | * | |
275 | * @return void | |
276 | * @access public | |
277 | */ | |
278 | function setActiveFields($fieldKeys) { | |
279 | $this->_activeFieldCount = count($fieldKeys); | |
280 | foreach ($fieldKeys as $key) { | |
281 | if (empty($this->_fields[$key])) { | |
282 | $this->_activeFields[] = new CRM_Member_Import_Field('', ts('- do not import -')); | |
283 | } | |
284 | else { | |
285 | $this->_activeFields[] = clone($this->_fields[$key]); | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
6a488035 TO |
290 | /** |
291 | * function to format the field values for input to the api | |
292 | * | |
293 | * @return array (reference ) associative array of name/value pairs | |
294 | * @access public | |
295 | */ | |
296 | function &getActiveFieldParams() { | |
297 | $params = array(); | |
298 | for ($i = 0; $i < $this->_activeFieldCount; $i++) { | |
299 | if (isset($this->_activeFields[$i]->_value) | |
300 | && !isset($params[$this->_activeFields[$i]->_name]) | |
301 | && !isset($this->_activeFields[$i]->_related) | |
302 | ) { | |
303 | ||
304 | $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value; | |
305 | } | |
306 | } | |
307 | return $params; | |
308 | } | |
309 | ||
6a488035 TO |
310 | function addField($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { |
311 | if (empty($name)) { | |
312 | $this->_fields['doNotImport'] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
313 | } | |
314 | else { | |
315 | ||
316 | //$tempField = CRM_Contact_BAO_Contact::importableFields('Individual', null ); | |
317 | $tempField = CRM_Contact_BAO_Contact::importableFields('All', NULL); | |
318 | if (!array_key_exists($name, $tempField)) { | |
319 | $this->_fields[$name] = new CRM_Member_Import_Field($name, $title, $type, $headerPattern, $dataPattern); | |
320 | } | |
321 | else { | |
719a6fec | 322 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, |
6a488035 TO |
323 | CRM_Utils_Array::value('hasLocationType', $tempField[$name]) |
324 | ); | |
325 | } | |
326 | } | |
327 | } | |
328 | ||
6a488035 TO |
329 | /** |
330 | * Store parser values | |
331 | * | |
332 | * @param CRM_Core_Session $store | |
333 | * | |
334 | * @return void | |
335 | * @access public | |
336 | */ | |
337 | function set($store, $mode = self::MODE_SUMMARY) { | |
338 | $store->set('fileSize', $this->_fileSize); | |
339 | $store->set('lineCount', $this->_lineCount); | |
340 | $store->set('seperator', $this->_seperator); | |
341 | $store->set('fields', $this->getSelectValues()); | |
342 | $store->set('fieldTypes', $this->getSelectTypes()); | |
343 | ||
344 | $store->set('headerPatterns', $this->getHeaderPatterns()); | |
345 | $store->set('dataPatterns', $this->getDataPatterns()); | |
346 | $store->set('columnCount', $this->_activeFieldCount); | |
347 | ||
348 | $store->set('totalRowCount', $this->_totalCount); | |
349 | $store->set('validRowCount', $this->_validCount); | |
350 | $store->set('invalidRowCount', $this->_invalidRowCount); | |
351 | $store->set('conflictRowCount', $this->_conflictCount); | |
352 | ||
353 | switch ($this->_contactType) { | |
354 | case 'Individual': | |
a05662ef | 355 | $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); |
6a488035 TO |
356 | break; |
357 | ||
358 | case 'Household': | |
a05662ef | 359 | $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD); |
6a488035 TO |
360 | break; |
361 | ||
362 | case 'Organization': | |
a05662ef | 363 | $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION); |
6a488035 TO |
364 | } |
365 | ||
366 | if ($this->_invalidRowCount) { | |
367 | $store->set('errorsFileName', $this->_errorFileName); | |
368 | } | |
369 | if ($this->_conflictCount) { | |
370 | $store->set('conflictsFileName', $this->_conflictFileName); | |
371 | } | |
372 | if (isset($this->_rows) && !empty($this->_rows)) { | |
373 | $store->set('dataValues', $this->_rows); | |
374 | } | |
375 | ||
376 | if ($mode == self::MODE_IMPORT) { | |
377 | $store->set('duplicateRowCount', $this->_duplicateCount); | |
378 | if ($this->_duplicateCount) { | |
379 | $store->set('duplicatesFileName', $this->_duplicateFileName); | |
380 | } | |
381 | } | |
382 | } | |
383 | ||
384 | /** | |
385 | * Export data to a CSV file | |
386 | * | |
387 | * @param string $filename | |
388 | * @param array $header | |
389 | * @param data $data | |
390 | * | |
391 | * @return void | |
392 | * @access public | |
393 | */ | |
394 | static function exportCSV($fileName, $header, $data) { | |
395 | $output = array(); | |
396 | $fd = fopen($fileName, 'w'); | |
397 | ||
398 | foreach ($header as $key => $value) { | |
399 | $header[$key] = "\"$value\""; | |
400 | } | |
401 | $config = CRM_Core_Config::singleton(); | |
402 | $output[] = implode($config->fieldSeparator, $header); | |
403 | ||
404 | foreach ($data as $datum) { | |
405 | foreach ($datum as $key => $value) { | |
406 | if (is_array($value)) { | |
407 | foreach ($value[0] as $k1 => $v1) { | |
408 | if ($k1 == 'location_type_id') { | |
409 | continue; | |
410 | } | |
411 | $datum[$k1] = $v1; | |
412 | } | |
413 | } | |
414 | else { | |
415 | $datum[$key] = "\"$value\""; | |
416 | } | |
417 | } | |
418 | $output[] = implode($config->fieldSeparator, $datum); | |
419 | } | |
420 | fwrite($fd, implode("\n", $output)); | |
421 | fclose($fd); | |
422 | } | |
423 | ||
6a488035 TO |
424 | } |
425 |