Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 | 32 | */ |
ec3811b1 | 33 | abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { |
6a488035 TO |
34 | |
35 | protected $_tableName; | |
36 | ||
6a488035 | 37 | /** |
100fef9d | 38 | * Total number of lines in file |
4815ab3d | 39 | * |
40 | * @var integer | |
6a488035 TO |
41 | */ |
42 | protected $_rowCount; | |
43 | ||
6a488035 | 44 | /** |
4815ab3d | 45 | * Running total number of un-matched Contacts. |
6a488035 TO |
46 | */ |
47 | protected $_unMatchCount; | |
48 | ||
49 | /** | |
100fef9d | 50 | * Array of unmatched lines |
6a488035 TO |
51 | */ |
52 | protected $_unMatch; | |
53 | ||
6a488035 | 54 | /** |
100fef9d | 55 | * Total number of contacts with unparsed addresses |
6a488035 TO |
56 | */ |
57 | protected $_unparsedAddressCount; | |
58 | ||
6a488035 | 59 | /** |
100fef9d | 60 | * Filename of mismatch data |
6a488035 TO |
61 | * |
62 | * @var string | |
63 | */ | |
64 | protected $_misMatchFilemName; | |
65 | ||
66 | protected $_primaryKeyName; | |
67 | protected $_statusFieldName; | |
68 | ||
6a488035 | 69 | /** |
100fef9d | 70 | * On duplicate |
6a488035 TO |
71 | * |
72 | * @var int | |
73 | */ | |
74 | public $_onDuplicate; | |
75 | ||
76 | /** | |
100fef9d | 77 | * Dedupe rule group id to use if set |
6a488035 TO |
78 | * |
79 | * @var int | |
80 | */ | |
03e04002 | 81 | public $_dedupeRuleGroupID = NULL; |
6a488035 | 82 | |
86538308 | 83 | /** |
4815ab3d | 84 | * Run import. |
85 | * | |
100fef9d | 86 | * @param string $tableName |
4815ab3d | 87 | * @param array $mapper |
86538308 EM |
88 | * @param int $mode |
89 | * @param int $contactType | |
90 | * @param string $primaryKeyName | |
91 | * @param string $statusFieldName | |
92 | * @param int $onDuplicate | |
100fef9d | 93 | * @param int $statusID |
8fd37b20 | 94 | * @param int $totalRowCount |
86538308 EM |
95 | * @param bool $doGeocodeAddress |
96 | * @param int $timeout | |
8fd37b20 | 97 | * @param string $contactSubType |
100fef9d | 98 | * @param int $dedupeRuleGroupID |
86538308 EM |
99 | * |
100 | * @return mixed | |
101 | */ | |
ae5ffbb7 | 102 | public function run( |
51ccfbbe | 103 | $tableName, |
6a488035 | 104 | &$mapper, |
52892e8b CW |
105 | $mode = self::MODE_PREVIEW, |
106 | $contactType = self::CONTACT_INDIVIDUAL, | |
107 | $primaryKeyName = '_id', | |
108 | $statusFieldName = '_status', | |
109 | $onDuplicate = self::DUPLICATE_SKIP, | |
110 | $statusID = NULL, | |
111 | $totalRowCount = NULL, | |
112 | $doGeocodeAddress = FALSE, | |
113 | $timeout = CRM_Contact_Import_Parser::DEFAULT_TIMEOUT, | |
114 | $contactSubType = NULL, | |
6a488035 TO |
115 | $dedupeRuleGroupID = NULL |
116 | ) { | |
117 | ||
118 | // TODO: Make the timeout actually work | |
119 | $this->_onDuplicate = $onDuplicate; | |
120 | $this->_dedupeRuleGroupID = $dedupeRuleGroupID; | |
121 | ||
122 | switch ($contactType) { | |
a05662ef | 123 | case CRM_Import_Parser::CONTACT_INDIVIDUAL: |
6a488035 TO |
124 | $this->_contactType = 'Individual'; |
125 | break; | |
126 | ||
a05662ef | 127 | case CRM_Import_Parser::CONTACT_HOUSEHOLD: |
6a488035 TO |
128 | $this->_contactType = 'Household'; |
129 | break; | |
130 | ||
a05662ef | 131 | case CRM_Import_Parser::CONTACT_ORGANIZATION: |
6a488035 TO |
132 | $this->_contactType = 'Organization'; |
133 | } | |
134 | ||
135 | $this->_contactSubType = $contactSubType; | |
136 | ||
137 | $this->init(); | |
138 | ||
139 | $this->_rowCount = $this->_warningCount = 0; | |
140 | $this->_invalidRowCount = $this->_validCount = 0; | |
141 | $this->_totalCount = $this->_conflictCount = 0; | |
142 | ||
143 | $this->_errors = array(); | |
144 | $this->_warnings = array(); | |
145 | $this->_conflicts = array(); | |
146 | $this->_unparsedAddresses = array(); | |
147 | ||
6a488035 TO |
148 | $this->_tableName = $tableName; |
149 | $this->_primaryKeyName = $primaryKeyName; | |
150 | $this->_statusFieldName = $statusFieldName; | |
151 | ||
152 | if ($mode == self::MODE_MAPFIELD) { | |
153 | $this->_rows = array(); | |
154 | } | |
155 | else { | |
156 | $this->_activeFieldCount = count($this->_activeFields); | |
157 | } | |
158 | ||
159 | if ($mode == self::MODE_IMPORT) { | |
160 | //get the key of email field | |
161 | foreach ($mapper as $key => $value) { | |
162 | if (strtolower($value) == 'email') { | |
163 | $emailKey = $key; | |
164 | break; | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | if ($statusID) { | |
170 | $skip = 50; | |
171 | // $skip = 1; | |
52892e8b | 172 | $config = CRM_Core_Config::singleton(); |
6a488035 | 173 | $statusFile = "{$config->uploadDir}status_{$statusID}.txt"; |
52892e8b | 174 | $status = "<div class='description'> " . ts('No processing status reported yet.') . "</div>"; |
6a488035 TO |
175 | |
176 | //do not force the browser to display the save dialog, CRM-7640 | |
177 | $contents = json_encode(array(0, $status)); | |
178 | ||
179 | file_put_contents($statusFile, $contents); | |
180 | ||
181 | $startTimestamp = $currTimestamp = $prevTimestamp = time(); | |
182 | } | |
183 | ||
184 | // get the contents of the temp. import table | |
185 | $query = "SELECT * FROM $tableName"; | |
186 | if ($mode == self::MODE_IMPORT) { | |
187 | $query .= " WHERE $statusFieldName = 'NEW'"; | |
188 | } | |
52892e8b CW |
189 | $dao = new CRM_Core_DAO(); |
190 | $db = $dao->getDatabaseConnection(); | |
6a488035 TO |
191 | $result = $db->query($query); |
192 | ||
193 | while ($values = $result->fetchRow(DB_FETCHMODE_ORDERED)) { | |
194 | $this->_rowCount++; | |
195 | ||
196 | /* trim whitespace around the values */ | |
6a488035 TO |
197 | foreach ($values as $k => $v) { |
198 | $values[$k] = trim($v, " \t\r\n"); | |
199 | } | |
200 | if (CRM_Utils_System::isNull($values)) { | |
201 | continue; | |
202 | } | |
203 | ||
204 | $this->_totalCount++; | |
205 | ||
206 | if ($mode == self::MODE_MAPFIELD) { | |
207 | $returnCode = $this->mapField($values); | |
208 | } | |
209 | elseif ($mode == self::MODE_PREVIEW) { | |
210 | $returnCode = $this->preview($values); | |
211 | } | |
212 | elseif ($mode == self::MODE_SUMMARY) { | |
213 | $returnCode = $this->summary($values); | |
214 | } | |
215 | elseif ($mode == self::MODE_IMPORT) { | |
216 | //print "Running parser in import mode<br/>\n"; | |
217 | $returnCode = $this->import($onDuplicate, $values, $doGeocodeAddress); | |
218 | if ($statusID && (($this->_rowCount % $skip) == 0)) { | |
219 | $currTimestamp = time(); | |
52892e8b CW |
220 | $totalTime = ($currTimestamp - $startTimestamp); |
221 | $time = ($currTimestamp - $prevTimestamp); | |
222 | $recordsLeft = $totalRowCount - $this->_rowCount; | |
6a488035 TO |
223 | if ($recordsLeft < 0) { |
224 | $recordsLeft = 0; | |
225 | } | |
226 | $estimatedTime = ($recordsLeft / $skip) * $time; | |
52892e8b | 227 | $estMinutes = floor($estimatedTime / 60); |
6a488035 TO |
228 | $timeFormatted = ''; |
229 | if ($estMinutes > 1) { | |
230 | $timeFormatted = $estMinutes . ' ' . ts('minutes') . ' '; | |
231 | $estimatedTime = $estimatedTime - ($estMinutes * 60); | |
232 | } | |
233 | $timeFormatted .= round($estimatedTime) . ' ' . ts('seconds'); | |
ce80b209 | 234 | $processedPercent = (int ) (($this->_rowCount * 100) / $totalRowCount); |
6a488035 TO |
235 | $statusMsg = ts('%1 of %2 records - %3 remaining', |
236 | array(1 => $this->_rowCount, 2 => $totalRowCount, 3 => $timeFormatted) | |
237 | ); | |
238 | $status = " | |
239 | <div class=\"description\"> | |
240 | <strong>{$statusMsg}</strong> | |
241 | </div> | |
242 | "; | |
243 | ||
ce80b209 | 244 | $contents = json_encode(array($processedPercent, $status)); |
6a488035 TO |
245 | |
246 | file_put_contents($statusFile, $contents); | |
247 | ||
248 | $prevTimestamp = $currTimestamp; | |
249 | } | |
250 | // sleep(1); | |
251 | } | |
252 | else { | |
253 | $returnCode = self::ERROR; | |
254 | } | |
255 | ||
256 | // note that a line could be valid but still produce a warning | |
257 | if ($returnCode & self::VALID) { | |
258 | $this->_validCount++; | |
259 | if ($mode == self::MODE_MAPFIELD) { | |
260 | $this->_rows[] = $values; | |
261 | $this->_activeFieldCount = max($this->_activeFieldCount, count($values)); | |
262 | } | |
263 | } | |
264 | ||
265 | if ($returnCode & self::WARNING) { | |
266 | $this->_warningCount++; | |
267 | if ($this->_warningCount < $this->_maxWarningCount) { | |
268 | $this->_warningCount[] = $line; | |
269 | } | |
270 | } | |
271 | ||
272 | if ($returnCode & self::ERROR) { | |
273 | $this->_invalidRowCount++; | |
274 | if ($this->_invalidRowCount < $this->_maxErrorCount) { | |
275 | array_unshift($values, $this->_rowCount); | |
276 | $this->_errors[] = $values; | |
277 | } | |
278 | } | |
279 | ||
280 | if ($returnCode & self::CONFLICT) { | |
281 | $this->_conflictCount++; | |
282 | array_unshift($values, $this->_rowCount); | |
283 | $this->_conflicts[] = $values; | |
284 | } | |
285 | ||
286 | if ($returnCode & self::NO_MATCH) { | |
287 | $this->_unMatchCount++; | |
288 | array_unshift($values, $this->_rowCount); | |
289 | $this->_unMatch[] = $values; | |
290 | } | |
291 | ||
292 | if ($returnCode & self::DUPLICATE) { | |
293 | if ($returnCode & self::MULTIPLE_DUPE) { | |
294 | /* TODO: multi-dupes should be counted apart from singles | |
e70a7fc0 | 295 | * on non-skip action */ |
6a488035 TO |
296 | } |
297 | $this->_duplicateCount++; | |
298 | array_unshift($values, $this->_rowCount); | |
299 | $this->_duplicates[] = $values; | |
300 | if ($onDuplicate != self::DUPLICATE_SKIP) { | |
301 | $this->_validCount++; | |
302 | } | |
303 | } | |
304 | ||
305 | if ($returnCode & self::UNPARSED_ADDRESS_WARNING) { | |
306 | $this->_unparsedAddressCount++; | |
307 | array_unshift($values, $this->_rowCount); | |
308 | $this->_unparsedAddresses[] = $values; | |
309 | } | |
310 | // we give the derived class a way of aborting the process | |
311 | // note that the return code could be multiple code or'ed together | |
312 | if ($returnCode & self::STOP) { | |
313 | break; | |
314 | } | |
315 | ||
316 | // if we are done processing the maxNumber of lines, break | |
317 | if ($this->_maxLinesToProcess > 0 && $this->_validCount >= $this->_maxLinesToProcess) { | |
318 | break; | |
319 | } | |
320 | ||
321 | // clean up memory from dao's | |
322 | CRM_Core_DAO::freeResult(); | |
323 | ||
324 | // see if we've hit our timeout yet | |
325 | /* if ( $the_thing_with_the_stuff ) { | |
e70a7fc0 TO |
326 | do_something( ); |
327 | } */ | |
6a488035 TO |
328 | } |
329 | ||
6a488035 TO |
330 | if ($mode == self::MODE_PREVIEW || $mode == self::MODE_IMPORT) { |
331 | $customHeaders = $mapper; | |
332 | ||
333 | $customfields = CRM_Core_BAO_CustomField::getFields($this->_contactType); | |
334 | foreach ($customHeaders as $key => $value) { | |
335 | if ($id = CRM_Core_BAO_CustomField::getKeyID($value)) { | |
336 | $customHeaders[$key] = $customfields[$id][0]; | |
337 | } | |
338 | } | |
339 | ||
340 | if ($this->_invalidRowCount) { | |
341 | // removed view url for invlaid contacts | |
353ffa53 TO |
342 | $headers = array_merge(array( |
343 | ts('Line Number'), | |
6a488035 TO |
344 | ts('Reason'), |
345 | ), | |
346 | $customHeaders | |
347 | ); | |
348 | $this->_errorFileName = self::errorFileName(self::ERROR); | |
349 | self::exportCSV($this->_errorFileName, $headers, $this->_errors); | |
350 | } | |
351 | if ($this->_conflictCount) { | |
353ffa53 TO |
352 | $headers = array_merge(array( |
353 | ts('Line Number'), | |
6a488035 TO |
354 | ts('Reason'), |
355 | ), | |
356 | $customHeaders | |
357 | ); | |
358 | $this->_conflictFileName = self::errorFileName(self::CONFLICT); | |
359 | self::exportCSV($this->_conflictFileName, $headers, $this->_conflicts); | |
360 | } | |
361 | if ($this->_duplicateCount) { | |
353ffa53 TO |
362 | $headers = array_merge(array( |
363 | ts('Line Number'), | |
6a488035 TO |
364 | ts('View Contact URL'), |
365 | ), | |
366 | $customHeaders | |
367 | ); | |
368 | ||
369 | $this->_duplicateFileName = self::errorFileName(self::DUPLICATE); | |
370 | self::exportCSV($this->_duplicateFileName, $headers, $this->_duplicates); | |
371 | } | |
372 | if ($this->_unMatchCount) { | |
353ffa53 TO |
373 | $headers = array_merge(array( |
374 | ts('Line Number'), | |
6a488035 TO |
375 | ts('Reason'), |
376 | ), | |
377 | $customHeaders | |
378 | ); | |
379 | ||
380 | $this->_misMatchFilemName = self::errorFileName(self::NO_MATCH); | |
381 | self::exportCSV($this->_misMatchFilemName, $headers, $this->_unMatch); | |
382 | } | |
383 | if ($this->_unparsedAddressCount) { | |
353ffa53 TO |
384 | $headers = array_merge(array( |
385 | ts('Line Number'), | |
6a488035 TO |
386 | ts('Contact Edit URL'), |
387 | ), | |
388 | $customHeaders | |
389 | ); | |
390 | $this->_errorFileName = self::errorFileName(self::UNPARSED_ADDRESS_WARNING); | |
391 | self::exportCSV($this->_errorFileName, $headers, $this->_unparsedAddresses); | |
392 | } | |
393 | } | |
394 | //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount"; | |
395 | return $this->fini(); | |
396 | } | |
397 | ||
6a488035 | 398 | /** |
fe482240 | 399 | * Given a list of the importable field keys that the user has selected. |
6a488035 TO |
400 | * set the active fields array to this list |
401 | * | |
ae5ffbb7 TO |
402 | * @param array $fieldKeys |
403 | * Mapped array of values. | |
6a488035 | 404 | */ |
00be9182 | 405 | public function setActiveFields($fieldKeys) { |
6a488035 TO |
406 | $this->_activeFieldCount = count($fieldKeys); |
407 | foreach ($fieldKeys as $key) { | |
408 | if (empty($this->_fields[$key])) { | |
719a6fec | 409 | $this->_activeFields[] = new CRM_Contact_Import_Field('', ts('- do not import -')); |
6a488035 TO |
410 | } |
411 | else { | |
412 | $this->_activeFields[] = clone($this->_fields[$key]); | |
413 | } | |
414 | } | |
415 | } | |
416 | ||
86538308 EM |
417 | /** |
418 | * @param $elements | |
419 | */ | |
00be9182 | 420 | public function setActiveFieldLocationTypes($elements) { |
6a488035 TO |
421 | for ($i = 0; $i < count($elements); $i++) { |
422 | $this->_activeFields[$i]->_hasLocationType = $elements[$i]; | |
423 | } | |
424 | } | |
425 | ||
86538308 EM |
426 | /** |
427 | * @param $elements | |
428 | */ | |
429 | /** | |
430 | * @param $elements | |
431 | */ | |
00be9182 | 432 | public function setActiveFieldPhoneTypes($elements) { |
6a488035 TO |
433 | for ($i = 0; $i < count($elements); $i++) { |
434 | $this->_activeFields[$i]->_phoneType = $elements[$i]; | |
435 | } | |
436 | } | |
437 | ||
86538308 EM |
438 | /** |
439 | * @param $elements | |
440 | */ | |
00be9182 | 441 | public function setActiveFieldWebsiteTypes($elements) { |
6a488035 TO |
442 | for ($i = 0; $i < count($elements); $i++) { |
443 | $this->_activeFields[$i]->_websiteType = $elements[$i]; | |
444 | } | |
445 | } | |
446 | ||
447 | /** | |
fe482240 | 448 | * Set IM Service Provider type fields. |
6a488035 | 449 | * |
77c5b619 TO |
450 | * @param array $elements |
451 | * IM service provider type ids. | |
6a488035 | 452 | */ |
00be9182 | 453 | public function setActiveFieldImProviders($elements) { |
6a488035 TO |
454 | for ($i = 0; $i < count($elements); $i++) { |
455 | $this->_activeFields[$i]->_imProvider = $elements[$i]; | |
456 | } | |
457 | } | |
458 | ||
86538308 EM |
459 | /** |
460 | * @param $elements | |
461 | */ | |
00be9182 | 462 | public function setActiveFieldRelated($elements) { |
6a488035 TO |
463 | for ($i = 0; $i < count($elements); $i++) { |
464 | $this->_activeFields[$i]->_related = $elements[$i]; | |
465 | } | |
466 | } | |
467 | ||
86538308 EM |
468 | /** |
469 | * @param $elements | |
470 | */ | |
00be9182 | 471 | public function setActiveFieldRelatedContactType($elements) { |
6a488035 TO |
472 | for ($i = 0; $i < count($elements); $i++) { |
473 | $this->_activeFields[$i]->_relatedContactType = $elements[$i]; | |
474 | } | |
475 | } | |
476 | ||
86538308 EM |
477 | /** |
478 | * @param $elements | |
479 | */ | |
00be9182 | 480 | public function setActiveFieldRelatedContactDetails($elements) { |
6a488035 TO |
481 | for ($i = 0; $i < count($elements); $i++) { |
482 | $this->_activeFields[$i]->_relatedContactDetails = $elements[$i]; | |
483 | } | |
484 | } | |
485 | ||
86538308 EM |
486 | /** |
487 | * @param $elements | |
488 | */ | |
00be9182 | 489 | public function setActiveFieldRelatedContactLocType($elements) { |
6a488035 TO |
490 | for ($i = 0; $i < count($elements); $i++) { |
491 | $this->_activeFields[$i]->_relatedContactLocType = $elements[$i]; | |
492 | } | |
493 | } | |
494 | ||
86538308 | 495 | /** |
4815ab3d | 496 | * Set active field for related contact's phone type. |
497 | * | |
498 | * @param array $elements | |
86538308 | 499 | */ |
00be9182 | 500 | public function setActiveFieldRelatedContactPhoneType($elements) { |
6a488035 TO |
501 | for ($i = 0; $i < count($elements); $i++) { |
502 | $this->_activeFields[$i]->_relatedContactPhoneType = $elements[$i]; | |
503 | } | |
504 | } | |
505 | ||
86538308 EM |
506 | /** |
507 | * @param $elements | |
508 | */ | |
00be9182 | 509 | public function setActiveFieldRelatedContactWebsiteType($elements) { |
6a488035 TO |
510 | for ($i = 0; $i < count($elements); $i++) { |
511 | $this->_activeFields[$i]->_relatedContactWebsiteType = $elements[$i]; | |
512 | } | |
513 | } | |
514 | ||
515 | /** | |
fe482240 | 516 | * Set IM Service Provider type fields for related contacts. |
6a488035 | 517 | * |
77c5b619 TO |
518 | * @param array $elements |
519 | * IM service provider type ids of related contact. | |
6a488035 | 520 | */ |
00be9182 | 521 | public function setActiveFieldRelatedContactImProvider($elements) { |
6a488035 TO |
522 | for ($i = 0; $i < count($elements); $i++) { |
523 | $this->_activeFields[$i]->_relatedContactImProvider = $elements[$i]; | |
524 | } | |
525 | } | |
526 | ||
527 | /** | |
fe482240 | 528 | * Format the field values for input to the api. |
6a488035 | 529 | * |
a6c01b45 CW |
530 | * @return array |
531 | * (reference ) associative array of name/value pairs | |
6a488035 | 532 | */ |
00be9182 | 533 | public function &getActiveFieldParams() { |
6a488035 TO |
534 | $params = array(); |
535 | ||
6a488035 TO |
536 | for ($i = 0; $i < $this->_activeFieldCount; $i++) { |
537 | if ($this->_activeFields[$i]->_name == 'do_not_import') { | |
538 | continue; | |
539 | } | |
540 | ||
541 | if (isset($this->_activeFields[$i]->_value)) { | |
542 | if (isset($this->_activeFields[$i]->_hasLocationType)) { | |
543 | if (!isset($params[$this->_activeFields[$i]->_name])) { | |
544 | $params[$this->_activeFields[$i]->_name] = array(); | |
545 | } | |
546 | ||
547 | $value = array( | |
ae5ffbb7 TO |
548 | $this->_activeFields[$i]->_name => $this->_activeFields[$i]->_value, |
549 | 'location_type_id' => $this->_activeFields[$i]->_hasLocationType, | |
6a488035 TO |
550 | ); |
551 | ||
552 | if (isset($this->_activeFields[$i]->_phoneType)) { | |
553 | $value['phone_type_id'] = $this->_activeFields[$i]->_phoneType; | |
554 | } | |
555 | ||
556 | // get IM service Provider type id | |
557 | if (isset($this->_activeFields[$i]->_imProvider)) { | |
558 | $value['provider_id'] = $this->_activeFields[$i]->_imProvider; | |
559 | } | |
560 | ||
561 | $params[$this->_activeFields[$i]->_name][] = $value; | |
562 | } | |
563 | elseif (isset($this->_activeFields[$i]->_websiteType)) { | |
564 | $value = array( | |
565 | $this->_activeFields[$i]->_name => $this->_activeFields[$i]->_value, | |
566 | 'website_type_id' => $this->_activeFields[$i]->_websiteType, | |
567 | ); | |
568 | ||
569 | $params[$this->_activeFields[$i]->_name][] = $value; | |
570 | } | |
571 | ||
572 | if (!isset($params[$this->_activeFields[$i]->_name])) { | |
573 | if (!isset($this->_activeFields[$i]->_related)) { | |
574 | $params[$this->_activeFields[$i]->_name] = $this->_activeFields[$i]->_value; | |
575 | } | |
576 | } | |
577 | ||
578 | //minor fix for CRM-4062 | |
579 | if (isset($this->_activeFields[$i]->_related)) { | |
580 | if (!isset($params[$this->_activeFields[$i]->_related])) { | |
581 | $params[$this->_activeFields[$i]->_related] = array(); | |
582 | } | |
583 | ||
584 | if (!isset($params[$this->_activeFields[$i]->_related]['contact_type']) && !empty($this->_activeFields[$i]->_relatedContactType)) { | |
585 | $params[$this->_activeFields[$i]->_related]['contact_type'] = $this->_activeFields[$i]->_relatedContactType; | |
586 | } | |
587 | ||
588 | if (isset($this->_activeFields[$i]->_relatedContactLocType) && !empty($this->_activeFields[$i]->_value)) { | |
7c7c2ddb | 589 | if (!empty($params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails]) && |
353ffa53 TO |
590 | !is_array($params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails]) |
591 | ) { | |
6a488035 TO |
592 | $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails] = array(); |
593 | } | |
594 | $value = array( | |
595 | $this->_activeFields[$i]->_relatedContactDetails => $this->_activeFields[$i]->_value, | |
596 | 'location_type_id' => $this->_activeFields[$i]->_relatedContactLocType, | |
597 | ); | |
598 | ||
599 | if (isset($this->_activeFields[$i]->_relatedContactPhoneType)) { | |
600 | $value['phone_type_id'] = $this->_activeFields[$i]->_relatedContactPhoneType; | |
601 | } | |
602 | ||
603 | // get IM service Provider type id for related contact | |
604 | if (isset($this->_activeFields[$i]->_relatedContactImProvider)) { | |
605 | $value['provider_id'] = $this->_activeFields[$i]->_relatedContactImProvider; | |
606 | } | |
607 | ||
608 | $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails][] = $value; | |
609 | } | |
610 | elseif (isset($this->_activeFields[$i]->_relatedContactWebsiteType)) { | |
611 | $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails][] = array( | |
612 | 'url' => $this->_activeFields[$i]->_value, | |
613 | 'website_type_id' => $this->_activeFields[$i]->_relatedContactWebsiteType, | |
614 | ); | |
615 | } | |
616 | else { | |
617 | $params[$this->_activeFields[$i]->_related][$this->_activeFields[$i]->_relatedContactDetails] = $this->_activeFields[$i]->_value; | |
618 | } | |
619 | } | |
620 | } | |
621 | } | |
622 | ||
623 | return $params; | |
624 | } | |
625 | ||
86538308 EM |
626 | /** |
627 | * @return array | |
628 | */ | |
00be9182 | 629 | public function getColumnPatterns() { |
6a488035 TO |
630 | $values = array(); |
631 | foreach ($this->_fields as $name => $field) { | |
632 | $values[$name] = $field->_columnPattern; | |
633 | } | |
634 | return $values; | |
635 | } | |
636 | ||
86538308 | 637 | /** |
100fef9d | 638 | * @param string $name |
86538308 EM |
639 | * @param $title |
640 | * @param int $type | |
641 | * @param string $headerPattern | |
642 | * @param string $dataPattern | |
643 | * @param bool $hasLocationType | |
644 | */ | |
ae5ffbb7 | 645 | public function addField( |
51ccfbbe | 646 | $name, $title, $type = CRM_Utils_Type::T_INT, |
6a488035 TO |
647 | $headerPattern = '//', $dataPattern = '//', |
648 | $hasLocationType = FALSE | |
649 | ) { | |
719a6fec | 650 | $this->_fields[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType); |
6a488035 | 651 | if (empty($name)) { |
719a6fec | 652 | $this->_fields['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType); |
6a488035 TO |
653 | } |
654 | } | |
655 | ||
6a488035 | 656 | /** |
fe482240 | 657 | * Store parser values. |
6a488035 TO |
658 | * |
659 | * @param CRM_Core_Session $store | |
660 | * | |
2a6da8d7 | 661 | * @param int $mode |
6a488035 | 662 | */ |
00be9182 | 663 | public function set($store, $mode = self::MODE_SUMMARY) { |
6a488035 TO |
664 | $store->set('rowCount', $this->_rowCount); |
665 | $store->set('fields', $this->getSelectValues()); | |
666 | $store->set('fieldTypes', $this->getSelectTypes()); | |
667 | ||
668 | $store->set('columnPatterns', $this->getColumnPatterns()); | |
669 | $store->set('dataPatterns', $this->getDataPatterns()); | |
670 | $store->set('columnCount', $this->_activeFieldCount); | |
671 | ||
672 | $store->set('totalRowCount', $this->_totalCount); | |
673 | $store->set('validRowCount', $this->_validCount); | |
674 | $store->set('invalidRowCount', $this->_invalidRowCount); | |
675 | $store->set('conflictRowCount', $this->_conflictCount); | |
676 | $store->set('unMatchCount', $this->_unMatchCount); | |
677 | ||
678 | switch ($this->_contactType) { | |
679 | case 'Individual': | |
a05662ef | 680 | $store->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL); |
6a488035 TO |
681 | break; |
682 | ||
683 | case 'Household': | |
a05662ef | 684 | $store->set('contactType', CRM_Import_Parser::CONTACT_HOUSEHOLD); |
6a488035 TO |
685 | break; |
686 | ||
687 | case 'Organization': | |
a05662ef | 688 | $store->set('contactType', CRM_Import_Parser::CONTACT_ORGANIZATION); |
6a488035 TO |
689 | } |
690 | ||
691 | if ($this->_invalidRowCount) { | |
692 | $store->set('errorsFileName', $this->_errorFileName); | |
693 | } | |
694 | if ($this->_conflictCount) { | |
695 | $store->set('conflictsFileName', $this->_conflictFileName); | |
696 | } | |
697 | if (isset($this->_rows) && !empty($this->_rows)) { | |
698 | $store->set('dataValues', $this->_rows); | |
699 | } | |
700 | ||
701 | if ($this->_unMatchCount) { | |
702 | $store->set('mismatchFileName', $this->_misMatchFilemName); | |
703 | } | |
704 | ||
705 | if ($mode == self::MODE_IMPORT) { | |
706 | $store->set('duplicateRowCount', $this->_duplicateCount); | |
707 | $store->set('unparsedAddressCount', $this->_unparsedAddressCount); | |
708 | if ($this->_duplicateCount) { | |
709 | $store->set('duplicatesFileName', $this->_duplicateFileName); | |
710 | } | |
711 | if ($this->_unparsedAddressCount) { | |
712 | $store->set('errorsFileName', $this->_errorFileName); | |
713 | } | |
714 | } | |
715 | //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount"; | |
716 | } | |
717 | ||
718 | /** | |
fe482240 | 719 | * Export data to a CSV file. |
6a488035 | 720 | * |
100fef9d | 721 | * @param string $fileName |
6a488035 | 722 | * @param array $header |
c490a46a | 723 | * @param array $data |
4815ab3d | 724 | */ |
00be9182 | 725 | public static function exportCSV($fileName, $header, $data) { |
6a488035 TO |
726 | |
727 | if (file_exists($fileName) && !is_writable($fileName)) { | |
728 | CRM_Core_Error::movedSiteError($fileName); | |
729 | } | |
730 | //hack to remove '_status', '_statusMsg' and '_id' from error file | |
731 | $errorValues = array(); | |
732 | $dbRecordStatus = array('IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW'); | |
733 | foreach ($data as $rowCount => $rowValues) { | |
734 | $count = 0; | |
735 | foreach ($rowValues as $key => $val) { | |
736 | if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) { | |
737 | break; | |
738 | } | |
739 | $errorValues[$rowCount][$key] = $val; | |
740 | $count++; | |
741 | } | |
742 | } | |
743 | $data = $errorValues; | |
744 | ||
745 | $output = array(); | |
746 | $fd = fopen($fileName, 'w'); | |
747 | ||
748 | foreach ($header as $key => $value) { | |
749 | $header[$key] = "\"$value\""; | |
750 | } | |
751 | $config = CRM_Core_Config::singleton(); | |
752 | $output[] = implode($config->fieldSeparator, $header); | |
753 | ||
754 | foreach ($data as $datum) { | |
755 | foreach ($datum as $key => $value) { | |
756 | $datum[$key] = "\"$value\""; | |
757 | } | |
758 | $output[] = implode($config->fieldSeparator, $datum); | |
759 | } | |
760 | fwrite($fd, implode("\n", $output)); | |
761 | fclose($fd); | |
762 | } | |
763 | ||
764 | /** | |
4815ab3d | 765 | * Update the record with PK $id in the import database table. |
6a488035 TO |
766 | * |
767 | * @param int $id | |
768 | * @param array $params | |
6a488035 TO |
769 | */ |
770 | public function updateImportRecord($id, &$params) { | |
771 | $statusFieldName = $this->_statusFieldName; | |
772 | $primaryKeyName = $this->_primaryKeyName; | |
773 | ||
774 | if ($statusFieldName && $primaryKeyName) { | |
775 | $dao = new CRM_Core_DAO(); | |
776 | $db = $dao->getDatabaseConnection(); | |
777 | ||
778 | $query = "UPDATE $this->_tableName | |
52892e8b | 779 | SET $statusFieldName = ?, |
6a488035 | 780 | ${statusFieldName}Msg = ? |
52892e8b | 781 | WHERE $primaryKeyName = ?"; |
6a488035 TO |
782 | $args = array( |
783 | $params[$statusFieldName], | |
784 | CRM_Utils_Array::value("${statusFieldName}Msg", $params), | |
785 | $id, | |
786 | ); | |
787 | ||
788 | //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>"; | |
789 | ||
790 | $db->query($query, $args); | |
791 | } | |
792 | } | |
793 | ||
5dc4d424 | 794 | /** |
795 | * Format common params data to proper format to store. | |
796 | * | |
797 | * @param array $params | |
798 | * Contain record values. | |
799 | * @param array $formatted | |
800 | * Array of formatted data. | |
801 | * @param array $contactFields | |
802 | * Contact DAO fields. | |
803 | */ | |
804 | public function formatCommonData($params, &$formatted, &$contactFields) { | |
805 | $csType = array( | |
806 | CRM_Utils_Array::value('contact_type', $formatted), | |
807 | ); | |
808 | ||
809 | //CRM-5125 | |
810 | //add custom fields for contact sub type | |
811 | if (!empty($this->_contactSubType)) { | |
812 | $csType = $this->_contactSubType; | |
813 | } | |
814 | ||
815 | if ($relCsType = CRM_Utils_Array::value('contact_sub_type', $formatted)) { | |
816 | $csType = $relCsType; | |
817 | } | |
818 | ||
819 | $customFields = CRM_Core_BAO_CustomField::getFields($formatted['contact_type'], FALSE, FALSE, $csType); | |
820 | ||
821 | $addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address'); | |
822 | $customFields = $customFields + $addressCustomFields; | |
823 | ||
824 | //if a Custom Email Greeting, Custom Postal Greeting or Custom Addressee is mapped, and no "Greeting / Addressee Type ID" is provided, then automatically set the type = Customized, CRM-4575 | |
825 | $elements = array( | |
826 | 'email_greeting_custom' => 'email_greeting', | |
827 | 'postal_greeting_custom' => 'postal_greeting', | |
828 | 'addressee_custom' => 'addressee', | |
829 | ); | |
830 | foreach ($elements as $k => $v) { | |
831 | if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) { | |
832 | $label = key(CRM_Core_OptionGroup::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"')); | |
833 | $params[$v] = $label; | |
834 | } | |
835 | } | |
836 | ||
837 | //format date first | |
838 | $session = CRM_Core_Session::singleton(); | |
839 | $dateType = $session->get("dateTypes"); | |
840 | foreach ($params as $key => $val) { | |
841 | $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key); | |
842 | if ($customFieldID && | |
843 | !array_key_exists($customFieldID, $addressCustomFields) | |
844 | ) { | |
845 | //we should not update Date to null, CRM-4062 | |
846 | if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) { | |
22161818 | 847 | //CRM-21267 |
160a5de1 | 848 | CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key); |
5dc4d424 | 849 | } |
850 | elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { | |
851 | if (empty($val) && !is_numeric($val) && $this->_onDuplicate == CRM_Import_Parser::DUPLICATE_FILL) { | |
852 | //retain earlier value when Import mode is `Fill` | |
853 | unset($params[$key]); | |
854 | } | |
855 | else { | |
856 | $params[$key] = CRM_Utils_String::strtoboolstr($val); | |
857 | } | |
858 | } | |
859 | } | |
860 | ||
861 | if ($key == 'birth_date' && $val) { | |
862 | CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); | |
863 | } | |
864 | elseif ($key == 'deceased_date' && $val) { | |
865 | CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); | |
866 | $params['is_deceased'] = 1; | |
867 | } | |
868 | elseif ($key == 'is_deceased' && $val) { | |
869 | $params[$key] = CRM_Utils_String::strtoboolstr($val); | |
870 | } | |
871 | elseif ($key == 'gender') { | |
872 | //CRM-4360 | |
873 | $params[$key] = $this->checkGender($val); | |
874 | } | |
875 | } | |
876 | ||
877 | //now format custom data. | |
878 | foreach ($params as $key => $field) { | |
879 | if (is_array($field)) { | |
880 | $isAddressCustomField = FALSE; | |
881 | foreach ($field as $value) { | |
882 | $break = FALSE; | |
883 | if (is_array($value)) { | |
884 | foreach ($value as $name => $testForEmpty) { | |
885 | if ($addressCustomFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
886 | $isAddressCustomField = TRUE; | |
887 | break; | |
888 | } | |
889 | // check if $value does not contain IM provider or phoneType | |
890 | if (($name !== 'phone_type_id' || $name !== 'provider_id') && ($testForEmpty === '' || $testForEmpty == NULL)) { | |
891 | $break = TRUE; | |
892 | break; | |
893 | } | |
894 | } | |
895 | } | |
896 | else { | |
897 | $break = TRUE; | |
898 | } | |
899 | ||
900 | if (!$break) { | |
901 | $this->formatContactParameters($value, $formatted); | |
902 | } | |
903 | } | |
904 | if (!$isAddressCustomField) { | |
905 | continue; | |
906 | } | |
907 | } | |
908 | ||
909 | $formatValues = array( | |
910 | $key => $field, | |
911 | ); | |
912 | ||
913 | if (($key !== 'preferred_communication_method') && (array_key_exists($key, $contactFields))) { | |
914 | // due to merging of individual table and | |
915 | // contact table, we need to avoid | |
916 | // preferred_communication_method forcefully | |
917 | $formatValues['contact_type'] = $formatted['contact_type']; | |
918 | } | |
919 | ||
920 | if ($key == 'id' && isset($field)) { | |
921 | $formatted[$key] = $field; | |
922 | } | |
923 | $this->formatContactParameters($formatValues, $formatted); | |
924 | ||
925 | //Handling Custom Data | |
926 | // note: Address custom fields will be handled separately inside formatContactParameters | |
927 | if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && | |
928 | array_key_exists($customFieldID, $customFields) && | |
929 | !array_key_exists($customFieldID, $addressCustomFields) | |
930 | ) { | |
931 | ||
932 | $extends = CRM_Utils_Array::value('extends', $customFields[$customFieldID]); | |
933 | $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]); | |
934 | switch ($htmlType) { | |
935 | case 'Select': | |
936 | case 'Radio': | |
937 | case 'Autocomplete-Select': | |
938 | if ($customFields[$customFieldID]['data_type'] == 'String' || $customFields[$customFieldID]['data_type'] == 'Int') { | |
939 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
940 | foreach ($customOption as $customValue) { | |
941 | $val = CRM_Utils_Array::value('value', $customValue); | |
942 | $label = CRM_Utils_Array::value('label', $customValue); | |
943 | $label = strtolower($label); | |
944 | $value = strtolower(trim($formatted[$key])); | |
945 | if (($value == $label) || ($value == strtolower($val))) { | |
946 | $params[$key] = $formatted[$key] = $val; | |
947 | } | |
948 | } | |
949 | } | |
950 | break; | |
951 | ||
952 | case 'CheckBox': | |
953 | case 'AdvMulti-Select': | |
954 | case 'Multi-Select': | |
955 | ||
956 | if (!empty($formatted[$key]) && !empty($params[$key])) { | |
957 | $mulValues = explode(',', $formatted[$key]); | |
958 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
959 | $formatted[$key] = array(); | |
960 | $params[$key] = array(); | |
961 | foreach ($mulValues as $v1) { | |
962 | foreach ($customOption as $v2) { | |
963 | if ((strtolower($v2['label']) == strtolower(trim($v1))) || | |
964 | (strtolower($v2['value']) == strtolower(trim($v1))) | |
965 | ) { | |
966 | if ($htmlType == 'CheckBox') { | |
967 | $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1; | |
968 | } | |
969 | else { | |
970 | $params[$key][] = $formatted[$key][] = $v2['value']; | |
971 | } | |
972 | } | |
973 | } | |
974 | } | |
975 | } | |
976 | break; | |
977 | } | |
978 | } | |
979 | } | |
980 | ||
981 | if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && array_key_exists($customFieldID, $customFields) && | |
982 | !array_key_exists($customFieldID, $addressCustomFields) | |
983 | ) { | |
984 | // @todo calling api functions directly is not supported | |
985 | _civicrm_api3_custom_format_params($params, $formatted, $extends); | |
986 | } | |
987 | ||
988 | // to check if not update mode and unset the fields with empty value. | |
989 | if (!$this->_updateWithId && array_key_exists('custom', $formatted)) { | |
990 | foreach ($formatted['custom'] as $customKey => $customvalue) { | |
991 | if (empty($formatted['custom'][$customKey][-1]['is_required'])) { | |
992 | $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required']; | |
993 | } | |
994 | $emptyValue = CRM_Utils_Array::value('value', $customvalue[-1]); | |
995 | if (!isset($emptyValue)) { | |
996 | unset($formatted['custom'][$customKey]); | |
997 | } | |
998 | } | |
999 | } | |
1000 | ||
1001 | // parse street address, CRM-5450 | |
1002 | if ($this->_parseStreetAddress) { | |
1003 | if (array_key_exists('address', $formatted) && is_array($formatted['address'])) { | |
1004 | foreach ($formatted['address'] as $instance => & $address) { | |
1005 | $streetAddress = CRM_Utils_Array::value('street_address', $address); | |
1006 | if (empty($streetAddress)) { | |
1007 | continue; | |
1008 | } | |
1009 | // parse address field. | |
1010 | $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($streetAddress); | |
1011 | ||
1012 | //street address consider to be parsed properly, | |
1013 | //If we get street_name and street_number. | |
1014 | if (empty($parsedFields['street_name']) || empty($parsedFields['street_number'])) { | |
1015 | $parsedFields = array_fill_keys(array_keys($parsedFields), ''); | |
1016 | } | |
1017 | ||
1018 | // merge parse address w/ main address block. | |
1019 | $address = array_merge($address, $parsedFields); | |
1020 | } | |
1021 | } | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | /** | |
1026 | * Format contact parameters. | |
1027 | * | |
1028 | * @todo this function needs re-writing & re-merging into the main function. | |
1029 | * | |
1030 | * Here be dragons. | |
1031 | * | |
1032 | * @param array $values | |
1033 | * @param array $params | |
1034 | * | |
1035 | * @return bool | |
1036 | */ | |
1037 | protected function formatContactParameters(&$values, &$params) { | |
1038 | // Crawl through the possible classes: | |
1039 | // Contact | |
1040 | // Individual | |
1041 | // Household | |
1042 | // Organization | |
1043 | // Location | |
1044 | // Address | |
1045 | ||
1046 | // Phone | |
1047 | // IM | |
1048 | // Note | |
1049 | // Custom | |
1050 | ||
1051 | // Cache the various object fields | |
1052 | static $fields = array(); | |
1053 | ||
1054 | // first add core contact values since for other Civi modules they are not added | |
1055 | $contactFields = CRM_Contact_DAO_Contact::fields(); | |
1056 | _civicrm_api3_store_values($contactFields, $values, $params); | |
1057 | ||
1058 | if (isset($values['contact_type'])) { | |
1059 | // we're an individual/household/org property | |
1060 | ||
1061 | $fields[$values['contact_type']] = CRM_Contact_DAO_Contact::fields(); | |
1062 | ||
1063 | _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params); | |
1064 | return TRUE; | |
1065 | } | |
1066 | ||
1067 | if (isset($values['individual_prefix'])) { | |
1068 | if (!empty($params['prefix_id'])) { | |
1069 | $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); | |
1070 | $params['prefix'] = $prefixes[$params['prefix_id']]; | |
1071 | } | |
1072 | else { | |
1073 | $params['prefix'] = $values['individual_prefix']; | |
1074 | } | |
1075 | return TRUE; | |
1076 | } | |
1077 | ||
1078 | if (isset($values['individual_suffix'])) { | |
1079 | if (!empty($params['suffix_id'])) { | |
1080 | $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); | |
1081 | $params['suffix'] = $suffixes[$params['suffix_id']]; | |
1082 | } | |
1083 | else { | |
1084 | $params['suffix'] = $values['individual_suffix']; | |
1085 | } | |
1086 | return TRUE; | |
1087 | } | |
1088 | ||
1089 | // CRM-4575 | |
1090 | if (isset($values['email_greeting'])) { | |
1091 | if (!empty($params['email_greeting_id'])) { | |
1092 | $emailGreetingFilter = array( | |
1093 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), | |
1094 | 'greeting_type' => 'email_greeting', | |
1095 | ); | |
1096 | $emailGreetings = CRM_Core_PseudoConstant::greeting($emailGreetingFilter); | |
1097 | $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']]; | |
1098 | } | |
1099 | else { | |
1100 | $params['email_greeting'] = $values['email_greeting']; | |
1101 | } | |
1102 | ||
1103 | return TRUE; | |
1104 | } | |
1105 | ||
1106 | if (isset($values['postal_greeting'])) { | |
1107 | if (!empty($params['postal_greeting_id'])) { | |
1108 | $postalGreetingFilter = array( | |
1109 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), | |
1110 | 'greeting_type' => 'postal_greeting', | |
1111 | ); | |
1112 | $postalGreetings = CRM_Core_PseudoConstant::greeting($postalGreetingFilter); | |
1113 | $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']]; | |
1114 | } | |
1115 | else { | |
1116 | $params['postal_greeting'] = $values['postal_greeting']; | |
1117 | } | |
1118 | return TRUE; | |
1119 | } | |
1120 | ||
1121 | if (isset($values['addressee'])) { | |
1122 | if (!empty($params['addressee_id'])) { | |
1123 | $addresseeFilter = array( | |
1124 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), | |
1125 | 'greeting_type' => 'addressee', | |
1126 | ); | |
1127 | $addressee = CRM_Core_PseudoConstant::addressee($addresseeFilter); | |
1128 | $params['addressee'] = $addressee[$params['addressee_id']]; | |
1129 | } | |
1130 | else { | |
1131 | $params['addressee'] = $values['addressee']; | |
1132 | } | |
1133 | return TRUE; | |
1134 | } | |
1135 | ||
1136 | if (isset($values['gender'])) { | |
1137 | if (!empty($params['gender_id'])) { | |
1138 | $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); | |
1139 | $params['gender'] = $genders[$params['gender_id']]; | |
1140 | } | |
1141 | else { | |
1142 | $params['gender'] = $values['gender']; | |
1143 | } | |
1144 | return TRUE; | |
1145 | } | |
1146 | ||
1147 | if (!empty($values['preferred_communication_method'])) { | |
1148 | $comm = array(); | |
1149 | $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER); | |
1150 | ||
1151 | $preffComm = explode(',', $values['preferred_communication_method']); | |
1152 | foreach ($preffComm as $v) { | |
1153 | $v = strtolower(trim($v)); | |
1154 | if (array_key_exists($v, $pcm)) { | |
1155 | $comm[$pcm[$v]] = 1; | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | $params['preferred_communication_method'] = $comm; | |
1160 | return TRUE; | |
1161 | } | |
1162 | ||
1163 | // format the website params. | |
1164 | if (!empty($values['url'])) { | |
1165 | static $websiteFields; | |
1166 | if (!is_array($websiteFields)) { | |
1167 | $websiteFields = CRM_Core_DAO_Website::fields(); | |
1168 | } | |
1169 | if (!array_key_exists('website', $params) || | |
1170 | !is_array($params['website']) | |
1171 | ) { | |
1172 | $params['website'] = array(); | |
1173 | } | |
1174 | ||
1175 | $websiteCount = count($params['website']); | |
1176 | _civicrm_api3_store_values($websiteFields, $values, | |
1177 | $params['website'][++$websiteCount] | |
1178 | ); | |
1179 | ||
1180 | return TRUE; | |
1181 | } | |
1182 | ||
1183 | // get the formatted location blocks into params - w/ 3.0 format, CRM-4605 | |
1184 | if (!empty($values['location_type_id'])) { | |
1185 | $blockTypes = array( | |
1186 | 'phone' => 'Phone', | |
1187 | 'email' => 'Email', | |
1188 | 'im' => 'IM', | |
1189 | 'openid' => 'OpenID', | |
1190 | 'phone_ext' => 'Phone', | |
1191 | ); | |
1192 | foreach ($blockTypes as $blockFieldName => $block) { | |
1193 | if (!array_key_exists($blockFieldName, $values)) { | |
1194 | continue; | |
1195 | } | |
1196 | ||
1197 | // block present in value array. | |
1198 | if (!array_key_exists($blockFieldName, $params) || !is_array($params[$blockFieldName])) { | |
1199 | $params[$blockFieldName] = array(); | |
1200 | } | |
1201 | ||
1202 | if (!array_key_exists($block, $fields)) { | |
1203 | $className = "CRM_Core_DAO_$block"; | |
1204 | $fields[$block] = $className::fields(); | |
1205 | } | |
1206 | ||
1207 | $blockCnt = count($params[$blockFieldName]); | |
1208 | ||
1209 | // copy value to dao field name. | |
1210 | if ($blockFieldName == 'im') { | |
1211 | $values['name'] = $values[$blockFieldName]; | |
1212 | } | |
1213 | ||
1214 | _civicrm_api3_store_values($fields[$block], $values, | |
1215 | $params[$blockFieldName][++$blockCnt] | |
1216 | ); | |
1217 | ||
1218 | if ($values['location_type_id'] === 'Primary') { | |
1219 | if (!empty($params['id'])) { | |
1220 | $primary = civicrm_api3($block, 'get', array('return' => 'location_type_id', 'contact_id' => $params['id'], 'is_primary' => 1, 'sequential' => 1)); | |
1221 | } | |
1222 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
1223 | $values['location_type_id'] = (isset($primary) && $primary['count']) ? $primary['values'][0]['location_type_id'] : $defaultLocationType->id; | |
1224 | $values['is_primary'] = 1; | |
1225 | } | |
1226 | ||
1227 | if (empty($params['id']) && ($blockCnt == 1)) { | |
1228 | $params[$blockFieldName][$blockCnt]['is_primary'] = TRUE; | |
1229 | } | |
1230 | ||
1231 | // we only process single block at a time. | |
1232 | return TRUE; | |
1233 | } | |
1234 | ||
1235 | // handle address fields. | |
1236 | if (!array_key_exists('address', $params) || !is_array($params['address'])) { | |
1237 | $params['address'] = array(); | |
1238 | } | |
1239 | ||
1240 | if (!array_key_exists('Address', $fields)) { | |
1241 | $fields['Address'] = CRM_Core_DAO_Address::fields(); | |
1242 | } | |
1243 | ||
1244 | // Note: we doing multiple value formatting here for address custom fields, plus putting into right format. | |
1245 | // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving | |
1246 | // the address in CRM_Core_BAO_Address::create method | |
1247 | if (!empty($values['location_type_id'])) { | |
1248 | static $customFields = array(); | |
1249 | if (empty($customFields)) { | |
1250 | $customFields = CRM_Core_BAO_CustomField::getFields('Address'); | |
1251 | } | |
1252 | // make a copy of values, as we going to make changes | |
1253 | $newValues = $values; | |
1254 | foreach ($values as $key => $val) { | |
1255 | $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key); | |
1256 | if ($customFieldID && array_key_exists($customFieldID, $customFields)) { | |
1257 | // mark an entry in fields array since we want the value of custom field to be copied | |
1258 | $fields['Address'][$key] = NULL; | |
1259 | ||
1260 | $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]); | |
1261 | switch ($htmlType) { | |
1262 | case 'CheckBox': | |
1263 | case 'AdvMulti-Select': | |
1264 | case 'Multi-Select': | |
1265 | if ($val) { | |
1266 | $mulValues = explode(',', $val); | |
1267 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
1268 | $newValues[$key] = array(); | |
1269 | foreach ($mulValues as $v1) { | |
1270 | foreach ($customOption as $v2) { | |
1271 | if ((strtolower($v2['label']) == strtolower(trim($v1))) || | |
1272 | (strtolower($v2['value']) == strtolower(trim($v1))) | |
1273 | ) { | |
1274 | if ($htmlType == 'CheckBox') { | |
1275 | $newValues[$key][$v2['value']] = 1; | |
1276 | } | |
1277 | else { | |
1278 | $newValues[$key][] = $v2['value']; | |
1279 | } | |
1280 | } | |
1281 | } | |
1282 | } | |
1283 | } | |
1284 | break; | |
1285 | } | |
1286 | } | |
1287 | } | |
1288 | // consider new values | |
1289 | $values = $newValues; | |
1290 | } | |
1291 | ||
1292 | _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$values['location_type_id']]); | |
1293 | ||
1294 | $addressFields = array( | |
1295 | 'county', | |
1296 | 'country', | |
1297 | 'state_province', | |
1298 | 'supplemental_address_1', | |
1299 | 'supplemental_address_2', | |
1300 | 'supplemental_address_3', | |
1301 | 'StateProvince.name', | |
1302 | ); | |
1303 | ||
1304 | foreach ($addressFields as $field) { | |
1305 | if (array_key_exists($field, $values)) { | |
1306 | if (!array_key_exists('address', $params)) { | |
1307 | $params['address'] = array(); | |
1308 | } | |
1309 | $params['address'][$values['location_type_id']][$field] = $values[$field]; | |
1310 | } | |
1311 | } | |
1312 | ||
1313 | if ($values['location_type_id'] === 'Primary') { | |
1314 | if (!empty($params['id'])) { | |
1315 | $primary = civicrm_api3('Address', 'get', array('return' => 'location_type_id', 'contact_id' => $params['id'], 'is_primary' => 1, 'sequential' => 1)); | |
1316 | } | |
1317 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
1318 | $params['address'][$values['location_type_id']]['location_type_id'] = (isset($primary) && $primary['count']) ? $primary['values'][0]['location_type_id'] : $defaultLocationType->id; | |
1319 | $params['address'][$values['location_type_id']]['is_primary'] = 1; | |
1320 | ||
1321 | } | |
1322 | return TRUE; | |
1323 | } | |
1324 | ||
1325 | if (isset($values['note'])) { | |
1326 | // add a note field | |
1327 | if (!isset($params['note'])) { | |
1328 | $params['note'] = array(); | |
1329 | } | |
1330 | $noteBlock = count($params['note']) + 1; | |
1331 | ||
1332 | $params['note'][$noteBlock] = array(); | |
1333 | if (!isset($fields['Note'])) { | |
1334 | $fields['Note'] = CRM_Core_DAO_Note::fields(); | |
1335 | } | |
1336 | ||
1337 | // get the current logged in civicrm user | |
1338 | $session = CRM_Core_Session::singleton(); | |
1339 | $userID = $session->get('userID'); | |
1340 | ||
1341 | if ($userID) { | |
1342 | $values['contact_id'] = $userID; | |
1343 | } | |
1344 | ||
1345 | _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]); | |
1346 | ||
1347 | return TRUE; | |
1348 | } | |
1349 | ||
1350 | // Check for custom field values | |
1351 | ||
1352 | if (empty($fields['custom'])) { | |
1353 | $fields['custom'] = &CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values), | |
1354 | FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE | |
1355 | ); | |
1356 | } | |
1357 | ||
1358 | foreach ($values as $key => $value) { | |
1359 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
1360 | // check if it's a valid custom field id | |
1361 | ||
1362 | if (!array_key_exists($customFieldID, $fields['custom'])) { | |
1363 | return civicrm_api3_create_error('Invalid custom field ID'); | |
1364 | } | |
1365 | else { | |
1366 | $params[$key] = $value; | |
1367 | } | |
1368 | } | |
1369 | } | |
1370 | return TRUE; | |
1371 | } | |
1372 | ||
6a488035 | 1373 | } |