3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * This api exposes CiviCRM relationships.
31 * @package CiviCRM_APIv3
35 * Add or update a relationship.
37 * @param array $params
40 * @throws API_Exception
45 function civicrm_api3_relationship_create($params) {
46 _civicrm_api3_handle_relationship_type($params);
47 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params, 'Relationship');
51 * Adjust Metadata for Create action.
53 * @param array $params
54 * Array of parameters determined by getfields.
56 function _civicrm_api3_relationship_create_spec(&$params) {
57 $params['contact_id_a']['api.required'] = 1;
58 $params['contact_id_b']['api.required'] = 1;
59 $params['relationship_type_id']['api.required'] = 1;
60 $params['is_active']['api.default'] = 1;
64 * Delete a relationship.
66 * @param array $params
71 function civicrm_api3_relationship_delete($params) {
73 if (!CRM_Utils_Rule
::integer($params['id'])) {
74 return civicrm_api3_create_error('Invalid value for relationship ID');
77 $relationBAO = new CRM_Contact_BAO_Relationship();
78 $relationBAO->id
= $params['id'];
79 if (!$relationBAO->find(TRUE)) {
80 return civicrm_api3_create_error('Relationship id is not valid');
83 $relationBAO->del($params['id']);
84 return civicrm_api3_create_success('Deleted relationship successfully');
89 * Get one or more relationship/s.
91 * @param array $params
94 * @todo Result is inconsistent depending on whether contact_id is passed in :
95 * - if you pass in contact_id - it just returns all relationships for 'contact_id'
96 * - if you don't pass in contact_id then it does a filter on the relationship table (DAO based search)
101 function civicrm_api3_relationship_get($params) {
102 $options = _civicrm_api3_get_options_from_params($params);
104 if (empty($params['contact_id'])) {
105 if (!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) {
106 CRM_Contact_BAO_Relationship
::membershipTypeToRelationshipTypes($params);
108 $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE, 'Relationship');
111 $relationships = CRM_Contact_BAO_Relationship
::getRelationship($params['contact_id'],
112 CRM_Utils_Array
::value('status_id', $params),
114 CRM_Utils_Array
::value('is_count', $options),
115 CRM_Utils_Array
::value('id', $params),
122 //perhaps we should add a 'getcount' but at this stage lets just handle getcount output
123 if ($options['is_count']) {
124 return array('count' => $relationships);
126 foreach ($relationships as $relationshipId => $values) {
127 _civicrm_api3_custom_data_get($relationships[$relationshipId], 'Relationship', $relationshipId, NULL, CRM_Utils_Array
::value('relationship_type_id', $values));
129 return civicrm_api3_create_success($relationships, $params);
133 * Legacy handling for relationship_type parameter.
135 * @param array $params
136 * Associative array of property name/value.
137 * pairs to insert in new contact.
139 function _civicrm_api3_handle_relationship_type(&$params) {
140 if (empty($params['relationship_type_id']) && !empty($params['relationship_type'])) {
141 $relationTypes = CRM_Core_PseudoConstant
::relationshipType('name');
142 foreach ($relationTypes as $relationshipTypeId => $relationshipValue) {
143 if (CRM_Utils_Array
::key(ucfirst($params['relationship_type']), $relationshipValue)) {
144 $params['relationship_type_id'] = $relationshipTypeId;
151 * Hack to ensure inherited membership got created/deleted on
152 * relationship add/delete respectively.
154 * @param array $params
155 * Array per getfields metadata.
159 function civicrm_api3_relationship_setvalue($params) {
160 require_once 'api/v3/Generic/Setvalue.php';
161 $result = civicrm_api3_generic_setValue(array("entity" => 'Relationship', 'params' => $params));
163 if (empty($result['is_error']) && CRM_Utils_String
::munge($params['field']) == 'is_active') {
164 $action = CRM_Core_Action
::DISABLE
;
165 if ($params['value'] == TRUE) {
166 $action = CRM_Core_Action
::ENABLE
;
168 CRM_Contact_BAO_Relationship
::disableEnableRelationship($params['id'], $action);