Merge pull request #417 from colemanw/CRM-12339
[civicrm-core.git] / api / v2 / Event.php
1 <?php
2 // $Id: Event.php 45502 2013-02-08 13:32:55Z kurund $
3
4
5 /*
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
12 | |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
21 | |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
29 */
30
31 /**
32 *
33 * File for the CiviCRM APIv2 event functions
34 *
35 * @package CiviCRM_APIv2
36 * @subpackage API_Event
37 *
38 * @copyright CiviCRM LLC (c) 2004-2013
39 * @version $Id: Event.php 45502 2013-02-08 13:32:55Z kurund $
40 *
41 */
42
43 /**
44 * Files required for this package
45 */
46 require_once 'api/v2/utils.php';
47
48 /**
49 * Create a Event
50 *
51 * This API is used for creating a Event
52 *
53 * @param array $params (reference ) input parameters
54 * Allowed @params array keys are:
55 * {@schema Event/Event.xml}
56 *
57 * @return array of newly created event property values.
58 * @access public
59 */
60 function civicrm_event_create(&$params) {
61 _civicrm_initialize();
62 $errorScope = CRM_Core_TemporaryErrorScope::useException();
63 try {
64 civicrm_api_check_permission(__FUNCTION__, $params, TRUE);
65 civicrm_verify_mandatory($params, 'CRM_Event_DAO_Event', array('start_date', 'event_type_id', 'title'));
66
67 // Do we really want $params[id], even if we have
68 // $params[event_id]? if yes then please uncomment the below line
69
70 //$ids['event' ] = $params['id'];
71
72 $ids['eventTypeId'] = (int) $params['event_type_id'];
73 $ids['startDate'] = $params['start_date'];
74 $ids['event_id'] = CRM_Utils_Array::value('event_id', $params);
75
76 require_once 'CRM/Event/BAO/Event.php';
77 $eventBAO = CRM_Event_BAO_Event::create($params, $ids);
78
79 if (is_a($eventBAO, 'CRM_Core_Error')) {
80 return civicrm_create_error("Event is not created");
81 }
82 else {
83 $event = array();
84 _civicrm_object_to_array($eventBAO, $event);
85 $values = array();
86 $values['event_id'] = $event['id'];
87 $values['is_error'] = 0;
88 }
89
90 return $values;
91 }
92 catch(Exception$e) {
93 return civicrm_create_error($e->getMessage());
94 }
95 }
96
97 /**
98 * Get an Event.
99 *
100 * This api is used to retrieve all data for an existing Event.
101 * Required parameters : id of event
102 *
103 * @param array $params an associative array of title/value property values of civicrm_event
104 *
105 * @return If successful array of event data; otherwise object of CRM_Core_Error.
106 * @access public
107 */
108 function civicrm_event_get(&$params) {
109 _civicrm_initialize();
110
111 if (!is_array($params)) {
112 return civicrm_create_error('Input parameters is not an array.');
113 }
114
115 if (empty($params)) {
116 return civicrm_create_error('Params cannot be empty.');
117 }
118
119 $event = &civicrm_event_search($params);
120
121 if (count($event) != 1 &&
122 !CRM_Utils_Array::value('returnFirst', $params)
123 ) {
124 return civicrm_create_error(ts('%1 events matching input params', array(1 => count($event))));
125 }
126
127 if (civicrm_error($event)) {
128 return $event;
129 }
130
131 $event = array_values($event);
132 $event[0]['is_error'] = 0;
133 return $event[0];
134 }
135
136 /**
137 * Get Event record.
138 *
139 *
140 * @param array $params an associative array of name/value property values of civicrm_event
141 *
142 * @return Array of all found event property values.
143 * @access public
144 */
145 function civicrm_event_search(&$params) {
146
147 if (!is_array($params)) {
148 return civicrm_create_error(ts('Input parameters is not an array.'));
149 }
150
151 $inputParams = array();
152 $returnProperties = array();
153 $returnCustomProperties = array();
154 $otherVars = array('sort', 'offset', 'rowCount', 'isCurrent');
155
156 $sort = array_key_exists('return.sort', $params) ? $params['return.sort'] : FALSE;
157
158 // don't check if empty, more meaningful error for API user instead of silent defaults
159 $offset = array_key_exists('return.offset', $params) ? $params['return.offset'] : 0;
160 $rowCount = array_key_exists('return.max_results', $params) ? $params['return.max_results'] : 25;
161 $isCurrent = array_key_exists('isCurrent', $params) ? $params['isCurrent'] : 0;
162
163 foreach ($params as $n => $v) {
164 if (substr($n, 0, 7) == 'return.') {
165 if (substr($n, 0, 14) == 'return.custom_') {
166 //take custom return properties separate
167 $returnCustomProperties[] = substr($n, 7);
168 }
169 elseif (!in_array(substr($n, 7), array(
170 'sort', 'offset', 'max_results'))) {
171 $returnProperties[] = substr($n, 7);
172 }
173 }
174 elseif (in_array($n, $otherVars)) {
175 $$n = $v;
176 }
177 else {
178 $inputParams[$n] = $v;
179 }
180 }
181
182 if (!empty($returnProperties)) {
183 $returnProperties[] = 'id';
184 $returnProperties[] = 'event_type_id';
185 }
186
187 require_once 'CRM/Core/BAO/CustomGroup.php';
188 require_once 'CRM/Event/BAO/Event.php';
189 $eventDAO = new CRM_Event_BAO_Event();
190 $eventDAO->copyValues($inputParams);
191 $event = array();
192 if (!empty($returnProperties)) {
193 $eventDAO->selectAdd();
194 $eventDAO->selectAdd(implode(',', $returnProperties));
195 }
196 $eventDAO->whereAdd('( is_template IS NULL ) OR ( is_template = 0 )');
197 if ($isCurrent) {
198 $eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())');
199 }
200
201 $eventDAO->orderBy($sort);
202 $eventDAO->limit((int)$offset, (int)$rowCount);
203 $eventDAO->find();
204 while ($eventDAO->fetch()) {
205 $event[$eventDAO->id] = array();
206 CRM_Core_DAO::storeValues($eventDAO, $event[$eventDAO->id]);
207 $groupTree = &CRM_Core_BAO_CustomGroup::getTree('Event',
208 CRM_Core_DAO::$_nullObject,
209 $eventDAO->id,
210 FALSE,
211 $eventDAO->event_type_id
212 );
213 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, CRM_Core_DAO::$_nullObject);
214 $defaults = array();
215 CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults);
216
217 if (!empty($defaults)) {
218 foreach ($defaults as $key => $val) {
219 if (!empty($returnCustomProperties)) {
220 $customKey = explode('_', $key);
221 //show only return properties
222 if (in_array('custom_' . $customKey['1'], $returnCustomProperties)) {
223 $event[$eventDAO->id][$key] = $val;
224 }
225 }
226 else {
227 $event[$eventDAO->id][$key] = $val;
228 }
229 }
230 }
231 }
232 //end of the loop
233 $eventDAO->free();
234 return $event;
235 }
236
237 /**
238 * Deletes an existing event
239 *
240 * This API is used for deleting a event
241 *
242 * @param Array $params array containing event_id to be deleted
243 *
244 * @return boolean true if success, error otherwise
245 * @access public
246 */
247 function civicrm_event_delete(&$params) {
248 if (empty($params)) {
249 return civicrm_create_error(ts('No input parameters present'));
250 }
251
252 $eventID = NULL;
253
254 $eventID = CRM_Utils_Array::value('event_id', $params);
255
256 if (!isset($eventID)) {
257 return civicrm_create_error(ts('Invalid value for eventID'));
258 }
259
260 require_once 'CRM/Event/BAO/Event.php';
261
262 return CRM_Event_BAO_Event::del($eventID) ? civicrm_create_success() : civicrm_create_error(ts('Error while deleting event'));
263 }
264