commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / api / v3 / Participant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * This api exposes CiviCRM participant.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34 /**
35 * Create an Event Participant.
36 *
37 * @param array $params
38 * An associative array of name/value property values of civicrm_participant.
39 *
40 * @return array
41 * API result array
42 */
43 function civicrm_api3_participant_create($params) {
44 // Check that event id is not an template - should be done @ BAO layer.
45 if (!empty($params['event_id'])) {
46 $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
47 if (!empty($isTemplate)) {
48 return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
49 }
50 }
51
52 $values = $participant = array();
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
58 if (empty($params['price_set_id']) && empty($params['id']) && !empty($params['fee_level'])) {
59 _civicrm_api3_participant_createlineitem($params, $participantBAO);
60 }
61 _civicrm_api3_object_to_array($participantBAO, $participant[$participantBAO->id]);
62
63 return civicrm_api3_create_success($participant, $params, 'Participant', 'create', $participantBAO);
64 }
65
66 /**
67 * Create a default participant line item.
68 *
69 * @todo this should be done in the BAO not the api
70 *
71 * @param array $params
72 * @param $participant
73 *
74 * @throws \CiviCRM_API3_Exception
75 */
76 function _civicrm_api3_participant_createlineitem(&$params, $participant) {
77 // it is possible that a fee level contains information about multiple
78 // price field values.
79
80 $priceFieldValueDetails = CRM_Utils_Array::explodePadded(
81 $params["fee_level"]);
82
83 foreach ($priceFieldValueDetails as $detail) {
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
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
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'
102 LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id
103 LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id
104 where ps.id is not null and pfv.label = %2
105 ";
106
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);
113 if ($dao->fetch()) {
114 $lineItemParams = array(
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,
123 'line_total' => $qty * $dao->amount,
124 );
125 civicrm_api3('line_item', 'create', $lineItemParams);
126 }
127
128 }
129 }
130
131
132 /**
133 * Adjust Metadata for Create action.
134 *
135 * The metadata is used for setting defaults, documentation & validation.
136 *
137 * @param array $params
138 * Array of parameters determined by getfields.
139 */
140 function _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;
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');
148 }
149
150 /**
151 * Retrieve a specific participant, given a set of input params.
152 *
153 * @param array $params
154 * input parameters.
155 *
156 * @return array
157 * array of properties, if error an array with an error id and error message
158 */
159 function civicrm_api3_participant_get($params) {
160 $mode = CRM_Contact_BAO_Query::MODE_EVENT;
161
162 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, 'Participant');
163
164 $participant = array();
165 while ($dao->fetch()) {
166 $query->convertToPseudoNames($dao, FALSE, TRUE);
167 $participant[$dao->participant_id] = $query->store($dao);
168 //@todo - is this required - contribution & pledge use the same query but don't self-retrieve custom data
169 _civicrm_api3_custom_data_get($participant[$dao->participant_id], 'Participant', $dao->participant_id, NULL);
170 }
171
172 return civicrm_api3_create_success($participant, $params, 'Participant', 'get', $dao);
173 }
174
175 /**
176 * Adjust Metadata for Get action.
177 *
178 * The metadata is used for setting defaults, documentation & validation.
179 *
180 * @param array $params
181 * Array of parameters determined by getfields.
182 */
183 function _civicrm_api3_participant_get_spec(&$params) {
184 $params['participant_test'] = array(
185 'api.default' => 0,
186 'title' => 'Get Test Participants',
187 'type' => CRM_Utils_Type::T_BOOLEAN,
188 );
189 }
190
191 /**
192 * Deletes an existing contact participant.
193 *
194 * This API is used for deleting a contact participant
195 *
196 * @param array $params
197 * Array containing Id of the contact participant to be deleted.
198 *
199 * @throws Exception
200 * @return array
201 */
202 function civicrm_api3_participant_delete($params) {
203
204 $result = CRM_Event_BAO_Participant::deleteParticipant($params['id']);
205
206 if ($result) {
207 return civicrm_api3_create_success();
208 }
209 else {
210 throw new Exception('Error while deleting participant');
211 }
212 }