3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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;
64 * Delete a relationship.
66 * @param array $params
71 function civicrm_api3_relationship_delete($params) {
73 if (!CRM_Utils_Rule
::integer($params['id'])) {
74 return civicrm_api3_create_error('Invalid value for relationship ID');
77 $relationBAO = new CRM_Contact_BAO_Relationship();
78 $relationBAO->id
= $params['id'];
79 if (!$relationBAO->find(TRUE)) {
80 return civicrm_api3_create_error('Relationship id is not valid');
83 $relationBAO->del($params['id']);
84 return civicrm_api3_create_success('Deleted relationship successfully');
89 * Get one or more relationship/s.
91 * @param array $params
94 * @todo Result is inconsistent depending on whether contact_id is passed in :
95 * - if you pass in contact_id - it just returns all relationships for 'contact_id'
96 * - if you don't pass in contact_id then it does a filter on the relationship table (DAO based search)
101 function civicrm_api3_relationship_get($params) {
102 $options = _civicrm_api3_get_options_from_params($params);
104 if (empty($params['contact_id'])) {
105 if (!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) {
106 CRM_Contact_BAO_Relationship
::membershipTypeToRelationshipTypes($params);
108 $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE, 'Relationship');
111 $relationships = CRM_Contact_BAO_Relationship
::getRelationship($params['contact_id'],
112 CRM_Utils_Array
::value('status_id', $params),
114 CRM_Utils_Array
::value('is_count', $options),
115 CRM_Utils_Array
::value('id', $params),
122 //perhaps we should add a 'getcount' but at this stage lets just handle getcount output
123 if ($options['is_count']) {
124 return array('count' => $relationships);
126 foreach ($relationships as $relationshipId => $values) {
127 _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));
129 return civicrm_api3_create_success($relationships, $params);
133 * Legacy handling for relationship_type parameter.
135 * @param array $params
136 * Associative array of property name/value.
137 * pairs to insert in new contact.
139 function _civicrm_api3_handle_relationship_type(&$params) {
140 if (empty($params['relationship_type_id']) && !empty($params['relationship_type'])) {
141 $relationTypes = CRM_Core_PseudoConstant
::relationshipType('name');
142 foreach ($relationTypes as $relationshipTypeId => $relationshipValue) {
143 if (CRM_Utils_Array
::key(ucfirst($params['relationship_type']), $relationshipValue)) {
144 $params['relationship_type_id'] = $relationshipTypeId;
151 * Hack to ensure inherited membership got created/deleted on
152 * relationship add/delete respectively.
154 * @param array $params
155 * Array per getfields metadata.
159 function civicrm_api3_relationship_setvalue($params) {
160 require_once 'api/v3/Generic/Setvalue.php';
161 $result = civicrm_api3_generic_setValue(array("entity" => 'Relationship', 'params' => $params));
163 if (empty($result['is_error']) && CRM_Utils_String
::munge($params['field']) == 'is_active') {
164 $action = CRM_Core_Action
::DISABLE
;
165 if ($params['value'] == TRUE) {
166 $action = CRM_Core_Action
::ENABLE
;
168 CRM_Contact_BAO_Relationship
::disableEnableRelationship($params['id'], $action);
173 function _civicrm_api3_relationship_getoptions_spec(&$params) {
174 $params['field']['options']['relationship_type_id'] = ts('Relationship Type ID');
176 // Add parameters for limiting relationship type ID
177 $relationshipTypePrefix = ts('(For relationship_type_id only) ');
178 $params['contact_id'] = [
179 'title' => ts('Contact ID'),
180 'description' => $relationshipTypePrefix . ts('Limits options to those'
181 . ' available to give contact'),
182 'type' => CRM_Utils_Type
::T_INT
,
183 'FKClassName' => 'CRM_Contact_DAO_Contact',
184 'FKApiName' => 'Contact',
186 $params['relationship_direction'] = [
187 'title' => ts('Relationship Direction'),
188 'description' => $relationshipTypePrefix . ts('For relationships where the '
189 . 'name is the same for both sides (i.e. "Spouse Of") show the option '
190 . 'from "A" (origin) side or "B" (target) side of the relationship?'),
191 'type' => CRM_Utils_Type
::T_STRING
,
192 'options' => ['a_b' => 'a_b', 'b_a' => 'b_a'],
193 'api.default' => 'a_b',
195 $params['relationship_id'] = [
196 'title' => ts('Reference Relationship ID'),
197 'description' => $relationshipTypePrefix . ts('If provided alongside '
198 . 'contact ID it will be used to establish the contact type of the "B" '
199 . 'side of the relationship and limit options based on it. If the '
200 . 'provided contact ID does not match the "A" side of this relationship '
201 . 'then the "A" side of this relationship will be used to limit options'),
202 'type' => CRM_Utils_Type
::T_INT
,
203 'FKClassName' => 'CRM_Contact_DAO_Relationship',
204 'FKApiName' => 'Relationship',
206 $contactTypes = CRM_Contact_BAO_ContactType
::contactTypes();
207 $params['contact_type'] = [
208 'title' => ts('Contact Type'),
209 'description' => $relationshipTypePrefix . ts('Limits options to those '
210 . 'available to this contact type. Overridden by the contact type of '
211 . 'contact ID (if provided)'),
212 'type' => CRM_Utils_Type
::T_STRING
,
213 'options' => array_combine($contactTypes, $contactTypes),
215 $params['is_form'] = [
216 'title' => ts('Is Form?'),
217 'description' => $relationshipTypePrefix . ts('Formats the options for use'
218 . ' in a form if true. The format is <id>_a_b => <label>'),
219 'type' => CRM_Utils_Type
::T_BOOLEAN