Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
731a0992 | 5 | | CiviCRM version 4.5 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
731a0992 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * File for the CiviCRM APIv3 relationship functions | |
31 | * | |
32 | * @package CiviCRM_APIv3 | |
33 | * @subpackage API_Relationship | |
34 | * | |
731a0992 | 35 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
36 | * @version $Id: Relationship.php 30486 2010-11-02 16:12:09Z shot $ |
37 | * | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Add or update a relationship | |
42 | * | |
43 | * @param array $params input parameters | |
44 | * | |
45 | * @example RelationshipCreate.php Std Create example | |
46 | * | |
47 | * @return array API Result Array | |
48 | * {@getfields relationship_create} | |
49 | * @static void | |
50 | * @access public | |
51 | * | |
52 | */ | |
53 | function civicrm_api3_relationship_create($params) { | |
54 | ||
55 | $values = array(); | |
56 | _civicrm_api3_relationship_format_params($params, $values); | |
57 | $ids = array(); | |
6a488035 | 58 | |
a7488080 | 59 | if (!empty($params['id'])) { |
6a488035 | 60 | $ids['contactTarget'] = $values['contact_id_b']; |
6a488035 TO |
61 | } |
62 | ||
63 | $values['relationship_type_id'] = $values['relationship_type_id'] . '_a_b'; | |
26dcc9eb | 64 | if(!empty($params['contact_id_b'])){ |
65 | $values['contact_check'] = array($params['contact_id_b'] => $params['contact_id_b']); | |
66 | } | |
67 | if(!empty($values['contact_id_a'])){ | |
68 | $ids['contact'] = $values['contact_id_a']; | |
69 | } | |
6a488035 TO |
70 | $relationshipBAO = CRM_Contact_BAO_Relationship::create($values, $ids); |
71 | ||
72 | if ($relationshipBAO[1]) { | |
26dcc9eb | 73 | throw new API_Exception('Relationship is not valid'); |
6a488035 TO |
74 | } |
75 | elseif ($relationshipBAO[2]) { | |
26dcc9eb | 76 | throw new API_Exception('Relationship already exists'); |
6a488035 | 77 | } |
26dcc9eb | 78 | $id = $relationshipBAO[4][0]; |
79 | $values = array(); | |
80 | _civicrm_api3_object_to_array($relationshipBAO[5][$id], $values[$id]); | |
81 | return civicrm_api3_create_success($values, $params, 'relationship', 'create'); | |
6a488035 | 82 | } |
11e09c59 TO |
83 | |
84 | /** | |
6a488035 TO |
85 | * Adjust Metadata for Create action |
86 | * | |
87 | * @param array $params array or parameters determined by getfields | |
88 | */ | |
89 | function _civicrm_api3_relationship_create_spec(&$params) { | |
90 | $params['contact_id_a']['api.required'] = 1; | |
91 | $params['contact_id_b']['api.required'] = 1; | |
92 | $params['relationship_type_id']['api.required'] = 1; | |
93 | $params['is_active']['api.default'] = 1; | |
94 | } | |
95 | ||
96 | /** | |
97 | * Delete a relationship | |
98 | * | |
99 | * @param array $params | |
100 | * | |
101 | * @return array API Result Array | |
102 | * {@getfields relationship_delete} | |
103 | * @example RelationshipDelete.php Delete Example | |
104 | * | |
105 | * @static void | |
106 | * @access public | |
107 | */ | |
108 | function civicrm_api3_relationship_delete($params) { | |
109 | ||
6a488035 TO |
110 | if (!CRM_Utils_Rule::integer($params['id'])) { |
111 | return civicrm_api3_create_error('Invalid value for relationship ID'); | |
112 | } | |
113 | ||
114 | $relationBAO = new CRM_Contact_BAO_Relationship(); | |
115 | $relationBAO->id = $params['id']; | |
116 | if (!$relationBAO->find(TRUE)) { | |
117 | return civicrm_api3_create_error('Relationship id is not valid'); | |
118 | } | |
119 | else { | |
120 | $relationBAO->del($params['id']); | |
121 | return civicrm_api3_create_success('Deleted relationship successfully'); | |
122 | } | |
123 | } | |
124 | ||
125 | /** | |
126 | * Function to get the relationship | |
127 | * | |
128 | * @param array $params input parameters. | |
129 | * @todo Result is inconsistent depending on whether contact_id is passed in : | |
130 | * - if you pass in contact_id - it just returns all relationships for 'contact_id' | |
131 | * - if you don't pass in contact_id then it does a filter on the relationship table (DAO based search) | |
132 | * | |
133 | * @return Array API Result Array | |
134 | * {@getfields relationship_get} | |
135 | * @example RelationshipGet.php | |
136 | * @access public | |
137 | */ | |
138 | function civicrm_api3_relationship_get($params) { | |
75638074 | 139 | $options = _civicrm_api3_get_options_from_params($params); |
c9c41397 | 140 | |
a7488080 | 141 | if (empty($params['contact_id'])) { |
c9c41397 | 142 | if(!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) { |
143 | CRM_Contact_BAO_Relationship::membershipTypeToRelationshipTypes($params); | |
144 | } | |
6a488035 TO |
145 | $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE); |
146 | } | |
147 | else { | |
148 | $relationships = array(); | |
149 | $relationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'], | |
150 | CRM_Utils_Array::value('status_id', $params), | |
151 | 0, | |
75638074 | 152 | CRM_Utils_Array::value('is_count', $options), |
153 | CRM_Utils_Array::value('id', $params), | |
154 | NULL, | |
155 | NULL, | |
156 | FALSE, | |
157 | $params | |
6a488035 TO |
158 | ); |
159 | } | |
75638074 | 160 | //perhaps we should add a 'getcount' but at this stage lets just handle getcount output |
161 | if($options['is_count']) { | |
162 | return array('count' => $relationships); | |
163 | } | |
6a488035 TO |
164 | foreach ($relationships as $relationshipId => $values) { |
165 | _civicrm_api3_custom_data_get($relationships[$relationshipId], 'Relationship', $relationshipId, NULL, CRM_Utils_Array::value('relationship_type_id',$values)); | |
166 | } | |
6a488035 TO |
167 | return civicrm_api3_create_success($relationships, $params); |
168 | } | |
169 | ||
170 | /** | |
171 | * take the input parameter list as specified in the data model and | |
172 | * convert it into the same format that we use in QF and BAO object | |
173 | * | |
174 | * @param array $params Associative array of property name/value | |
175 | * pairs to insert in new contact. | |
176 | * @param array $values The reformatted properties that we can use internally | |
177 | * ' | |
178 | * | |
179 | * @return array|CRM_Error | |
180 | * @access public | |
181 | */ | |
182 | function _civicrm_api3_relationship_format_params($params, &$values) { | |
183 | // copy all the relationship fields as is | |
184 | ||
185 | $fields = CRM_Contact_DAO_Relationship::fields(); | |
186 | _civicrm_api3_store_values($fields, $params, $values); | |
187 | ||
188 | $relationTypes = CRM_Core_PseudoConstant::relationshipType('name'); | |
a7488080 | 189 | if (!empty($params['id'])) { |
6a488035 TO |
190 | $relation = new CRM_Contact_BAO_Relationship(); |
191 | $relation->id = $params['id']; | |
192 | if (!$relation->find(TRUE)) { | |
193 | throw new Exception('Relationship id is not valid'); | |
194 | } | |
195 | else { | |
196 | if ((isset($params['contact_id_a']) && $params['contact_id_a'] != $relation->contact_id_a) || | |
197 | (isset($params['contact_id_b']) && $params['contact_id_b'] != $relation->contact_id_b) | |
198 | ) { | |
199 | throw new Exception('Cannot change the contacts once relationship has been created'); | |
200 | } | |
201 | else { | |
202 | // since the BAO function is not std & won't accept just 'id' (aargh) let's | |
203 | // at least return our BAO here | |
204 | $values = array(); | |
205 | _civicrm_api3_object_to_array($relation, $values); | |
206 | $values = array_merge($values, $params); | |
207 | // and we need to reformat our date fields.... | |
208 | $dateFields = array('start_date', 'end_date'); | |
209 | foreach ($dateFields as $dateField){ | |
210 | $values[$dateField] = CRM_Utils_Date::processDate($values[$dateField]); | |
211 | } | |
212 | } | |
213 | } | |
214 | } | |
215 | ||
216 | foreach ($params as $key => $value) { | |
217 | // ignore empty values or empty arrays etc | |
6a488035 TO |
218 | if (CRM_Utils_System::isNull($value)) { |
219 | continue; | |
220 | } | |
221 | ||
222 | switch ($key) { | |
223 | case 'contact_id_a': | |
224 | case 'contact_id_b': | |
6a488035 TO |
225 | if (!CRM_Utils_Rule::integer($value)) { |
226 | throw new Exception("contact_id not valid: $value"); | |
227 | } | |
228 | $dao = new CRM_Core_DAO(); | |
229 | $qParams = array(); | |
230 | $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value", | |
231 | $qParams | |
232 | ); | |
233 | if (!$svq) { | |
234 | throw new Exception("Invalid Contact ID: There is no contact record with contact_id = $value."); | |
235 | } | |
236 | break; | |
237 | ||
238 | case 'relationship_type': | |
239 | foreach ($relationTypes as $relTypId => $relValue) { | |
240 | if (CRM_Utils_Array::key(ucfirst($value), $relValue)) { | |
241 | $relationshipTypeId = $relTypId; | |
242 | break; | |
243 | } | |
244 | } | |
245 | ||
246 | if ($relationshipTypeId) { | |
a7488080 | 247 | if (!empty($values['relationship_type_id']) && |
6a488035 TO |
248 | $relationshipTypeId != $values['relationship_type_id'] |
249 | ) { | |
250 | throw new Exception('Mismatched Relationship Type and Relationship Type Id'); | |
251 | } | |
252 | $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId; | |
253 | } | |
254 | else { | |
255 | throw new Exception('Invalid Relationship Type'); | |
256 | } | |
257 | case 'relationship_type_id': | |
258 | if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) { | |
259 | throw new Exception("$key not a valid: $value"); | |
260 | } | |
261 | ||
262 | // execute for both relationship_type and relationship_type_id | |
263 | $relation = $relationTypes[$params['relationship_type_id']]; | |
264 | if (!empty($params['contact_id_a']) && $relation['contact_type_a'] && | |
265 | $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a']) | |
266 | ) { | |
267 | throw new Exception("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}"); | |
268 | } | |
269 | if (!empty($params['contact_id_b']) && $relation['contact_type_b'] && | |
270 | $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b']) | |
271 | ) { | |
272 | throw new Exception("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}"); | |
273 | } | |
274 | break; | |
275 | ||
276 | default: | |
277 | break; | |
278 | } | |
279 | } | |
280 | ||
281 | if (array_key_exists('note', $params)) { | |
282 | $values['note'] = $params['note']; | |
283 | } | |
284 | _civicrm_api3_custom_format_params($params, $values, 'Relationship'); | |
285 | ||
286 | return array(); | |
287 | } |