2 // $Id: Relationship.php 45502 2013-02-08 13:32:55Z kurund $
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
32 * File for the CiviCRM APIv2 relationship functions
34 * @package CiviCRM_APIv2
35 * @subpackage API_Relationship
37 * @copyright CiviCRM LLC (c) 2004-2013
38 * @version $Id: Relationship.php 45502 2013-02-08 13:32:55Z kurund $
43 * Include utility functions
45 require_once 'api/v2/utils.php';
46 require_once 'CRM/Contact/BAO/Relationship.php';
47 require_once 'CRM/Contact/BAO/RelationshipType.php';
50 * Add or update a relationship
52 * @param array $params (reference ) input parameters
54 * @return array (reference) id of created or updated record
58 function civicrm_relationship_create(&$params) {
59 _civicrm_initialize();
61 // check params for required fields (add/update)
62 $error = _civicrm_relationship_check_params($params);
63 if (civicrm_error($error)) {
67 require_once 'CRM/Contact/BAO/Relationship.php';
68 $error = _civicrm_relationship_format_params($params, $values);
70 if (civicrm_error($error)) {
75 $action = CRM_Core_Action
::ADD
;
76 require_once 'CRM/Utils/Array.php';
78 if (CRM_Utils_Array
::value('id', $params)) {
79 $ids['relationship'] = $params['id'];
80 $ids['contactTarget'] = $params['contact_id_b'];
81 $action = CRM_Core_Action
::UPDATE
;
84 $values['relationship_type_id'] = $params['relationship_type_id'] . '_a_b';
85 $values['contact_check'] = array($params['contact_id_b'] => $params['contact_id_b']);
86 $ids['contact'] = $params['contact_id_a'];
88 $relationshipBAO = CRM_Contact_BAO_Relationship
::create($values, $ids);
90 if (is_a($relationshipBAO, 'CRM_Core_Error')) {
91 return civicrm_create_error('Relationship can not be created');
93 elseif ($relationshipBAO[1]) {
94 return civicrm_create_error('Relationship is not valid');
96 elseif ($relationshipBAO[2]) {
97 return civicrm_create_error('Relationship already exists');
99 CRM_Contact_BAO_Relationship
::relatedMemberships($params['contact_id_a'], $values, $ids, $action);
101 return civicrm_create_success(array('id' => implode(',', $relationshipBAO[4])));
105 * Delete a relationship
107 * @param id of relationship $id
109 * @return boolean true if success, else false
113 function civicrm_relationship_delete(&$params) {
115 if (empty($params)) {
116 return civicrm_create_error('No input parameter present');
119 if (!is_array($params)) {
120 return civicrm_create_error(ts('Input parameter is not an array'));
123 if (!CRM_Utils_Array
::value('id', $params)) {
124 return civicrm_create_error('Missing required parameter');
126 require_once 'CRM/Utils/Rule.php';
127 if ($params['id'] != NULL && !CRM_Utils_Rule
::integer($params['id'])) {
128 return civicrm_create_error('Invalid value for relationship ID');
131 $relationBAO = new CRM_Contact_BAO_Relationship();
132 $relationBAO->id
= $params['id'];
133 if (!$relationBAO->find(TRUE)) {
134 return civicrm_create_error(ts('Relationship id is not valid'));
137 $relationBAO->del($params['id']);
138 return civicrm_create_success(ts('Deleted relationship successfully'));
143 * Function to update relationship
145 * @param array $params Associative array of property name/value pairs to update the relationship
147 * @return array Array with relationship information
152 function civicrm_relationship_update($params) {
154 _civicrm_initialize();
155 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
158 * Erik Hommel, 5 Oct 2010 : fix for CRM-6895
159 * check if required field relationship_id is in the parms. As the
160 * CRM_Contact_BAO_Relationship::getRelatonship throws up some issues
161 * (CRM-6905) the relationship is retrieved with a direct query
165 civicrm_verify_mandatory($params, 'CRM_Contact_DAO_Relationship', array('relationship_id'));
168 'id', 'contact_id_a', 'contact_id_b',
169 'relationship_type_id', 'start_date', 'end_date', 'is_active',
170 'description', 'is_permission_a_b', 'is_permission_b_a', 'case_id',
173 $relationship_id = (int) $params['relationship_id'];
174 $query = "SELECT * FROM civicrm_relationship WHERE id = $relationship_id";
175 $daoRelations = CRM_Core_DAO
::executeQuery($query);
176 while ($daoRelations->fetch()) {
177 foreach ($names as $name) {
178 $current_values[$name] = $daoRelations->$name;
181 $params = array_merge($current_values, $params);
182 $params['start_date'] = date("Ymd", strtotime($params['start_date']));
183 $params['end_date'] = date("Ymd", strtotime($params['end_date']));
185 return civicrm_relationship_create($params);
187 catch(PEAR_Exception
$e) {
188 return civicrm_create_error($e->getMessage());
191 return civicrm_create_error($e->getMessage());
196 * Function to get the relationship
198 * @param array $params (reference ) input parameters
199 param['contact_id'] is mandatory
201 * @return Array of all relationship.
205 function civicrm_relationship_get($params) {
206 if (!isset($params['contact_id'])) {
207 return civicrm_create_error(ts('Could not find contact_id in input parameters.'));
210 return civicrm_contact_relationship_get($params);
214 * backward compatibility function to match broken naming convention in v2.2.1 and prior
216 function civicrm_get_relationships($contact_a, $contact_b = NULL, $relationshipTypes = NULL, $sort = NULL) {
217 return civicrm_contact_relationship_get($contact_a, $contact_b, $relationshipTypes, $sort);
221 * Function to get the relationship
223 * @param array $contact_a (reference ) input parameters.
224 * @param array $contact_b (reference ) input parameters.
225 * @param array $relationshipTypes an array of Relationship Type Name.
226 * @param string $sort sort all relationship by relationshipId (eg asc/desc)
228 * @return Array of all relationship.
232 function civicrm_contact_relationship_get($contact_a, $contact_b = NULL, $relationshipTypes = NULL, $sort = NULL) {
233 if (!is_array($contact_a)) {
234 return civicrm_create_error(ts('Input parameter is not an array'));
237 if (!isset($contact_a['contact_id'])) {
238 return civicrm_create_error(ts('Could not find contact_id in input parameters.'));
240 require_once 'CRM/Contact/BAO/Relationship.php';
241 $contactID = $contact_a['contact_id'];
242 $relationships = CRM_Contact_BAO_Relationship
::getRelationship($contactID);
244 if (!empty($relationshipTypes)) {
246 foreach ($relationshipTypes as $relationshipName) {
247 foreach ($relationships as $key => $relationship) {
248 if ($relationship['relation'] == $relationshipName) {
249 $result[$key] = $relationship;
253 $relationships = $result;
256 if (isset($contact_b['contact_id'])) {
257 $cid = $contact_b['contact_id'];
260 foreach ($relationships as $key => $relationship) {
261 if ($relationship['cid'] == $cid) {
262 $result[$key] = $relationship;
265 $relationships = $result;
268 //sort by relationship id
270 if (strtolower($sort) == 'asc') {
271 ksort($relationships);
273 elseif (strtolower($sort) == 'desc') {
274 krsort($relationships);
278 //handle custom data.
279 require_once 'CRM/Core/BAO/CustomGroup.php';
281 foreach ($relationships as $relationshipId => $values) {
282 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree('Relationship', CRM_Core_DAO
::$_nullObject, $relationshipId, FALSE,
283 $values['civicrm_relationship_type_id']
285 $formatTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, CRM_Core_DAO
::$_nullObject);
288 CRM_Core_BAO_CustomGroup
::setDefaults($formatTree, $defaults);
290 if (!empty($defaults)) {
291 foreach ($defaults as $key => $val) {
292 $relationships[$relationshipId][$key] = $val;
297 if ($relationships) {
298 return civicrm_create_success($relationships);
301 return civicrm_create_error(ts('Invalid Data'));
306 * take the input parameter list as specified in the data model and
307 * convert it into the same format that we use in QF and BAO object
309 * @param array $params Associative array of property name/value
310 * pairs to insert in new contact.
311 * @param array $values The reformatted properties that we can use internally
314 * @return array|CRM_Error
317 function _civicrm_relationship_format_params(&$params, &$values) {
318 // copy all the relationship fields as is
320 $fields = CRM_Contact_DAO_Relationship
::fields();
321 _civicrm_store_values($fields, $params, $values);
323 $relationTypes = CRM_Core_PseudoConstant
::relationshipType('name', TRUE);
325 foreach ($params as $key => $value) {
326 // ignore empty values or empty arrays etc
327 require_once 'CRM/Utils/System.php';
328 if (CRM_Utils_System
::isNull($value)) {
335 require_once 'CRM/Utils/Rule.php';
336 if (!CRM_Utils_Rule
::integer($value)) {
337 return civicrm_create_error("contact_id not valid: $value");
339 $dao = new CRM_Core_DAO();
341 $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
345 return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
351 if (!CRM_Utils_Rule
::qfDate($value)) {
352 return civicrm_create_error("$key not a valid date: $value");
356 case 'relationship_type':
357 foreach ($relationTypes as $relTypId => $relValue) {
358 if (CRM_Utils_Array
::key(ucfirst($value), $relValue)) {
359 $relationshipTypeId = $relTypId;
364 if ($relationshipTypeId) {
365 if (CRM_Utils_Array
::value('relationship_type_id', $values) &&
366 $relationshipTypeId != $values['relationship_type_id']
368 return civicrm_create_error('Mismatched Relationship Type and Relationship Type Id');
370 $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId;
373 return civicrm_create_error('Invalid Relationship Type');
375 case 'relationship_type_id':
376 if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) {
377 return civicrm_create_error("$key not a valid: $value");
380 // execute for both relationship_type and relationship_type_id
381 $relation = $relationTypes[$params['relationship_type_id']];
382 require_once 'CRM/Contact/BAO/Contact.php';
383 if ($relation['contact_type_a'] &&
384 $relation['contact_type_a'] != CRM_Contact_BAO_Contact
::getContactType($params['contact_id_a'])
386 return civicrm_create_error("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}");
388 if ($relation['contact_type_b'] &&
389 $relation['contact_type_b'] != CRM_Contact_BAO_Contact
::getContactType($params['contact_id_b'])
391 return civicrm_create_error("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}");
400 if (array_key_exists('note', $params)) {
401 $values['note'] = $params['note'];
403 _civicrm_custom_format_params($params, $values, 'Relationship');
409 * This function ensures that we have the right input parameters
411 * We also need to make sure we run all the form rules on the params list
412 * to ensure that the params are valid
414 * @param array $params Associative array of property name/value
415 * pairs to insert in new relationship.
417 * @return bool|CRM_Utils_Error
420 function _civicrm_relationship_check_params(&$params) {
421 static $required = array(
422 'contact_id_a' => NULL,
423 'contact_id_b' => NULL,
424 'relationship_type_id' => 'relationship_type',
427 // params should be an array
428 if (!is_array($params)) {
429 return civicrm_create_error('Input parameter is not an array');
431 // cannot create with empty params
432 if (empty($params)) {
433 return civicrm_create_error('Input Parameters empty');
435 // check params for validity of Relationship id
436 if (CRM_Utils_Array
::value('id', $params)) {
437 require_once 'CRM/Contact/BAO/Relationship.php';
438 $relation = new CRM_Contact_BAO_Relationship();
439 $relation->id
= $params['id'];
440 if (!$relation->find(TRUE)) {
441 return civicrm_create_error('Relationship id is not valid');
444 if (($params['contact_id_a'] != $relation->contact_id_a
) ||
445 ($params['contact_id_b'] != $relation->contact_id_b
)
447 return civicrm_create_error('Cannot change the contacts once relationship has been created');
454 foreach ($required as $field => $eitherField) {
455 if (!CRM_Utils_Array
::value($field, $params)) {
456 if ($eitherField && CRM_Utils_Array
::value($eitherField, $params)) {
465 return civicrm_create_error('Required fields not found' . $error);