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