Merge pull request #766 from eileenmcnaughton/test-fix
[civicrm-core.git] / api / v3 / Relationship.php
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 */
54 function 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 /**
87 * Adjust Metadata for Create action
88 *
89 * @param array $params array or parameters determined by getfields
90 */
91 function _civicrm_api3_relationship_create_spec(&$params) {
92 $params['contact_id_a']['api.required'] = 1;
93 $params['contact_id_b']['api.required'] = 1;
94 $params['relationship_type_id']['api.required'] = 1;
95 $params['is_active']['api.default'] = 1;
96 }
97
98 /**
99 * Delete a relationship
100 *
101 * @param array $params
102 *
103 * @return array API Result Array
104 * {@getfields relationship_delete}
105 * @example RelationshipDelete.php Delete Example
106 *
107 * @static void
108 * @access public
109 */
110 function civicrm_api3_relationship_delete($params) {
111
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 */
140 function 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 */
174 function _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 if (CRM_Utils_System::isNull($value)) {
211 continue;
212 }
213
214 switch ($key) {
215 case 'contact_id_a':
216 case 'contact_id_b':
217 if (!CRM_Utils_Rule::integer($value)) {
218 throw new Exception("contact_id not valid: $value");
219 }
220 $dao = new CRM_Core_DAO();
221 $qParams = array();
222 $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
223 $qParams
224 );
225 if (!$svq) {
226 throw new Exception("Invalid Contact ID: There is no contact record with contact_id = $value.");
227 }
228 break;
229
230 case 'relationship_type':
231 foreach ($relationTypes as $relTypId => $relValue) {
232 if (CRM_Utils_Array::key(ucfirst($value), $relValue)) {
233 $relationshipTypeId = $relTypId;
234 break;
235 }
236 }
237
238 if ($relationshipTypeId) {
239 if (CRM_Utils_Array::value('relationship_type_id', $values) &&
240 $relationshipTypeId != $values['relationship_type_id']
241 ) {
242 throw new Exception('Mismatched Relationship Type and Relationship Type Id');
243 }
244 $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId;
245 }
246 else {
247 throw new Exception('Invalid Relationship Type');
248 }
249 case 'relationship_type_id':
250 if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) {
251 throw new Exception("$key not a valid: $value");
252 }
253
254 // execute for both relationship_type and relationship_type_id
255 $relation = $relationTypes[$params['relationship_type_id']];
256 if (!empty($params['contact_id_a']) && $relation['contact_type_a'] &&
257 $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])
258 ) {
259 throw new Exception("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}");
260 }
261 if (!empty($params['contact_id_b']) && $relation['contact_type_b'] &&
262 $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b'])
263 ) {
264 throw new Exception("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}");
265 }
266 break;
267
268 default:
269 break;
270 }
271 }
272
273 if (array_key_exists('note', $params)) {
274 $values['note'] = $params['note'];
275 }
276 _civicrm_api3_custom_format_params($params, $values, 'Relationship');
277
278 return array();
279 }