Merge pull request #227 from deepak-srivastava/crm
[civicrm-core.git] / api / v2 / Contact.php
1 <?php
2 // $Id: Contact.php 45502 2013-02-08 13:32:55Z kurund $
3
4
5 /*
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
12 | |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
21 | |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
29 */
30
31 /**
32 * new version of civicrm apis. See blog post at
33 * http://civicrm.org/node/131
34 * @todo Write sth
35 *
36 * @package CiviCRM_APIv2
37 * @subpackage API_Contact
38 * @copyright CiviCRM LLC (c) 2004-2013
39 * $Id: Contact.php 45502 2013-02-08 13:32:55Z kurund $
40 *
41 */
42
43 /**
44 * Include common API util functions
45 */
46 require_once 'api/v2/utils.php';
47 require_once 'CRM/Contact/BAO/Contact.php';
48
49 /**
50 * @todo Write sth
51 *
52 * @param array $params (reference ) input parameters
53 *
54 * Allowed @params array keys are:
55 * {@schema Contact/Contact.xml}
56 * {@schema Core/Address.xml}}
57 *
58 * @return array (reference ) contact_id of created or updated contact
59 *
60 * @static void
61 * @access public
62 */
63 function civicrm_contact_create(&$params) {
64 // call update and tell it to create a new contact
65 _civicrm_initialize();
66 $errorScope = CRM_Core_TemporaryErrorScope::useException();
67 try {
68 civicrm_api_check_permission(__FUNCTION__, $params, TRUE);
69 $create_new = TRUE;
70 return civicrm_contact_update($params, $create_new);
71 }
72 catch(Exception$e) {
73 return civicrm_create_error($e->getMessage());
74 }
75 }
76
77 /**
78 * @todo Write sth
79 * @todo Serious FIXMES in the code! File issues.
80 */
81 function civicrm_contact_update(&$params, $create_new = FALSE) {
82 _civicrm_initialize();
83 try {
84 civicrm_api_check_permission(__FUNCTION__, $params, TRUE);
85 }
86 catch(Exception$e) {
87 return civicrm_create_error($e->getMessage());
88 }
89 require_once 'CRM/Utils/Array.php';
90 $entityId = CRM_Utils_Array::value('contact_id', $params, NULL);
91 if (!CRM_Utils_Array::value('contact_type', $params) &&
92 $entityId
93 ) {
94 $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($entityId);
95 }
96 $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE);
97 $values = civicrm_contact_check_params($params, $dupeCheck);
98 if ($values) {
99 return $values;
100 }
101
102 if ($create_new) {
103 // Make sure nothing is screwed up before we create a new contact
104 if (!empty($entityId)) {
105 return civicrm_create_error('Cannot create new contact when contact_id is present');
106 }
107 if (empty($params['contact_type'])) {
108 return civicrm_create_error('Contact Type not specified');
109 }
110
111 // If we get here, we're ready to create a new contact
112 if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) {
113 require_once 'CRM/Core/BAO/LocationType.php';
114 $defLocType = CRM_Core_BAO_LocationType::getDefault();
115 $params['email'] = array(
116 1 => array('email' => $email,
117 'is_primary' => 1,
118 'location_type_id' => ($defLocType->id) ? $defLocType->id : 1,
119 ),
120 );
121 }
122 }
123
124 if ($homeUrl = CRM_Utils_Array::value('home_url', $params)) {
125 require_once 'CRM/Core/PseudoConstant.php';
126 $websiteTypes = CRM_Core_PseudoConstant::websiteType();
127 $params['website'] = array(1 => array('website_type_id' => key($websiteTypes),
128 'url' => $homeUrl,
129 ),
130 );
131 }
132 // FIXME: Some legacy support cruft, should get rid of this in 3.1
133 $change = array(
134 'individual_prefix' => 'prefix',
135 'prefix' => 'prefix_id',
136 'individual_suffix' => 'suffix',
137 'suffix' => 'suffix_id',
138 'gender' => 'gender_id',
139 );
140
141 foreach ($change as $field => $changeAs) {
142 if (array_key_exists($field, $params)) {
143 $params[$changeAs] = $params[$field];
144 unset($params[$field]);
145 }
146 }
147 // End legacy support cruft
148
149 if (isset($params['suffix_id']) &&
150 !(is_numeric($params['suffix_id']))
151 ) {
152 $params['suffix_id'] = array_search($params['suffix_id'], CRM_Core_PseudoConstant::individualSuffix());
153 }
154
155 if (isset($params['prefix_id']) &&
156 !(is_numeric($params['prefix_id']))
157 ) {
158 $params['prefix_id'] = array_search($params['prefix_id'], CRM_Core_PseudoConstant::individualPrefix());
159 }
160
161 if (isset($params['gender_id'])
162 && !(is_numeric($params['gender_id']))
163 ) {
164 $params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::gender());
165 }
166
167 $error = _civicrm_greeting_format_params($params);
168 if (civicrm_error($error)) {
169 return $error;
170 }
171
172 $values = array();
173
174 if (!($csType = CRM_Utils_Array::value('contact_sub_type', $params)) &&
175 $entityId
176 ) {
177 require_once 'CRM/Contact/BAO/Contact.php';
178 $csType = CRM_Contact_BAO_Contact::getContactSubType($entityId);
179 }
180
181 $customValue = civicrm_contact_check_custom_params($params, $csType);
182
183 if ($customValue) {
184 return $customValue;
185 }
186 _civicrm_custom_format_params($params, $values, $params['contact_type'], $entityId);
187
188 $params = array_merge($params, $values);
189
190 $contact = &_civicrm_contact_update($params, $entityId);
191
192 if (is_a($contact, 'CRM_Core_Error')) {
193 return civicrm_create_error($contact->_errors[0]['message']);
194 }
195 else {
196 $values = array();
197 $values['contact_id'] = $contact->id;
198 $values['is_error'] = 0;
199 }
200
201 return $values;
202 }
203
204 /**
205 * Add or update a contact. If a dupe is found, check for
206 * ignoreDupe flag to ignore or return error
207 *
208 * @deprecated deprecated since version 2.2.3; use civicrm_contact_create or civicrm_contact_update instead
209 *
210 * @param array $params (reference ) input parameters
211 *
212 * @return array (reference ) contact_id of created or updated contact
213 * @static void
214 * @access public
215 */
216 function &civicrm_contact_add(&$params) {
217 _civicrm_initialize();
218
219 $contactID = CRM_Utils_Array::value('contact_id', $params);
220
221 if (!empty($contactID)) {
222 $result = civicrm_contact_update($params);
223 }
224 else {
225 $result = civicrm_contact_create($params);
226 }
227 return $result;
228 }
229
230 /**
231 * Validate the addressee or email or postal greetings
232 *
233 * @param $params Associative array of property name/value
234 * pairs to insert in new contact.
235 *
236 * @return array (reference ) null on success, error message otherwise
237 *
238 * @access public
239 */
240 function _civicrm_greeting_format_params(&$params) {
241 $greetingParams = array('', '_id', '_custom');
242 foreach (array(
243 'email', 'postal', 'addressee') as $key) {
244 $greeting = '_greeting';
245 if ($key == 'addressee') {
246 $greeting = '';
247 }
248
249 $formatParams = FALSE;
250 // unset display value from params.
251 if (isset($params["{$key}{$greeting}_display"])) {
252 unset($params["{$key}{$greeting}_display"]);
253 }
254
255 // check if greetings are present in present
256 foreach ($greetingParams as $greetingValues) {
257 if (array_key_exists("{$key}{$greeting}{$greetingValues}", $params)) {
258 $formatParams = TRUE;
259 break;
260 }
261 }
262
263 if (!$formatParams) {
264
265 continue;
266 }
267
268 // format params
269 if (CRM_Utils_Array::value('contact_type', $params) == 'Organization' && $key != 'addressee') {
270 return civicrm_create_error(ts('You cannot use email/postal greetings for contact type %1.',
271 array(1 => $params['contact_type'])
272 ));
273 }
274
275 $nullValue = FALSE;
276 $filter = array(
277 'contact_type' => $params['contact_type'],
278 'greeting_type' => "{$key}{$greeting}",
279 );
280
281 $greetings = CRM_Core_PseudoConstant::greeting($filter);
282 $greetingId = CRM_Utils_Array::value("{$key}{$greeting}_id", $params);
283 $greetingVal = CRM_Utils_Array::value("{$key}{$greeting}", $params);
284 $customGreeting = CRM_Utils_Array::value("{$key}{$greeting}_custom", $params);
285
286 if (!$greetingId && $greetingVal) {
287 $params["{$key}{$greeting}_id"] = CRM_Utils_Array::key($params["{$key}{$greeting}"], $greetings);
288 }
289
290 if ($customGreeting && $greetingId &&
291 ($greetingId != array_search('Customized', $greetings))
292 ) {
293 return civicrm_create_error(ts('Provide either %1 greeting id and/or %1 greeting or custom %1 greeting',
294 array(1 => $key)
295 ));
296 }
297
298 if ($greetingVal && $greetingId &&
299 ($greetingId != CRM_Utils_Array::key($greetingVal, $greetings))
300 ) {
301 return civicrm_create_error(ts('Mismatch in %1 greeting id and %1 greeting',
302 array(1 => $key)
303 ));
304 }
305
306 if ($greetingId) {
307
308 if (!array_key_exists($greetingId, $greetings)) {
309 return civicrm_create_error(ts('Invalid %1 greeting Id', array(1 => $key)));
310 }
311
312 if (!$customGreeting && ($greetingId == array_search('Customized', $greetings))) {
313 return civicrm_create_error(ts('Please provide a custom value for %1 greeting',
314 array(1 => $key)
315 ));
316 }
317 }
318 elseif ($greetingVal) {
319
320 if (!in_array($greetingVal, $greetings)) {
321 return civicrm_create_error(ts('Invalid %1 greeting', array(1 => $key)));
322 }
323
324 $greetingId = CRM_Utils_Array::key($greetingVal, $greetings);
325 }
326
327 if ($customGreeting) {
328 $greetingId = CRM_Utils_Array::key('Customized', $greetings);
329 }
330
331 $customValue = $params['contact_id'] ? CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
332 $params['contact_id'],
333 "{$key}{$greeting}_custom"
334 ) : FALSE;
335
336 if (array_key_exists("{$key}{$greeting}_id", $params) && empty($params["{$key}{$greeting}_id"])) {
337 $nullValue = TRUE;
338 }
339 elseif (array_key_exists("{$key}{$greeting}", $params) && empty($params["{$key}{$greeting}"])) {
340 $nullValue = TRUE;
341 }
342 elseif ($customValue && array_key_exists("{$key}{$greeting}_custom", $params)
343 && empty($params["{$key}{$greeting}_custom"])
344 ) {
345 $nullValue = TRUE;
346 }
347
348 $params["{$key}{$greeting}_id"] = $greetingId;
349
350 if (!$customValue && !$customGreeting && array_key_exists("{$key}{$greeting}_custom", $params)) {
351 unset($params["{$key}{$greeting}_custom"]);
352 }
353
354 if ($nullValue) {
355 $params["{$key}{$greeting}_id"] = '';
356 $params["{$key}{$greeting}_custom"] = '';
357 }
358
359 if (isset($params["{$key}{$greeting}"])) {
360 unset($params["{$key}{$greeting}"]);
361 }
362 }
363 }
364
365 /**
366 * Retrieve one or more contacts, given a set of search params
367 *
368 * @param mixed[] (reference ) input parameters
369 * @param bool follow the pre-2.2.3 behavior of this function
370 *
371 * @return array (reference ) array of properties, if error an array with an error id and error message
372 * @static void
373 * @access public
374 */
375 function civicrm_contact_get(&$params, $deprecated_behavior = FALSE) {
376 _civicrm_initialize();
377
378 if ($deprecated_behavior) {
379 return _civicrm_contact_get_deprecated($params);
380 }
381
382 // fix for CRM-7384 cater for soft deleted contacts
383 $params['contact_is_deleted'] = 0;
384 if (isset($params['showAll'])) {
385 if (strtolower($params['showAll']) == "active") {
386 $params['contact_is_deleted'] = 0;
387 }
388 if (strtolower($params['showAll']) == "trash") {
389 $params['contact_is_deleted'] = 1;
390 }
391 if (strtolower($params['showAll']) == "all" && isset($params['contact_is_deleted'])) {
392 unset($params['contact_is_deleted']);
393 }
394 }
395
396 $inputParams = array();
397 $returnProperties = array();
398 $otherVars = array('sort', 'offset', 'rowCount', 'smartGroupCache');
399
400 $sort = NULL;
401 $offset = 0;
402 $rowCount = 25;
403 $smartGroupCache = FALSE;
404 foreach ($params as $n => $v) {
405 if (substr($n, 0, 6) == 'return') {
406 $returnProperties[substr($n, 7)] = $v;
407 }
408 elseif (in_array($n, $otherVars)) {
409 $$n = $v;
410 }
411 else {
412 $inputParams[$n] = $v;
413 }
414 }
415
416 if (empty($returnProperties)) {
417 $returnProperties = NULL;
418 }
419
420 require_once 'CRM/Contact/BAO/Query.php';
421 $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams);
422 list($contacts, $options) = CRM_Contact_BAO_Query::apiQuery($newParams,
423 $returnProperties,
424 NULL,
425 $sort,
426 $offset,
427 $rowCount,
428 $smartGroupCache
429 );
430 return $contacts;
431 }
432
433 /**
434 * Retrieve a specific contact, given a set of search params
435 *
436 * @deprecated deprecated since version 2.2.3
437 *
438 * @param array $params (reference ) input parameters
439 *
440 * @return array (reference ) array of properties, if error an array with an error id and error message
441 * @static void
442 * @access public
443 */
444 function _civicrm_contact_get_deprecated(&$params) {
445 $values = array();
446 if (empty($params)) {
447 return civicrm_create_error(ts('No input parameters present'));
448 }
449
450 if (!is_array($params)) {
451 return civicrm_create_error(ts('Input parameters is not an array'));
452 }
453
454 $contacts = &civicrm_contact_search($params);
455 if (civicrm_error($contacts)) {
456 return $contacts;
457 }
458
459 if (count($contacts) != 1 &&
460 !CRM_Utils_Array::value('returnFirst', $params)
461 ) {
462 return civicrm_create_error(ts('%1 contacts matching input params', array(1 => count($contacts))));
463 }
464 elseif (count($contacts) == 0) {
465 return civicrm_create_error(ts('No contacts match the input params'));
466 }
467
468 $contacts = array_values($contacts);
469 return $contacts[0];
470 }
471
472 /**
473 * Delete a contact with given contact id
474 *
475 * @param array $params (reference ) input parameters, contact_id element required
476 *
477 * @return boolean true if success, else false
478 * @static void
479 * @access public
480 */
481 function civicrm_contact_delete(&$params) {
482 require_once 'CRM/Contact/BAO/Contact.php';
483
484 $contactID = CRM_Utils_Array::value('contact_id', $params);
485 if (!$contactID) {
486 return civicrm_create_error(ts('Could not find contact_id in input parameters'));
487 }
488
489 $session = CRM_Core_Session::singleton();
490 if ($contactID == $session->get('userID')) {
491 return civicrm_create_error(ts('This contact record is linked to the currently logged in user account - and cannot be deleted.'));
492 }
493 $restore = CRM_Utils_Array::value('restore', $params) ? $params['restore'] : FALSE;
494 $skipUndelete = CRM_Utils_Array::value('skip_undelete', $params) ? $params['skip_undelete'] : FALSE;
495 if (CRM_Contact_BAO_Contact::deleteContact($contactID, $restore, $skipUndelete)) {
496 return civicrm_create_success();
497 }
498 else {
499 return civicrm_create_error(ts('Could not delete contact'));
500 }
501 }
502
503 /**
504 * Retrieve a set of contacts, given a set of input params
505 *
506 * @deprecated deprecated since version 2.2.3
507 *
508 * @param array $params (reference ) input parameters
509 * @param array $returnProperties Which properties should be included in the
510 * returned Contact object. If NULL, the default
511 * set of properties will be included.
512 *
513 * @return array (reference ) array of contacts, if error an array with an error id and error message
514 * @static void
515 * @access public
516 */
517 function &civicrm_contact_search(&$params) {
518 _civicrm_initialize();
519
520 $inputParams = $returnProperties = array();
521 $otherVars = array('sort', 'offset', 'rowCount', 'smartGroupCache');
522
523 $sort = NULL;
524 $offset = 0;
525 $rowCount = 25;
526 $smartGroupCache = FALSE;
527 foreach ($params as $n => $v) {
528 if (substr($n, 0, 6) == 'return') {
529 $returnProperties[substr($n, 7)] = $v;
530 }
531 elseif (in_array($n, $otherVars)) {
532 $$n = $v;
533 }
534 else {
535 $inputParams[$n] = $v;
536 }
537 }
538
539 // explicitly suppress all deleted contacts
540 // this is fixed in api v3
541 // CRM-8809
542 $inputParams['contact_is_deleted'] = 0;
543
544 if (empty($returnProperties)) {
545 $returnProperties = NULL;
546 }
547
548 require_once 'CRM/Contact/BAO/Query.php';
549 $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams);
550 list($contacts, $options) = CRM_Contact_BAO_Query::apiQuery($newParams,
551 $returnProperties,
552 NULL,
553 $sort,
554 $offset,
555 $rowCount,
556 $smartGroupCache
557 );
558 return $contacts;
559 }
560
561 /**
562 * Ensure that we have the right input parameters
563 *
564 * @todo We also need to make sure we run all the form rules on the params list
565 * to ensure that the params are valid
566 *
567 * @param array $params Associative array of property name/value
568 * pairs to insert in new contact.
569 * @param boolean $dupeCheck Should we check for duplicate contacts
570 * @param boolean $dupeErrorArray Should we return values of error
571 * object in array foramt
572 * @param boolean $requiredCheck Should we check if required params
573 * are present in params array
574 * @param int $dedupeRuleGroupID - the dedupe rule ID to use if present
575 *
576 * @return null on success, error message otherwise
577 * @access public
578 */
579 function civicrm_contact_check_params(&$params,
580 $dupeCheck = TRUE,
581 $dupeErrorArray = FALSE,
582 $requiredCheck = TRUE,
583 $dedupeRuleGroupID = NULL
584 ) {
585 if ($requiredCheck) {
586 $required = array(
587 'Individual' => array(
588 array('first_name', 'last_name'),
589 'email',
590 ),
591 'Household' => array(
592 'household_name',
593 ),
594 'Organization' => array(
595 'organization_name',
596 ),
597 );
598
599 // cannot create a contact with empty params
600 if (empty($params)) {
601 return civicrm_create_error('Input Parameters empty');
602 }
603
604 if (!array_key_exists('contact_type', $params)) {
605 return civicrm_create_error('Contact Type not specified');
606 }
607
608 // contact_type has a limited number of valid values
609 $fields = CRM_Utils_Array::value($params['contact_type'], $required);
610 if ($fields == NULL) {
611 return civicrm_create_error("Invalid Contact Type: {$params['contact_type']}");
612 }
613
614 if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) {
615 if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) {
616 return civicrm_create_error("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$csType));
617 }
618 }
619
620 if (!CRM_Utils_Array::value('contact_id', $params)) {
621 $valid = FALSE;
622 $error = '';
623 foreach ($fields as $field) {
624 if (is_array($field)) {
625 $valid = TRUE;
626 foreach ($field as $element) {
627 if (!CRM_Utils_Array::value($element, $params)) {
628 $valid = FALSE;
629 $error .= $element;
630 break;
631 }
632 }
633 }
634 else {
635 if (CRM_Utils_Array::value($field, $params)) {
636 $valid = TRUE;
637 }
638 }
639 if ($valid) {
640 break;
641 }
642 }
643
644 if (!$valid) {
645 return civicrm_create_error("Required fields not found for {$params['contact_type']} : $error");
646 }
647 }
648 }
649
650 if ($dupeCheck) {
651 // check for record already existing
652 require_once 'CRM/Dedupe/Finder.php';
653 $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']);
654
655 // CRM-6431
656 // setting 'check_permission' here means that the dedupe checking will be carried out even if the
657 // person does not have permission to carry out de-dupes
658 // this is similar to the front end form
659 if (isset($params['check_permission'])) {
660 $dedupeParams['check_permission'] = $fields['check_permission'];
661 }
662
663 $ids = implode(',',
664 CRM_Dedupe_Finder::dupesByParams($dedupeParams,
665 $params['contact_type'],
666 'Strict',
667 array(),
668 $dedupeRuleGroupID
669 )
670 );
671
672 if ($ids != NULL) {
673 if ($dupeErrorArray) {
674 $error = CRM_Core_Error::createError("Found matching contacts: $ids",
675 CRM_Core_Error::DUPLICATE_CONTACT,
676 'Fatal', $ids
677 );
678 return civicrm_create_error($error->pop());
679 }
680
681 return civicrm_create_error("Found matching contacts: $ids", array($ids));
682 }
683 }
684
685 //check for organisations with same name
686 if (CRM_Utils_Array::value('current_employer', $params)) {
687 $organizationParams = array();
688 $organizationParams['organization_name'] = $params['current_employer'];
689
690 require_once 'CRM/Dedupe/Finder.php';
691 $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization');
692
693 $dedupParams['check_permission'] = FALSE;
694 $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Fuzzy');
695
696 // check for mismatch employer name and id
697 if (CRM_Utils_Array::value('employer_id', $params)
698 && !in_array($params['employer_id'], $dupeIds)
699 ) {
700 return civicrm_create_error('Employer name and Employer id Mismatch');
701 }
702
703 // show error if multiple organisation with same name exist
704 if (!CRM_Utils_Array::value('employer_id', $params)
705 && (count($dupeIds) > 1)
706 ) {
707 return civicrm_create_error('Found more than one Organisation with same Name.');
708 }
709 }
710
711 return NULL;
712 }
713
714 /**
715 * @todo What does this do? If it's still useful, figure out where it should live and what it should be named.
716 *
717 * @deprecated deprecated since version 2.2.3
718 */
719 function civicrm_replace_contact_formatted($contactId, &$params, &$fields) {
720 //$contact = civcrm_get_contact(array('contact_id' => $contactId));
721
722 $delContact = array('contact_id' => $contactId);
723
724 civicrm_contact_delete($delContact);
725
726 $cid = CRM_Contact_BAO_Contact::createProfileContact($params, $fields,
727 NULL, NULL, NULL,
728 $params['contact_type']
729 );
730 return civicrm_create_success($cid);
731 }
732
733 /**
734 * Takes an associative array and creates a contact object and all the associated
735 * derived objects (i.e. individual, location, email, phone etc)
736 *
737 * @param array $params (reference ) an assoc array of name/value pairs
738 * @param int $contactID if present the contact with that ID is updated
739 *
740 * @return object CRM_Contact_BAO_Contact object
741 * @access public
742 * @static
743 */
744 function _civicrm_contact_update(&$params, $contactID = NULL) {
745 require_once 'CRM/Core/Transaction.php';
746 $transaction = new CRM_Core_Transaction();
747
748 if ($contactID) {
749 $params['contact_id'] = $contactID;
750 }
751 require_once 'CRM/Contact/BAO/Contact.php';
752
753 $contact = CRM_Contact_BAO_Contact::create($params);
754
755 $transaction->commit();
756
757 return $contact;
758 }
759
760 /**
761 * @todo Move this to ContactFormat.php
762 * @deprecated
763 */
764 function civicrm_contact_format_create(&$params) {
765 _civicrm_initialize();
766
767 CRM_Core_DAO::freeResult();
768
769 // return error if we have no params
770 if (empty($params)) {
771 return civicrm_create_error('Input Parameters empty');
772 }
773
774 $error = _civicrm_required_formatted_contact($params);
775 if (civicrm_error($error)) {
776 return $error;
777 }
778
779 $error = _civicrm_validate_formatted_contact($params);
780 if (civicrm_error($error)) {
781 return $error;
782 }
783
784 //get the prefix id etc if exists
785 require_once 'CRM/Contact/BAO/Contact.php';
786 CRM_Contact_BAO_Contact::resolveDefaults($params, TRUE);
787
788 require_once 'CRM/Import/Parser.php';
789 if (CRM_Utils_Array::value('onDuplicate', $params) != CRM_Import_Parser::DUPLICATE_NOCHECK) {
790 CRM_Core_Error::reset();
791 $error = _civicrm_duplicate_formatted_contact($params);
792 if (civicrm_error($error)) {
793 return $error;
794 }
795 }
796
797 $contact = CRM_Contact_BAO_Contact::create($params,
798 CRM_Utils_Array::value('fixAddress', $params)
799 );
800
801 _civicrm_object_to_array($contact, $contactArray);
802 return $contactArray;
803 }
804
805 /**
806 * Returns the number of Contact objects which match the search criteria specified in $params.
807 *
808 * @deprecated deprecated since version 2.2.3; civicrm_contact_get now returns a record_count value
809 *
810 * @param array $params
811 *
812 * @return int
813 * @access public
814 */
815 function civicrm_contact_search_count(&$params) {
816 // convert the params to new format
817 require_once 'CRM/Contact/Form/Search.php';
818 $newP = CRM_Contact_BAO_Query::convertFormValues($params);
819 $query = new CRM_Contact_BAO_Query($newP);
820 return $query->searchQuery(0, 0, NULL, TRUE);
821 }
822
823 /**
824 * Ensure that we have the right input parameters for custom data
825 *
826 * @param array $params Associative array of property name/value
827 * pairs to insert in new contact.
828 * @param string $csType contact subtype if exists/passed.
829 *
830 * @return null on success, error message otherwise
831 * @access public
832 */
833 function civicrm_contact_check_custom_params($params, $csType = NULL) {
834 empty($csType) ? $onlyParent = TRUE : $onlyParent = FALSE;
835
836 require_once 'CRM/Core/BAO/CustomField.php';
837 $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $csType, NULL, $onlyParent);
838
839 foreach ($params as $key => $value) {
840 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
841 /* check if it's a valid custom field id */
842
843
844 if (!array_key_exists($customFieldID, $customFields)) {
845
846 $errorMsg = ts("Invalid Custom Field Contact Type: {$params['contact_type']}");
847 if ($csType) {
848 $errorMsg .= ts(" or Mismatched SubType: " . implode(', ', (array)$csType));
849 }
850 return civicrm_create_error($errorMsg);
851 }
852 }
853 }
854 }
855