2 // $Id: Event.php 45502 2013-02-08 13:32:55Z kurund $
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
33 * File for the CiviCRM APIv2 event functions
35 * @package CiviCRM_APIv2
36 * @subpackage API_Event
38 * @copyright CiviCRM LLC (c) 2004-2013
39 * @version $Id: Event.php 45502 2013-02-08 13:32:55Z kurund $
44 * Files required for this package
46 require_once 'api/v2/utils.php';
51 * This API is used for creating a Event
53 * @param array $params (reference ) input parameters
54 * Allowed @params array keys are:
55 * {@schema Event/Event.xml}
57 * @return array of newly created event property values.
60 function civicrm_event_create(&$params) {
61 _civicrm_initialize();
62 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
64 civicrm_api_check_permission(__FUNCTION__
, $params, TRUE);
65 civicrm_verify_mandatory($params, 'CRM_Event_DAO_Event', array('start_date', 'event_type_id', 'title'));
67 // Do we really want $params[id], even if we have
68 // $params[event_id]? if yes then please uncomment the below line
70 //$ids['event' ] = $params['id'];
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);
76 require_once 'CRM/Event/BAO/Event.php';
77 $eventBAO = CRM_Event_BAO_Event
::create($params, $ids);
79 if (is_a($eventBAO, 'CRM_Core_Error')) {
80 return civicrm_create_error("Event is not created");
84 _civicrm_object_to_array($eventBAO, $event);
86 $values['event_id'] = $event['id'];
87 $values['is_error'] = 0;
93 return civicrm_create_error($e->getMessage());
100 * This api is used to retrieve all data for an existing Event.
101 * Required parameters : id of event
103 * @param array $params an associative array of title/value property values of civicrm_event
105 * @return If successful array of event data; otherwise object of CRM_Core_Error.
108 function civicrm_event_get(&$params) {
109 _civicrm_initialize();
111 if (!is_array($params)) {
112 return civicrm_create_error('Input parameters is not an array.');
115 if (empty($params)) {
116 return civicrm_create_error('Params cannot be empty.');
119 $event = &civicrm_event_search($params);
121 if (count($event) != 1 &&
122 !CRM_Utils_Array
::value('returnFirst', $params)
124 return civicrm_create_error(ts('%1 events matching input params', array(1 => count($event))));
127 if (civicrm_error($event)) {
131 $event = array_values($event);
132 $event[0]['is_error'] = 0;
140 * @param array $params an associative array of name/value property values of civicrm_event
142 * @return Array of all found event property values.
145 function civicrm_event_search(&$params) {
147 if (!is_array($params)) {
148 return civicrm_create_error(ts('Input parameters is not an array.'));
151 $inputParams = array();
152 $returnProperties = array();
153 $returnCustomProperties = array();
154 $otherVars = array('sort', 'offset', 'rowCount', 'isCurrent');
156 $sort = array_key_exists('return.sort', $params) ?
$params['return.sort'] : FALSE;
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;
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);
169 elseif (!in_array(substr($n, 7), array(
170 'sort', 'offset', 'max_results'))) {
171 $returnProperties[] = substr($n, 7);
174 elseif (in_array($n, $otherVars)) {
178 $inputParams[$n] = $v;
182 if (!empty($returnProperties)) {
183 $returnProperties[] = 'id';
184 $returnProperties[] = 'event_type_id';
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);
192 if (!empty($returnProperties)) {
193 $eventDAO->selectAdd();
194 $eventDAO->selectAdd(implode(',', $returnProperties));
196 $eventDAO->whereAdd('( is_template IS NULL ) OR ( is_template = 0 )');
198 $eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())');
201 $eventDAO->orderBy($sort);
202 $eventDAO->limit((int)$offset, (int)$rowCount);
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,
211 $eventDAO->event_type_id
213 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, CRM_Core_DAO
::$_nullObject);
215 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $defaults);
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;
227 $event[$eventDAO->id
][$key] = $val;
238 * Deletes an existing event
240 * This API is used for deleting a event
242 * @param Array $params array containing event_id to be deleted
244 * @return boolean true if success, error otherwise
247 function civicrm_event_delete(&$params) {
248 if (empty($params)) {
249 return civicrm_create_error(ts('No input parameters present'));
254 $eventID = CRM_Utils_Array
::value('event_id', $params);
256 if (!isset($eventID)) {
257 return civicrm_create_error(ts('Invalid value for eventID'));
260 require_once 'CRM/Event/BAO/Event.php';
262 return CRM_Event_BAO_Event
::del($eventID) ?
civicrm_create_success() : civicrm_create_error(ts('Error while deleting event'));