3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This api exposes CiviCRM relationships.
31 * @package CiviCRM_APIv3
35 * Add or update a relationship.
37 * @param array $params
40 * @throws API_Exception
45 function civicrm_api3_relationship_create($params) {
46 _civicrm_api3_handle_relationship_type($params);
47 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params, 'Relationship');
51 * Adjust Metadata for Create action.
53 * @param array $params
54 * Array of parameters determined by getfields.
56 function _civicrm_api3_relationship_create_spec(&$params) {
57 $params['contact_id_a']['api.required'] = 1;
58 $params['contact_id_b']['api.required'] = 1;
59 $params['relationship_type_id']['api.required'] = 1;
60 $params['is_active']['api.default'] = 1;
61 $params['is_current_employer'] = [
62 'title' => 'Is Current Employer?',
63 'description' => 'This is a optional parameter used to add employer contact when \'Employee Of\' relationship is created',
64 'type' => CRM_Utils_Type
::T_BOOLEAN
,
69 * Delete a relationship.
71 * @param array $params
76 function civicrm_api3_relationship_delete($params) {
78 if (!CRM_Utils_Rule
::integer($params['id'])) {
79 return civicrm_api3_create_error('Invalid value for relationship ID');
82 $relationBAO = new CRM_Contact_BAO_Relationship();
83 $relationBAO->id
= $params['id'];
84 if (!$relationBAO->find(TRUE)) {
85 return civicrm_api3_create_error('Relationship id is not valid');
88 $relationBAO->del($params['id']);
89 return civicrm_api3_create_success('Deleted relationship successfully');
94 * Get one or more relationship/s.
96 * @param array $params
99 * @todo Result is inconsistent depending on whether contact_id is passed in :
100 * - if you pass in contact_id - it just returns all relationships for 'contact_id'
101 * - if you don't pass in contact_id then it does a filter on the relationship table (DAO based search)
106 function civicrm_api3_relationship_get($params) {
107 $options = _civicrm_api3_get_options_from_params($params);
109 if (empty($params['contact_id'])) {
110 if (!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) {
111 CRM_Contact_BAO_Relationship
::membershipTypeToRelationshipTypes($params);
113 $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE, 'Relationship');
116 $relationships = CRM_Contact_BAO_Relationship
::getRelationship($params['contact_id'],
117 CRM_Utils_Array
::value('status_id', $params),
119 CRM_Utils_Array
::value('is_count', $options),
120 CRM_Utils_Array
::value('id', $params),
127 //perhaps we should add a 'getcount' but at this stage lets just handle getcount output
128 if ($options['is_count']) {
129 return ['count' => $relationships];
131 foreach ($relationships as $relationshipId => $values) {
132 _civicrm_api3_custom_data_get($relationships[$relationshipId], CRM_Utils_Array
::value('check_permissions', $params), 'Relationship', $relationshipId, NULL, CRM_Utils_Array
::value('relationship_type_id', $values));
134 return civicrm_api3_create_success($relationships, $params);
138 * Legacy handling for relationship_type parameter.
140 * @param array $params
141 * Associative array of property name/value.
142 * pairs to insert in new contact.
144 function _civicrm_api3_handle_relationship_type(&$params) {
145 if (empty($params['relationship_type_id']) && !empty($params['relationship_type'])) {
146 $relationTypes = CRM_Core_PseudoConstant
::relationshipType('name');
147 foreach ($relationTypes as $relationshipTypeId => $relationshipValue) {
148 if (CRM_Utils_Array
::key(ucfirst($params['relationship_type']), $relationshipValue)) {
149 $params['relationship_type_id'] = $relationshipTypeId;
156 * Hack to ensure inherited membership got created/deleted on
157 * relationship add/delete respectively.
159 * @param array $params
160 * Array per getfields metadata.
164 function civicrm_api3_relationship_setvalue($params) {
165 require_once 'api/v3/Generic/Setvalue.php';
166 $result = civicrm_api3_generic_setValue(["entity" => 'Relationship', 'params' => $params]);
168 if (empty($result['is_error']) && CRM_Utils_String
::munge($params['field']) == 'is_active') {
169 $action = CRM_Core_Action
::DISABLE
;
170 if ($params['value'] == TRUE) {
171 $action = CRM_Core_Action
::ENABLE
;
173 CRM_Contact_BAO_Relationship
::disableEnableRelationship($params['id'], $action);
178 function _civicrm_api3_relationship_getoptions_spec(&$params) {
179 $params['field']['options']['relationship_type_id'] = ts('Relationship Type ID');
181 // Add parameters for limiting relationship type ID
182 $relationshipTypePrefix = ts('(For relationship_type_id only) ');
183 $params['contact_id'] = [
184 'title' => ts('Contact ID'),
185 'description' => $relationshipTypePrefix . ts('Limits options to those'
186 . ' available to give contact'),
187 'type' => CRM_Utils_Type
::T_INT
,
188 'FKClassName' => 'CRM_Contact_DAO_Contact',
189 'FKApiName' => 'Contact',
191 $params['relationship_direction'] = [
192 'title' => ts('Relationship Direction'),
193 'description' => $relationshipTypePrefix . ts('For relationships where the '
194 . 'name is the same for both sides (i.e. "Spouse Of") show the option '
195 . 'from "A" (origin) side or "B" (target) side of the relationship?'),
196 'type' => CRM_Utils_Type
::T_STRING
,
197 'options' => ['a_b' => 'a_b', 'b_a' => 'b_a'],
198 'api.default' => 'a_b',
200 $params['relationship_id'] = [
201 'title' => ts('Reference Relationship ID'),
202 'description' => $relationshipTypePrefix . ts('If provided alongside '
203 . 'contact ID it will be used to establish the contact type of the "B" '
204 . 'side of the relationship and limit options based on it. If the '
205 . 'provided contact ID does not match the "A" side of this relationship '
206 . 'then the "A" side of this relationship will be used to limit options'),
207 'type' => CRM_Utils_Type
::T_INT
,
208 'FKClassName' => 'CRM_Contact_DAO_Relationship',
209 'FKApiName' => 'Relationship',
211 $contactTypes = CRM_Contact_BAO_ContactType
::contactTypes();
212 $params['contact_type'] = [
213 'title' => ts('Contact Type'),
214 'description' => $relationshipTypePrefix . ts('Limits options to those '
215 . 'available to this contact type. Overridden by the contact type of '
216 . 'contact ID (if provided)'),
217 'type' => CRM_Utils_Type
::T_STRING
,
218 'options' => array_combine($contactTypes, $contactTypes),
220 $params['is_form'] = [
221 'title' => ts('Is Form?'),
222 'description' => $relationshipTypePrefix . ts('Formats the options for use'
223 . ' in a form if true. The format is <id>_a_b => <label>'),
224 'type' => CRM_Utils_Type
::T_BOOLEAN
,