CRM-16019 - Expose date/time format preferences to clientside
[civicrm-core.git] / api / v3 / Participant.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
accc2d37 52 $values = $participant = array();
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
ca4329ca
JV
107 $qParams = array(
108 1 => array($params['event_id'], 'Integer'),
109 2 => array($label, 'String'),
110 );
111
112 $dao = CRM_Core_DAO::executeQuery($sql, $qParams);
7ad3b4bd 113 if ($dao->fetch()) {
accc2d37 114 $lineItemParams = array(
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,
7ad3b4bd 124 );
accc2d37 125 civicrm_api3('line_item', 'create', $lineItemParams);
7ad3b4bd
JV
126 }
127
6a488035
TO
128 }
129}
130
131
11e09c59 132/**
0aa0303c
EM
133 * Adjust Metadata for Create action.
134 *
135 * The metadata is used for setting defaults, documentation & validation.
6a488035 136 *
cf470720 137 * @param array $params
b081365f 138 * Array of parameters determined by getfields.
6a488035
TO
139 */
140function _civicrm_api3_participant_create_spec(&$params) {
141 $params['status_id']['api.default'] = "1";
142 $params['register_date']['api.default'] = "now";
143 $params['event_id']['api.required'] = 1;
144 $params['contact_id']['api.required'] = 1;
e7808bdd
CW
145 // These are for the sake of search builder options - can be removed if that is fixed
146 $params['role_id']['api.aliases'] = array('participant_role');
147 $params['status_id']['api.aliases'] = array('participant_status');
6a488035
TO
148}
149
150/**
9d32e6f7 151 * Retrieve a specific participant, given a set of input params.
6a488035 152 *
cf470720 153 * @param array $params
9d32e6f7 154 * input parameters.
6a488035 155 *
a6c01b45 156 * @return array
9d32e6f7 157 * array of properties, if error an array with an error id and error message
6a488035
TO
158 */
159function civicrm_api3_participant_get($params) {
82f7d8b2 160 $mode = CRM_Contact_BAO_Query::MODE_EVENT;
6a488035 161
244bbdd8 162 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, 'Participant');
6a488035
TO
163
164 $participant = array();
165 while ($dao->fetch()) {
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
6a488035
TO
168 _civicrm_api3_custom_data_get($participant[$dao->participant_id], 'Participant', $dao->participant_id, NULL);
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) {
183 $params['participant_test']['api.default'] = 0;
4c41ecb2 184 $params['participant_test']['title'] = 'Get Test Participants';
6a488035
TO
185}
186
187/**
9d32e6f7 188 * Deletes an existing contact participant.
6a488035
TO
189 *
190 * This API is used for deleting a contact participant
191 *
cf470720
TO
192 * @param array $params
193 * Array containing Id of the contact participant to be deleted.
6a488035 194 *
77b97be7
EM
195 * @throws Exception
196 * @return array
6a488035
TO
197 */
198function civicrm_api3_participant_delete($params) {
199
200 $result = CRM_Event_BAO_Participant::deleteParticipant($params['id']);
201
202 if ($result) {
203 return civicrm_api3_create_success();
204 }
205 else {
206 throw new Exception('Error while deleting participant');
207 }
208}