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