Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-05-21-13-15-18
[civicrm-core.git] / api / v2 / GroupNesting.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 * File for the CiviCRM APIv2 group nesting functions
32 *
33 * @package CiviCRM_APIv2
34 * @subpackage API_Group
35 *
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: GroupNesting.php 21624 2009-08-07 22:02:55Z wmorgan $
38 *
39 */
40
41/**
42 * Include utility functions
43 */
44require_once 'api/v2/utils.php';
45
46/**
47 * Provides group nesting record(s) given parent and/or child id.
48 *
49 * @param array $params an array containing at least child_group_id or parent_group_id
50 *
51 * @return array list of group nesting records
52 */
53function civicrm_group_nesting_get(&$params) {
54 _civicrm_initialize();
55
56 if (!is_array($params)) {
57 return civicrm_create_error('Params need to be of type array!');
58 }
59
60 if (!array_key_exists('child_group_id', $params) &&
61 !array_key_exists('parent_group_id', $params)
62 ) {
63 return civicrm_create_error(ts('At least one of child_group_id or parent_group_id is a required field'));
64 }
65
66 require_once 'CRM/Contact/DAO/GroupNesting.php';
67 $dao = new CRM_Contact_DAO_GroupNesting();
68 if (array_key_exists('child_group_id', $params)) {
69 $dao->child_group_id = $params['child_group_id'];
70 }
71 if (array_key_exists('parent_group_id', $params)) {
72 $dao->parent_group_id = $params['parent_group_id'];
73 }
74
75 $values = array();
76
77 if ($dao->find()) {
78 while ($dao->fetch()) {
79 $temp = array();
80 _civicrm_object_to_array($dao, $temp);
81 $values[$dao->id] = $temp;
82 }
83 $values['is_error'] = 0;
84 }
85 else {
86 return civicrm_create_error('No records found.');
87 }
88
89 return $values;
90}
91
92/**
93 * Creates group nesting record for given parent and child id.
94 * Parent and child groups need to exist.
95 *
96 * @param array &$params parameters array - allowed array keys include:
97 * {@schema Contact/GroupNesting.xml}
98 *
99 * @return array TBD
100 *
101 * @todo Work out the return value.
102 */
103function civicrm_group_nesting_create(&$params) {
104
105 if (!is_array($params)) {
106 return civicrm_create_error('Params need to be of type array!');
107 }
108
109 require_once 'CRM/Contact/BAO/GroupNesting.php';
110
111 if (!array_key_exists('child_group_id', $params) &&
112 !array_key_exists('parent_group_id', $params)
113 ) {
114 return civicrm_create_error(ts('You need to define parent_group_id and child_group_id in params.'));
115 }
116
117 CRM_Contact_BAO_GroupNesting::add($params['parent_group_id'], $params['child_group_id']);
118
119 // FIXME: CRM_Contact_BAO_GroupNesting requires some work
120 $result = array('is_error' => 0);
121 return $result;
122}
123
124/**
125 * Removes specific nesting records.
126 *
127 * @param array &$params parameters array - allowed array keys include:
128 * {@schema Contact/GroupNesting.xml}
129 *
130 * @return array TBD
131 *
132 * @todo Work out the return value.
133 */
134function civicrm_group_nesting_remove(&$params) {
135
136 if (!is_array($params)) {
137 return civicrm_create_error('Params need to be of type array!');
138 }
139
140 if (!array_key_exists('child_group_id', $params) ||
141 !array_key_exists('parent_group_id', $params)
142 ) {
143 return civicrm_create_error(ts('You need to define parent_group_id and child_group_id in params.'));
144 }
145
146 require_once 'CRM/Contact/DAO/GroupNesting.php';
147 $dao = new CRM_Contact_DAO_GroupNesting();
148 $dao->copyValues($params);
149
150 if ($dao->delete()) {
151 $result = array('is_error' => 0);
152 }
153 return $result;
154}
155