Merge pull request #5187 from mallezie/CRM-15982
[civicrm-core.git] / api / v3 / Relationship.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
c28e1768 29 * This api exposes CiviCRM relationships.
6a488035
TO
30 *
31 * @package CiviCRM_APIv3
6a488035
TO
32 */
33
34/**
9d32e6f7 35 * Add or update a relationship.
6a488035 36 *
cf470720
TO
37 * @param array $params
38 * Input parameters.
6a488035 39 *
77b97be7 40 * @throws API_Exception
6a488035 41 *
a6c01b45 42 * @return array
72b3a70c 43 * API Result Array
6a488035
TO
44 */
45function civicrm_api3_relationship_create($params) {
936e43d6
EM
46 _civicrm_api3_handle_relationship_type($params);
47 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Relationship');
6a488035 48}
11e09c59
TO
49
50/**
0aa0303c 51 * Adjust Metadata for Create action.
6a488035 52 *
cf470720 53 * @param array $params
b081365f 54 * Array of parameters determined by getfields.
6a488035
TO
55 */
56function _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;
61}
62
63/**
9d32e6f7 64 * Delete a relationship.
6a488035 65 *
cf470720 66 * @param array $params
6a488035 67 *
a6c01b45 68 * @return array
72b3a70c 69 * API Result Array
6a488035
TO
70 */
71function civicrm_api3_relationship_delete($params) {
72
6a488035
TO
73 if (!CRM_Utils_Rule::integer($params['id'])) {
74 return civicrm_api3_create_error('Invalid value for relationship ID');
75 }
76
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');
81 }
82 else {
83 $relationBAO->del($params['id']);
84 return civicrm_api3_create_success('Deleted relationship successfully');
85 }
86}
87
88/**
9d32e6f7 89 * Get one or more relationship/s.
6a488035 90 *
cf470720
TO
91 * @param array $params
92 * Input parameters.
9d32e6f7 93 *
6a488035
TO
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)
97 *
408b79bf 98 * @return array
72b3a70c 99 * API Result Array
6a488035
TO
100 */
101function civicrm_api3_relationship_get($params) {
75638074 102 $options = _civicrm_api3_get_options_from_params($params);
c9c41397 103
a7488080 104 if (empty($params['contact_id'])) {
22e263ad 105 if (!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) {
c9c41397 106 CRM_Contact_BAO_Relationship::membershipTypeToRelationshipTypes($params);
107 }
bf38705f 108 $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Relationship');
6a488035
TO
109 }
110 else {
6a488035
TO
111 $relationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'],
112 CRM_Utils_Array::value('status_id', $params),
113 0,
75638074 114 CRM_Utils_Array::value('is_count', $options),
115 CRM_Utils_Array::value('id', $params),
116 NULL,
117 NULL,
118 FALSE,
119 $params
6a488035
TO
120 );
121 }
75638074 122 //perhaps we should add a 'getcount' but at this stage lets just handle getcount output
22e263ad 123 if ($options['is_count']) {
75638074 124 return array('count' => $relationships);
125 }
6a488035 126 foreach ($relationships as $relationshipId => $values) {
35671d00 127 _civicrm_api3_custom_data_get($relationships[$relationshipId], 'Relationship', $relationshipId, NULL, CRM_Utils_Array::value('relationship_type_id', $values));
6a488035 128 }
6a488035
TO
129 return civicrm_api3_create_success($relationships, $params);
130}
131
132/**
9d32e6f7 133 * Legacy handling for relationship_type parameter.
6a488035 134 *
cf470720
TO
135 * @param array $params
136 * Associative array of property name/value.
137 * pairs to insert in new contact.
6a488035 138 */
936e43d6
EM
139function _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;
6a488035
TO
145 }
146 }
147 }
6a488035 148}
882b2baf 149
f72be46d 150/**
151 * Hack to ensure inherited membership got created/deleted on
152 * relationship add/delete respectively.
153 *
154 * @param array $params
155 * Array per getfields metadata.
156 *
157 * @return array
158 */
882b2baf 159function civicrm_api3_relationship_setvalue($params) {
160 require_once 'api/v3/Generic/Setvalue.php';
161 $result = civicrm_api3_generic_setValue(array("entity" => 'Relationship', 'params' => $params));
162
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;
167 }
168 CRM_Contact_BAO_Relationship::disableEnableRelationship($params['id'], $action);
169 }
170 return $result;
171}