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