Merge pull request #15837 from totten/master-prtmpl
[civicrm-core.git] / api / v3 / Relationship.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM relationships.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Add or update a relationship.
20 *
21 * @param array $params
22 * Input parameters.
23 *
24 * @throws API_Exception
25 *
26 * @return array
27 * API Result Array
28 */
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');
32 }
33
34 /**
35 * Adjust Metadata for Create action.
36 *
37 * @param array $params
38 * Array of parameters determined by getfields.
39 */
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,
49 ];
50 }
51
52 /**
53 * Delete a relationship.
54 *
55 * @param array $params
56 *
57 * @return array
58 * API Result Array
59 */
60 function civicrm_api3_relationship_delete($params) {
61
62 if (!CRM_Utils_Rule::integer($params['id'])) {
63 return civicrm_api3_create_error('Invalid value for relationship ID');
64 }
65
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');
70 }
71 else {
72 $relationBAO->del($params['id']);
73 return civicrm_api3_create_success('Deleted relationship successfully');
74 }
75 }
76
77 /**
78 * Get one or more relationship/s.
79 *
80 * @param array $params
81 * Input parameters.
82 *
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)
86 *
87 * @return array
88 * API Result Array
89 */
90 function civicrm_api3_relationship_get($params) {
91 $options = _civicrm_api3_get_options_from_params($params);
92
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);
96 }
97 $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Relationship');
98 }
99 else {
100 $relationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'],
101 CRM_Utils_Array::value('status_id', $params),
102 0,
103 CRM_Utils_Array::value('is_count', $options),
104 CRM_Utils_Array::value('id', $params),
105 NULL,
106 NULL,
107 FALSE,
108 $params
109 );
110 }
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];
114 }
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));
117 }
118 return civicrm_api3_create_success($relationships, $params);
119 }
120
121 /**
122 * Legacy handling for relationship_type parameter.
123 *
124 * @param array $params
125 * Associative array of property name/value.
126 * pairs to insert in new contact.
127 */
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;
134 }
135 }
136 }
137 }
138
139 /**
140 * Hack to ensure inherited membership got created/deleted on
141 * relationship add/delete respectively.
142 *
143 * @param array $params
144 * Array per getfields metadata.
145 *
146 * @return array
147 */
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]);
151
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;
156 }
157 CRM_Contact_BAO_Relationship::disableEnableRelationship($params['id'], $action);
158 }
159 return $result;
160 }
161
162 function _civicrm_api3_relationship_getoptions_spec(&$params) {
163 $params['field']['options']['relationship_type_id'] = ts('Relationship Type ID');
164
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',
174 ];
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',
183 ];
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',
194 ];
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),
203 ];
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 &lt;id&gt;_a_b => &lt;label&gt;'),
208 'type' => CRM_Utils_Type::T_BOOLEAN,
209 ];
210 }