(NFC) (dev/core#878) Simplify copyright header (tests/*)
[civicrm-core.git] / api / v3 / Relationship.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
c28e1768 29 * This api exposes CiviCRM relationships.
6a488035
TO
30 *
31 * @package CiviCRM_APIv3
6a488035
TO
32 */
33
34/**
9d32e6f7 35 * Add or update a relationship.
6a488035 36 *
cf470720
TO
37 * @param array $params
38 * Input parameters.
6a488035 39 *
77b97be7 40 * @throws API_Exception
6a488035 41 *
a6c01b45 42 * @return array
72b3a70c 43 * API Result Array
6a488035
TO
44 */
45function civicrm_api3_relationship_create($params) {
936e43d6
EM
46 _civicrm_api3_handle_relationship_type($params);
47 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Relationship');
6a488035 48}
11e09c59
TO
49
50/**
0aa0303c 51 * Adjust Metadata for Create action.
6a488035 52 *
cf470720 53 * @param array $params
b081365f 54 * Array of parameters determined by getfields.
6a488035
TO
55 */
56function _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;
f42bcfb1
MD
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,
65 ];
6a488035
TO
66}
67
68/**
9d32e6f7 69 * Delete a relationship.
6a488035 70 *
cf470720 71 * @param array $params
6a488035 72 *
a6c01b45 73 * @return array
72b3a70c 74 * API Result Array
6a488035
TO
75 */
76function civicrm_api3_relationship_delete($params) {
77
6a488035
TO
78 if (!CRM_Utils_Rule::integer($params['id'])) {
79 return civicrm_api3_create_error('Invalid value for relationship ID');
80 }
81
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');
86 }
87 else {
88 $relationBAO->del($params['id']);
89 return civicrm_api3_create_success('Deleted relationship successfully');
90 }
91}
92
93/**
9d32e6f7 94 * Get one or more relationship/s.
6a488035 95 *
cf470720
TO
96 * @param array $params
97 * Input parameters.
9d32e6f7 98 *
6a488035
TO
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)
102 *
408b79bf 103 * @return array
72b3a70c 104 * API Result Array
6a488035
TO
105 */
106function civicrm_api3_relationship_get($params) {
75638074 107 $options = _civicrm_api3_get_options_from_params($params);
c9c41397 108
a7488080 109 if (empty($params['contact_id'])) {
22e263ad 110 if (!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) {
c9c41397 111 CRM_Contact_BAO_Relationship::membershipTypeToRelationshipTypes($params);
112 }
bf38705f 113 $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Relationship');
6a488035
TO
114 }
115 else {
6a488035
TO
116 $relationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'],
117 CRM_Utils_Array::value('status_id', $params),
118 0,
75638074 119 CRM_Utils_Array::value('is_count', $options),
120 CRM_Utils_Array::value('id', $params),
121 NULL,
122 NULL,
123 FALSE,
124 $params
6a488035
TO
125 );
126 }
75638074 127 //perhaps we should add a 'getcount' but at this stage lets just handle getcount output
22e263ad 128 if ($options['is_count']) {
cf8f0fff 129 return ['count' => $relationships];
75638074 130 }
6a488035 131 foreach ($relationships as $relationshipId => $values) {
e9ff5391 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));
6a488035 133 }
6a488035
TO
134 return civicrm_api3_create_success($relationships, $params);
135}
136
137/**
9d32e6f7 138 * Legacy handling for relationship_type parameter.
6a488035 139 *
cf470720
TO
140 * @param array $params
141 * Associative array of property name/value.
142 * pairs to insert in new contact.
6a488035 143 */
936e43d6
EM
144function _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;
6a488035
TO
150 }
151 }
152 }
6a488035 153}
882b2baf 154
f72be46d 155/**
156 * Hack to ensure inherited membership got created/deleted on
157 * relationship add/delete respectively.
158 *
159 * @param array $params
160 * Array per getfields metadata.
161 *
162 * @return array
163 */
882b2baf 164function civicrm_api3_relationship_setvalue($params) {
165 require_once 'api/v3/Generic/Setvalue.php';
cf8f0fff 166 $result = civicrm_api3_generic_setValue(["entity" => 'Relationship', 'params' => $params]);
882b2baf 167
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;
172 }
173 CRM_Contact_BAO_Relationship::disableEnableRelationship($params['id'], $action);
174 }
175 return $result;
176}
44d259be
MD
177
178function _civicrm_api3_relationship_getoptions_spec(&$params) {
179 $params['field']['options']['relationship_type_id'] = ts('Relationship Type ID');
180
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',
190 ];
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',
199 ];
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',
210 ];
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),
219 ];
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 &lt;id&gt;_a_b => &lt;label&gt;'),
7c31ae57 224 'type' => CRM_Utils_Type::T_BOOLEAN,
44d259be
MD
225 ];
226}