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