Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 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 | * | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * This class generates form components for processing a participation | |
39 | * in an event | |
40 | */ | |
6bd6d108 | 41 | class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment { |
6a488035 | 42 | |
96f50de2 CW |
43 | public $useLivePageJS = TRUE; |
44 | ||
6a488035 | 45 | /** |
100fef9d | 46 | * The values for the contribution db object |
6a488035 TO |
47 | * |
48 | * @var array | |
49 | * @protected | |
50 | */ | |
51 | public $_values; | |
52 | ||
53 | /** | |
100fef9d | 54 | * The values for the quickconfig for priceset |
6a488035 TO |
55 | * |
56 | * @var boolean | |
57 | * @protected | |
58 | */ | |
59 | public $_quickConfig = NULL; | |
60 | ||
61 | /** | |
62 | * Price Set ID, if the new price set method is used | |
63 | * | |
64 | * @var int | |
65 | * @protected | |
66 | */ | |
67 | public $_priceSetId; | |
68 | ||
69 | /** | |
70 | * Array of fields for the price set | |
71 | * | |
72 | * @var array | |
73 | * @protected | |
74 | */ | |
75 | public $_priceSet; | |
76 | ||
77 | /** | |
100fef9d | 78 | * The id of the participation that we are proceessing |
6a488035 TO |
79 | * |
80 | * @var int | |
81 | * @protected | |
82 | */ | |
83 | public $_id; | |
84 | ||
85 | /** | |
100fef9d | 86 | * The id of the note |
6a488035 TO |
87 | * |
88 | * @var int | |
89 | * @protected | |
90 | */ | |
91 | protected $_noteId = NULL; | |
92 | ||
93 | /** | |
100fef9d | 94 | * The id of the contact associated with this participation |
6a488035 TO |
95 | * |
96 | * @var int | |
97 | * @protected | |
98 | */ | |
99 | public $_contactId; | |
100 | ||
101 | /** | |
100fef9d | 102 | * Array of event values |
6a488035 TO |
103 | * |
104 | * @var array | |
105 | * @protected | |
106 | */ | |
107 | protected $_event; | |
108 | ||
109 | /** | |
110 | * Are we operating in "single mode", i.e. adding / editing only | |
111 | * one participant record, or is this a batch add operation | |
112 | * | |
113 | * @var boolean | |
114 | */ | |
115 | public $_single = FALSE; | |
116 | ||
117 | /** | |
118 | * If event is paid or unpaid | |
119 | */ | |
120 | public $_isPaidEvent; | |
121 | ||
122 | /** | |
123 | * Page action | |
124 | */ | |
125 | public $_action; | |
126 | ||
127 | /** | |
128 | * Role Id | |
129 | */ | |
130 | protected $_roleId = NULL; | |
131 | ||
132 | /** | |
133 | * Event Type Id | |
134 | */ | |
135 | protected $_eventTypeId = NULL; | |
136 | ||
137 | /** | |
100fef9d | 138 | * Participant status Id |
6a488035 TO |
139 | */ |
140 | protected $_statusId = NULL; | |
141 | ||
142 | /** | |
100fef9d | 143 | * Cache all the participant statuses |
6a488035 TO |
144 | */ |
145 | protected $_participantStatuses; | |
146 | ||
147 | /** | |
100fef9d | 148 | * Participant mode |
6a488035 TO |
149 | */ |
150 | public $_mode = NULL; | |
151 | ||
152 | /** | |
100fef9d | 153 | * Event ID preselect |
6a488035 TO |
154 | */ |
155 | public $_eID = NULL; | |
156 | ||
c490a46a CW |
157 | /** |
158 | * Line Item for Price Set | |
159 | */ | |
6a488035 | 160 | public $_lineItem = NULL; |
6a488035 | 161 | |
c490a46a CW |
162 | /* |
163 | * Contribution mode for event registration for offline mode | |
164 | */ | |
6a488035 TO |
165 | public $_contributeMode = 'direct'; |
166 | ||
167 | public $_online; | |
168 | ||
169 | /** | |
100fef9d | 170 | * Store id of role custom data type ( option value ) |
6a488035 TO |
171 | */ |
172 | protected $_roleCustomDataTypeID; | |
173 | ||
174 | /** | |
100fef9d | 175 | * Store id of event Name custom data type ( option value) |
6a488035 TO |
176 | */ |
177 | protected $_eventNameCustomDataTypeID; | |
178 | ||
179 | /** | |
100fef9d | 180 | * Selected discount id |
6a488035 TO |
181 | */ |
182 | public $_originalDiscountId = NULL; | |
183 | ||
184 | /** | |
100fef9d | 185 | * Event id |
6a488035 TO |
186 | */ |
187 | public $_eventId = NULL; | |
188 | ||
189 | /** | |
100fef9d | 190 | * Id of payment, if any |
6a488035 TO |
191 | */ |
192 | public $_paymentId = NULL; | |
193 | ||
9a4df24c EM |
194 | /** |
195 | * @todo add explanatory note about this | |
196 | * @var null | |
197 | */ | |
198 | public $_onlinePendingContributionId = NULL; | |
199 | ||
6a488035 | 200 | /** |
100fef9d | 201 | * Set variables up before form is built |
6a488035 TO |
202 | * |
203 | * @return void | |
204 | * @access public | |
205 | */ | |
206 | public function preProcess() { | |
207 | $this->_showFeeBlock = CRM_Utils_Array::value('eventId', $_GET); | |
208 | $this->assign('showFeeBlock', FALSE); | |
209 | $this->assign('feeBlockPaid', FALSE); | |
210 | ||
211 | $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
212 | $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this); | |
213 | $this->_eID = CRM_Utils_Request::retrieve('eid', 'Positive', $this); | |
214 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
215 | $this->assign('context', $this->_context); | |
216 | ||
217 | if ($this->_contactId) { | |
218 | $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId); | |
219 | $this->assign('displayName', $displayName); | |
e2046b33 CW |
220 | $this->setPageTitle(ts('Event Registration for %1', array(1 => $displayName))); |
221 | } | |
222 | else { | |
223 | $this->setPageTitle(ts('Event Registration')); | |
6a488035 TO |
224 | } |
225 | ||
226 | // check the current path, if search based, then dont get participantID | |
227 | // CRM-5792 | |
228 | $path = CRM_Utils_System::currentPath(); | |
0edbaec6 JM |
229 | if ( |
230 | strpos($path, 'civicrm/contact/search') === 0 || | |
231 | strpos($path, 'civicrm/group/search') === 0 | |
232 | ) { | |
6a488035 TO |
233 | $this->_id = NULL; |
234 | } | |
235 | else { | |
236 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
237 | } | |
238 | ||
239 | if ($this->_id) { | |
29c61b58 | 240 | $this->assign('participantId', $this->_id); |
29c61b58 | 241 | |
6a488035 TO |
242 | $this->_paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', |
243 | $this->_id, 'id', 'participant_id' | |
244 | ); | |
0b2b58ea PJ |
245 | |
246 | $this->assign('hasPayment', $this->_paymentId); | |
247 | ||
a4e0d7b5 DG |
248 | // CRM-12615 - Get payment information from the primary registration |
249 | if ((!$this->_paymentId) && ($this->_action == CRM_Core_Action::UPDATE)) { | |
250 | $registered_by_id = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', | |
251 | $this->_id, 'registered_by_id', 'id' | |
252 | ); | |
253 | if ($registered_by_id) { | |
254 | $this->_paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
255 | $registered_by_id, 'id', 'participant_id' | |
256 | ); | |
257 | $this->assign('registeredByParticipantId', $registered_by_id); | |
258 | } | |
259 | } | |
6a488035 TO |
260 | } |
261 | ||
262 | // get the option value for custom data type | |
263 | $this->_roleCustomDataTypeID = CRM_Core_OptionGroup::getValue('custom_data_type', 'ParticipantRole', 'name'); | |
264 | $this->_eventNameCustomDataTypeID = CRM_Core_OptionGroup::getValue('custom_data_type', 'ParticipantEventName', 'name'); | |
265 | $this->_eventTypeCustomDataTypeID = CRM_Core_OptionGroup::getValue('custom_data_type', 'ParticipantEventType', 'name'); | |
266 | $this->assign('roleCustomDataTypeID', $this->_roleCustomDataTypeID); | |
267 | $this->assign('eventNameCustomDataTypeID', $this->_eventNameCustomDataTypeID); | |
268 | $this->assign('eventTypeCustomDataTypeID', $this->_eventTypeCustomDataTypeID); | |
269 | ||
270 | if ($this->_mode) { | |
271 | $this->assign('participantMode', $this->_mode); | |
6bd6d108 | 272 | $this->assignPaymentRelatedVariables(); |
6a488035 TO |
273 | } |
274 | ||
275 | if ($this->_showFeeBlock) { | |
276 | $this->assign('showFeeBlock', TRUE); | |
277 | $isMonetary = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_showFeeBlock, 'is_monetary'); | |
278 | if ($isMonetary) { | |
279 | $this->assign('feeBlockPaid', TRUE); | |
280 | } | |
281 | return CRM_Event_Form_EventFees::preProcess($this); | |
282 | } | |
283 | ||
284 | //custom data related code | |
285 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
286 | $this->assign('cdType', FALSE); | |
287 | if ($this->_cdType) { | |
288 | $this->assign('cdType', TRUE); | |
63dbed83 | 289 | return CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, NULL, NULL, NULL, TRUE); |
6a488035 TO |
290 | } |
291 | ||
292 | //check the mode when this form is called either single or as | |
293 | //search task action | |
294 | if ($this->_id || $this->_contactId || $this->_context == 'standalone') { | |
295 | $this->_single = TRUE; | |
296 | $this->assign('urlPath', 'civicrm/contact/view/participant'); | |
297 | if (!$this->_id && !$this->_contactId) { | |
298 | $breadCrumbs = array(array('title' => ts('CiviEvent Dashboard'), | |
299 | 'url' => CRM_Utils_System::url('civicrm/event', 'reset=1'), | |
300 | )); | |
301 | ||
302 | CRM_Utils_System::appendBreadCrumb($breadCrumbs); | |
303 | } | |
304 | } | |
305 | else { | |
306 | //set the appropriate action | |
307 | $context = $this->get('context'); | |
308 | $urlString = 'civicrm/contact/search'; | |
309 | $this->_action = CRM_Core_Action::BASIC; | |
310 | switch ($context) { | |
311 | case 'advanced': | |
312 | $urlString = 'civicrm/contact/search/advanced'; | |
313 | $this->_action = CRM_Core_Action::ADVANCED; | |
314 | break; | |
315 | ||
316 | case 'builder': | |
317 | $urlString = 'civicrm/contact/search/builder'; | |
318 | $this->_action = CRM_Core_Action::PROFILE; | |
319 | break; | |
320 | ||
321 | case 'basic': | |
322 | $urlString = 'civicrm/contact/search/basic'; | |
323 | $this->_action = CRM_Core_Action::BASIC; | |
324 | break; | |
325 | ||
326 | case 'custom': | |
327 | $urlString = 'civicrm/contact/search/custom'; | |
328 | $this->_action = CRM_Core_Action::COPY; | |
329 | break; | |
330 | } | |
331 | parent::preProcess(); | |
332 | ||
333 | $this->_single = FALSE; | |
334 | $this->_contactId = NULL; | |
335 | ||
336 | //set ajax path, this used for custom data building | |
337 | $this->assign('urlPath', $urlString); | |
338 | $this->assign('urlPathVar', "_qf_Participant_display=true&qfKey={$this->controller->_key}"); | |
339 | } | |
340 | ||
341 | $this->assign('single', $this->_single); | |
342 | ||
343 | if (!$this->_id) { | |
344 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
345 | } | |
346 | $this->assign('action', $this->_action); | |
347 | ||
348 | // check for edit permission | |
349 | if (!CRM_Core_Permission::checkActionPermission('CiviEvent', $this->_action)) { | |
0499b0ad | 350 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
351 | } |
352 | ||
353 | if ($this->_action & CRM_Core_Action::DELETE) { | |
354 | // check delete permission for contribution | |
355 | if ($this->_id && $this->_paymentId && !CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { | |
356 | CRM_Core_Error::fatal(ts("This Participant is linked to a contribution. You must have 'delete in CiviContribute' permission in order to delete this record.")); | |
357 | } | |
358 | return; | |
359 | } | |
360 | ||
361 | if ($this->_id) { | |
362 | // assign participant id to the template | |
363 | $this->assign('participantId', $this->_id); | |
364 | $this->_roleId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'role_id'); | |
365 | } | |
366 | ||
367 | // when fee amount is included in form | |
8cc574cf | 368 | if (!empty($_POST['hidden_feeblock']) || !empty($_POST['send_receipt'])) { |
6a488035 TO |
369 | CRM_Event_Form_EventFees::preProcess($this); |
370 | CRM_Event_Form_EventFees::buildQuickForm($this); | |
371 | CRM_Event_Form_EventFees::setDefaultValues($this); | |
372 | } | |
373 | ||
374 | // when custom data is included in this page | |
a7488080 | 375 | if (!empty($_POST['hidden_custom'])) { |
7df021e5 | 376 | // Custom data of type participant role |
377 | // Note: Some earlier commits imply $_POST['role_id'] could be a comma separated string, | |
378 | // not sure if that ever really happens | |
a7488080 | 379 | if (!empty($_POST['role_id'])) { |
7df021e5 | 380 | foreach ($_POST['role_id'] as $roleID) { |
381 | CRM_Custom_Form_CustomData::preProcess($this, $this->_roleCustomDataTypeID, $roleID, 1, 'Participant', $this->_id); | |
6a488035 TO |
382 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
383 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
384 | } | |
385 | } | |
386 | ||
387 | //custom data of type participant event | |
388 | CRM_Custom_Form_CustomData::preProcess($this, $this->_eventNameCustomDataTypeID, $_POST['event_id'], 1, 'Participant', $this->_id); | |
389 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
390 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
391 | ||
392 | // custom data of type participant event type | |
393 | $eventTypeId = NULL; | |
394 | if ($eventId = CRM_Utils_Array::value('event_id', $_POST)) { | |
395 | $eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'event_type_id', 'id'); | |
396 | } | |
397 | CRM_Custom_Form_CustomData::preProcess($this, $this->_eventTypeCustomDataTypeID, $eventTypeId, | |
398 | 1, 'Participant', $this->_id | |
399 | ); | |
400 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
401 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
402 | ||
403 | //custom data of type participant, ( we 'null' to reset subType and subName) | |
404 | CRM_Custom_Form_CustomData::preProcess($this, 'null', 'null', 1, 'Participant', $this->_id); | |
405 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
406 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
407 | } | |
408 | ||
409 | // CRM-4395, get the online pending contribution id. | |
410 | $this->_onlinePendingContributionId = NULL; | |
411 | if (!$this->_mode && $this->_id && ($this->_action & CRM_Core_Action::UPDATE)) { | |
412 | $this->_onlinePendingContributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($this->_id, | |
413 | 'Event' | |
414 | ); | |
415 | } | |
416 | $this->set('onlinePendingContributionId', $this->_onlinePendingContributionId); | |
6a488035 TO |
417 | } |
418 | ||
419 | /** | |
420 | * This function sets the default values for the form in edit/view mode | |
421 | * the default values are retrieved from the database | |
422 | * | |
423 | * @access public | |
424 | * | |
355ba699 | 425 | * @return void |
6a488035 TO |
426 | */ |
427 | public function setDefaultValues() { | |
428 | if ($this->_showFeeBlock) { | |
429 | return CRM_Event_Form_EventFees::setDefaultValues($this); | |
430 | } | |
431 | ||
432 | if ($this->_cdType) { | |
433 | return CRM_Custom_Form_CustomData::setDefaultValues($this); | |
434 | } | |
435 | ||
436 | $defaults = array(); | |
437 | ||
438 | if ($this->_action & CRM_Core_Action::DELETE) { | |
439 | return $defaults; | |
440 | } | |
441 | ||
442 | if ($this->_id) { | |
443 | $ids = array(); | |
444 | $params = array('id' => $this->_id); | |
445 | ||
446 | CRM_Event_BAO_Participant::getValues($params, $defaults, $ids); | |
447 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
448 | if ($defaults[$this->_id]['role_id']) { | |
bdc20176 | 449 | $roleIDs = explode($sep, $defaults[$this->_id]['role_id']); |
6a488035 TO |
450 | } |
451 | $this->_contactId = $defaults[$this->_id]['contact_id']; | |
452 | $this->_statusId = $defaults[$this->_id]['participant_status_id']; | |
453 | ||
454 | //set defaults for note | |
455 | $noteDetails = CRM_Core_BAO_Note::getNote($this->_id, 'civicrm_participant'); | |
456 | $defaults[$this->_id]['note'] = array_pop($noteDetails); | |
457 | ||
458 | // Check if this is a primaryParticipant (registered for others) and retrieve additional participants if true (CRM-4859) | |
459 | if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { | |
460 | $this->assign('additionalParticipants', CRM_Event_BAO_Participant::getAdditionalParticipants($this->_id)); | |
461 | } | |
462 | ||
463 | // Get registered_by contact ID and display_name if participant was registered by someone else (CRM-4859) | |
a7488080 | 464 | if (!empty($defaults[$this->_id]['participant_registered_by_id'])) { |
6a488035 TO |
465 | $registered_by_contact_id = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', |
466 | $defaults[$this->_id]['participant_registered_by_id'], | |
467 | 'contact_id', 'id' | |
468 | ); | |
469 | $this->assign('participant_registered_by_id', $defaults[$this->_id]['participant_registered_by_id']); | |
470 | $this->assign('registered_by_contact_id', $registered_by_contact_id); | |
471 | $this->assign('registered_by_display_name', CRM_Contact_BAO_Contact::displayName($registered_by_contact_id)); | |
472 | } | |
473 | } | |
474 | ||
475 | if ($this->_action & (CRM_Core_Action::VIEW | CRM_Core_Action::BROWSE)) { | |
476 | $inactiveNeeded = TRUE; | |
477 | $viewMode = TRUE; | |
478 | } | |
479 | else { | |
480 | $viewMode = FALSE; | |
481 | $inactiveNeeded = FALSE; | |
482 | } | |
483 | ||
484 | //setting default register date | |
485 | if ($this->_action == CRM_Core_Action::ADD) { | |
486 | $statuses = array_flip($this->_participantStatuses); | |
487 | $defaults[$this->_id]['status_id'] = CRM_Utils_Array::value(ts('Registered'), $statuses); | |
a7488080 | 488 | if (!empty($defaults[$this->_id]['event_id'])) { |
6a488035 TO |
489 | $contributionTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', |
490 | $defaults[$this->_id]['event_id'], | |
bdc20176 | 491 | 'financial_type_id' |
d96cf288 | 492 | ); |
6a488035 | 493 | if ($contributionTypeId) { |
d96cf288 | 494 | $defaults[$this->_id]['financial_type_id'] = $contributionTypeId; |
6a488035 TO |
495 | } |
496 | } | |
497 | ||
498 | if ($this->_mode) { | |
499 | $fields["email-{$this->_bltID}"] = 1; | |
500 | $fields['email-Primary'] = 1; | |
501 | ||
502 | if ($this->_contactId) { | |
503 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactId, $fields, $defaults); | |
504 | } | |
505 | ||
506 | if (empty($defaults["email-{$this->_bltID}"]) && | |
507 | !empty($defaults['email-Primary']) | |
508 | ) { | |
509 | $defaults[$this->_id]["email-{$this->_bltID}"] = $defaults['email-Primary']; | |
510 | } | |
511 | } | |
512 | ||
513 | $submittedRole = $this->getElementValue('role_id'); | |
a7488080 | 514 | if (!empty($submittedRole[0])) { |
6a488035 TO |
515 | $roleID = $submittedRole[0]; |
516 | } | |
517 | $submittedEvent = $this->getElementValue('event_id'); | |
a4e49a48 | 518 | if (!empty($submittedEvent[0])) { |
6a488035 TO |
519 | $eventID = $submittedEvent[0]; |
520 | } | |
521 | } | |
522 | else { | |
523 | $defaults[$this->_id]['record_contribution'] = 0; | |
524 | ||
525 | if ($defaults[$this->_id]['participant_is_pay_later']) { | |
526 | $this->assign('participant_is_pay_later', TRUE); | |
527 | } | |
528 | ||
529 | $this->assign('participant_status_id', $defaults[$this->_id]['participant_status_id']); | |
6a488035 TO |
530 | $eventID = $defaults[$this->_id]['event_id']; |
531 | ||
532 | $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); | |
533 | ||
534 | $this->_discountId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'discount_id'); | |
535 | if ($this->_discountId) { | |
536 | $this->set('discountId', $this->_discountId); | |
537 | } | |
538 | } | |
539 | ||
540 | list($defaults[$this->_id]['register_date'], | |
541 | $defaults[$this->_id]['register_date_time'] | |
542 | ) = CRM_Utils_Date::setDateDefaults( | |
543 | CRM_Utils_Array::value('register_date', $defaults[$this->_id]), 'activityDateTime' | |
544 | ); | |
545 | ||
546 | //assign event and role id, this is needed for Custom data building | |
547 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
a7488080 | 548 | if (!empty($defaults[$this->_id]['participant_role_id'])) { |
6a488035 TO |
549 | $roleIDs = explode($sep, $defaults[$this->_id]['participant_role_id']); |
550 | } | |
6a488035 TO |
551 | if (isset($_POST['event_id'])) { |
552 | $eventID = $_POST['event_id']; | |
bdc20176 PJ |
553 | } |
554 | ||
555 | if($this->_eID) { | |
556 | $eventID = $this->_eID; | |
557 | //@todo - rationalise the $this->_eID with $POST['event_id'], $this->_eid is set when eid=x is in the url | |
558 | $roleID = CRM_Core_DAO::getFieldValue( | |
559 | 'CRM_Event_DAO_Event', | |
560 | $this->_eID, | |
561 | 'default_role_id' | |
562 | ); | |
563 | if(empty($roleIDs)) { | |
564 | $roleIDs = (array) $defaults[$this->_id]['participant_role_id'] = $roleID; | |
6a488035 | 565 | } |
bdc20176 PJ |
566 | $defaults[$this->_id]['event_id'] = $eventID; |
567 | } | |
568 | if (!empty($eventID)) { | |
569 | $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); | |
570 | } | |
571 | //these should take precedence so we state them last | |
572 | $urlRoleIDS = CRM_Utils_Request::retrieve('roles', 'String'); | |
573 | if($urlRoleIDS) { | |
574 | $roleIDs = explode(',', $urlRoleIDS); | |
575 | } | |
576 | if (isset($roleIDs)) { | |
cd120ea0 | 577 | $defaults[$this->_id]['role_id'] = implode(',', $roleIDs); |
6a488035 TO |
578 | } |
579 | ||
580 | if (isset($eventID)) { | |
581 | $this->assign('eventID', $eventID); | |
582 | $this->set('eventId', $eventID); | |
583 | } | |
584 | ||
585 | if (isset($this->_eventTypeId)) { | |
586 | $this->assign('eventTypeID', $this->_eventTypeId); | |
587 | } | |
588 | ||
589 | $this->assign('event_is_test', CRM_Utils_Array::value('event_is_test', $defaults[$this->_id])); | |
590 | return $defaults[$this->_id]; | |
591 | } | |
592 | ||
593 | /** | |
c490a46a | 594 | * Build the form object |
6a488035 | 595 | * |
355ba699 | 596 | * @return void |
6a488035 TO |
597 | * @access public |
598 | */ | |
599 | public function buildQuickForm() { | |
e8cf3013 PJ |
600 | $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); |
601 | $partiallyPaidStatusId = array_search('Partially paid', $participantStatuses); | |
81f3d017 | 602 | $this->assign('partiallyPaidStatusId', $partiallyPaidStatusId); |
e8cf3013 | 603 | |
6a488035 TO |
604 | if ($this->_showFeeBlock) { |
605 | return CRM_Event_Form_EventFees::buildQuickForm($this); | |
606 | } | |
607 | ||
608 | if ($this->_cdType) { | |
609 | return CRM_Custom_Form_CustomData::buildQuickForm($this); | |
610 | } | |
611 | ||
612 | //need to assign custom data type to the template | |
613 | $this->assign('customDataType', 'Participant'); | |
614 | ||
615 | $this->applyFilter('__ALL__', 'trim'); | |
616 | ||
617 | if ($this->_action & CRM_Core_Action::DELETE) { | |
618 | if ($this->_single) { | |
619 | $additionalParticipant = count(CRM_Event_BAO_Event::buildCustomProfile($this->_id, | |
620 | NULL, | |
621 | $this->_contactId, | |
622 | FALSE, | |
623 | TRUE | |
624 | )) - 1; | |
625 | if ($additionalParticipant) { | |
626 | $deleteParticipants = array(1 => ts('Delete this participant record along with associated participant record(s).'), | |
627 | 2 => ts('Delete only this participant record.'), | |
628 | ); | |
629 | $this->addRadio('delete_participant', NULL, $deleteParticipants, NULL, '<br />'); | |
630 | $this->setDefaults(array('delete_participant' => 1)); | |
631 | $this->assign('additionalParticipant', $additionalParticipant); | |
632 | } | |
633 | } | |
634 | $this->addButtons(array( | |
635 | array( | |
636 | 'type' => 'next', | |
637 | 'name' => ts('Delete'), | |
638 | 'spacing' => ' ', | |
639 | 'isDefault' => TRUE, | |
640 | ), | |
641 | array( | |
642 | 'type' => 'cancel', | |
643 | 'name' => ts('Cancel'), | |
644 | ), | |
645 | ) | |
646 | ); | |
647 | return; | |
648 | } | |
649 | ||
650 | ||
01bfdd10 CW |
651 | if ($this->_single && $this->_context == 'standalone') { |
652 | $this->addEntityRef('contact_id', ts('Contact'), array('create' => TRUE, 'api' => array('extra' => array('email'))), TRUE); | |
6a488035 | 653 | } |
6a488035 | 654 | |
1beb268a CW |
655 | $eventFieldParams = array( |
656 | 'entity' => 'event', | |
657 | 'select' => array('minimumInputLength' => 0), | |
658 | 'api' => array( | |
659 | 'extra' => array('campaign_id', 'default_role_id', 'event_type_id'), | |
660 | ) | |
661 | ); | |
6a488035 TO |
662 | |
663 | if ($this->_mode) { | |
1beb268a CW |
664 | // exclude events which are not monetary when credit card registration is used |
665 | $eventFieldParams['api']['params']['is_monetary'] = 1; | |
6a488035 TO |
666 | $this->add('select', 'payment_processor_id', ts('Payment Processor'), $this->_processors, TRUE); |
667 | } | |
668 | ||
1beb268a | 669 | $element = $this->addEntityRef('event_id', ts('Event'), $eventFieldParams, TRUE); |
6a488035 TO |
670 | |
671 | //frozen the field fix for CRM-4171 | |
672 | if ($this->_action & CRM_Core_Action::UPDATE && $this->_id) { | |
673 | if (CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
674 | $this->_id, 'contribution_id', 'participant_id' | |
675 | )) { | |
676 | $element->freeze(); | |
677 | } | |
678 | } | |
679 | ||
680 | //CRM-7362 --add campaigns. | |
681 | $campaignId = NULL; | |
682 | if ($this->_id) { | |
683 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'campaign_id'); | |
684 | } | |
685 | if (!$campaignId) { | |
686 | $eventId = CRM_Utils_Request::retrieve('eid', 'Positive', $this); | |
687 | if ($eventId) { | |
688 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'campaign_id'); | |
689 | } | |
690 | } | |
691 | CRM_Campaign_BAO_Campaign::addCampaign($this, $campaignId); | |
692 | ||
693 | $this->addDateTime('register_date', ts('Registration Date'), TRUE, array('formatType' => 'activityDateTime')); | |
694 | ||
695 | if ($this->_id) { | |
696 | $this->assign('entityID', $this->_id); | |
697 | } | |
698 | ||
cd120ea0 | 699 | $this->addSelect('role_id', array('multiple' => TRUE, 'class' => 'huge'), TRUE); |
6a488035 TO |
700 | |
701 | // CRM-4395 | |
702 | $checkCancelledJs = array('onchange' => "return sendNotification( );"); | |
703 | $confirmJS = NULL; | |
704 | if ($this->_onlinePendingContributionId) { | |
705 | $cancelledparticipantStatusId = array_search('Cancelled', CRM_Event_PseudoConstant::participantStatus()); | |
706 | $cancelledContributionStatusId = array_search('Cancelled', | |
707 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
708 | ); | |
709 | $checkCancelledJs = array( | |
710 | 'onchange' => | |
711 | "checkCancelled( this.value, {$cancelledparticipantStatusId},{$cancelledContributionStatusId});", | |
712 | ); | |
713 | ||
714 | $participantStatusId = array_search('Pending from pay later', | |
715 | CRM_Event_PseudoConstant::participantStatus() | |
716 | ); | |
717 | $contributionStatusId = array_search('Completed', | |
718 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
719 | ); | |
720 | $confirmJS = array('onclick' => "return confirmStatus( {$participantStatusId}, {$contributionStatusId} );"); | |
721 | } | |
722 | ||
453ed6d1 KJ |
723 | // get the participant status names to build special status array which is used to show notification |
724 | // checkbox below participant status select | |
725 | $participantStatusName = CRM_Event_PseudoConstant::participantStatus(); | |
726 | $notificationStatuses = array( | |
727 | 'Cancelled', | |
728 | 'Pending from waitlist', | |
729 | 'Pending from approval', | |
730 | 'Expired', | |
731 | ); | |
732 | ||
733 | // get the required status and then implode only ids | |
734 | $notificationStatusIds = implode(',', array_keys(array_intersect($participantStatusName, $notificationStatuses))); | |
735 | $this->assign('notificationStatusIds', $notificationStatusIds); | |
736 | ||
f76b27fe | 737 | $this->_participantStatuses = $statusOptions = CRM_Event_BAO_Participant::buildOptions('status_id', 'create'); |
f6bae84f | 738 | |
f76b27fe CW |
739 | // Only show refund status when editing |
740 | if ($this->_action & CRM_Core_Action::ADD) { | |
741 | $pendingRefundStatusId = array_search('Pending refund', $participantStatusName); | |
742 | if ($pendingRefundStatusId) { | |
743 | unset($statusOptions[$pendingRefundStatusId]); | |
744 | } | |
745 | } | |
746 | ||
747 | $this->addSelect('status_id', $checkCancelledJs + array('options' => $statusOptions, 'option_url' => 'civicrm/admin/participant_status'), TRUE); | |
6a488035 TO |
748 | |
749 | $this->addElement('checkbox', 'is_notify', ts('Send Notification'), NULL); | |
750 | ||
751 | $this->add('text', 'source', ts('Event Source')); | |
752 | $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note'); | |
753 | $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']); | |
754 | ||
755 | $buttons[] = array( | |
756 | 'type' => 'upload', | |
757 | 'name' => ts('Save'), | |
758 | 'isDefault' => TRUE, | |
759 | 'js' => $confirmJS, | |
760 | ); | |
761 | ||
762 | $path = CRM_Utils_System::currentPath(); | |
239a5027 PJ |
763 | $excludeForPaths = array( |
764 | 'civicrm/contact/search', | |
765 | 'civicrm/group/search' | |
766 | ); | |
767 | if (!in_array($path, $excludeForPaths)) { | |
6a488035 TO |
768 | $buttons[] = array( |
769 | 'type' => 'upload', | |
770 | 'name' => ts('Save and New'), | |
771 | 'subName' => 'new', | |
772 | 'js' => $confirmJS, | |
773 | ); | |
774 | } | |
239a5027 | 775 | |
6a488035 TO |
776 | $buttons[] = array( |
777 | 'type' => 'cancel', | |
778 | 'name' => ts('Cancel'), | |
779 | ); | |
780 | ||
781 | $this->addButtons($buttons); | |
782 | if ($this->_action == CRM_Core_Action::VIEW) { | |
783 | $this->freeze(); | |
784 | } | |
785 | } | |
786 | ||
787 | /** | |
788 | * Add local and global form rules | |
789 | * | |
790 | * @access protected | |
791 | * | |
792 | * @return void | |
793 | */ | |
794 | function addRules() { | |
795 | $this->addFormRule(array('CRM_Event_Form_Participant', 'formRule'), $this); | |
796 | } | |
797 | ||
798 | /** | |
100fef9d | 799 | * Global validation rules for the form |
6a488035 | 800 | * |
c490a46a | 801 | * @param array $values posted values of the form |
2a6da8d7 EM |
802 | * @param $files |
803 | * @param $self | |
804 | * | |
6a488035 TO |
805 | * @return array list of errors to be posted back to the form |
806 | * @static | |
807 | * @access public | |
808 | */ | |
809 | static function formRule($values, $files, $self) { | |
810 | // If $values['_qf_Participant_next'] is Delete or | |
811 | // $values['event_id'] is empty, then return | |
812 | // instead of proceeding further. | |
813 | ||
814 | if ((CRM_Utils_Array::value('_qf_Participant_next', $values) == 'Delete') || | |
815 | (!$values['event_id']) | |
816 | ) { | |
817 | return TRUE; | |
818 | } | |
819 | ||
820 | $errorMsg = array(); | |
6a488035 | 821 | |
a7488080 | 822 | if (!empty($values['payment_processor_id'])) { |
6a488035 | 823 | // make sure that credit card number and cvv are valid |
7cb3d4f0 | 824 | CRM_Core_Payment_Form::validateCreditCard($values, $errorMsg); |
6a488035 TO |
825 | } |
826 | ||
a7488080 CW |
827 | if (!empty($values['record_contribution'])) { |
828 | if (empty($values['financial_type_id'])) { | |
d96cf288 DG |
829 | $errorMsg['financial_type_id'] = ts('Please enter the associated Financial Type'); |
830 | } | |
a7488080 | 831 | if (empty($values['payment_instrument_id'])) { |
d96cf288 DG |
832 | $errorMsg['payment_instrument_id'] = ts('Paid By is a required field.'); |
833 | } | |
6a488035 TO |
834 | } |
835 | ||
836 | // validate contribution status for 'Failed'. | |
8cc574cf | 837 | if ($self->_onlinePendingContributionId && !empty($values['record_contribution']) && |
6a488035 TO |
838 | (CRM_Utils_Array::value('contribution_status_id', $values) == |
839 | array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')) | |
840 | ) | |
841 | ) { | |
842 | $errorMsg['contribution_status_id'] = ts('Please select a valid payment status before updating.'); | |
843 | } | |
844 | ||
845 | // do the amount validations. | |
846 | //skip for update mode since amount is freeze, CRM-6052 | |
8cc574cf | 847 | if ((!$self->_id && empty($values['total_amount']) && |
6a488035 TO |
848 | empty($self->_values['line_items']) |
849 | ) || | |
850 | ($self->_id && !$self->_paymentId && isset($self->_values['line_items']) && is_array($self->_values['line_items'])) | |
851 | ) { | |
852 | if ($priceSetId = CRM_Utils_Array::value('priceSetId', $values)) { | |
9da8dc8c | 853 | CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $values, $errorMsg, TRUE); |
6a488035 TO |
854 | } |
855 | } | |
4d3f1e42 JM |
856 | // For single additions - show validation error if the contact has already been registered |
857 | // for this event with the same role. | |
858 | if($self->_single && ($self->_action & CRM_Core_Action::ADD)) { | |
859 | $contactId = $self->_contactId; | |
860 | $eventId = CRM_Utils_Array::value('event_id', $values); | |
861 | if(!empty($contactId) && !empty($eventId)) { | |
862 | $dupeCheck = new CRM_Event_BAO_Participant; | |
863 | $dupeCheck->contact_id = $contactId; | |
864 | $dupeCheck->event_id = $eventId; | |
865 | $dupeCheck->find(TRUE); | |
866 | if(!empty($dupeCheck->id)) { | |
867 | $errorMsg['event_id'] = ts("This contact has already been assigned to this event."); | |
868 | } | |
869 | } | |
870 | } | |
6a488035 TO |
871 | return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg; |
872 | } | |
873 | ||
874 | /** | |
c490a46a | 875 | * Process the form submission |
6a488035 TO |
876 | * |
877 | * @access public | |
878 | */ | |
879 | public function postProcess() { | |
880 | // get the submitted form values. | |
881 | $params = $this->controller->exportValues($this->_name); | |
882 | ||
883 | if ($this->_action & CRM_Core_Action::DELETE) { | |
884 | if (CRM_Utils_Array::value('delete_participant', $params) == 2) { | |
885 | $additionalId = (CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id)); | |
886 | $participantLinks = (CRM_Event_BAO_Participant::getAdditionalParticipantUrl($additionalId)); | |
887 | } | |
888 | if (CRM_Utils_Array::value('delete_participant', $params) == 1) { | |
889 | $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id); | |
890 | foreach ($additionalIds as $value) { | |
891 | CRM_Event_BAO_Participant::deleteParticipant($value); | |
892 | } | |
893 | } | |
894 | CRM_Event_BAO_Participant::deleteParticipant($this->_id); | |
895 | CRM_Core_Session::setStatus(ts('Selected participant was deleted successfully.'), ts('Record Deleted'), 'success'); | |
896 | if (!empty($participantLinks)) { | |
897 | $status = ts('The following participants no longer have an event fee recorded. You can edit their registration and record a replacement contribution by clicking the links below:') . '<br>' . $participantLinks; | |
898 | CRM_Core_Session::setStatus($status, ts('Group Payment Deleted')); | |
899 | } | |
900 | return; | |
901 | } | |
4d3f1e42 JM |
902 | // When adding a single contact, the formRule prevents you from adding duplicates |
903 | // (See above in formRule()). When adding more than one contact, the duplicates are | |
904 | // removed automatically and the user receives one notification. | |
905 | if ($this->_action & CRM_Core_Action::ADD) { | |
aae2f9e6 JM |
906 | $event_id = $this->_eventId; |
907 | if(empty($event_id) && !empty($params['event_id'])) { | |
908 | $event_id = $params['event_id']; | |
909 | } | |
910 | if(!$this->_single && !empty($event_id)) { | |
4d3f1e42 JM |
911 | $duplicateContacts = 0; |
912 | while(list($k,$dupeCheckContactId) = each($this->_contactIds)) { | |
913 | // Eliminate contacts that have already been assigned to this event. | |
914 | $dupeCheck = new CRM_Event_BAO_Participant; | |
915 | $dupeCheck->contact_id = $dupeCheckContactId; | |
aae2f9e6 | 916 | $dupeCheck->event_id = $event_id; |
4d3f1e42 JM |
917 | $dupeCheck->find(TRUE); |
918 | if(!empty($dupeCheck->id)) { | |
919 | $duplicateContacts++; | |
920 | unset($this->_contactIds[$k]); | |
921 | } | |
922 | } | |
923 | if($duplicateContacts > 0) { | |
924 | $msg = ts( | |
925 | "%1 contacts have already been assigned to this event. They were not added a second time.", | |
926 | array(1 => $duplicateContacts) | |
927 | ); | |
928 | CRM_Core_Session::setStatus($msg); | |
929 | } | |
930 | if(count($this->_contactIds) == 0) { | |
931 | CRM_Core_Session::setStatus(ts("No participants were added.")); | |
932 | return; | |
933 | } | |
aef8149f JM |
934 | // We have to re-key $this->_contactIds so each contact has the same |
935 | // key as their corresponding record in the $participants array that | |
936 | // will be created below. | |
937 | $this->_contactIds = array_values($this->_contactIds); | |
4d3f1e42 JM |
938 | } |
939 | } | |
940 | ||
6a488035 | 941 | |
aac57c29 | 942 | $participantStatus = CRM_Event_PseudoConstant::participantStatus(); |
6a488035 | 943 | // set the contact, when contact is selected |
479696ed CW |
944 | if (!empty($params['contact_id'])) { |
945 | $this->_contactId = $params['contact_id']; | |
6a488035 | 946 | } |
9da8dc8c | 947 | if ($this->_priceSetId && $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
6a488035 TO |
948 | $this->_quickConfig = $isQuickConfig; |
949 | } | |
950 | ||
951 | if ($this->_id) { | |
952 | $params['id'] = $this->_id; | |
953 | } | |
954 | ||
955 | $config = CRM_Core_Config::singleton(); | |
956 | if ($this->_isPaidEvent) { | |
957 | ||
958 | $contributionParams = array(); | |
959 | $lineItem = array(); | |
960 | $additionalParticipantDetails = array(); | |
961 | if (($this->_id && $this->_action & CRM_Core_Action::UPDATE) && $this->_paymentId) { | |
962 | $participantBAO = new CRM_Event_BAO_Participant(); | |
963 | $participantBAO->id = $this->_id; | |
964 | $participantBAO->find(TRUE); | |
965 | $contributionParams['total_amount'] = $participantBAO->fee_amount; | |
966 | ||
967 | $params['discount_id'] = NULL; | |
968 | //re-enter the values for UPDATE mode | |
969 | $params['fee_level'] = $params['amount_level'] = $participantBAO->fee_level; | |
970 | $params['fee_amount'] = $participantBAO->fee_amount; | |
971 | if (isset($params['priceSetId'])) { | |
972 | $lineItem[0] = CRM_Price_BAO_LineItem::getLineItems($this->_id); | |
973 | } | |
974 | //also add additional participant's fee level/priceset | |
975 | if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { | |
976 | $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id); | |
977 | $hasLineItems = CRM_Utils_Array::value('priceSetId', $params, FALSE); | |
978 | $additionalParticipantDetails = CRM_Event_BAO_Participant::getFeeDetails($additionalIds, | |
979 | $hasLineItems | |
980 | ); | |
981 | } | |
982 | } | |
983 | else { | |
984 | ||
985 | //check if discount is selected | |
a7488080 | 986 | if (!empty($params['discount_id'])) { |
6a488035 TO |
987 | $discountId = $params['discount_id']; |
988 | } | |
989 | else { | |
990 | $discountId = $params['discount_id'] = 'null'; | |
991 | } | |
992 | ||
993 | //lets carry currency, CRM-4453 | |
994 | $params['fee_currency'] = $config->defaultCurrency; | |
9da8dc8c | 995 | CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], |
8567d0f8 PN |
996 | $params, $lineItem[0] |
997 | ); | |
6a488035 TO |
998 | //CRM-11529 for quick config backoffice transactions |
999 | //when financial_type_id is passed in form, update the | |
1000 | //lineitems with the financial type selected in form | |
1001 | $submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $params); | |
1002 | $isPaymentRecorded = CRM_Utils_Array::value('record_contribution', $params); | |
1003 | if ($isPaymentRecorded && $this->_quickConfig && $submittedFinancialType) { | |
1004 | foreach ($lineItem[0] as &$values) { | |
1005 | $values['financial_type_id'] = $submittedFinancialType; | |
1006 | } | |
1007 | } | |
1008 | ||
1009 | $params['fee_level'] = $params['amount_level']; | |
1010 | $contributionParams['total_amount'] = $params['amount']; | |
f6bae84f | 1011 | if ($this->_quickConfig && !empty($params['total_amount']) && |
372ad74d | 1012 | $params['status_id'] != array_search('Partially paid', $participantStatus)) { |
6a488035 TO |
1013 | $params['fee_amount'] = $params['total_amount']; |
1014 | } else { | |
1015 | //fix for CRM-3086 | |
1016 | $params['fee_amount'] = $params['amount']; | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | if (isset($params['priceSetId'])) { | |
1021 | if (!empty($lineItem[0])) { | |
1022 | $this->set('lineItem', $lineItem); | |
1023 | ||
1024 | $this->_lineItem = $lineItem; | |
1025 | $lineItem = array_merge($lineItem, $additionalParticipantDetails); | |
1026 | ||
1027 | $participantCount = array(); | |
1028 | foreach ($lineItem as $k) { | |
1029 | foreach ($k as $v) { | |
1030 | if (CRM_Utils_Array::value('participant_count', $v) > 0) { | |
1031 | $participantCount[] = $v['participant_count']; | |
1032 | } | |
1033 | } | |
1034 | } | |
1035 | } | |
1036 | if (isset($participantCount)) { | |
1037 | $this->assign('pricesetFieldsCount', $participantCount); | |
1038 | } | |
1039 | $this->assign('lineItem', empty($lineItem[0]) || $this->_quickConfig? FALSE : $lineItem); | |
1040 | } | |
1041 | else { | |
1042 | $this->assign('amount_level', $params['amount_level']); | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | $this->_params = $params; | |
fbc54416 PJ |
1047 | $amountOwed = NULL; |
1048 | if (isset($params['amount'])) { | |
1049 | $amountOwed = $params['amount']; | |
1050 | unset($params['amount']); | |
1051 | } | |
6a488035 TO |
1052 | $params['register_date'] = CRM_Utils_Date::processDate($params['register_date'], $params['register_date_time']); |
1053 | $params['receive_date'] = CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $params)); | |
1054 | $params['contact_id'] = $this->_contactId; | |
1055 | ||
1056 | // overwrite actual payment amount if entered | |
a7488080 | 1057 | if (!empty($params['total_amount'])) { |
6a488035 TO |
1058 | $contributionParams['total_amount'] = CRM_Utils_Array::value('total_amount', $params); |
1059 | } | |
1060 | ||
1061 | ||
1062 | // Retrieve the name and email of the current user - this will be the FROM for the receipt email | |
1063 | $session = CRM_Core_Session::singleton(); | |
1064 | $userID = $session->get('userID'); | |
1065 | list($userName, | |
1066 | $userEmail | |
1067 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); | |
1068 | ||
1069 | if ($this->_contactId) { | |
1070 | list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId); | |
1071 | } | |
1072 | ||
7df021e5 | 1073 | //modify params according to parameter used in create |
1074 | //participant method (addParticipant) | |
1075 | $this->_params['participant_status_id'] = $params['status_id']; | |
1076 | $this->_params['participant_role_id'] = is_array($params['role_id']) ? $params['role_id'] : explode(',', $params['role_id']); | |
1077 | $this->_params['participant_register_date'] = $params['register_date']; | |
1078 | $roleIdWithSeparator = implode(CRM_Core_DAO::VALUE_SEPARATOR, $this->_params['participant_role_id']); | |
1079 | ||
6a488035 TO |
1080 | if ($this->_mode) { |
1081 | if (!$this->_isPaidEvent) { | |
1082 | CRM_Core_Error::fatal(ts('Selected Event is not Paid Event ')); | |
1083 | } | |
6a488035 TO |
1084 | |
1085 | $eventTitle = | |
1086 | CRM_Core_DAO::getFieldValue( | |
1087 | 'CRM_Event_DAO_Event', | |
1088 | $params['event_id'], | |
1089 | 'title' | |
1090 | ); | |
1091 | ||
1092 | // set source if not set | |
1093 | if (empty($params['source'])) { | |
03e04002 | 1094 | $this->_params['participant_source'] = ts('Offline Registration for Event: %2 by: %1', array(1 => $userName, 2 => $eventTitle)); |
6a488035 TO |
1095 | } |
1096 | else { | |
03e04002 | 1097 | $this->_params['participant_source'] = $params['source']; |
6a488035 TO |
1098 | } |
1099 | $this->_params['description'] = $this->_params['participant_source']; | |
1100 | ||
1101 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_params['payment_processor_id'], | |
1102 | $this->_mode | |
1103 | ); | |
1104 | $now = date('YmdHis'); | |
1105 | $fields = array(); | |
1106 | ||
1107 | // set email for primary location. | |
1108 | $fields['email-Primary'] = 1; | |
1109 | $params['email-Primary'] = $params["email-{$this->_bltID}"] = $this->_contributorEmail; | |
1110 | ||
1111 | $params['register_date'] = $now; | |
1112 | ||
1113 | // now set the values for the billing location. | |
1114 | foreach ($this->_fields as $name => $dontCare) { | |
1115 | $fields[$name] = 1; | |
1116 | } | |
1117 | ||
1118 | // also add location name to the array | |
1119 | $params["address_name-{$this->_bltID}"] = | |
1120 | CRM_Utils_Array::value('billing_first_name', $params) . ' ' . | |
1121 | CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . | |
1122 | CRM_Utils_Array::value('billing_last_name', $params); | |
1123 | ||
1124 | $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]); | |
1125 | $fields["address_name-{$this->_bltID}"] = 1; | |
1126 | $fields["email-{$this->_bltID}"] = 1; | |
1127 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); | |
1128 | ||
1129 | $nameFields = array('first_name', 'middle_name', 'last_name'); | |
1130 | ||
1131 | foreach ($nameFields as $name) { | |
1132 | $fields[$name] = 1; | |
1133 | if (array_key_exists("billing_$name", $params)) { | |
1134 | $params[$name] = $params["billing_{$name}"]; | |
1135 | $params['preserveDBName'] = TRUE; | |
1136 | } | |
1137 | } | |
1138 | $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $this->_contactId, NULL, NULL, $ctype); | |
1139 | } | |
1140 | ||
e03317f1 | 1141 | if (!empty($this->_params['participant_role_id'])) { |
7df021e5 | 1142 | $customFieldsRole = array(); |
1143 | foreach ($this->_params['participant_role_id'] as $roleKey) { | |
1144 | $customFieldsRole = CRM_Utils_Array::crmArrayMerge(CRM_Core_BAO_CustomField::getFields('Participant', | |
1145 | FALSE, FALSE, $roleKey, $this->_roleCustomDataTypeID), $customFieldsRole); | |
6a488035 | 1146 | } |
7df021e5 | 1147 | $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant', |
1148 | FALSE, | |
1149 | FALSE, | |
1150 | CRM_Utils_Array::value('event_id', $params), | |
1151 | $this->_eventNameCustomDataTypeID | |
1152 | ); | |
1153 | $customFieldsEventType = CRM_Core_BAO_CustomField::getFields('Participant', | |
1154 | FALSE, | |
1155 | FALSE, | |
1156 | $this->_eventTypeId, | |
1157 | $this->_eventTypeCustomDataTypeID | |
1158 | ); | |
1159 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole, | |
1160 | CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE) | |
1161 | ); | |
1162 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields); | |
1163 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields); | |
1164 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
1165 | $customFields, | |
1166 | $this->_id, | |
1167 | 'Participant' | |
1168 | ); | |
6a488035 TO |
1169 | } |
1170 | ||
1171 | //do cleanup line items if participant edit the Event Fee. | |
1172 | if (($this->_lineItem || !isset($params['proceSetId'])) && !$this->_paymentId && $this->_id) { | |
1173 | CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_participant'); | |
1174 | } | |
03e04002 | 1175 | |
6a488035 | 1176 | if ($this->_mode) { |
dc8cdf3a | 1177 | // add all the additional payment params we need |
6a488035 TO |
1178 | $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]); |
1179 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
1180 | ||
1181 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); | |
1182 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
1183 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); | |
1184 | $this->_params['amount'] = $params['fee_amount']; | |
1185 | $this->_params['amount_level'] = $params['amount_level']; | |
1186 | $this->_params['currencyID'] = $config->defaultCurrency; | |
1187 | $this->_params['payment_action'] = 'Sale'; | |
1188 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
1189 | ||
1190 | // at this point we've created a contact and stored its address etc | |
1191 | // all the payment processors expect the name and address to be in the | |
1192 | // so we copy stuff over to first_name etc. | |
1193 | $paymentParams = $this->_params; | |
a7488080 | 1194 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
1195 | $paymentParams['email'] = $this->_contributorEmail; |
1196 | } | |
1197 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); | |
1198 | ||
1199 | $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); | |
1200 | ||
9b7e7d4c PH |
1201 | // CRM-15622: fix for incorrect contribution.fee_amount |
1202 | $paymentParams['fee_amount'] = NULL; | |
bf2c70af | 1203 | $result = $payment->doDirectPayment($paymentParams); |
6a488035 TO |
1204 | |
1205 | if (is_a($result, 'CRM_Core_Error')) { | |
1206 | CRM_Core_Error::displaySessionError($result); | |
1207 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/participant', | |
1208 | "reset=1&action=add&cid={$this->_contactId}&context=participant&mode={$this->_mode}" | |
1209 | )); | |
1210 | } | |
1211 | ||
1212 | if ($result) { | |
1213 | $this->_params = array_merge($this->_params, $result); | |
1214 | } | |
1215 | ||
1216 | $this->_params['receive_date'] = $now; | |
1217 | ||
a7488080 | 1218 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
1219 | $this->_params['receipt_date'] = $now; |
1220 | } | |
1221 | else { | |
1222 | $this->_params['receipt_date'] = NULL; | |
1223 | } | |
1224 | ||
1225 | $this->set('params', $this->_params); | |
1226 | $this->assign('trxn_id', $result['trxn_id']); | |
1227 | $this->assign('receive_date', | |
1228 | CRM_Utils_Date::processDate($this->_params['receive_date']) | |
1229 | ); | |
1230 | ||
1231 | //add contribution record | |
1232 | $this->_params['financial_type_id'] = | |
1233 | CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'financial_type_id'); | |
1234 | $this->_params['mode'] = $this->_mode; | |
1235 | ||
1236 | //add contribution reocord | |
1237 | $contributions[] = $contribution = CRM_Event_Form_Registration_Confirm::processContribution($this, $this->_params, $result, $contactID, FALSE); | |
1238 | ||
1239 | // add participant record | |
1240 | $participants = array(); | |
dc8cdf3a K |
1241 | if (!empty($this->_params['role_id']) && is_array($this->_params['role_id'])) { |
1242 | $this->_params['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1243 | $this->_params['role_id'] | |
6a488035 TO |
1244 | ); |
1245 | } | |
0e81467c | 1246 | |
5c320924 C |
1247 | //CRM-15372 patch to fix fee amount replacing amount |
1248 | $this->_params['fee_amount'] = $this->_params['amount']; | |
1249 | ||
bf2c70af | 1250 | $participants[] = CRM_Event_Form_Registration::addParticipant($this, $contactID); |
6a488035 TO |
1251 | |
1252 | //add custom data for participant | |
1253 | CRM_Core_BAO_CustomValueTable::postProcess($this->_params, | |
1254 | CRM_Core_DAO::$_nullArray, | |
1255 | 'civicrm_participant', | |
1256 | $participants[0]->id, | |
1257 | 'Participant' | |
1258 | ); | |
1259 | //add participant payment | |
1260 | $paymentParticipant = array( | |
1261 | 'participant_id' => $participants[0]->id, | |
1262 | 'contribution_id' => $contribution->id, | |
1263 | ); | |
1264 | $ids = array(); | |
1265 | ||
1266 | CRM_Event_BAO_ParticipantPayment::create($paymentParticipant, $ids); | |
1267 | $this->_contactIds[] = $this->_contactId; | |
1268 | } | |
1269 | else { | |
1270 | $participants = array(); | |
1271 | if ($this->_single) { | |
1272 | if ($params['role_id']) { | |
7df021e5 | 1273 | $params['role_id'] = $roleIdWithSeparator; |
6a488035 TO |
1274 | } |
1275 | else { | |
1276 | $params['role_id'] = 'NULL'; | |
1277 | } | |
1278 | $participants[] = CRM_Event_BAO_Participant::create($params); | |
1279 | } | |
1280 | else { | |
1281 | foreach ($this->_contactIds as $contactID) { | |
1282 | $commonParams = $params; | |
1283 | $commonParams['contact_id'] = $contactID; | |
1284 | if ($commonParams['role_id']) { | |
dc8cdf3a | 1285 | $commonParams['role_id'] = $commonParams['role_id'] = str_replace(',', CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']); |
6a488035 TO |
1286 | } |
1287 | else { | |
1288 | $commonParams['role_id'] = 'NULL'; | |
1289 | } | |
1290 | $participants[] = CRM_Event_BAO_Participant::create($commonParams); | |
1291 | } | |
1292 | } | |
1293 | ||
1294 | if (isset($params['event_id'])) { | |
1295 | $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', | |
1296 | $params['event_id'], | |
1297 | 'title' | |
1298 | ); | |
1299 | } | |
1300 | ||
1301 | if ($this->_single) { | |
1302 | $this->_contactIds[] = $this->_contactId; | |
1303 | } | |
1304 | ||
1305 | $contributions = array(); | |
a7488080 CW |
1306 | if (!empty($params['record_contribution'])) { |
1307 | if (!empty($params['id'])) { | |
6a488035 TO |
1308 | if ($this->_onlinePendingContributionId) { |
1309 | $ids['contribution'] = $this->_onlinePendingContributionId; | |
1310 | } | |
1311 | else { | |
1312 | $ids['contribution'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
1313 | $params['id'], | |
1314 | 'contribution_id', | |
1315 | 'participant_id' | |
1316 | ); | |
1317 | } | |
1318 | } | |
1319 | unset($params['note']); | |
1320 | ||
1321 | //build contribution params | |
1322 | if (!$this->_onlinePendingContributionId) { | |
0e81467c | 1323 | if (empty($params['source'])) { |
13bb29e4 | 1324 | $contributionParams['source'] = ts('%1 : Offline registration (by %2)', array(1 => $eventTitle, 2 => $userName)); |
0e81467c EM |
1325 | } |
1326 | else { | |
1327 | $contributionParams['source'] = $params['source']; | |
1328 | } | |
1329 | } | |
6a488035 TO |
1330 | |
1331 | $contributionParams['currency'] = $config->defaultCurrency; | |
1332 | $contributionParams['non_deductible_amount'] = 'null'; | |
0d8afee2 | 1333 | $contributionParams['receipt_date'] = !empty($params['send_receipt']) ? CRM_Utils_Array::value('receive_date', $params) : 'null'; |
6a488035 TO |
1334 | |
1335 | $recordContribution = array( 'contact_id', 'financial_type_id', | |
1336 | 'payment_instrument_id', 'trxn_id', | |
1337 | 'contribution_status_id', 'receive_date', | |
1338 | 'check_number', 'campaign_id', | |
1339 | ); | |
1340 | ||
1341 | foreach ($recordContribution as $f) { | |
1342 | $contributionParams[$f] = CRM_Utils_Array::value($f, $params); | |
1343 | if ($f == 'trxn_id') { | |
1344 | $this->assign('trxn_id', $contributionParams[$f]); | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | //insert financial type name in receipt. | |
1349 | $this->assign('contributionTypeName', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $contributionParams['financial_type_id'])); | |
1350 | $contributionParams['skipLineItem'] = 1; | |
7030a002 PN |
1351 | if ($this->_id) { |
1352 | $contributionParams['contribution_mode'] = 'participant'; | |
1353 | $contributionParams['participant_id'] = $this->_id; | |
1354 | } | |
6a488035 TO |
1355 | // Set is_pay_later flag for back-office offline Pending status contributions |
1356 | if ($contributionParams['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name')) { | |
1357 | $contributionParams['is_pay_later'] = 1; | |
1358 | } | |
d6dce594 | 1359 | elseif ($contributionParams['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name')) { |
d75f2f47 EM |
1360 | $contributionParams['is_pay_later'] = 0; |
1361 | } | |
f8325309 | 1362 | |
aac57c29 | 1363 | if ($params['status_id'] == array_search('Partially paid', $participantStatus)) { |
5e494d5c PJ |
1364 | if (!$amountOwed && $this->_action & CRM_Core_Action::UPDATE) { |
1365 | $amountOwed = $params['fee_amount']; | |
1366 | } | |
1367 | ||
356579e9 | 1368 | // if multiple participants are link, consider contribution total amount as the amount Owed |
ea707c1b | 1369 | if ($this->_id && CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { |
356579e9 PJ |
1370 | $amountOwed = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', |
1371 | $ids['contribution'], | |
1372 | 'total_amount' | |
1373 | ); | |
1374 | } | |
1375 | ||
aac57c29 PJ |
1376 | // CRM-13964 partial_payment_total |
1377 | if ($amountOwed > $params['total_amount']) { | |
1378 | // the owed amount | |
1379 | $contributionParams['partial_payment_total'] = $amountOwed; | |
1380 | // the actual amount paid | |
1381 | $contributionParams['partial_amount_pay'] = $params['total_amount']; | |
1382 | } | |
f8325309 | 1383 | } |
372ad74d | 1384 | |
d91b8b33 | 1385 | if (CRM_Utils_Array::value('tax_amount', $this->_params)) { |
1386 | $contributionParams['tax_amount'] = $this->_params['tax_amount']; | |
1387 | } | |
1388 | ||
6a488035 | 1389 | if ($this->_single) { |
dbd48d4e | 1390 | if (empty($ids)) { |
1391 | $ids = array(); | |
1392 | } | |
6a488035 TO |
1393 | $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids); |
1394 | } | |
1395 | else { | |
1396 | $ids = array(); | |
1397 | foreach ($this->_contactIds as $contactID) { | |
1398 | $contributionParams['contact_id'] = $contactID; | |
1399 | $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids); | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | //insert payment record for this participation | |
dbd48d4e | 1404 | if (empty($ids['contribution'])) { |
6a488035 TO |
1405 | foreach ($this->_contactIds as $num => $contactID) { |
1406 | $ppDAO = new CRM_Event_DAO_ParticipantPayment(); | |
1407 | $ppDAO->participant_id = $participants[$num]->id; | |
1408 | $ppDAO->contribution_id = $contributions[$num]->id; | |
1409 | $ppDAO->save(); | |
1410 | } | |
1411 | } | |
1412 | // next create the transaction record | |
1413 | $transaction = new CRM_Core_Transaction(); | |
1414 | ||
1415 | // CRM-11124 | |
1416 | if ($this->_quickConfig) { | |
a7488080 | 1417 | if (!empty($this->_params['amount_priceset_level_radio'])) { |
7030a002 PN |
1418 | $feeLevel = $this->_params['amount_priceset_level_radio']; |
1419 | } | |
1420 | else { | |
1421 | $feeLevel[] = $this->_params['fee_level'] ; | |
1422 | } | |
1423 | CRM_Event_BAO_Participant::createDiscountTrxn($this->_eventId, $contributionParams, $feeLevel); | |
6a488035 TO |
1424 | } |
1425 | $transaction->commit(); | |
1426 | } | |
1427 | } | |
1428 | ||
1429 | // also store lineitem stuff here | |
1430 | if ((($this->_lineItem & $this->_action & CRM_Core_Action::ADD) || | |
1431 | ($this->_lineItem && CRM_Core_Action::UPDATE && !$this->_paymentId)) | |
1432 | ) { | |
1433 | foreach ($this->_contactIds as $num => $contactID) { | |
1434 | foreach ($this->_lineItem as $key => $value) { | |
1435 | if (is_array($value) && $value != 'skip') { | |
1436 | foreach ($value as $lineKey => $line) { | |
aac57c29 | 1437 | //10117 update the line items for participants if contribution amount is recorded |
f6bae84f | 1438 | if ($this->_quickConfig && !empty($params['total_amount']) && |
372ad74d | 1439 | ($params['status_id'] != array_search('Partially paid', $participantStatus)) |
aac57c29 PJ |
1440 | ) { |
1441 | $line['unit_price'] = $line['line_total'] = $params['total_amount']; | |
d91b8b33 | 1442 | if (!empty($params['tax_amount'])) { |
1443 | $line['unit_price'] = $line['unit_price'] - $params['tax_amount']; | |
ff9b6b50 | 1444 | $line['line_total'] = $line['line_total'] - $params['tax_amount']; |
d91b8b33 | 1445 | } |
aac57c29 | 1446 | } |
6a488035 TO |
1447 | $lineItem[$this->_priceSetId][$lineKey] = $line; |
1448 | } | |
1449 | CRM_Price_BAO_LineItem::processPriceSet($participants[$num]->id, $lineItem, CRM_Utils_Array::value($num, $contributions, NULL), 'civicrm_participant'); | |
1450 | } | |
1451 | } | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | $updateStatusMsg = NULL; | |
1456 | //send mail when participant status changed, CRM-4326 | |
1457 | if ($this->_id && $this->_statusId && | |
8cc574cf | 1458 | $this->_statusId != CRM_Utils_Array::value('status_id', $params) && !empty($params['is_notify'])) { |
6a488035 TO |
1459 | |
1460 | $updateStatusMsg = CRM_Event_BAO_Participant::updateStatusMessage($this->_id, | |
1461 | $params['status_id'], | |
1462 | $this->_statusId | |
1463 | ); | |
1464 | } | |
1465 | ||
1466 | $sent = array(); | |
1467 | $notSent = array(); | |
a7488080 | 1468 | if (!empty($params['send_receipt'])) { |
6a488035 TO |
1469 | if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) { |
1470 | $receiptFrom = $params['from_email_address']; | |
1471 | } | |
1472 | ||
1473 | $this->assign('module', 'Event Registration'); | |
1474 | //use of the message template below requires variables in different format | |
1475 | $event = $events = array(); | |
1476 | $returnProperties = array('fee_label', 'start_date', 'end_date', 'is_show_location', 'title'); | |
1477 | ||
1478 | //get all event details. | |
1479 | CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $params['event_id'], $events, $returnProperties); | |
1480 | $event = $events[$params['event_id']]; | |
1481 | unset($event['start_date']); | |
1482 | unset($event['end_date']); | |
1483 | ||
1484 | $role = CRM_Event_PseudoConstant::participantRole(); | |
1485 | $participantRoles = CRM_Utils_Array::value('role_id', $params); | |
1486 | if (is_array($participantRoles)) { | |
1487 | $selectedRoles = array(); | |
c7101803 | 1488 | foreach ($participantRoles as $roleId) { |
6a488035 TO |
1489 | $selectedRoles[] = $role[$roleId]; |
1490 | } | |
1491 | $event['participant_role'] = implode(', ', $selectedRoles); | |
1492 | } | |
1493 | else { | |
1494 | $event['participant_role'] = CRM_Utils_Array::value($participantRoles, $role); | |
1495 | } | |
1496 | $event['is_monetary'] = $this->_isPaidEvent; | |
1497 | ||
1498 | if ($params['receipt_text']) { | |
1499 | $event['confirm_email_text'] = $params['receipt_text']; | |
1500 | } | |
1501 | ||
1502 | $this->assign('isAmountzero', 1); | |
1503 | $this->assign('event', $event); | |
1504 | ||
1505 | $this->assign('isShowLocation', $event['is_show_location']); | |
1506 | if (CRM_Utils_Array::value('is_show_location', $event) == 1) { | |
1507 | $locationParams = array( | |
1508 | 'entity_id' => $params['event_id'], | |
1509 | 'entity_table' => 'civicrm_event', | |
1510 | ); | |
1511 | $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE); | |
1512 | $this->assign('location', $location); | |
1513 | } | |
1514 | ||
1515 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
1516 | if ($this->_isPaidEvent) { | |
1517 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
1518 | if (!$this->_mode) { | |
1519 | if (isset($params['payment_instrument_id'])) { | |
1520 | $this->assign('paidBy', | |
1521 | CRM_Utils_Array::value($params['payment_instrument_id'], | |
1522 | $paymentInstrument | |
1523 | ) | |
1524 | ); | |
1525 | } | |
1526 | } | |
1527 | ||
1528 | $this->assign('totalAmount', $contributionParams['total_amount']); | |
81f3d017 PJ |
1529 | if (isset($contributionParams['partial_payment_total'])) { |
1530 | // balance amount | |
1531 | $balanceAmount = $contributionParams['partial_payment_total'] - $contributionParams['partial_amount_pay']; | |
1532 | $this->assign('balanceAmount', $balanceAmount ); | |
1533 | } | |
6a488035 TO |
1534 | $this->assign('isPrimary', 1); |
1535 | $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params)); | |
1536 | } | |
1537 | if ($this->_mode) { | |
a7488080 | 1538 | if (!empty($params['billing_first_name'])) { |
6a488035 TO |
1539 | $name = $params['billing_first_name']; |
1540 | } | |
1541 | ||
a7488080 | 1542 | if (!empty($params['billing_middle_name'])) { |
6a488035 TO |
1543 | $name .= " {$params['billing_middle_name']}"; |
1544 | } | |
1545 | ||
a7488080 | 1546 | if (!empty($params['billing_last_name'])) { |
6a488035 TO |
1547 | $name .= " {$params['billing_last_name']}"; |
1548 | } | |
1549 | $this->assign('billingName', $name); | |
1550 | ||
1551 | // assign the address formatted up for display | |
1552 | $addressParts = array( | |
1553 | "street_address-{$this->_bltID}", | |
1554 | "city-{$this->_bltID}", | |
1555 | "postal_code-{$this->_bltID}", | |
1556 | "state_province-{$this->_bltID}", | |
1557 | "country-{$this->_bltID}", | |
1558 | ); | |
1559 | $addressFields = array(); | |
1560 | foreach ($addressParts as $part) { | |
1561 | list($n, $id) = explode('-', $part); | |
1562 | if (isset($this->_params['billing_' . $part])) { | |
1563 | $addressFields[$n] = $this->_params['billing_' . $part]; | |
1564 | } | |
1565 | } | |
1566 | $this->assign('address', CRM_Utils_Address::format($addressFields)); | |
1567 | ||
1568 | $date = CRM_Utils_Date::format($params['credit_card_exp_date']); | |
1569 | $date = CRM_Utils_Date::mysqlToIso($date); | |
1570 | $this->assign('credit_card_exp_date', $date); | |
1571 | $this->assign('credit_card_number', | |
1572 | CRM_Utils_System::mungeCreditCard($params['credit_card_number']) | |
1573 | ); | |
1574 | $this->assign('credit_card_type', $params['credit_card_type']); | |
1575 | $this->assign('contributeMode', 'direct'); | |
1576 | $this->assign('isAmountzero', 0); | |
1577 | $this->assign('is_pay_later', 0); | |
1578 | $this->assign('isPrimary', 1); | |
1579 | } | |
1580 | ||
1581 | $this->assign('register_date', $params['register_date']); | |
1582 | if ($params['receive_date']) { | |
1583 | $this->assign('receive_date', $params['receive_date']); | |
1584 | } | |
1585 | ||
1586 | $participant = array(array('participant_id', '=', $participants[0]->id, 0, 0)); | |
1587 | // check whether its a test drive ref CRM-3075 | |
a7488080 | 1588 | if (!empty($this->_defaultValues['is_test'])) { |
6a488035 TO |
1589 | $participant[] = array('participant_test', '=', 1, 0, 0); |
1590 | } | |
1591 | ||
1592 | $template = CRM_Core_Smarty::singleton(); | |
1593 | $customGroup = array(); | |
1594 | //format submitted data | |
1595 | foreach ($params['custom'] as $fieldID => $values) { | |
1596 | foreach ($values as $fieldValue) { | |
1597 | $customValue = array('data' => $fieldValue['value']); | |
1598 | $customFields[$fieldID]['id'] = $fieldID; | |
1599 | $formattedValue = CRM_Core_BAO_CustomGroup::formatCustomValues($customValue, $customFields[$fieldID], TRUE); | |
1600 | $customGroup[$customFields[$fieldID]['groupTitle']][$customFields[$fieldID]['label']] = str_replace(' ', '', $formattedValue); | |
1601 | } | |
1602 | } | |
1603 | ||
1604 | foreach ($this->_contactIds as $num => $contactID) { | |
1605 | // Retrieve the name and email of the contact - this will be the TO for receipt email | |
1606 | list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($contactID); | |
1607 | ||
1608 | $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName; | |
1609 | ||
1610 | $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); | |
1611 | if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) { | |
1612 | $this->assign('isOnWaitlist', TRUE); | |
1613 | } | |
1614 | ||
1615 | $this->assign('customGroup', $customGroup); | |
1616 | $this->assign('contactID', $contactID); | |
1617 | $this->assign('participantID', $participants[$num]->id); | |
1618 | ||
1619 | $this->_id = $participants[$num]->id; | |
1620 | ||
1621 | if ($this->_isPaidEvent) { | |
1622 | // fix amount for each of participants ( for bulk mode ) | |
1623 | $eventAmount = array(); | |
03b412ae PB |
1624 | $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,'contribution_invoice_settings'); |
1625 | $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); | |
79d001a2 | 1626 | $totalTaxAmount = 0; |
0e81467c | 1627 | |
03b412ae | 1628 | //add dataArray in the receipts in ADD and UPDATE condition |
79d001a2 PB |
1629 | $dataArray = array(); |
1630 | if ($this->_action & CRM_Core_Action::ADD) { | |
1631 | $line = $lineItem[0]; | |
1632 | } | |
1633 | elseif ($this->_action & CRM_Core_Action::UPDATE) { | |
1634 | $line = $this->_values['line_items']; | |
1635 | } | |
03b412ae PB |
1636 | if ($invoicing) { |
1637 | foreach ($line as $key => $value) { | |
1638 | if (isset($value['tax_amount'])) { | |
1639 | $totalTaxAmount += $value['tax_amount']; | |
288ec043 PB |
1640 | if (isset($dataArray[(string) $value['tax_rate']])) { |
1641 | $dataArray[(string) $value['tax_rate']] = $dataArray[(string) $value['tax_rate']] + CRM_Utils_Array::value('tax_amount', $value); | |
03b412ae PB |
1642 | } |
1643 | else { | |
288ec043 | 1644 | $dataArray[(string) $value['tax_rate']] = CRM_Utils_Array::value('tax_amount', $value); |
03b412ae | 1645 | } |
79d001a2 PB |
1646 | } |
1647 | } | |
03b412ae | 1648 | $this->assign('totalTaxAmount', $totalTaxAmount); |
01604562 | 1649 | $this->assign('taxTerm', CRM_Utils_Array::value('tax_term', $invoiceSettings)); |
03b412ae | 1650 | $this->assign('dataArray', $dataArray); |
79d001a2 | 1651 | } |
6a488035 TO |
1652 | if (!empty($additionalParticipantDetails)) { |
1653 | $params['amount_level'] = preg_replace('/\ 1/', '', $params['amount_level']) . ' - ' . $this->_contributorDisplayName; | |
1654 | } | |
1655 | ||
1656 | $eventAmount[$num] = array( | |
1657 | 'label' => preg_replace('/\ 1/', '', $params['amount_level']), | |
1658 | 'amount' => $params['fee_amount'], | |
1659 | ); | |
1660 | //as we are using same template for online & offline registration. | |
1661 | //So we have to build amount as array. | |
1662 | $eventAmount = array_merge($eventAmount, $additionalParticipantDetails); | |
1663 | $this->assign('amount', $eventAmount); | |
1664 | } | |
1665 | ||
1666 | $sendTemplateParams = array( | |
1667 | 'groupName' => 'msg_tpl_workflow_event', | |
1668 | 'valueName' => 'event_offline_receipt', | |
1669 | 'contactId' => $contactID, | |
1670 | 'isTest' => (bool) CRM_Utils_Array::value('is_test', $this->_defaultValues), | |
99deba35 | 1671 | 'PDFFilename' => ts('confirmation').'.pdf', |
6a488035 TO |
1672 | ); |
1673 | ||
1674 | // try to send emails only if email id is present | |
1675 | // and the do-not-email option is not checked for that contact | |
1676 | if ($this->_contributorEmail and !$this->_toDoNotEmail) { | |
1677 | $sendTemplateParams['from'] = $receiptFrom; | |
1678 | $sendTemplateParams['toName'] = $this->_contributorDisplayName; | |
1679 | $sendTemplateParams['toEmail'] = $this->_contributorEmail; | |
1680 | $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc', $this->_fromEmails); | |
1681 | $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc', $this->_fromEmails); | |
1682 | } | |
1683 | ||
9161952c PD |
1684 | //send email with pdf invoice |
1685 | $template = CRM_Core_Smarty::singleton( ); | |
1686 | $taxAmt = $template->get_template_vars('dataArray'); | |
1687 | $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
1688 | $this->_id, 'contribution_id', 'participant_id' | |
1689 | ); | |
bb600eae | 1690 | $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); |
9161952c PD |
1691 | $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue); |
1692 | if (count($taxAmt) > 0 && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) { | |
0e81467c EM |
1693 | $sendTemplateParams['isEmailPdf'] = True; |
1694 | $sendTemplateParams['contributionId'] = $contributionId; | |
9161952c | 1695 | } |
c6327d7d | 1696 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
6a488035 TO |
1697 | if ($mailSent) { |
1698 | $sent[] = $contactID; | |
1699 | foreach ($participants as $ids => $values) { | |
1700 | if ($values->contact_id == $contactID) { | |
641f812f | 1701 | $values->details = CRM_Utils_Array::value('receipt_text', $params); |
6a488035 TO |
1702 | CRM_Activity_BAO_Activity::addActivity($values, 'Email'); |
1703 | break; | |
1704 | } | |
1705 | } | |
1706 | } | |
1707 | else { | |
1708 | $notSent[] = $contactID; | |
1709 | } | |
1710 | } | |
1711 | } | |
1712 | ||
1713 | // set the participant id if it is not set | |
1714 | if (!$this->_id) { | |
1715 | $this->_id = $participants[0]->id; | |
1716 | } | |
1717 | ||
1718 | if (($this->_action & CRM_Core_Action::UPDATE)) { | |
1719 | $statusMsg = ts('Event registration information for %1 has been updated.', array(1 => $this->_contributorDisplayName)); | |
a7488080 | 1720 | if (!empty($params['send_receipt']) && count($sent)) { |
6a488035 TO |
1721 | $statusMsg .= ' ' . ts('A confirmation email has been sent to %1', array(1 => $this->_contributorEmail)); |
1722 | } | |
1723 | ||
1724 | if ($updateStatusMsg) { | |
1725 | $statusMsg = "{$statusMsg} {$updateStatusMsg}"; | |
1726 | } | |
1727 | } | |
1728 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
1729 | if ($this->_single) { | |
1730 | $statusMsg = ts('Event registration for %1 has been added.', array(1 => $this->_contributorDisplayName)); | |
a7488080 | 1731 | if (!empty($params['send_receipt']) && count($sent)) { |
6a488035 TO |
1732 | $statusMsg .= ' ' . ts('A confirmation email has been sent to %1.', array(1 => $this->_contributorEmail)); |
1733 | } | |
1734 | } | |
1735 | else { | |
1736 | $statusMsg = ts('Total Participant(s) added to event: %1.', array(1 => count($this->_contactIds))); | |
1737 | if (count($notSent) > 0) { | |
1738 | $statusMsg .= ' ' . ts('Email has NOT been sent to %1 contact(s) - communication preferences specify DO NOT EMAIL OR valid Email is NOT present. ', array(1 => count($notSent))); | |
1739 | } | |
1740 | elseif (isset($params['send_receipt'])) { | |
1741 | $statusMsg .= ' ' . ts('A confirmation email has been sent to ALL participants'); | |
1742 | } | |
1743 | } | |
1744 | } | |
1745 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); | |
1746 | ||
1747 | $buttonName = $this->controller->getButtonName(); | |
1748 | if ($this->_context == 'standalone') { | |
1749 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
1750 | $urlParams = 'reset=1&action=add&context=standalone'; | |
1751 | if ($this->_mode) { | |
1752 | $urlParams .= '&mode=' . $this->_mode; | |
1753 | } | |
1754 | if ($this->_eID) { | |
1755 | $urlParams .= '&eid=' . $this->_eID; | |
1756 | } | |
1757 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/participant/add', $urlParams)); | |
1758 | } | |
1759 | else { | |
1760 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
1761 | "reset=1&cid={$this->_contactId}&selectedChild=participant" | |
1762 | )); | |
1763 | } | |
1764 | } | |
1765 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
1766 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/participant', | |
1767 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactId}" | |
1768 | )); | |
1769 | } | |
1770 | } | |
1771 | } |