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