Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-12-16-09-32
[civicrm-core.git] / api / v3 / Relationship.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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
44 * Input parameters.
45 *
46 * @throws API_Exception
47 * @example RelationshipCreate.php Std Create example
48 *
49 * @return array
50 * API Result Array
51 * {@getfields relationship_create}
52 * @static void
53 * @access public
54 */
55 function civicrm_api3_relationship_create($params) {
56 _civicrm_api3_handle_relationship_type($params);
57 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Relationship');
58 }
59
60 /**
61 * Adjust Metadata for Create action
62 *
63 * @param array $params
64 * Array or parameters determined by getfields.
65 */
66 function _civicrm_api3_relationship_create_spec(&$params) {
67 $params['contact_id_a']['api.required'] = 1;
68 $params['contact_id_b']['api.required'] = 1;
69 $params['relationship_type_id']['api.required'] = 1;
70 $params['is_active']['api.default'] = 1;
71 }
72
73 /**
74 * Delete a relationship
75 *
76 * @param array $params
77 *
78 * @return array
79 * API Result Array
80 * {@getfields relationship_delete}
81 * @example RelationshipDelete.php Delete Example
82 *
83 * @static void
84 * @access public
85 */
86 function civicrm_api3_relationship_delete($params) {
87
88 if (!CRM_Utils_Rule::integer($params['id'])) {
89 return civicrm_api3_create_error('Invalid value for relationship ID');
90 }
91
92 $relationBAO = new CRM_Contact_BAO_Relationship();
93 $relationBAO->id = $params['id'];
94 if (!$relationBAO->find(TRUE)) {
95 return civicrm_api3_create_error('Relationship id is not valid');
96 }
97 else {
98 $relationBAO->del($params['id']);
99 return civicrm_api3_create_success('Deleted relationship successfully');
100 }
101 }
102
103 /**
104 * get the relationship
105 *
106 * @param array $params
107 * Input parameters.
108 * @todo Result is inconsistent depending on whether contact_id is passed in :
109 * - if you pass in contact_id - it just returns all relationships for 'contact_id'
110 * - if you don't pass in contact_id then it does a filter on the relationship table (DAO based search)
111 *
112 * @return Array
113 * API Result Array
114 * {@getfields relationship_get}
115 * @example RelationshipGet.php
116 * @access public
117 */
118 function civicrm_api3_relationship_get($params) {
119 $options = _civicrm_api3_get_options_from_params($params);
120
121 if (empty($params['contact_id'])) {
122 if (!empty($params['membership_type_id']) && empty($params['relationship_type_id'])) {
123 CRM_Contact_BAO_Relationship::membershipTypeToRelationshipTypes($params);
124 }
125 $relationships = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Relationship');
126 }
127 else {
128 $relationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'],
129 CRM_Utils_Array::value('status_id', $params),
130 0,
131 CRM_Utils_Array::value('is_count', $options),
132 CRM_Utils_Array::value('id', $params),
133 NULL,
134 NULL,
135 FALSE,
136 $params
137 );
138 }
139 //perhaps we should add a 'getcount' but at this stage lets just handle getcount output
140 if ($options['is_count']) {
141 return array('count' => $relationships);
142 }
143 foreach ($relationships as $relationshipId => $values) {
144 _civicrm_api3_custom_data_get($relationships[$relationshipId], 'Relationship', $relationshipId, NULL, CRM_Utils_Array::value('relationship_type_id', $values));
145 }
146 return civicrm_api3_create_success($relationships, $params);
147 }
148
149 /**
150 * legacy handling for relationship_type parameter
151 *
152 * @param array $params
153 * Associative array of property name/value.
154 * pairs to insert in new contact.
155 */
156 function _civicrm_api3_handle_relationship_type(&$params) {
157 if (empty($params['relationship_type_id']) && !empty($params['relationship_type'])) {
158 $relationTypes = CRM_Core_PseudoConstant::relationshipType('name');
159 foreach ($relationTypes as $relationshipTypeId => $relationshipValue) {
160 if (CRM_Utils_Array::key(ucfirst($params['relationship_type']), $relationshipValue)) {
161 $params['relationship_type_id'] = $relationshipTypeId;
162 }
163 }
164 }
165 }