Merge pull request #18950 from MegaphoneJon/event-44
[civicrm-core.git] / api / v3 / Participant.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
a30c801b
TO
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 |
6a488035
TO
9 +--------------------------------------------------------------------+
10 */
11
12/**
c28e1768 13 * This api exposes CiviCRM participant.
6a488035
TO
14 *
15 * @package CiviCRM_APIv3
6a488035
TO
16 */
17
6a488035 18/**
9d32e6f7 19 * Create an Event Participant.
6a488035 20 *
cf470720
TO
21 * @param array $params
22 * An associative array of name/value property values of civicrm_participant.
6a488035 23 *
a6c01b45 24 * @return array
9d32e6f7 25 * API result array
4132c927 26 *
27 * @throws \CiviCRM_API3_Exception
28 * @throws \CRM_Core_Exception
6a488035
TO
29 */
30function civicrm_api3_participant_create($params) {
9d32e6f7 31 // Check that event id is not an template - should be done @ BAO layer.
a7488080 32 if (!empty($params['event_id'])) {
6a488035
TO
33 $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
34 if (!empty($isTemplate)) {
3770b22a 35 return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
6a488035
TO
36 }
37 }
38
cf8f0fff 39 $values = $participant = [];
6a488035
TO
40 _civicrm_api3_custom_format_params($params, $values, 'Participant');
41 $params = array_merge($values, $params);
42
43 $participantBAO = CRM_Event_BAO_Participant::create($params);
44
9b873358 45 if (empty($params['price_set_id']) && empty($params['id']) && !empty($params['fee_level'])) {
6a488035
TO
46 _civicrm_api3_participant_createlineitem($params, $participantBAO);
47 }
48 _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id]);
49
244bbdd8 50 return civicrm_api3_create_success($participant, $params, 'Participant', 'create', $participantBAO);
6a488035
TO
51}
52
11e09c59 53/**
22242c87 54 * Create a default participant line item.
9d32e6f7
EM
55 *
56 * @todo this should be done in the BAO not the api
57 *
d0997921 58 * @param array $params
645ee340 59 * @param $participant
9d32e6f7 60 *
645ee340 61 * @throws \CiviCRM_API3_Exception
6a488035 62 */
9b873358 63function _civicrm_api3_participant_createlineitem(&$params, $participant) {
7ad3b4bd
JV
64 // it is possible that a fee level contains information about multiple
65 // price field values.
66
ca4329ca 67 $priceFieldValueDetails = CRM_Utils_Array::explodePadded(
7ad3b4bd
JV
68 $params["fee_level"]);
69
22e263ad 70 foreach ($priceFieldValueDetails as $detail) {
7ad3b4bd
JV
71 if (preg_match('/- ([0-9]+)$/', $detail, $matches)) {
72 // it is possible that a price field value is payd for multiple times.
73 // (FIXME: if the price field value ends in minus followed by whitespace
74 // and a number, things will go wrong.)
75
76 $qty = $matches[1];
77 preg_match('/^(.*) - [0-9]+$/', $detail, $matches);
78 $label = $matches[1];
79 }
80 else {
81 $label = $detail;
82 $qty = 1;
83 }
84
7ad3b4bd
JV
85 $sql = "
86 SELECT ps.id AS setID, pf.id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.amount AS amount
87 FROM civicrm_price_set_entity cpse
ca4329ca 88 LEFT JOIN civicrm_price_set ps ON cpse.price_set_id = ps.id AND cpse.entity_id = %1 AND cpse.entity_table = 'civicrm_event'
7ad3b4bd 89 LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
43867728 90 LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
ca4329ca 91 where ps.id is not null and pfv.label = %2
7ad3b4bd
JV
92 ";
93
cf8f0fff
CW
94 $qParams = [
95 1 => [$params['event_id'], 'Integer'],
96 2 => [$label, 'String'],
97 ];
ca4329ca
JV
98
99 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
7ad3b4bd 100 if ($dao->fetch()) {
cf8f0fff 101 $lineItemParams = [
7ad3b4bd
JV
102 'price_field_id' => $dao->priceFieldID,
103 'price_field_value_id' => $dao->priceFieldValueID,
104 'entity_table' => 'civicrm_participant',
105 'entity_id' => $participant->id,
106 'label' => $label,
107 'qty' => $qty,
108 'participant_count' => 0,
109 'unit_price' => $dao->amount,
35671d00 110 'line_total' => $qty * $dao->amount,
cf8f0fff 111 ];
accc2d37 112 civicrm_api3('line_item', 'create', $lineItemParams);
7ad3b4bd
JV
113 }
114
6a488035
TO
115 }
116}
117
11e09c59 118/**
0aa0303c
EM
119 * Adjust Metadata for Create action.
120 *
121 * The metadata is used for setting defaults, documentation & validation.
6a488035 122 *
cf470720 123 * @param array $params
b081365f 124 * Array of parameters determined by getfields.
6a488035
TO
125 */
126function _civicrm_api3_participant_create_spec(&$params) {
127 $params['status_id']['api.default'] = "1";
128 $params['register_date']['api.default'] = "now";
129 $params['event_id']['api.required'] = 1;
130 $params['contact_id']['api.required'] = 1;
e7808bdd 131 // These are for the sake of search builder options - can be removed if that is fixed
cf8f0fff
CW
132 $params['role_id']['api.aliases'] = ['participant_role'];
133 $params['status_id']['api.aliases'] = ['participant_status'];
6a488035
TO
134}
135
136/**
9d32e6f7 137 * Retrieve a specific participant, given a set of input params.
6a488035 138 *
cf470720 139 * @param array $params
9d32e6f7 140 * input parameters.
6a488035 141 *
a6c01b45 142 * @return array
9d32e6f7 143 * array of properties, if error an array with an error id and error message
6a488035
TO
144 */
145function civicrm_api3_participant_get($params) {
82f7d8b2 146 $mode = CRM_Contact_BAO_Query::MODE_EVENT;
6a488035 147
244bbdd8 148 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, 'Participant');
6a488035 149
cf8f0fff 150 $participant = [];
6a488035 151 while ($dao->fetch()) {
a5e77b37 152 $query->convertToPseudoNames($dao, FALSE, TRUE);
6a488035 153 $participant[$dao->participant_id] = $query->store($dao);
82f7d8b2 154 //@todo - is this required - contribution & pledge use the same query but don't self-retrieve custom data
e9ff5391 155 _civicrm_api3_custom_data_get($participant[$dao->participant_id], CRM_Utils_Array::value('check_permissions', $params), 'Participant', $dao->participant_id, NULL);
6a488035
TO
156 }
157
244bbdd8 158 return civicrm_api3_create_success($participant, $params, 'Participant', 'get', $dao);
6a488035
TO
159}
160
11e09c59 161/**
9d32e6f7 162 * Adjust Metadata for Get action.
6a488035 163 *
0aa0303c
EM
164 * The metadata is used for setting defaults, documentation & validation.
165 *
cf470720 166 * @param array $params
b081365f 167 * Array of parameters determined by getfields.
6a488035
TO
168 */
169function _civicrm_api3_participant_get_spec(&$params) {
cf8f0fff 170 $params['participant_test'] = [
d142432b
EM
171 'api.default' => 0,
172 'title' => 'Get Test Participants',
173 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff 174 ];
6a488035
TO
175}
176
177/**
9d32e6f7 178 * Deletes an existing contact participant.
6a488035
TO
179 *
180 * This API is used for deleting a contact participant
181 *
cf470720
TO
182 * @param array $params
183 * Array containing Id of the contact participant to be deleted.
6a488035 184 *
77b97be7
EM
185 * @throws Exception
186 * @return array
6a488035
TO
187 */
188function civicrm_api3_participant_delete($params) {
189
190 $result = CRM_Event_BAO_Participant::deleteParticipant($params['id']);
191
192 if ($result) {
193 return civicrm_api3_create_success();
194 }
195 else {
196 throw new Exception('Error while deleting participant');
197 }
198}