Merge pull request #353 from eileenmcnaughton/trunk-kill-eval
[civicrm-core.git] / api / v2 / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28*/
29
30/**
31 * new version of civicrm apis. See blog post at
32 * http://civicrm.org/node/131
33 *
34 * @package CiviCRM_APIv2
35 * @subpackage API_Contact
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * $Id: Contact.php 30415 2010-10-29 12:02:47Z shot $
38 *
39 */
40
41/**
42 * Include common API util functions
43 */
44require_once 'api/v2/utils.php';
45
46//require ("api/v2/Relationship.php");
47
48/**
49 * Function to update relationship type
50 *
51 * @param array $params Associative array of property name/value pairs to update the relationship type.
52 *
53 * @return array Array with relationship type information
54 *
55 * @access public
56 *
57 * @todo Requires some work
58 */
59function civicrm_relationship_type_update($params) {
60 return civicrm_relationship_type_add($params);
61}
62/*
63 * Deprecated function to create relationship type
64 *
65 * @param array $params Associative array of property name/value pairs to insert in new relationship type.
66 *
67 * @return Newly created Relationship_type object
68 *
69 * @access public
70 *
71 * @deprecated
72 */
73function civicrm_relationship_type_add($params) {
74 $result = civicrm_relationship_type_create($params);
75 return $result;
76}
77
78/**
79 * Function to create relationship type
80 *
81 * @param array $params Associative array of property name/value pairs to insert in new relationship type.
82 *
83 * @return Newly created Relationship_type object
84 *
85 * @access public
86 *
87 */
88function civicrm_relationship_type_create($params) {
89
90 if (empty($params)) {
91 return civicrm_create_error(ts('No input parameters present'));
92 }
93
94 if (!is_array($params)) {
95 return civicrm_create_error(ts('Parameter is not an array'));
96 }
97
98 if (!isset($params['contact_types_a']) &&
99 !isset($params['contact_types_b']) &&
100 !isset($params['name_a_b']) &&
101 !isset($params['name_b_a'])
102 ) {
103
104 return civicrm_create_error(ts('Missing some required parameters (contact_types_a contact_types_b name_a_b name b_a)'));
105 }
106
107 if (!isset($params['label_a_b'])) {
108
109 $params['label_a_b'] = $params['name_a_b'];
110 }
111
112 if (!isset($params['label_b_a'])) {
113
114 $params['label_b_a'] = $params['name_b_a'];
115 }
116
117 require_once 'CRM/Utils/Rule.php';
118
119 $ids = array();
120 if (isset($params['id']) && !CRM_Utils_Rule::integer($params['id'])) {
121 return civicrm_create_error('Invalid value for relationship type ID');
122 }
123 else {
124 $ids['relationshipType'] = CRM_Utils_Array::value('id', $params);
125 }
126
127 require_once 'CRM/Contact/BAO/RelationshipType.php';
128 $relationType = CRM_Contact_BAO_RelationshipType::add($params, $ids);
129
130 $relType = array();
131 _civicrm_object_to_array($relationType, $relType);
132
133 return $relType;
134}
135
136/**
137 * Function to get all relationship type
138 * retruns An array of Relationship_type
139 * * @access public
140 */
141function civicrm_relationship_types_get($params = NULL) {
142 _civicrm_initialize();
143 require_once 'CRM/Contact/DAO/RelationshipType.php';
144 $relationshipTypes = array();
145 $relationshipType = array();
146 $relationType = new CRM_Contact_DAO_RelationshipType();
147 if (!empty($params) && is_array($params)) {
148 $properties = array_keys($relationType->fields());
149 foreach ($properties as $name) {
150 if (array_key_exists($name, $params)) {
151 $relationType->$name = $params[$name];
152 }
153 }
154 }
155 $relationType->find();
156 while ($relationType->fetch()) {
157 _civicrm_object_to_array(clone($relationType), $relationshipType);
158 $relationshipTypes[] = $relationshipType;
159 }
160 return $relationshipTypes;
161}
162
163/**
164 * Delete a relationship type delete
165 *
166 * @param id of relationship type $id
167 *
168 * @return boolean true if success, else false
169 * @static void
170 * @access public
171 */
172function civicrm_relationship_type_delete(&$params) {
173
174 if (!CRM_Utils_Array::value('id', $params)) {
175 return civicrm_create_error('Missing required parameter');
176 }
177 require_once 'CRM/Utils/Rule.php';
178 if ($params['id'] != NULL && !CRM_Utils_Rule::integer($params['id'])) {
179 return civicrm_create_error('Invalid value for relationship type ID');
180 }
181
182 $relationTypeBAO = new CRM_Contact_BAO_RelationshipType();
183 return $relationTypeBAO->del($params['id']) ? civicrm_create_success(ts('Deleted relationship type successfully')) : civicrm_create_error(ts('Could not delete relationship type'));
184}
185
186/**
187 * Wrapper to support rest calls, CRM-6860
188 * return An array of Relationship_type
189 * * @access public
190 */
191function civicrm_relationshipType_get($params = NULL) {
192 return civicrm_relationship_types_get($params);
193}
194