CRM-13863 - Whitelist open-inline contribution links
[civicrm-core.git] / CRM / Event / Form / ParticipantFeeSelection.php
CommitLineData
bc2eeabb
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 usefusul, 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 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
2429e40c 37 * This form used for changing / updating fee selections for the events
bc2eeabb
PJ
38 * event/contribution is partially paid
39 *
40 */
41class CRM_Event_Form_ParticipantFeeSelection extends CRM_Core_Form {
bc2eeabb
PJ
42
43 protected $_contactId = NULL;
44
45 protected $_contributorDisplayName = NULL;
46
47 protected $_contributorEmail = NULL;
48
49 protected $_toDoNotEmail = NULL;
50
51 protected $_contributionId = NULL;
52
53 protected $fromEmailId = NULL;
54
55 protected $_eventId = NULL;
56
57 public $_action = NULL;
58
59 public $_values = NULL;
60
bc2eeabb
PJ
61 public $_participantId = NULL;
62
b17ee3ee
PJ
63 protected $_participantStatus = NULL;
64
0aaf8fe9
PJ
65 protected $_paidAmount = NULL;
66
67 public $_isPaidEvent = NULL;
68
69 protected $contributionAmt = NULL;
70
bc2eeabb
PJ
71 public function preProcess() {
72 $this->_participantId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
73 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
bc2eeabb 74 $this->_eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'event_id');
2429e40c
PJ
75 $this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($this->_eventId);
76
bc2eeabb
PJ
77 $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participantId, 'contribution_id', 'participant_id');
78 if ($this->_contributionId) {
79 $this->_isPaidEvent = TRUE;
80 }
81 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, TRUE);
82
83 list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId);
84 $this->assign('displayName', $this->_contributorDisplayName);
2429e40c
PJ
85 $this->assign('email', $this->_contributorEmail);
86
b17ee3ee 87 $this->_participantStatus = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_participantId, 'status_id');
bc2eeabb
PJ
88 //set the payment mode - _mode property is defined in parent class
89 $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
90
91 $this->assign('contactId', $this->_contactId);
92 $this->assign('id', $this->_participantId);
93
94 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_participantId, 'event');
0aaf8fe9 95 $this->_paidAmount = $paymentInfo['paid'];
bc2eeabb 96 $this->assign('paymentInfo', $paymentInfo);
1010c4e1 97 $this->assign('feePaid', $this->_paidAmount);
bc2eeabb
PJ
98
99 $title = "Change selections for {$this->_contributorDisplayName}";
100 if ($title) {
101 CRM_Utils_System::setTitle(ts('%1', array(1 => $title)));
102 }
103 }
104
105 public function setDefaultValues() {
106 $params = array('id' => $this->_participantId);
107
108 CRM_Event_BAO_Participant::getValues($params, $defaults, $ids);
109 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_eventId);
110
111 $priceSetValues = CRM_Event_Form_EventFees::setDefaultPriceSet($this->_participantId, $this->_eventId);
112 if (!empty($priceSetValues)) {
113 $defaults[$this->_participantId] = array_merge($defaults[$this->_participantId], $priceSetValues);
114 }
115
116 $this->assign('totalAmount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId]));
117 if ($this->_action == CRM_Core_Action::UPDATE) {
118 $fee_level = $defaults[$this->_participantId]['fee_level'];
119 CRM_Event_BAO_Participant::fixEventLevel($fee_level);
120 $this->assign('fee_level', $fee_level);
121 $this->assign('fee_amount', CRM_Utils_Array::value('fee_amount', $defaults[$this->_participantId]));
122 }
123 $defaults = $defaults[$this->_participantId];
124 return $defaults;
125 }
126
127 public function buildQuickForm() {
f1cf499b
CW
128 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajax_popups_enabled', NULL, TRUE)) {
129 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
130 }
2429e40c
PJ
131
132 $statuses = CRM_Event_PseudoConstant::participantStatus();
1010c4e1
PJ
133 $this->assign('partiallyPaid', array_search('Partially paid', $statuses));
134 $this->assign('pendingRefund', array_search('Pending refund', $statuses));
135 $this->assign('participantStatus', $this->_participantStatus);
2429e40c 136
bc2eeabb
PJ
137 $config = CRM_Core_Config::singleton();
138 $this->assign('currencySymbol', $config->defaultCurrencySymbol);
139
140 // line items block
141 $lineItem = $event = array();
142 $params = array('id' => $this->_eventId);
143 CRM_Event_BAO_Event::retrieve($params, $event);
144
145 //retrieve custom information
146 $this->_values = array();
147 CRM_Event_Form_Registration::initEventFee($this, $event['id']);
148 CRM_Event_Form_Registration_Register::buildAmount($this, TRUE);
149
150 if (!CRM_Utils_System::isNull(CRM_Utils_Array::value('line_items', $this->_values))) {
151 $lineItem[] = $this->_values['line_items'];
152 }
153 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
154 $event = CRM_Event_BAO_Event::getEvents(0, $this->_eventId);
155 $this->assign('eventName', $event[$this->_eventId]);
156
157 $statusOptions = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label');
158 $this->add('select', 'status_id', ts('Participant Status'),
159 array(
160 '' => ts('- select -')) + $statusOptions,
161 TRUE
162 );
163
2429e40c
PJ
164 $this->addElement('checkbox',
165 'send_receipt',
166 ts('Send Confirmation?'), NULL,
167 array('onclick' => "showHideByValue('send_receipt','','notice','table-row','radio',false); showHideByValue('send_receipt','','from-email','table-row','radio',false);")
168 );
169
170 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails['from_email_id']);
171
172 $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
173
174 $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note');
175 $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']);
176
bc2eeabb
PJ
177 $buttons[] = array(
178 'type' => 'upload',
179 'name' => ts('Save'),
180 'isDefault' => TRUE,
181 );
182
2429e40c
PJ
183 $buttons[] = array(
184 'type' => 'upload',
185 'name' => ts('Save and Record Payment'),
186 'subName' => 'new'
187 );
188
bc2eeabb
PJ
189 $buttons[] = array(
190 'type' => 'cancel',
191 'name' => ts('Cancel'),
192 );
193
194 $this->addButtons($buttons);
195 $this->addFormRule(array('CRM_Event_Form_ParticipantFeeSelection', 'formRule'), $this);
196 }
197
198 static function formRule($fields, $files, $self) {
199 $errors = array();
200 return $errors;
201 }
202
203 public function postProcess() {
204 $params = $this->controller->exportValues($this->_name);
0aaf8fe9
PJ
205
206 $feeBlock = $this->_values['fee'];
207 $lineItems = $this->_values['line_items'];
208 CRM_Event_BAO_Participant::changeFeeSelections($params, $this->_participantId, $this->_contributionId, $feeBlock, $lineItems, $this->_paidAmount, $params['priceSetId']);
209 $this->contributionAmt = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $this->_contributionId, 'total_amount');
210 // email sending
211 if (CRM_Utils_Array::value('send_receipt', $params)){
212 $fetchParticipantVals = array('id' => $this->_participantId);
213 CRM_Event_BAO_Participant::getValues($fetchParticipantVals, $participantDetails, CRM_Core_DAO::$_nullArray);
214 $participantParams = array_merge($params, $participantDetails[$this->_participantId]);
215 $mailSent = $this->emailReceipt($participantParams);
216 }
217
218 // update participant
219 CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Participant', $this->_participantId, 'status_id', $params['status_id']);
220 if(!empty($params['note'])) {
221 $noteParams = array(
222 'entity_table' => 'civicrm_participant',
223 'note' => $params['note'],
224 'entity_id' => $this->_participantId,
225 'contact_id' => $this->_contactId,
226 'modified_date' => date('Ymd'),
227 );
228 CRM_Core_BAO_Note::add($noteParams);
229 }
230 CRM_Core_Session::setStatus(ts("The fee selection has been changed for this participant"), ts('Saved'), 'success');
231
2429e40c
PJ
232 $buttonName = $this->controller->getButtonName();
233 if ($buttonName == $this->getButtonName('upload', 'new')) {
0aaf8fe9 234 $session = CRM_Core_Session::singleton();
1010c4e1 235 $session->pushUserContext(CRM_Utils_System::url('civicrm/payment/add',
2429e40c
PJ
236 "reset=1&action=add&component=event&id={$this->_participantId}&cid={$this->_contactId}"
237 ));
238 }
0aaf8fe9
PJ
239 }
240
241 function emailReceipt(&$params) {
d5397f2f 242 $updatedLineItem = CRM_Price_BAO_LineItem::getLineItems($this->_participantId, 'participant', NULL, FALSE);
d7b68882
PJ
243 $lineItem = array();
244 if ($updatedLineItem) {
245 $lineItem[] = $updatedLineItem;
246 }
247 $this->assign('lineItem', empty($lineItem) ? FALSE : $lineItem);
248
0aaf8fe9
PJ
249 // offline receipt sending
250 if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) {
251 $receiptFrom = $params['from_email_address'];
252 }
253
254 $this->assign('module', 'Event Registration');
255 //use of the message template below requires variables in different format
256 $event = $events = array();
257 $returnProperties = array('fee_label', 'start_date', 'end_date', 'is_show_location', 'title');
258
259 //get all event details.
260 CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $params['event_id'], $events, $returnProperties);
261 $event = $events[$params['event_id']];
262 unset($event['start_date']);
263 unset($event['end_date']);
264
265 $role = CRM_Event_PseudoConstant::participantRole();
266 $participantRoles = CRM_Utils_Array::value('role_id', $params);
267 if (is_array($participantRoles)) {
268 $selectedRoles = array();
269 foreach (array_keys($participantRoles) as $roleId) {
270 $selectedRoles[] = $role[$roleId];
271 }
272 $event['participant_role'] = implode(', ', $selectedRoles);
273 }
274 else {
275 $event['participant_role'] = CRM_Utils_Array::value($participantRoles, $role);
276 }
277 $event['is_monetary'] = $this->_isPaidEvent;
278
279 if ($params['receipt_text']) {
280 $event['confirm_email_text'] = $params['receipt_text'];
281 }
282
283 $this->assign('isAmountzero', 1);
284 $this->assign('event', $event);
285
286 $this->assign('isShowLocation', $event['is_show_location']);
287 if (CRM_Utils_Array::value('is_show_location', $event) == 1) {
288 $locationParams = array(
289 'entity_id' => $params['event_id'],
290 'entity_table' => 'civicrm_event',
291 );
292 $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
293 $this->assign('location', $location);
294 }
295
296 $status = CRM_Event_PseudoConstant::participantStatus();
297 if ($this->_isPaidEvent) {
298 $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
299 if (!$this->_mode) {
300 if (isset($params['payment_instrument_id'])) {
301 $this->assign('paidBy',
302 CRM_Utils_Array::value($params['payment_instrument_id'],
303 $paymentInstrument
304 )
305 );
306 }
307 }
308
309 $this->assign('totalAmount', $this->contributionAmt);
310
311 $this->assign('isPrimary', 1);
312 $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params));
313 }
314
315 $this->assign('register_date', $params['register_date']);
316 $template = CRM_Core_Smarty::singleton();
317
318 // Retrieve the name and email of the contact - this will be the TO for receipt email
319 list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId);
320
321 $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName;
322
323 $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
324 if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) {
325 $this->assign('isOnWaitlist', TRUE);
326 }
327 $this->assign('contactID', $this->_contactId);
328 $this->assign('participantID', $this->_participantId);
329
330 $sendTemplateParams = array(
331 'groupName' => 'msg_tpl_workflow_event',
332 'valueName' => 'event_offline_receipt',
333 'contactId' => $this->_contactId,
334 'isTest' => FALSE,
335 'PDFFilename' => ts('confirmation').'.pdf',
336 );
337
338 // try to send emails only if email id is present
339 // and the do-not-email option is not checked for that contact
340 if ($this->_contributorEmail && !$this->_toDoNotEmail) {
341 $sendTemplateParams['from'] = $receiptFrom;
342 $sendTemplateParams['toName'] = $this->_contributorDisplayName;
343 $sendTemplateParams['toEmail'] = $this->_contributorEmail;
344 $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc', $this->_fromEmails);
345 $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc', $this->_fromEmails);
346 }
bc2eeabb 347
0aaf8fe9
PJ
348 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
349 return $mailSent;
bc2eeabb 350 }
d5397f2f 351}