3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This api exposes CiviCRM participant.
15 * @package CiviCRM_APIv3
19 * Create an Event Participant.
21 * @param array $params
22 * An associative array of name/value property values of civicrm_participant.
27 function civicrm_api3_participant_create($params) {
28 // Check that event id is not an template - should be done @ BAO layer.
29 if (!empty($params['event_id'])) {
30 $isTemplate = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
31 if (!empty($isTemplate)) {
32 return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
36 $values = $participant = [];
37 _civicrm_api3_custom_format_params($params, $values, 'Participant');
38 $params = array_merge($values, $params);
40 $participantBAO = CRM_Event_BAO_Participant
::create($params);
42 if (empty($params['price_set_id']) && empty($params['id']) && !empty($params['fee_level'])) {
43 _civicrm_api3_participant_createlineitem($params, $participantBAO);
45 _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id
]);
47 return civicrm_api3_create_success($participant, $params, 'Participant', 'create', $participantBAO);
51 * Create a default participant line item.
53 * @todo this should be done in the BAO not the api
55 * @param array $params
58 * @throws \CiviCRM_API3_Exception
60 function _civicrm_api3_participant_createlineitem(&$params, $participant) {
61 // it is possible that a fee level contains information about multiple
62 // price field values.
64 $priceFieldValueDetails = CRM_Utils_Array
::explodePadded(
65 $params["fee_level"]);
67 foreach ($priceFieldValueDetails as $detail) {
68 if (preg_match('/- ([0-9]+)$/', $detail, $matches)) {
69 // it is possible that a price field value is payd for multiple times.
70 // (FIXME: if the price field value ends in minus followed by whitespace
71 // and a number, things will go wrong.)
74 preg_match('/^(.*) - [0-9]+$/', $detail, $matches);
83 SELECT ps.id AS setID, pf.id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.amount AS amount
84 FROM civicrm_price_set_entity cpse
85 LEFT JOIN civicrm_price_set ps ON cpse.price_set_id = ps.id AND cpse.entity_id = %1 AND cpse.entity_table = 'civicrm_event'
86 LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
87 LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
88 where ps.id is not null and pfv.label = %2
92 1 => [$params['event_id'], 'Integer'],
93 2 => [$label, 'String'],
96 $dao = CRM_Core_DAO
::executeQuery($sql, $qParams);
99 'price_field_id' => $dao->priceFieldID
,
100 'price_field_value_id' => $dao->priceFieldValueID
,
101 'entity_table' => 'civicrm_participant',
102 'entity_id' => $participant->id
,
105 'participant_count' => 0,
106 'unit_price' => $dao->amount
,
107 'line_total' => $qty * $dao->amount
,
109 civicrm_api3('line_item', 'create', $lineItemParams);
116 * Adjust Metadata for Create action.
118 * The metadata is used for setting defaults, documentation & validation.
120 * @param array $params
121 * Array of parameters determined by getfields.
123 function _civicrm_api3_participant_create_spec(&$params) {
124 $params['status_id']['api.default'] = "1";
125 $params['register_date']['api.default'] = "now";
126 $params['event_id']['api.required'] = 1;
127 $params['contact_id']['api.required'] = 1;
128 // These are for the sake of search builder options - can be removed if that is fixed
129 $params['role_id']['api.aliases'] = ['participant_role'];
130 $params['status_id']['api.aliases'] = ['participant_status'];
134 * Retrieve a specific participant, given a set of input params.
136 * @param array $params
140 * array of properties, if error an array with an error id and error message
142 function civicrm_api3_participant_get($params) {
143 $mode = CRM_Contact_BAO_Query
::MODE_EVENT
;
145 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, 'Participant');
148 while ($dao->fetch()) {
149 $query->convertToPseudoNames($dao, FALSE, TRUE);
150 $participant[$dao->participant_id
] = $query->store($dao);
151 //@todo - is this required - contribution & pledge use the same query but don't self-retrieve custom data
152 _civicrm_api3_custom_data_get($participant[$dao->participant_id
], CRM_Utils_Array
::value('check_permissions', $params), 'Participant', $dao->participant_id
, NULL);
155 return civicrm_api3_create_success($participant, $params, 'Participant', 'get', $dao);
159 * Adjust Metadata for Get action.
161 * The metadata is used for setting defaults, documentation & validation.
163 * @param array $params
164 * Array of parameters determined by getfields.
166 function _civicrm_api3_participant_get_spec(&$params) {
167 $params['participant_test'] = [
169 'title' => 'Get Test Participants',
170 'type' => CRM_Utils_Type
::T_BOOLEAN
,
175 * Deletes an existing contact participant.
177 * This API is used for deleting a contact participant
179 * @param array $params
180 * Array containing Id of the contact participant to be deleted.
185 function civicrm_api3_participant_delete($params) {
187 $result = CRM_Event_BAO_Participant
::deleteParticipant($params['id']);
190 return civicrm_api3_create_success();
193 throw new Exception('Error while deleting participant');