Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
a30c801b TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
c28e1768 | 13 | * This api exposes CiviCRM relationships. |
6a488035 TO |
14 | * |
15 | * @package CiviCRM_APIv3 | |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
9d32e6f7 | 19 | * Add or update a relationship. |
6a488035 | 20 | * |
cf470720 TO |
21 | * @param array $params |
22 | * Input parameters. | |
6a488035 | 23 | * |
77b97be7 | 24 | * @throws API_Exception |
6a488035 | 25 | * |
a6c01b45 | 26 | * @return array |
72b3a70c | 27 | * API Result Array |
6a488035 TO |
28 | */ |
29 | function civicrm_api3_relationship_create($params) { | |
936e43d6 EM |
30 | _civicrm_api3_handle_relationship_type($params); |
31 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Relationship'); | |
6a488035 | 32 | } |
11e09c59 TO |
33 | |
34 | /** | |
0aa0303c | 35 | * Adjust Metadata for Create action. |
6a488035 | 36 | * |
cf470720 | 37 | * @param array $params |
b081365f | 38 | * Array of parameters determined by getfields. |
6a488035 TO |
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; | |
f42bcfb1 MD |
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 | ]; | |
6a488035 TO |
50 | } |
51 | ||
52 | /** | |
9d32e6f7 | 53 | * Delete a relationship. |
6a488035 | 54 | * |
cf470720 | 55 | * @param array $params |
6a488035 | 56 | * |
a6c01b45 | 57 | * @return array |
72b3a70c | 58 | * API Result Array |
6a488035 TO |
59 | */ |
60 | function civicrm_api3_relationship_delete($params) { | |
61 | ||
6a488035 TO |
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 | /** | |
9d32e6f7 | 78 | * Get one or more relationship/s. |
6a488035 | 79 | * |
cf470720 TO |
80 | * @param array $params |
81 | * Input parameters. | |
9d32e6f7 | 82 | * |
6a488035 TO |
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 | * | |
408b79bf | 87 | * @return array |
72b3a70c | 88 | * API Result Array |
6a488035 TO |
89 | */ |
90 | function civicrm_api3_relationship_get($params) { | |
75638074 | 91 | $options = _civicrm_api3_get_options_from_params($params); |
c9c41397 | 92 | |
a7488080 | 93 | if (empty($params['contact_id'])) { |
22e263ad | 94 | if (!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) { |
c9c41397 | 95 | CRM_Contact_BAO_Relationship::membershipTypeToRelationshipTypes($params); |
96 | } | |
bf38705f | 97 | $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Relationship'); |
6a488035 TO |
98 | } |
99 | else { | |
6a488035 TO |
100 | $relationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'], |
101 | CRM_Utils_Array::value('status_id', $params), | |
102 | 0, | |
75638074 | 103 | CRM_Utils_Array::value('is_count', $options), |
104 | CRM_Utils_Array::value('id', $params), | |
105 | NULL, | |
106 | NULL, | |
107 | FALSE, | |
108 | $params | |
6a488035 TO |
109 | ); |
110 | } | |
75638074 | 111 | //perhaps we should add a 'getcount' but at this stage lets just handle getcount output |
22e263ad | 112 | if ($options['is_count']) { |
cf8f0fff | 113 | return ['count' => $relationships]; |
75638074 | 114 | } |
6a488035 | 115 | foreach ($relationships as $relationshipId => $values) { |
e9ff5391 | 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)); |
6a488035 | 117 | } |
6a488035 TO |
118 | return civicrm_api3_create_success($relationships, $params); |
119 | } | |
120 | ||
121 | /** | |
9d32e6f7 | 122 | * Legacy handling for relationship_type parameter. |
6a488035 | 123 | * |
cf470720 TO |
124 | * @param array $params |
125 | * Associative array of property name/value. | |
126 | * pairs to insert in new contact. | |
6a488035 | 127 | */ |
936e43d6 EM |
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; | |
6a488035 TO |
134 | } |
135 | } | |
136 | } | |
6a488035 | 137 | } |
882b2baf | 138 | |
f72be46d | 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 | */ | |
882b2baf | 148 | function civicrm_api3_relationship_setvalue($params) { |
149 | require_once 'api/v3/Generic/Setvalue.php'; | |
cf8f0fff | 150 | $result = civicrm_api3_generic_setValue(["entity" => 'Relationship', 'params' => $params]); |
882b2baf | 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 | } | |
44d259be MD |
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 <id>_a_b => <label>'), | |
7c31ae57 | 208 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
44d259be MD |
209 | ]; |
210 | } |