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