Merge pull request #15843 from totten/master-simplehead
[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
6a488035
TO
26 */
27function civicrm_api3_participant_create($params) {
9d32e6f7 28 // Check that event id is not an template - should be done @ BAO layer.
a7488080 29 if (!empty($params['event_id'])) {
6a488035
TO
30 $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
31 if (!empty($isTemplate)) {
3770b22a 32 return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
6a488035
TO
33 }
34 }
35
cf8f0fff 36 $values = $participant = [];
6a488035
TO
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
9b873358 42 if (empty($params['price_set_id']) && empty($params['id']) && !empty($params['fee_level'])) {
6a488035
TO
43 _civicrm_api3_participant_createlineitem($params, $participantBAO);
44 }
45 _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id]);
46
244bbdd8 47 return civicrm_api3_create_success($participant, $params, 'Participant', 'create', $participantBAO);
6a488035
TO
48}
49
11e09c59 50/**
22242c87 51 * Create a default participant line item.
9d32e6f7
EM
52 *
53 * @todo this should be done in the BAO not the api
54 *
d0997921 55 * @param array $params
645ee340 56 * @param $participant
9d32e6f7 57 *
645ee340 58 * @throws \CiviCRM_API3_Exception
6a488035 59 */
9b873358 60function _civicrm_api3_participant_createlineitem(&$params, $participant) {
7ad3b4bd
JV
61 // it is possible that a fee level contains information about multiple
62 // price field values.
63
ca4329ca 64 $priceFieldValueDetails = CRM_Utils_Array::explodePadded(
7ad3b4bd
JV
65 $params["fee_level"]);
66
22e263ad 67 foreach ($priceFieldValueDetails as $detail) {
7ad3b4bd
JV
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
7ad3b4bd
JV
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
ca4329ca 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'
7ad3b4bd 86 LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
43867728 87 LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
ca4329ca 88 where ps.id is not null and pfv.label = %2
7ad3b4bd
JV
89 ";
90
cf8f0fff
CW
91 $qParams = [
92 1 => [$params['event_id'], 'Integer'],
93 2 => [$label, 'String'],
94 ];
ca4329ca
JV
95
96 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
7ad3b4bd 97 if ($dao->fetch()) {
cf8f0fff 98 $lineItemParams = [
7ad3b4bd
JV
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,
35671d00 107 'line_total' => $qty * $dao->amount,
cf8f0fff 108 ];
accc2d37 109 civicrm_api3('line_item', 'create', $lineItemParams);
7ad3b4bd
JV
110 }
111
6a488035
TO
112 }
113}
114
11e09c59 115/**
0aa0303c
EM
116 * Adjust Metadata for Create action.
117 *
118 * The metadata is used for setting defaults, documentation & validation.
6a488035 119 *
cf470720 120 * @param array $params
b081365f 121 * Array of parameters determined by getfields.
6a488035
TO
122 */
123function _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;
e7808bdd 128 // These are for the sake of search builder options - can be removed if that is fixed
cf8f0fff
CW
129 $params['role_id']['api.aliases'] = ['participant_role'];
130 $params['status_id']['api.aliases'] = ['participant_status'];
6a488035
TO
131}
132
133/**
9d32e6f7 134 * Retrieve a specific participant, given a set of input params.
6a488035 135 *
cf470720 136 * @param array $params
9d32e6f7 137 * input parameters.
6a488035 138 *
a6c01b45 139 * @return array
9d32e6f7 140 * array of properties, if error an array with an error id and error message
6a488035
TO
141 */
142function civicrm_api3_participant_get($params) {
82f7d8b2 143 $mode = CRM_Contact_BAO_Query::MODE_EVENT;
6a488035 144
244bbdd8 145 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, 'Participant');
6a488035 146
cf8f0fff 147 $participant = [];
6a488035 148 while ($dao->fetch()) {
a5e77b37 149 $query->convertToPseudoNames($dao, FALSE, TRUE);
6a488035 150 $participant[$dao->participant_id] = $query->store($dao);
82f7d8b2 151 //@todo - is this required - contribution & pledge use the same query but don't self-retrieve custom data
e9ff5391 152 _civicrm_api3_custom_data_get($participant[$dao->participant_id], CRM_Utils_Array::value('check_permissions', $params), 'Participant', $dao->participant_id, NULL);
6a488035
TO
153 }
154
244bbdd8 155 return civicrm_api3_create_success($participant, $params, 'Participant', 'get', $dao);
6a488035
TO
156}
157
11e09c59 158/**
9d32e6f7 159 * Adjust Metadata for Get action.
6a488035 160 *
0aa0303c
EM
161 * The metadata is used for setting defaults, documentation & validation.
162 *
cf470720 163 * @param array $params
b081365f 164 * Array of parameters determined by getfields.
6a488035
TO
165 */
166function _civicrm_api3_participant_get_spec(&$params) {
cf8f0fff 167 $params['participant_test'] = [
d142432b
EM
168 'api.default' => 0,
169 'title' => 'Get Test Participants',
170 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff 171 ];
6a488035
TO
172}
173
174/**
9d32e6f7 175 * Deletes an existing contact participant.
6a488035
TO
176 *
177 * This API is used for deleting a contact participant
178 *
cf470720
TO
179 * @param array $params
180 * Array containing Id of the contact participant to be deleted.
6a488035 181 *
77b97be7
EM
182 * @throws Exception
183 * @return array
6a488035
TO
184 */
185function 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}