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