| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Implement the "match" and "match-mandatory" options. If the submitted record doesn't have an ID |
| 30 | * but a "match" key is specified, then we will automatically search for pre-existing record and |
| 31 | * fill-in the missing ID. The "match" or "match-mandatory" can specified as a string (the name of the key |
| 32 | * to match on) or array (the names of several keys to match on). |
| 33 | * |
| 34 | * Note that "match" and "match-mandatory" behave the same in the case where one matching record |
| 35 | * exists (ie they update the record). They also behave the same if there are multiple matching |
| 36 | * records (ie they throw an error). However, if there is no matching record, they differ: |
| 37 | * - "match-mandatory" will generate an error |
| 38 | * - "match" will allow action to proceed -- thus inserting a new record |
| 39 | * |
| 40 | * @code |
| 41 | * $result = civicrm_api('contact', 'create', array( |
| 42 | * 'options' => array( |
| 43 | * 'match' => array('last_name', 'first_name') |
| 44 | * ), |
| 45 | * 'first_name' => 'Jeffrey', |
| 46 | * 'last_name' => 'Lebowski', |
| 47 | * 'nick_name' => 'The Dude', |
| 48 | * )); |
| 49 | * @endcode |
| 50 | * |
| 51 | * @package CRM |
| 52 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 53 | * $Id$ |
| 54 | */ |
| 55 | |
| 56 | require_once 'api/Wrapper.php'; |
| 57 | |
| 58 | /** |
| 59 | * Class CRM_Utils_API_MatchOption |
| 60 | */ |
| 61 | class CRM_Utils_API_MatchOption implements API_Wrapper { |
| 62 | |
| 63 | /** |
| 64 | * @var CRM_Utils_API_MatchOption |
| 65 | */ |
| 66 | private static $_singleton = NULL; |
| 67 | |
| 68 | /** |
| 69 | * @return CRM_Utils_API_MatchOption |
| 70 | */ |
| 71 | public static function singleton() { |
| 72 | if (self::$_singleton === NULL) { |
| 73 | self::$_singleton = new CRM_Utils_API_MatchOption(); |
| 74 | } |
| 75 | return self::$_singleton; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * {@inheritDoc} |
| 80 | */ |
| 81 | public function fromApiInput($apiRequest) { |
| 82 | // Parse options.match or options.match-mandatory |
| 83 | $keys = NULL; // array of fields to match against |
| 84 | if (isset($apiRequest['params'], $apiRequest['params']['options'])) { |
| 85 | if (isset($apiRequest['params']['options']['match-mandatory'])) { |
| 86 | $isMandatory = TRUE; |
| 87 | $keys = $apiRequest['params']['options']['match-mandatory']; |
| 88 | } |
| 89 | elseif (isset($apiRequest['params']['options']['match'])) { |
| 90 | $isMandatory = FALSE; |
| 91 | $keys = $apiRequest['params']['options']['match']; |
| 92 | } |
| 93 | if (is_string($keys)) { |
| 94 | $keys = array($keys); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // If one of the options was specified, then try to match records. |
| 99 | // Matching logic differs for 'create' and 'replace' actions. |
| 100 | if ($keys !== NULL) { |
| 101 | switch ($apiRequest['action']) { |
| 102 | case 'create': |
| 103 | if (empty($apiRequest['params']['id'])) { |
| 104 | $apiRequest['params'] = $this->match($apiRequest['entity'], $apiRequest['params'], $keys, $isMandatory); |
| 105 | } |
| 106 | break; |
| 107 | |
| 108 | case 'replace': |
| 109 | // In addition to matching on the listed keys, also match on the set-definition keys. |
| 110 | // For example, if the $apiRequest is to "replace the set of civicrm_emails for contact_id=123 while |
| 111 | // matching emails on location_type_id", then we would need to search for pre-existing emails using |
| 112 | // both 'contact_id' and 'location_type_id' |
| 113 | $baseParams = _civicrm_api3_generic_replace_base_params($apiRequest['params']); |
| 114 | $keys = array_unique(array_merge( |
| 115 | array_keys($baseParams), |
| 116 | $keys |
| 117 | )); |
| 118 | |
| 119 | // attempt to match each replacement item |
| 120 | foreach ($apiRequest['params']['values'] as $offset => $createParams) { |
| 121 | $createParams = array_merge($baseParams, $createParams); |
| 122 | $createParams = $this->match($apiRequest['entity'], $createParams, $keys, $isMandatory); |
| 123 | $apiRequest['params']['values'][$offset] = $createParams; |
| 124 | } |
| 125 | break; |
| 126 | |
| 127 | default: |
| 128 | // be forgiveful of sloppily api calls |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return $apiRequest; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Attempt to match a contact. This filters/updates the $createParams if there is a match. |
| 137 | * |
| 138 | * @param string $entity |
| 139 | * @param array $createParams |
| 140 | * @param array $keys |
| 141 | * @param bool $isMandatory |
| 142 | * @return array revised $createParams, including 'id' if known |
| 143 | * @throws API_Exception |
| 144 | */ |
| 145 | public function match($entity, $createParams, $keys, $isMandatory) { |
| 146 | $getParams = $this->createGetParams($createParams, $keys); |
| 147 | $getResult = civicrm_api3($entity, 'get', $getParams); |
| 148 | if ($getResult['count'] == 0) { |
| 149 | if ($isMandatory) { |
| 150 | throw new API_Exception("Failed to match existing record"); |
| 151 | } |
| 152 | return $createParams; // OK, don't care |
| 153 | } |
| 154 | elseif ($getResult['count'] == 1) { |
| 155 | $item = array_shift($getResult['values']); |
| 156 | $createParams['id'] = $item['id']; |
| 157 | return $createParams; |
| 158 | } |
| 159 | else { |
| 160 | throw new API_Exception("Ambiguous match criteria"); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * {@inheritDoc} |
| 166 | */ |
| 167 | public function toApiOutput($apiRequest, $result) { |
| 168 | return $result; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Create APIv3 "get" parameters to lookup an existing record using $keys |
| 173 | * |
| 174 | * @param array $origParams |
| 175 | * Api request. |
| 176 | * @param array $keys |
| 177 | * List of keys to match against. |
| 178 | * |
| 179 | * @return array APIv3 $params |
| 180 | */ |
| 181 | public function createGetParams($origParams, $keys) { |
| 182 | $params = array('version' => 3); |
| 183 | foreach ($keys as $key) { |
| 184 | $params[$key] = CRM_Utils_Array::value($key, $origParams, ''); |
| 185 | } |
| 186 | return $params; |
| 187 | } |
| 188 | } |