Commit | Line | Data |
---|---|---|
97d8187a | 1 | <?php |
97d8187a DG |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
97d8187a | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
97d8187a DG |
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 | * @package CRM | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
97d8187a DG |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components to allow an Event to be cancelled or transferred from an email link | |
38 | * | |
39 | */ | |
40 | class CRM_Event_Form_SelfSvcUpdate extends CRM_Core_Form { | |
41 | /** | |
42 | * particpant id | |
43 | * | |
44 | * @var string | |
45 | * | |
46 | */ | |
47 | protected $_participant_id; | |
48 | /** | |
49 | * contact id | |
50 | * | |
51 | * @var string | |
52 | * | |
53 | */ | |
54 | protected $_contact_id; | |
55 | /** | |
56 | * name of the particpant | |
57 | * | |
58 | * @var string | |
59 | * | |
60 | */ | |
61 | protected $_contact_name; | |
62 | /** | |
63 | * email of participant | |
64 | * | |
65 | * @var string | |
66 | */ | |
67 | protected $_contact_email; | |
68 | /** | |
69 | * event to be cancelled/transferred | |
70 | * | |
71 | * @var string | |
72 | */ | |
73 | protected $_event_id; | |
74 | /** | |
75 | * event title | |
76 | * | |
77 | * @var string | |
78 | */ | |
79 | protected $_event_title; | |
80 | /** | |
81 | * event title | |
82 | * | |
83 | * @var string | |
84 | */ | |
85 | protected $_event_start_date; | |
86 | /** | |
87 | * action | |
88 | * | |
89 | * @var string | |
90 | */ | |
3a936dab | 91 | public $_action; |
97d8187a DG |
92 | /** |
93 | * participant object | |
94 | * | |
95 | * @var string | |
96 | */ | |
be2fb01f | 97 | protected $_participant = []; |
97d8187a DG |
98 | /** |
99 | * particpant values | |
100 | * | |
101 | * @var string | |
102 | */ | |
103 | protected $_part_values; | |
104 | /** | |
105 | * details of event registration values | |
106 | * | |
107 | * @var array | |
108 | */ | |
be2fb01f | 109 | protected $_details = []; |
e219d53b | 110 | /** |
111 | * Is backoffice form? | |
112 | * | |
90b461f1 | 113 | * @var bool |
e219d53b | 114 | */ |
115 | protected $isBackoffice = FALSE; | |
90b461f1 | 116 | |
97d8187a DG |
117 | /** |
118 | * Set variables up before form is built based on participant ID from URL | |
119 | * | |
120 | * @return void | |
121 | */ | |
122 | public function preProcess() { | |
123 | $config = CRM_Core_Config::singleton(); | |
124 | $session = CRM_Core_Session::singleton(); | |
125 | $this->_userContext = $session->readUserContext(); | |
be2fb01f | 126 | $participant = $values = []; |
97d8187a | 127 | $this->_participant_id = CRM_Utils_Request::retrieve('pid', 'Positive', $this, FALSE, NULL, 'REQUEST'); |
af29aaac | 128 | $this->_userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this, FALSE, NULL, 'REQUEST'); |
e219d53b | 129 | $this->isBackoffice = CRM_Utils_Request::retrieve('is_backoffice', 'String', $this, FALSE, NULL, 'REQUEST'); |
be2fb01f | 130 | $params = ['id' => $this->_participant_id]; |
97d8187a DG |
131 | $this->_participant = CRM_Event_BAO_Participant::getValues($params, $values, $participant); |
132 | $this->_part_values = $values[$this->_participant_id]; | |
133 | $this->set('values', $this->_part_values); | |
134 | //fetch Event by event_id, verify that this event can still be xferred/cancelled | |
135 | $this->_event_id = $this->_part_values['event_id']; | |
af29aaac | 136 | $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}"); |
97d8187a | 137 | $this->_contact_id = $this->_part_values['participant_contact_id']; |
af29aaac | 138 | $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($this->_contact_id, $this->_userChecksum); |
e3b510fe | 139 | if (!$validUser && !CRM_Core_Permission::check('edit all events')) { |
140 | CRM_Core_Error::statusBounce(ts('You do not have sufficient permission to transfer/cancel this participant.'), $url); | |
141 | } | |
97d8187a DG |
142 | $this->assign('action', $this->_action); |
143 | if ($this->_participant_id) { | |
144 | $this->assign('participantId', $this->_participant_id); | |
145 | } | |
be2fb01f | 146 | $event = []; |
97d8187a DG |
147 | $daoName = 'title'; |
148 | $this->_event_title = CRM_Event_BAO_Event::getFieldValue('CRM_Event_DAO_Event', $this->_event_id, $daoName); | |
149 | $daoName = 'start_date'; | |
150 | $this->_event_start_date = CRM_Event_BAO_Event::getFieldValue('CRM_Event_DAO_Event', $this->_event_id, $daoName); | |
151 | list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contact_id); | |
152 | $this->_contact_name = $displayName; | |
153 | $this->_contact_email = $email; | |
be2fb01f | 154 | $details = []; |
97d8187a | 155 | $details = CRM_Event_BAO_Participant::participantDetails($this->_participant_id); |
4abc4ee1 | 156 | $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'participant_role', 'id', 'name'); |
157 | $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participant_id, 'contribution_id', 'participant_id'); | |
158 | $this->assign('contributionId', $contributionId); | |
97d8187a | 159 | $query = " |
974bdb44 | 160 | SELECT cpst.name as status, cov.name as role, cp.fee_level, cp.fee_amount, cp.register_date, cp.status_id, civicrm_event.start_date |
97d8187a DG |
161 | FROM civicrm_participant cp |
162 | LEFT JOIN civicrm_participant_status_type cpst ON cpst.id = cp.status_id | |
4abc4ee1 | 163 | LEFT JOIN civicrm_option_value cov ON cov.value = cp.role_id and cov.option_group_id = {$optionGroupId} |
974bdb44 | 164 | LEFT JOIN civicrm_event ON civicrm_event.id = cp.event_id |
97d8187a | 165 | WHERE cp.id = {$this->_participant_id}"; |
33621c4f | 166 | $dao = CRM_Core_DAO::executeQuery($query); |
97d8187a DG |
167 | while ($dao->fetch()) { |
168 | $details['status'] = $dao->status; | |
169 | $details['role'] = $dao->role; | |
e219d53b | 170 | $details['fee_level'] = trim($dao->fee_level, CRM_Core_DAO::VALUE_SEPARATOR); |
97d8187a DG |
171 | $details['fee_amount'] = $dao->fee_amount; |
172 | $details['register_date'] = $dao->register_date; | |
0d21c8b1 | 173 | $details['event_start_date'] = $dao->start_date; |
97d8187a DG |
174 | } |
175 | //verify participant status is still Registered | |
176 | if ($details['status'] != "Registered") { | |
8b9023b8 DG |
177 | $status = "You cannot transfer or cancel your registration for " . $this->_event_title . ' as you are not currently registered for this event.'; |
178 | CRM_Core_Session::setStatus($status, ts('Sorry'), 'alert'); | |
4abc4ee1 | 179 | CRM_Utils_System::redirect($url); |
97d8187a DG |
180 | } |
181 | $query = "select start_date as start, selfcancelxfer_time as time from civicrm_event where id = " . $this->_event_id; | |
33621c4f | 182 | $dao = CRM_Core_DAO::executeQuery($query); |
97d8187a DG |
183 | while ($dao->fetch()) { |
184 | $time_limit = $dao->time; | |
185 | $start_date = $dao->start; | |
186 | } | |
4abc4ee1 | 187 | $start_time = new Datetime($start_date); |
188 | $timenow = new Datetime(); | |
e219d53b | 189 | if (!$this->isBackoffice && !empty($start_time) && $start_time < $timenow) { |
8b9023b8 DG |
190 | $status = ts("Registration for this event cannot be cancelled or transferred once the event has begun. Contact the event organizer if you have questions."); |
191 | CRM_Core_Error::statusBounce($status, $url, ts('Sorry')); | |
4abc4ee1 | 192 | } |
e219d53b | 193 | if (!$this->isBackoffice && !empty($time_limit) && $time_limit > 0) { |
97d8187a DG |
194 | $interval = $timenow->diff($start_time); |
195 | $days = $interval->format('%d'); | |
196 | $hours = $interval->format('%h'); | |
197 | if ($hours <= $time_limit && $days < 1) { | |
be2fb01f | 198 | $status = ts("Registration for this event cannot be cancelled or transferred less than %1 hours prior to the event's start time. Contact the event organizer if you have questions.", [1 => $time_limit]); |
8b9023b8 | 199 | CRM_Core_Error::statusBounce($status, $url, ts('Sorry')); |
97d8187a DG |
200 | } |
201 | } | |
202 | $this->assign('details', $details); | |
203 | $this->selfsvcupdateUrl = CRM_Utils_System::url('civicrm/event/selfsvcupdate', "reset=1&id={$this->_participant_id}&id=0"); | |
204 | $this->selfsvcupdateText = ts('Update'); | |
205 | $this->selfsvcupdateButtonText = ts('Update'); | |
206 | // Based on those ids retrieve event and verify it is eligible | |
207 | // for self update (event.start_date > today, event can be 'self_updated' | |
208 | // retrieve contact name and email, and let user verify his/her identity | |
209 | } | |
90b461f1 | 210 | |
97d8187a DG |
211 | /** |
212 | * buildQuickForm -populate input variables for source Event | |
213 | * to cancel or transfer to another person | |
214 | * | |
215 | * return @void | |
216 | */ | |
217 | public function buildQuickForm() { | |
be2fb01f CW |
218 | $this->add('select', 'action', ts('Transfer or Cancel Registration'), [ts('-select-'), ts('Transfer'), ts('Cancel')], TRUE); |
219 | $this->addButtons([ | |
220 | [ | |
97d8187a DG |
221 | 'type' => 'submit', |
222 | 'name' => ts('Submit'), | |
be2fb01f CW |
223 | ], |
224 | ]); | |
225 | $this->addFormRule(['CRM_Event_Form_SelfSvcUpdate', 'formRule'], $this); | |
97d8187a DG |
226 | parent::buildQuickForm(); |
227 | } | |
974bdb44 | 228 | |
97d8187a DG |
229 | /** |
230 | * Set default values for contact | |
231 | * | |
232 | * return @void | |
233 | */ | |
234 | public function setDefaultValues() { | |
be2fb01f | 235 | $this->_defaults = []; |
97d8187a DG |
236 | $this->_defaults['details'] = $this->_details; |
237 | return $this->_defaults; | |
238 | } | |
974bdb44 | 239 | |
e3b510fe | 240 | /** |
241 | * Validate action input | |
242 | * @param array $fields | |
243 | * Posted fields of the form. | |
244 | * @param $files | |
245 | * @param $self | |
246 | * | |
247 | * @return array | |
248 | * list of errors to be posted back to the form | |
249 | */ | |
250 | public static function formRule($fields, $files, $self) { | |
be2fb01f | 251 | $errors = []; |
e3b510fe | 252 | if (empty($fields['action'])) { |
253 | $errors['action'] = ts("Please select Transfer OR Cancel action."); | |
254 | } | |
255 | return empty($errors) ? TRUE : $errors; | |
256 | } | |
257 | ||
97d8187a DG |
258 | /** |
259 | * Process submit form - based on user selection of action | |
260 | * transfer or cancel the event | |
261 | * | |
262 | * return @void | |
263 | */ | |
264 | public function postProcess() { | |
265 | //if selection is cancel, cancel this participant' registration, process refund | |
266 | //if transfer, process form to allow selection of transferree | |
267 | $params = $this->controller->exportValues($this->_name); | |
268 | $action = $params['action']; | |
269 | if ($action == "1") { | |
270 | $action = "Transfer Event"; | |
271 | $this->transferParticipant($params); | |
272 | } | |
273 | elseif ($action == "2") { | |
274 | $action = "Cancel Event"; | |
275 | $this->cancelParticipant($params); | |
276 | } | |
277 | } | |
974bdb44 | 278 | |
97d8187a DG |
279 | /** |
280 | * Transfer to a new form, allowing selection of a new contact | |
281 | * based on email and name. The Event will be transferred to this new participant | |
282 | * | |
283 | * return @void | |
284 | */ | |
285 | public function transferParticipant($params) { | |
e219d53b | 286 | CRM_Utils_System::redirect(CRM_Utils_System::url( |
287 | 'civicrm/event/selfsvctransfer', | |
be2fb01f | 288 | [ |
e219d53b | 289 | 'reset' => 1, |
290 | 'action' => 'add', | |
291 | 'pid' => $this->_participant_id, | |
292 | 'cs' => $this->_userChecksum, | |
cb98c7f9 | 293 | 'is_backoffice' => $this->isBackoffice, |
be2fb01f | 294 | ] |
e219d53b | 295 | )); |
97d8187a | 296 | } |
974bdb44 | 297 | |
97d8187a DG |
298 | /** |
299 | * Cancel this participant and finish, send cancellation email. At this point no | |
300 | * auto-cancellation of payment is handled, so payment needs to be manually cancelled | |
301 | * | |
302 | * return @void | |
303 | */ | |
304 | public function cancelParticipant($params) { | |
305 | //set participant record status to Cancelled, refund payment if possible | |
306 | // send email to participant and admin, and log Activity | |
be2fb01f | 307 | $value = []; |
97d8187a DG |
308 | $value['id'] = $this->_participant_id; |
309 | $cancelledId = array_search('Cancelled', | |
310 | CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'")); | |
311 | $value['status_id'] = $cancelledId; | |
312 | CRM_Event_BAO_Participant::create($value); | |
be2fb01f | 313 | $domainValues = []; |
97d8187a | 314 | $domain = CRM_Core_BAO_Domain::getDomain(); |
be2fb01f | 315 | $tokens = [ |
97d8187a | 316 | 'domain' => |
be2fb01f | 317 | [ |
97d8187a DG |
318 | 'name', |
319 | 'phone', | |
320 | 'address', | |
321 | 'email', | |
be2fb01f | 322 | ], |
97d8187a | 323 | 'contact' => CRM_Core_SelectValues::contactTokens(), |
be2fb01f | 324 | ]; |
97d8187a DG |
325 | foreach ($tokens['domain'] as $token) { |
326 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); | |
327 | } | |
be2fb01f | 328 | $participantRoles = []; |
97d8187a | 329 | $participantRoles = CRM_Event_PseudoConstant::participantRole(); |
be2fb01f | 330 | $participantDetails = []; |
97d8187a DG |
331 | $query = "SELECT * FROM civicrm_participant WHERE id = {$this->_participant_id}"; |
332 | $dao = CRM_Core_DAO::executeQuery($query); | |
333 | while ($dao->fetch()) { | |
be2fb01f | 334 | $participantDetails[$dao->id] = [ |
97d8187a DG |
335 | 'id' => $dao->id, |
336 | 'role' => $participantRoles[$dao->role_id], | |
337 | 'is_test' => $dao->is_test, | |
338 | 'event_id' => $dao->event_id, | |
339 | 'status_id' => $dao->status_id, | |
340 | 'fee_amount' => $dao->fee_amount, | |
341 | 'contact_id' => $dao->contact_id, | |
342 | 'register_date' => $dao->register_date, | |
343 | 'registered_by_id' => $dao->registered_by_id, | |
be2fb01f | 344 | ]; |
97d8187a | 345 | } |
be2fb01f CW |
346 | $eventDetails = []; |
347 | $eventParams = ['id' => $this->_event_id]; | |
97d8187a DG |
348 | CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails[$this->_event_id]); |
349 | //get default participant role. | |
350 | $eventDetails[$this->_event_id]['participant_role'] = CRM_Utils_Array::value($eventDetails[$this->_event_id]['default_role_id'], $participantRoles); | |
351 | //get the location info | |
be2fb01f | 352 | $locParams = ['entity_id' => $this->_event_id, 'entity_table' => 'civicrm_event']; |
97d8187a DG |
353 | $eventDetails[$this->_event_id]['location'] = CRM_Core_BAO_Location::getValues($locParams, TRUE); |
354 | //get contact details | |
355 | $contactIds[$this->_contact_id] = $this->_contact_id; | |
356 | list($currentContactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL, | |
be2fb01f | 357 | FALSE, FALSE, NULL, [], |
97d8187a DG |
358 | 'CRM_Event_BAO_Participant' |
359 | ); | |
360 | foreach ($currentContactDetails as $contactId => $contactValues) { | |
361 | $contactDetails[$this->_contact_id] = $contactValues; | |
362 | } | |
363 | //send a 'cancelled' email to user, and cc the event's cc_confirm email | |
364 | $mail = CRM_Event_BAO_Participant::sendTransitionParticipantMail($this->_participant_id, | |
365 | $participantDetails[$this->_participant_id], | |
366 | $eventDetails[$this->_event_id], | |
367 | $contactDetails[$this->_contact_id], | |
368 | $domainValues, | |
369 | "Cancelled", | |
370 | "" | |
371 | ); | |
be2fb01f CW |
372 | $statusMsg = ts('Event registration information for %1 has been updated.', [1 => $this->_contact_name]); |
373 | $statusMsg .= ' ' . ts('A cancellation email has been sent to %1.', [1 => $this->_contact_email]); | |
8b9023b8 | 374 | CRM_Core_Session::setStatus($statusMsg, ts('Thanks'), 'success'); |
e219d53b | 375 | if (!empty($this->isBackoffice)) { |
376 | return; | |
377 | } | |
4abc4ee1 | 378 | $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}&noFullMsg=true"); |
379 | CRM_Utils_System::redirect($url); | |
97d8187a DG |
380 | } |
381 | ||
382 | } |