3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Implement the "match" and "match-mandatory" options. If the submitted record doesn't have an ID
14 * but a "match" key is specified, then we will automatically search for pre-existing record and
15 * fill-in the missing ID. The "match" or "match-mandatory" can specified as a string (the name of the key
16 * to match on) or array (the names of several keys to match on).
18 * Note that "match" and "match-mandatory" behave the same in the case where one matching record
19 * exists (ie they update the record). They also behave the same if there are multiple matching
20 * records (ie they throw an error). However, if there is no matching record, they differ:
21 * - "match-mandatory" will generate an error
22 * - "match" will allow action to proceed -- thus inserting a new record
25 * $result = civicrm_api('contact', 'create', array(
27 * 'match' => array('last_name', 'first_name')
29 * 'first_name' => 'Jeffrey',
30 * 'last_name' => 'Lebowski',
31 * 'nick_name' => 'The Dude',
36 * @copyright CiviCRM LLC https://civicrm.org/licensing
39 require_once 'api/Wrapper.php';
42 * Class CRM_Utils_API_MatchOption
44 class CRM_Utils_API_MatchOption
implements API_Wrapper
{
47 * @var CRM_Utils_API_MatchOption
49 private static $_singleton = NULL;
54 * @return CRM_Utils_API_MatchOption
56 public static function singleton() {
57 if (self
::$_singleton === NULL) {
58 self
::$_singleton = new CRM_Utils_API_MatchOption();
60 return self
::$_singleton;
66 public function fromApiInput($apiRequest) {
68 // Parse options.match or options.match-mandatory
70 if (isset($apiRequest['params'], $apiRequest['params']['options']) && is_array($apiRequest['params']['options'])) {
71 if (isset($apiRequest['params']['options']['match-mandatory'])) {
73 $keys = $apiRequest['params']['options']['match-mandatory'];
75 elseif (isset($apiRequest['params']['options']['match'])) {
77 $keys = $apiRequest['params']['options']['match'];
79 if (is_string($keys)) {
84 // If one of the options was specified, then try to match records.
85 // Matching logic differs for 'create' and 'replace' actions.
87 switch ($apiRequest['action']) {
89 if (empty($apiRequest['params']['id'])) {
90 $apiRequest['params'] = $this->match($apiRequest['entity'], $apiRequest['params'], $keys, $isMandatory);
95 // In addition to matching on the listed keys, also match on the set-definition keys.
96 // For example, if the $apiRequest is to "replace the set of civicrm_emails for contact_id=123 while
97 // matching emails on location_type_id", then we would need to search for pre-existing emails using
98 // both 'contact_id' and 'location_type_id'
99 $baseParams = _civicrm_api3_generic_replace_base_params($apiRequest['params']);
100 $keys = array_unique(array_merge(
101 array_keys($baseParams),
105 // attempt to match each replacement item
106 foreach ($apiRequest['params']['values'] as $offset => $createParams) {
107 $createParams = array_merge($baseParams, $createParams);
108 $createParams = $this->match($apiRequest['entity'], $createParams, $keys, $isMandatory);
109 $apiRequest['params']['values'][$offset] = $createParams;
114 // be forgiving of sloppy api calls
122 * Attempt to match a contact. This filters/updates the $createParams if there is a match.
124 * @param string $entity
125 * @param array $createParams
127 * @param bool $isMandatory
130 * revised $createParams, including 'id' if known
131 * @throws API_Exception
133 public function match($entity, $createParams, $keys, $isMandatory) {
134 $getParams = $this->createGetParams($createParams, $keys);
135 $getResult = civicrm_api3($entity, 'get', $getParams);
136 if ($getResult['count'] == 0) {
138 throw new API_Exception("Failed to match existing record");
141 return $createParams;
143 elseif ($getResult['count'] == 1) {
144 $item = array_shift($getResult['values']);
145 $createParams['id'] = $item['id'];
146 return $createParams;
149 throw new API_Exception("Ambiguous match criteria");
156 public function toApiOutput($apiRequest, $result) {
161 * Create APIv3 "get" parameters to lookup an existing record using $keys
163 * @param array $origParams
166 * List of keys to match against.
171 public function createGetParams($origParams, $keys) {
172 $params = ['version' => 3];
173 foreach ($keys as $key) {
174 $params[$key] = CRM_Utils_Array
::value($key, $origParams, '');