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 * This api exposes CiviCRM relationships.
15 * @package CiviCRM_APIv3
19 * Add or update a relationship.
21 * @param array $params
24 * @throws API_Exception
29 function civicrm_api3_relationship_create($params) {
30 _civicrm_api3_handle_relationship_type($params);
31 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params, 'Relationship');
35 * Adjust Metadata for Create action.
37 * @param array $params
38 * Array of parameters determined by getfields.
40 function _civicrm_api3_relationship_create_spec(&$params) {
41 $params['contact_id_a']['api.required'] = 1;
42 $params['contact_id_b']['api.required'] = 1;
43 $params['relationship_type_id']['api.required'] = 1;
44 $params['is_active']['api.default'] = 1;
45 $params['is_current_employer'] = [
46 'title' => 'Is Current Employer?',
47 'description' => 'This is a optional parameter used to add employer contact when \'Employee Of\' relationship is created',
48 'type' => CRM_Utils_Type
::T_BOOLEAN
,
53 * Delete a relationship.
55 * @param array $params
60 function civicrm_api3_relationship_delete($params) {
62 if (!CRM_Utils_Rule
::integer($params['id'])) {
63 return civicrm_api3_create_error('Invalid value for relationship ID');
66 $relationBAO = new CRM_Contact_BAO_Relationship();
67 $relationBAO->id
= $params['id'];
68 if (!$relationBAO->find(TRUE)) {
69 return civicrm_api3_create_error('Relationship id is not valid');
72 $relationBAO->del($params['id']);
73 return civicrm_api3_create_success('Deleted relationship successfully');
78 * Get one or more relationship/s.
80 * @param array $params
83 * @todo Result is inconsistent depending on whether contact_id is passed in :
84 * - if you pass in contact_id - it just returns all relationships for 'contact_id'
85 * - if you don't pass in contact_id then it does a filter on the relationship table (DAO based search)
90 function civicrm_api3_relationship_get($params) {
91 $options = _civicrm_api3_get_options_from_params($params);
93 if (empty($params['contact_id'])) {
94 if (!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) {
95 CRM_Contact_BAO_Relationship
::membershipTypeToRelationshipTypes($params);
97 $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE, 'Relationship');
100 $relationships = CRM_Contact_BAO_Relationship
::getRelationship($params['contact_id'],
101 CRM_Utils_Array
::value('status_id', $params),
103 CRM_Utils_Array
::value('is_count', $options),
104 CRM_Utils_Array
::value('id', $params),
111 //perhaps we should add a 'getcount' but at this stage lets just handle getcount output
112 if ($options['is_count']) {
113 return ['count' => $relationships];
115 foreach ($relationships as $relationshipId => $values) {
116 _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));
118 return civicrm_api3_create_success($relationships, $params);
122 * Legacy handling for relationship_type parameter.
124 * @param array $params
125 * Associative array of property name/value.
126 * pairs to insert in new contact.
128 function _civicrm_api3_handle_relationship_type(&$params) {
129 if (empty($params['relationship_type_id']) && !empty($params['relationship_type'])) {
130 $relationTypes = CRM_Core_PseudoConstant
::relationshipType('name');
131 foreach ($relationTypes as $relationshipTypeId => $relationshipValue) {
132 if (CRM_Utils_Array
::key(ucfirst($params['relationship_type']), $relationshipValue)) {
133 $params['relationship_type_id'] = $relationshipTypeId;
140 * Hack to ensure inherited membership got created/deleted on
141 * relationship add/delete respectively.
143 * @param array $params
144 * Array per getfields metadata.
148 function civicrm_api3_relationship_setvalue($params) {
149 require_once 'api/v3/Generic/Setvalue.php';
150 $result = civicrm_api3_generic_setValue(["entity" => 'Relationship', 'params' => $params]);
152 if (empty($result['is_error']) && CRM_Utils_String
::munge($params['field']) == 'is_active') {
153 $action = CRM_Core_Action
::DISABLE
;
154 if ($params['value'] == TRUE) {
155 $action = CRM_Core_Action
::ENABLE
;
157 CRM_Contact_BAO_Relationship
::disableEnableRelationship($params['id'], $action);
162 function _civicrm_api3_relationship_getoptions_spec(&$params) {
163 $params['field']['options']['relationship_type_id'] = ts('Relationship Type ID');
165 // Add parameters for limiting relationship type ID
166 $relationshipTypePrefix = ts('(For relationship_type_id only) ');
167 $params['contact_id'] = [
168 'title' => ts('Contact ID'),
169 'description' => $relationshipTypePrefix . ts('Limits options to those'
170 . ' available to give contact'),
171 'type' => CRM_Utils_Type
::T_INT
,
172 'FKClassName' => 'CRM_Contact_DAO_Contact',
173 'FKApiName' => 'Contact',
175 $params['relationship_direction'] = [
176 'title' => ts('Relationship Direction'),
177 'description' => $relationshipTypePrefix . ts('For relationships where the '
178 . 'name is the same for both sides (i.e. "Spouse Of") show the option '
179 . 'from "A" (origin) side or "B" (target) side of the relationship?'),
180 'type' => CRM_Utils_Type
::T_STRING
,
181 'options' => ['a_b' => 'a_b', 'b_a' => 'b_a'],
182 'api.default' => 'a_b',
184 $params['relationship_id'] = [
185 'title' => ts('Reference Relationship ID'),
186 'description' => $relationshipTypePrefix . ts('If provided alongside '
187 . 'contact ID it will be used to establish the contact type of the "B" '
188 . 'side of the relationship and limit options based on it. If the '
189 . 'provided contact ID does not match the "A" side of this relationship '
190 . 'then the "A" side of this relationship will be used to limit options'),
191 'type' => CRM_Utils_Type
::T_INT
,
192 'FKClassName' => 'CRM_Contact_DAO_Relationship',
193 'FKApiName' => 'Relationship',
195 $contactTypes = CRM_Contact_BAO_ContactType
::contactTypes();
196 $params['contact_type'] = [
197 'title' => ts('Contact Type'),
198 'description' => $relationshipTypePrefix . ts('Limits options to those '
199 . 'available to this contact type. Overridden by the contact type of '
200 . 'contact ID (if provided)'),
201 'type' => CRM_Utils_Type
::T_STRING
,
202 'options' => array_combine($contactTypes, $contactTypes),
204 $params['is_form'] = [
205 'title' => ts('Is Form?'),
206 'description' => $relationshipTypePrefix . ts('Formats the options for use'
207 . ' in a form if true. The format is <id>_a_b => <label>'),
208 'type' => CRM_Utils_Type
::T_BOOLEAN
,