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