standardise naming of _spec functions & add support for 'id' as pseudonym of domain_id
[civicrm-core.git] / api / v3 / Event.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 *
32 * File for the CiviCRM APIv3 event functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Event
36 *
37 * @copyright CiviCRM LLC (c) 2004-2013
38 * @version $Id: Event.php 30964 2010-11-29 09:41:54Z shot $
39 *
40 */
41
42/**
43 * Files required for this package
44 */
45require_once 'CRM/Event/BAO/Event.php';
46
47/**
48 * Create a Event
49 *
50 * This API is used for creating a Event
51 *
52 * @param array $params input parameters
53 * Allowed @params array keys are:
54 * {@getfields event_create}
55 *
56 * @return array API result Array.
57 * @access public
58 */
59function civicrm_api3_event_create($params) {
60
61 _civicrm_api3_event_create_legacy_support_42($params);
62 //format custom fields so they can be added
63 $value = array();
64 _civicrm_api3_custom_format_params($params, $values, 'Event');
65 $params = array_merge($values, $params);
66 require_once 'CRM/Event/BAO/Event.php';
67
68 $eventBAO = CRM_Event_BAO_Event::create($params);
69 $event = array();
70 _civicrm_api3_object_to_array($eventBAO, $event[$eventBAO->id]);
71 return civicrm_api3_create_success($event, $params);
72}
73/*
74 * Adjust Metadata for Create action
75 *
76 * The metadata is used for setting defaults, documentation & validation
77 * @param array $params array or parameters determined by getfields
78 */
79function _civicrm_api3_event_create_spec(&$params) {
80 $params['event_type_id']['api.required'] = 1;;
81 $params['start_date']['api.required'] = 1;
82 $params['title']['api.required'] = 1;
83 $params['is_active']['api.default'] = 1;
84 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
85}
86/*
87 * Support for schema changes made in 4.2
88 * The main purpose of the API is to provide integrators a level of stability not provided by
89 * the core code or schema - this means we have to provide support for api calls (where possible)
90 * across schema changes.
91 */
92function _civicrm_api3_event_create_legacy_support_42(&$params){
93 if(!empty($params['payment_processor_id'])){
94 $params['payment_processor'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['payment_processor_id'] . CRM_Core_DAO::VALUE_SEPARATOR;
95 }
96}
97
98/**
99 * Get Event record.
100 *
101 *
102 * @param array $params an associative array of name/value property values of civicrm_event
103 * {@getfields event_get}
104 *
105 * @return Array of all found event property values.
106 * @access public
107 *
108 */
109function civicrm_api3_event_get($params) {
110
111 //legacy support for $params['return.sort']
112 if (CRM_Utils_Array::value('return.sort', $params)) {
113 $params['options']['sort'] = $params['return.sort'];
114 unset($params['return.sort']);
115 }
116
117 //legacy support for $params['return.sort']
118 if (CRM_Utils_Array::value('return.offset', $params)) {
119 $params['options']['offset'] = $params['return.offset'];
120 unset($params['return.offset']);
121 }
122
123 //legacy support for $params['return.max_results']
124 if (CRM_Utils_Array::value('return.max_results', $params)) {
125 $params['options']['limit'] = $params['return.max_results'];
126 unset($params['return.max_results']);
127 }
128
129 $eventDAO = new CRM_Event_BAO_Event();
130 _civicrm_api3_dao_set_filter($eventDAO, $params, TRUE, 'Event');
131
132 if (CRM_Utils_Array::value('is_template', $params)) {
133 $eventDAO->whereAdd( '( is_template = 1 )' );
134 }
135 else {
136 $eventDAO->whereAdd('( is_template IS NULL ) OR ( is_template = 0 )');
137 }
138
139 if (CRM_Utils_Array::value('isCurrent', $params)) {
140 $eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())');
141 }
142
143 // @todo should replace all this with _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity) - but we still have
144 // the return.is_full to deal with.
145 // NB the std dao_to_array function should only return custom if required.
146 $event = array();
147 $eventDAO->find();
148 while ($eventDAO->fetch()) {
149 $event[$eventDAO->id] = array();
150 CRM_Core_DAO::storeValues($eventDAO, $event[$eventDAO->id]);
151 if (CRM_Utils_Array::value('return.is_full', $params)) {
152 _civicrm_api3_event_getisfull($event, $eventDAO->id);
153 }
154 _civicrm_api3_event_get_legacy_support_42($event, $eventDAO->id);
155 _civicrm_api3_custom_data_get($event[$eventDAO->id], 'Event', $eventDAO->id, NULL, $eventDAO->event_type_id);
156 }
157 //end of the loop
158
159 return civicrm_api3_create_success($event, $params, 'event', 'get', $eventDAO);
160}
161/*
162 * Adjust Metadata for Get action
163*
164* The metadata is used for setting defaults, documentation & validation
165* @param array $params array or parameters determined by getfields
166*/
167function _civicrm_api3_event_get_spec(&$params) {
168 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
169}
170/*
171 * Support for schema changes made in 4.2
172 * The main purpose of the API is to provide integrators a level of stability not provided by
173 * the core code or schema - this means we have to provide support for api calls (where possible)
174 * across schema changes.
175 */
176function _civicrm_api3_event_get_legacy_support_42(&$event, $event_id){
177 if(!empty($event[$event_id]['payment_processor'])){
178 $processors = explode(CRM_Core_DAO::VALUE_SEPARATOR,$event[$event_id]['payment_processor']);
179 if(count($processors) == 3 ){
180 $event[$event_id]['payment_processor_id'] = $processors[1];
181 }
182 }
183}
184
185/**
186 * Deletes an existing event
187 *
188 * This API is used for deleting a event
189 *
190 * @param Array $params array containing event_id to be deleted
191 *
192 * @return boolean true if success, error otherwise
193 * @access public
194 * note API has legacy support for 'event_id'
195 * {@getfields event_delete}
196 */
197function civicrm_api3_event_delete($params) {
198
199 return CRM_Event_BAO_Event::del($params['id']) ? civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while deleting event'));
200}
201/*
202
203/*
204 * Function to add 'is_full' & 'available_seats' to the return array. (this might be better in the BAO)
205 * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form
206 *
207 * @param array $event return array of the event
208 * @param int $event_id Id of the event to be updated
209 *
210 */
211function _civicrm_api3_event_getisfull(&$event, $event_id) {
212 require_once 'CRM/Event/BAO/Participant.php';
213 $eventFullResult = CRM_Event_BAO_Participant::eventFull($event_id, 1);
214 if (!empty($eventFullResult) && is_int($eventFullResult)) {
215 $event[$event_id]['available_places'] = $eventFullResult;
216 }
217 else {
218 $event[$event_id]['available_places'] = 0;
219 }
220 $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ? 1 : 0;
221}
222