Merge pull request #972 from deepak-srivastava/hr
[civicrm-core.git] / api / v3 / Relationship.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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 *
35 * @copyright CiviCRM LLC (c) 2004-2013
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();
58 $action = CRM_Core_Action::ADD;
59
60 if (CRM_Utils_Array::value('id', $params)) {
61 $ids['relationship'] = $params['id'];
62 $ids['contactTarget'] = $values['contact_id_b'];
63 $action = CRM_Core_Action::UPDATE;
64 }
65
66 $values['relationship_type_id'] = $values['relationship_type_id'] . '_a_b';
67 $values['contact_check'] = array($params['contact_id_b'] => $params['contact_id_b']);
68 $ids['contact'] = $values['contact_id_a'];
69 $relationshipBAO = CRM_Contact_BAO_Relationship::create($values, $ids);
70
71 if ($relationshipBAO[1]) {
72 return civicrm_api3_create_error('Relationship is not valid');
73 }
74 elseif ($relationshipBAO[2]) {
75 return civicrm_api3_create_error('Relationship already exists');
76 }
77 CRM_Contact_BAO_Relationship::relatedMemberships($params['contact_id_a'], $values, $ids, $action);
78 $relationID = $relationshipBAO[4][0];
79 return civicrm_api3_create_success(array(
80 $relationID => array('id' => $relationID,
81 'moreIDs' => implode(',', $relationshipBAO[4]),
82 )));
83 }
84
85 /**
86 * Adjust Metadata for Create action
87 *
88 * @param array $params array or parameters determined by getfields
89 */
90 function _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 */
109 function civicrm_api3_relationship_delete($params) {
110
111 if (!CRM_Utils_Rule::integer($params['id'])) {
112 return civicrm_api3_create_error('Invalid value for relationship ID');
113 }
114
115 $relationBAO = new CRM_Contact_BAO_Relationship();
116 $relationBAO->id = $params['id'];
117 if (!$relationBAO->find(TRUE)) {
118 return civicrm_api3_create_error('Relationship id is not valid');
119 }
120 else {
121 $relationBAO->del($params['id']);
122 return civicrm_api3_create_success('Deleted relationship successfully');
123 }
124 }
125
126 /**
127 * Function to get the relationship
128 *
129 * @param array $params input parameters.
130 * @todo Result is inconsistent depending on whether contact_id is passed in :
131 * - if you pass in contact_id - it just returns all relationships for 'contact_id'
132 * - if you don't pass in contact_id then it does a filter on the relationship table (DAO based search)
133 *
134 * @return Array API Result Array
135 * {@getfields relationship_get}
136 * @example RelationshipGet.php
137 * @access public
138 */
139 function civicrm_api3_relationship_get($params) {
140
141 if (!CRM_Utils_Array::value('contact_id', $params)) {
142 $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
143 }
144 else {
145 $relationships = array();
146 $relationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'],
147 CRM_Utils_Array::value('status_id', $params),
148 0,
149 0,
150 CRM_Utils_Array::value('id', $params), NULL
151 );
152 }
153 foreach ($relationships as $relationshipId => $values) {
154 _civicrm_api3_custom_data_get($relationships[$relationshipId], 'Relationship', $relationshipId, NULL, CRM_Utils_Array::value('relationship_type_id',$values));
155 }
156
157
158 return civicrm_api3_create_success($relationships, $params);
159 }
160
161 /**
162 * take the input parameter list as specified in the data model and
163 * convert it into the same format that we use in QF and BAO object
164 *
165 * @param array $params Associative array of property name/value
166 * pairs to insert in new contact.
167 * @param array $values The reformatted properties that we can use internally
168 * '
169 *
170 * @return array|CRM_Error
171 * @access public
172 */
173 function _civicrm_api3_relationship_format_params($params, &$values) {
174 // copy all the relationship fields as is
175
176 $fields = CRM_Contact_DAO_Relationship::fields();
177 _civicrm_api3_store_values($fields, $params, $values);
178
179 $relationTypes = CRM_Core_PseudoConstant::relationshipType('name');
180 if (CRM_Utils_Array::value('id', $params)) {
181 $relation = new CRM_Contact_BAO_Relationship();
182 $relation->id = $params['id'];
183 if (!$relation->find(TRUE)) {
184 throw new Exception('Relationship id is not valid');
185 }
186 else {
187 if ((isset($params['contact_id_a']) && $params['contact_id_a'] != $relation->contact_id_a) ||
188 (isset($params['contact_id_b']) && $params['contact_id_b'] != $relation->contact_id_b)
189 ) {
190 throw new Exception('Cannot change the contacts once relationship has been created');
191 }
192 else {
193 // since the BAO function is not std & won't accept just 'id' (aargh) let's
194 // at least return our BAO here
195 $values = array();
196 _civicrm_api3_object_to_array($relation, $values);
197 $values = array_merge($values, $params);
198 // and we need to reformat our date fields....
199 $dateFields = array('start_date', 'end_date');
200 foreach ($dateFields as $dateField){
201 $values[$dateField] = CRM_Utils_Date::processDate($values[$dateField]);
202 }
203 }
204 }
205 }
206
207 foreach ($params as $key => $value) {
208 // ignore empty values or empty arrays etc
209 if (CRM_Utils_System::isNull($value)) {
210 continue;
211 }
212
213 switch ($key) {
214 case 'contact_id_a':
215 case 'contact_id_b':
216 if (!CRM_Utils_Rule::integer($value)) {
217 throw new Exception("contact_id not valid: $value");
218 }
219 $dao = new CRM_Core_DAO();
220 $qParams = array();
221 $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
222 $qParams
223 );
224 if (!$svq) {
225 throw new Exception("Invalid Contact ID: There is no contact record with contact_id = $value.");
226 }
227 break;
228
229 case 'relationship_type':
230 foreach ($relationTypes as $relTypId => $relValue) {
231 if (CRM_Utils_Array::key(ucfirst($value), $relValue)) {
232 $relationshipTypeId = $relTypId;
233 break;
234 }
235 }
236
237 if ($relationshipTypeId) {
238 if (CRM_Utils_Array::value('relationship_type_id', $values) &&
239 $relationshipTypeId != $values['relationship_type_id']
240 ) {
241 throw new Exception('Mismatched Relationship Type and Relationship Type Id');
242 }
243 $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId;
244 }
245 else {
246 throw new Exception('Invalid Relationship Type');
247 }
248 case 'relationship_type_id':
249 if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) {
250 throw new Exception("$key not a valid: $value");
251 }
252
253 // execute for both relationship_type and relationship_type_id
254 $relation = $relationTypes[$params['relationship_type_id']];
255 if (!empty($params['contact_id_a']) && $relation['contact_type_a'] &&
256 $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])
257 ) {
258 throw new Exception("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}");
259 }
260 if (!empty($params['contact_id_b']) && $relation['contact_type_b'] &&
261 $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b'])
262 ) {
263 throw new Exception("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}");
264 }
265 break;
266
267 default:
268 break;
269 }
270 }
271
272 if (array_key_exists('note', $params)) {
273 $values['note'] = $params['note'];
274 }
275 _civicrm_api3_custom_format_params($params, $values, 'Relationship');
276
277 return array();
278 }