Merge pull request #16004 from civicrm/5.20
[civicrm-core.git] / api / v3 / Participant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM participant.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Create an Event Participant.
20 *
21 * @param array $params
22 * An associative array of name/value property values of civicrm_participant.
23 *
24 * @return array
25 * API result array
26 */
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.'));
33 }
34 }
35
36 $values = $participant = [];
37 _civicrm_api3_custom_format_params($params, $values, 'Participant');
38 $params = array_merge($values, $params);
39
40 $participantBAO = CRM_Event_BAO_Participant::create($params);
41
42 if (empty($params['price_set_id']) && empty($params['id']) && !empty($params['fee_level'])) {
43 _civicrm_api3_participant_createlineitem($params, $participantBAO);
44 }
45 _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id]);
46
47 return civicrm_api3_create_success($participant, $params, 'Participant', 'create', $participantBAO);
48 }
49
50 /**
51 * Create a default participant line item.
52 *
53 * @todo this should be done in the BAO not the api
54 *
55 * @param array $params
56 * @param $participant
57 *
58 * @throws \CiviCRM_API3_Exception
59 */
60 function _civicrm_api3_participant_createlineitem(&$params, $participant) {
61 // it is possible that a fee level contains information about multiple
62 // price field values.
63
64 $priceFieldValueDetails = CRM_Utils_Array::explodePadded(
65 $params["fee_level"]);
66
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.)
72
73 $qty = $matches[1];
74 preg_match('/^(.*) - [0-9]+$/', $detail, $matches);
75 $label = $matches[1];
76 }
77 else {
78 $label = $detail;
79 $qty = 1;
80 }
81
82 $sql = "
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
89 ";
90
91 $qParams = [
92 1 => [$params['event_id'], 'Integer'],
93 2 => [$label, 'String'],
94 ];
95
96 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
97 if ($dao->fetch()) {
98 $lineItemParams = [
99 'price_field_id' => $dao->priceFieldID,
100 'price_field_value_id' => $dao->priceFieldValueID,
101 'entity_table' => 'civicrm_participant',
102 'entity_id' => $participant->id,
103 'label' => $label,
104 'qty' => $qty,
105 'participant_count' => 0,
106 'unit_price' => $dao->amount,
107 'line_total' => $qty * $dao->amount,
108 ];
109 civicrm_api3('line_item', 'create', $lineItemParams);
110 }
111
112 }
113 }
114
115 /**
116 * Adjust Metadata for Create action.
117 *
118 * The metadata is used for setting defaults, documentation & validation.
119 *
120 * @param array $params
121 * Array of parameters determined by getfields.
122 */
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'];
131 }
132
133 /**
134 * Retrieve a specific participant, given a set of input params.
135 *
136 * @param array $params
137 * input parameters.
138 *
139 * @return array
140 * array of properties, if error an array with an error id and error message
141 */
142 function civicrm_api3_participant_get($params) {
143 $mode = CRM_Contact_BAO_Query::MODE_EVENT;
144
145 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, 'Participant');
146
147 $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);
153 }
154
155 return civicrm_api3_create_success($participant, $params, 'Participant', 'get', $dao);
156 }
157
158 /**
159 * Adjust Metadata for Get action.
160 *
161 * The metadata is used for setting defaults, documentation & validation.
162 *
163 * @param array $params
164 * Array of parameters determined by getfields.
165 */
166 function _civicrm_api3_participant_get_spec(&$params) {
167 $params['participant_test'] = [
168 'api.default' => 0,
169 'title' => 'Get Test Participants',
170 'type' => CRM_Utils_Type::T_BOOLEAN,
171 ];
172 }
173
174 /**
175 * Deletes an existing contact participant.
176 *
177 * This API is used for deleting a contact participant
178 *
179 * @param array $params
180 * Array containing Id of the contact participant to be deleted.
181 *
182 * @throws Exception
183 * @return array
184 */
185 function civicrm_api3_participant_delete($params) {
186
187 $result = CRM_Event_BAO_Participant::deleteParticipant($params['id']);
188
189 if ($result) {
190 return civicrm_api3_create_success();
191 }
192 else {
193 throw new Exception('Error while deleting participant');
194 }
195 }