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