Merge pull request #365 from eileenmcnaughton/4.3
[civicrm-core.git] / api / v3 / RelationshipType.php
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_APIv3
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 */
44 require_once 'CRM/Contact/BAO/RelationshipType.php';
45
46 /**
47 * Function to create relationship type
48 *
49 * @param array $params Associative array of property name/value pairs to insert in new relationship type.
50 *
51 * @return Newly created Relationship_type object
52 * {@getfields RelationshipType_create}
53 * @access public
54 * {@schema Contact/RelationshipType.xml}
55 */
56 function civicrm_api3_relationship_type_create($params) {
57
58 if (!isset($params['label_a_b'])) {
59
60 $params['label_a_b'] = $params['name_a_b'];
61 }
62
63 if (!isset($params['label_b_a'])) {
64
65 $params['label_b_a'] = $params['name_b_a'];
66 }
67
68 require_once 'CRM/Utils/Rule.php';
69
70 $ids = array();
71 if (isset($params['id']) && !CRM_Utils_Rule::integer($params['id'])) {
72 return civicrm_api3_create_error('Invalid value for relationship type ID');
73 }
74 else {
75 $ids['relationshipType'] = CRM_Utils_Array::value('id', $params);
76 }
77
78 $relationType = new CRM_Contact_BAO_RelationshipType();
79 $relationType = CRM_Contact_BAO_RelationshipType::add($params, $ids);
80
81 $relType = array();
82
83 _civicrm_api3_object_to_array($relationType, $relType[$relationType->id]);
84
85 return civicrm_api3_create_success($relType, $params, 'relationship_type', 'create', $relationType);
86 }
87
88 /**
89 * Adjust Metadata for Create action
90 *
91 * The metadata is used for setting defaults, documentation & validation
92 * @param array $params array or parameters determined by getfields
93 */
94 function _civicrm_api3_relationship_type_create_spec(&$params) {
95 $params['contact_type_a']['api.required'] = 1;
96 $params['contact_type_b']['api.required'] = 1;
97 $params['name_a_b']['api.required'] = 1;
98 $params['name_b_a']['api.required'] = 1;
99 }
100
101 /**
102 * Function to get all relationship type
103 * retruns An array of Relationship_type
104 * @access public
105 * {@getfields RelationshipType_get}
106 * @example RelationshipTypeGet.php
107 */
108 function civicrm_api3_relationship_type_get($params) {
109
110 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
111 }
112
113 /**
114 * Delete a relationship type delete
115 *
116 * @param id of relationship type $id
117 *
118 * @return array API Result Array
119 * {@getfields RelationshipType_delete}
120 * @static void
121 * @access public
122 */
123 function civicrm_api3_relationship_type_delete($params) {
124
125 require_once 'CRM/Utils/Rule.php';
126 if ($params['id'] != NULL && !CRM_Utils_Rule::integer($params['id'])) {
127 return civicrm_api3_create_error('Invalid value for relationship type ID');
128 }
129
130 $relationTypeBAO = new CRM_Contact_BAO_RelationshipType();
131 $result = $relationTypeBAO->del($params['id']);
132 if (!$result) {
133 return civicrm_api3_create_error('Could not delete relationship type');
134 }
135 return civicrm_api3_create_success($result, $params, 'relationship_type', 'delete', $relationTypeBAO);
136 }
137