5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * File for the CiviCRM APIv3 relationship functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_Relationship
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: Relationship.php 30486 2010-11-02 16:12:09Z shot $
42 * Add or update a relationship
44 * @param array $params input parameters
46 * @example RelationshipCreate.php Std Create example
48 * @return array API Result Array
49 * {@getfields relationship_create}
54 function civicrm_api3_relationship_create($params) {
57 _civicrm_api3_relationship_format_params($params, $values);
59 $action = CRM_Core_Action
::ADD
;
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
;
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);
72 if ($relationshipBAO[1]) {
73 return civicrm_api3_create_error('Relationship is not valid');
75 elseif ($relationshipBAO[2]) {
76 return civicrm_api3_create_error('Relationship already exists');
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]),
87 * Adjust Metadata for Create action
89 * @param array $params array or parameters determined by getfields
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;
99 * Delete a relationship
101 * @param array $params
103 * @return array API Result Array
104 * {@getfields relationship_delete}
105 * @example RelationshipDelete.php Delete Example
110 function civicrm_api3_relationship_delete($params) {
112 if (!CRM_Utils_Rule
::integer($params['id'])) {
113 return civicrm_api3_create_error('Invalid value for relationship ID');
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');
122 $relationBAO->del($params['id']);
123 return civicrm_api3_create_success('Deleted relationship successfully');
128 * Function to get the relationship
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)
135 * @return Array API Result Array
136 * {@getfields relationship_get}
137 * @example RelationshipGet.php
140 function civicrm_api3_relationship_get($params) {
142 if (!CRM_Utils_Array
::value('contact_id', $params)) {
143 $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE);
146 $relationships = array();
147 $relationships = CRM_Contact_BAO_Relationship
::getRelationship($params['contact_id'],
148 CRM_Utils_Array
::value('status_id', $params),
151 CRM_Utils_Array
::value('id', $params), NULL
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));
159 return civicrm_api3_create_success($relationships, $params);
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
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
171 * @return array|CRM_Error
174 function _civicrm_api3_relationship_format_params($params, &$values) {
175 // copy all the relationship fields as is
177 $fields = CRM_Contact_DAO_Relationship
::fields();
178 _civicrm_api3_store_values($fields, $params, $values);
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');
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
)
191 throw new Exception('Cannot change the contacts once relationship has been created');
194 // since the BAO function is not std & won't accept just 'id' (aargh) let's
195 // at least return our BAO here
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]);
208 foreach ($params as $key => $value) {
209 // ignore empty values or empty arrays etc
210 if (CRM_Utils_System
::isNull($value)) {
217 if (!CRM_Utils_Rule
::integer($value)) {
218 throw new Exception("contact_id not valid: $value");
220 $dao = new CRM_Core_DAO();
222 $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
226 throw new Exception("Invalid Contact ID: There is no contact record with contact_id = $value.");
230 case 'relationship_type':
231 foreach ($relationTypes as $relTypId => $relValue) {
232 if (CRM_Utils_Array
::key(ucfirst($value), $relValue)) {
233 $relationshipTypeId = $relTypId;
238 if ($relationshipTypeId) {
239 if (CRM_Utils_Array
::value('relationship_type_id', $values) &&
240 $relationshipTypeId != $values['relationship_type_id']
242 throw new Exception('Mismatched Relationship Type and Relationship Type Id');
244 $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId;
247 throw new Exception('Invalid Relationship Type');
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");
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'])
259 throw new Exception("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}");
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'])
264 throw new Exception("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}");
273 if (array_key_exists('note', $params)) {
274 $values['note'] = $params['note'];
276 _civicrm_api3_custom_format_params($params, $values, 'Relationship');