| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.5 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 8 | +--------------------------------------------------------------------+ |
| 9 | | This file is a part of CiviCRM. | |
| 10 | | | |
| 11 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 12 | | under the terms of the GNU Affero General Public License | |
| 13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 14 | | | |
| 15 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 18 | | See the GNU Affero General Public License for more details. | |
| 19 | | | |
| 20 | | You should have received a copy of the GNU Affero General Public | |
| 21 | | License and the CiviCRM Licensing Exception along | |
| 22 | | with this program; if not, contact CiviCRM LLC | |
| 23 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 24 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 26 | +--------------------------------------------------------------------+ |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * |
| 31 | * File for the CiviCRM APIv3 event functions |
| 32 | * |
| 33 | * @package CiviCRM_APIv3 |
| 34 | * @subpackage API_Event |
| 35 | * |
| 36 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 37 | * @version $Id: Event.php 30964 2010-11-29 09:41:54Z shot $ |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | /** |
| 42 | * Files required for this package |
| 43 | */ |
| 44 | |
| 45 | /** |
| 46 | * Create a Event |
| 47 | * |
| 48 | * This API is used for creating a Event |
| 49 | * |
| 50 | * @param array $params input parameters |
| 51 | * Allowed @params array keys are: |
| 52 | * {@getfields event_create} |
| 53 | * |
| 54 | * @return array API result Array. |
| 55 | * @access public |
| 56 | */ |
| 57 | function civicrm_api3_event_create($params) { |
| 58 | civicrm_api3_verify_one_mandatory($params, NULL, array('event_type_id', 'template_id')); |
| 59 | |
| 60 | // Clone event from template |
| 61 | if (!empty($params['template_id']) && empty($params['id'])) { |
| 62 | $copy = CRM_Event_BAO_Event::copy($params['template_id']); |
| 63 | $params['id'] = $copy->id; |
| 64 | unset($params['template_id']); |
| 65 | } |
| 66 | |
| 67 | _civicrm_api3_event_create_legacy_support_42($params); |
| 68 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Event'); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Adjust Metadata for Create action |
| 73 | * |
| 74 | * The metadata is used for setting defaults, documentation & validation |
| 75 | * @param array $params array or parameters determined by getfields |
| 76 | */ |
| 77 | function _civicrm_api3_event_create_spec(&$params) { |
| 78 | $params['start_date']['api.required'] = 1; |
| 79 | $params['title']['api.required'] = 1; |
| 80 | $params['is_active']['api.default'] = 1; |
| 81 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id'); |
| 82 | $params['is_template']['api.default'] = 0; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Support for schema changes made in 4.2 |
| 87 | * The main purpose of the API is to provide integrators a level of stability not provided by |
| 88 | * the core code or schema - this means we have to provide support for api calls (where possible) |
| 89 | * across schema changes. |
| 90 | */ |
| 91 | function _civicrm_api3_event_create_legacy_support_42(&$params){ |
| 92 | if(!empty($params['payment_processor_id'])){ |
| 93 | $params['payment_processor'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['payment_processor_id'] . CRM_Core_DAO::VALUE_SEPARATOR; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get Event record. |
| 99 | * |
| 100 | * |
| 101 | * @param array $params an associative array of name/value property values of civicrm_event |
| 102 | * {@getfields event_get} |
| 103 | * |
| 104 | * @return Array of all found event property values. |
| 105 | * @access public |
| 106 | * |
| 107 | */ |
| 108 | function civicrm_api3_event_get($params) { |
| 109 | |
| 110 | //legacy support for $params['return.sort'] |
| 111 | if (!empty($params['return.sort'])) { |
| 112 | $params['options']['sort'] = $params['return.sort']; |
| 113 | unset($params['return.sort']); |
| 114 | } |
| 115 | |
| 116 | //legacy support for $params['return.offset'] |
| 117 | if (!empty($params['return.offset'])) { |
| 118 | $params['options']['offset'] = $params['return.offset']; |
| 119 | unset($params['return.offset']); |
| 120 | } |
| 121 | |
| 122 | //legacy support for $params['return.max_results'] |
| 123 | if (!empty($params['return.max_results'])) { |
| 124 | $params['options']['limit'] = $params['return.max_results']; |
| 125 | unset($params['return.max_results']); |
| 126 | } |
| 127 | |
| 128 | $eventDAO = new CRM_Event_BAO_Event(); |
| 129 | _civicrm_api3_dao_set_filter($eventDAO, $params, TRUE, 'Event'); |
| 130 | |
| 131 | if (!empty($params['isCurrent'])) { |
| 132 | $eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())'); |
| 133 | } |
| 134 | |
| 135 | // @todo should replace all this with _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity) - but we still have |
| 136 | // the return.is_full to deal with. |
| 137 | // NB the std dao_to_array function should only return custom if required. |
| 138 | $event = array(); |
| 139 | $options = _civicrm_api3_get_options_from_params($params); |
| 140 | |
| 141 | $eventDAO->find(); |
| 142 | while ($eventDAO->fetch()) { |
| 143 | $event[$eventDAO->id] = array(); |
| 144 | CRM_Core_DAO::storeValues($eventDAO, $event[$eventDAO->id]); |
| 145 | if (!empty($params['return.is_full'])) { |
| 146 | _civicrm_api3_event_getisfull($event, $eventDAO->id); |
| 147 | } |
| 148 | _civicrm_api3_event_get_legacy_support_42($event, $eventDAO->id); |
| 149 | _civicrm_api3_custom_data_get($event[$eventDAO->id], 'Event', $eventDAO->id, NULL, $eventDAO->event_type_id); |
| 150 | if(!empty($options['return'])) { |
| 151 | $event[$eventDAO->id]['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventDAO->id); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return civicrm_api3_create_success($event, $params, 'event', 'get', $eventDAO); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Adjust Metadata for Get action |
| 160 | * |
| 161 | * The metadata is used for setting defaults, documentation & validation |
| 162 | * @param array $params array or parameters determined by getfields |
| 163 | */ |
| 164 | function _civicrm_api3_event_get_spec(&$params) { |
| 165 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id'); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Support for schema changes made in 4.2 |
| 170 | * The main purpose of the API is to provide integrators a level of stability not provided by |
| 171 | * the core code or schema - this means we have to provide support for api calls (where possible) |
| 172 | * across schema changes. |
| 173 | */ |
| 174 | function _civicrm_api3_event_get_legacy_support_42(&$event, $event_id){ |
| 175 | if(!empty($event[$event_id]['payment_processor'])){ |
| 176 | $processors = explode(CRM_Core_DAO::VALUE_SEPARATOR,$event[$event_id]['payment_processor']); |
| 177 | if(count($processors) == 3 ){ |
| 178 | $event[$event_id]['payment_processor_id'] = $processors[1]; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Deletes an existing event |
| 185 | * |
| 186 | * This API is used for deleting a event |
| 187 | * |
| 188 | * @param Array $params array containing event_id to be deleted |
| 189 | * |
| 190 | * @return boolean true if success, error otherwise |
| 191 | * @access public |
| 192 | * note API has legacy support for 'event_id' |
| 193 | * {@getfields event_delete} |
| 194 | */ |
| 195 | function civicrm_api3_event_delete($params) { |
| 196 | |
| 197 | return CRM_Event_BAO_Event::del($params['id']) ? civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while deleting event')); |
| 198 | } |
| 199 | /* |
| 200 | |
| 201 | /** |
| 202 | * add 'is_full' & 'available_seats' to the return array. (this might be better in the BAO) |
| 203 | * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form |
| 204 | * |
| 205 | * @param array $event return array of the event |
| 206 | * @param int $event_id Id of the event to be updated |
| 207 | * |
| 208 | */ |
| 209 | /** |
| 210 | * @param $event |
| 211 | * @param int $event_id |
| 212 | */ |
| 213 | function _civicrm_api3_event_getisfull(&$event, $event_id) { |
| 214 | $eventFullResult = CRM_Event_BAO_Participant::eventFull($event_id, 1); |
| 215 | if (!empty($eventFullResult) && is_int($eventFullResult)) { |
| 216 | $event[$event_id]['available_places'] = $eventFullResult; |
| 217 | } |
| 218 | else { |
| 219 | $event[$event_id]['available_places'] = 0; |
| 220 | } |
| 221 | $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ? 1 : 0; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * @see _civicrm_api3_generic_getlist_params. |
| 227 | * |
| 228 | * @param $request array |
| 229 | */ |
| 230 | function _civicrm_api3_event_getlist_params(&$request) { |
| 231 | $fieldsToReturn = array('start_date', 'event_type_id', 'title', 'summary'); |
| 232 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); |
| 233 | $request['params']['options']['sort'] = 'start_date DESC'; |
| 234 | $request['params'] += array( |
| 235 | 'is_template' => 0, |
| 236 | 'is_active' => 1, |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @see _civicrm_api3_generic_getlist_output |
| 242 | * |
| 243 | * @param $result array |
| 244 | * @param $request array |
| 245 | * |
| 246 | * @return array |
| 247 | */ |
| 248 | function _civicrm_api3_event_getlist_output($result, $request) { |
| 249 | $output = array(); |
| 250 | if (!empty($result['values'])) { |
| 251 | foreach ($result['values'] as $row) { |
| 252 | $data = array( |
| 253 | 'id' => $row[$request['id_field']], |
| 254 | 'label' => $row[$request['label_field']], |
| 255 | 'description' => array(CRM_Core_Pseudoconstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $row['event_type_id'])), |
| 256 | ); |
| 257 | if (!empty($row['start_date'])) { |
| 258 | $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date']); |
| 259 | } |
| 260 | if (!empty($row['summary'])) { |
| 261 | $data['description'][] = $row['summary']; |
| 262 | } |
| 263 | foreach ($request['extra'] as $field) { |
| 264 | $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; |
| 265 | } |
| 266 | $output[] = $data; |
| 267 | } |
| 268 | } |
| 269 | return $output; |
| 270 | } |