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