3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * This class generates form components to allow an Event to be cancelled or transferred from an email link
24 class CRM_Event_Form_SelfSvcUpdate
extends CRM_Core_Form
{
31 protected $_participant_id;
38 protected $_contact_id;
40 * name of the particpant
45 protected $_contact_name;
47 * email of participant
51 protected $_contact_email;
53 * event to be cancelled/transferred
63 protected $_event_title;
69 protected $_event_start_date;
81 protected $_participant = [];
87 protected $_part_values;
89 * details of event registration values
93 protected $_details = [];
99 protected $isBackoffice = FALSE;
102 * Set variables up before form is built based on participant ID from URL
106 public function preProcess() {
107 $config = CRM_Core_Config
::singleton();
108 $session = CRM_Core_Session
::singleton();
109 $this->_userContext
= $session->readUserContext();
110 $participant = $values = [];
111 $this->_participant_id
= CRM_Utils_Request
::retrieve('pid', 'Positive', $this, FALSE, NULL, 'REQUEST');
112 $this->_userChecksum
= CRM_Utils_Request
::retrieve('cs', 'String', $this, FALSE, NULL, 'REQUEST');
113 $this->isBackoffice
= CRM_Utils_Request
::retrieve('is_backoffice', 'String', $this, FALSE, NULL, 'REQUEST');
114 $params = ['id' => $this->_participant_id
];
115 $this->_participant
= CRM_Event_BAO_Participant
::getValues($params, $values, $participant);
116 $this->_part_values
= $values[$this->_participant_id
];
117 $this->set('values', $this->_part_values
);
118 //fetch Event by event_id, verify that this event can still be xferred/cancelled
119 $this->_event_id
= $this->_part_values
['event_id'];
120 $url = CRM_Utils_System
::url('civicrm/event/info', "reset=1&id={$this->_event_id}");
121 $this->_contact_id
= $this->_part_values
['participant_contact_id'];
122 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($this->_contact_id
, $this->_userChecksum
);
123 if (!$validUser && !CRM_Core_Permission
::check('edit all events')) {
124 CRM_Core_Error
::statusBounce(ts('You do not have sufficient permission to transfer/cancel this participant.'), $url);
126 $this->assign('action', $this->_action
);
127 if ($this->_participant_id
) {
128 $this->assign('participantId', $this->_participant_id
);
132 $this->_event_title
= CRM_Event_BAO_Event
::getFieldValue('CRM_Event_DAO_Event', $this->_event_id
, $daoName);
133 $daoName = 'start_date';
134 $this->_event_start_date
= CRM_Event_BAO_Event
::getFieldValue('CRM_Event_DAO_Event', $this->_event_id
, $daoName);
135 list($displayName, $email) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contact_id
);
136 $this->_contact_name
= $displayName;
137 $this->_contact_email
= $email;
139 $details = CRM_Event_BAO_Participant
::participantDetails($this->_participant_id
);
140 $optionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'participant_role', 'id', 'name');
141 $contributionId = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participant_id
, 'contribution_id', 'participant_id');
142 $this->assign('contributionId', $contributionId);
144 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
145 FROM civicrm_participant cp
146 LEFT JOIN civicrm_participant_status_type cpst ON cpst.id = cp.status_id
147 LEFT JOIN civicrm_option_value cov ON cov.value = cp.role_id and cov.option_group_id = {$optionGroupId}
148 LEFT JOIN civicrm_event ON civicrm_event.id = cp.event_id
149 WHERE cp.id = {$this->_participant_id}";
150 $dao = CRM_Core_DAO
::executeQuery($query);
151 while ($dao->fetch()) {
152 $details['status'] = $dao->status
;
153 $details['role'] = $dao->role
;
154 $details['fee_level'] = trim($dao->fee_level
, CRM_Core_DAO
::VALUE_SEPARATOR
);
155 $details['fee_amount'] = $dao->fee_amount
;
156 $details['register_date'] = $dao->register_date
;
157 $details['event_start_date'] = $dao->start_date
;
159 //verify participant status is still Registered
160 if ($details['status'] != "Registered") {
161 $status = "You cannot transfer or cancel your registration for " . $this->_event_title
. ' as you are not currently registered for this event.';
162 CRM_Core_Session
::setStatus($status, ts('Sorry'), 'alert');
163 CRM_Utils_System
::redirect($url);
165 $query = "select start_date as start, selfcancelxfer_time as time from civicrm_event where id = " . $this->_event_id
;
166 $dao = CRM_Core_DAO
::executeQuery($query);
167 while ($dao->fetch()) {
168 $time_limit = $dao->time
;
169 $start_date = $dao->start
;
171 $start_time = new Datetime($start_date);
172 $timenow = new Datetime();
173 if (!$this->isBackoffice
&& !empty($start_time) && $start_time < $timenow) {
174 $status = ts("Registration for this event cannot be cancelled or transferred once the event has begun. Contact the event organizer if you have questions.");
175 CRM_Core_Error
::statusBounce($status, $url, ts('Sorry'));
177 if (!$this->isBackoffice
&& !empty($time_limit) && $time_limit > 0) {
178 $interval = $timenow->diff($start_time);
179 $days = $interval->format('%d');
180 $hours = $interval->format('%h');
181 if ($hours <= $time_limit && $days < 1) {
182 $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]);
183 CRM_Core_Error
::statusBounce($status, $url, ts('Sorry'));
186 $this->assign('details', $details);
187 $this->selfsvcupdateUrl
= CRM_Utils_System
::url('civicrm/event/selfsvcupdate', "reset=1&id={$this->_participant_id}&id=0");
188 $this->selfsvcupdateText
= ts('Update');
189 $this->selfsvcupdateButtonText
= ts('Update');
190 // Based on those ids retrieve event and verify it is eligible
191 // for self update (event.start_date > today, event can be 'self_updated'
192 // retrieve contact name and email, and let user verify his/her identity
196 * buildQuickForm -populate input variables for source Event
197 * to cancel or transfer to another person
201 public function buildQuickForm() {
202 $this->add('select', 'action', ts('Transfer or Cancel Registration'), [ts('-select-'), ts('Transfer'), ts('Cancel')], TRUE);
206 'name' => ts('Submit'),
209 $this->addFormRule(['CRM_Event_Form_SelfSvcUpdate', 'formRule'], $this);
210 parent
::buildQuickForm();
214 * Set default values for contact
218 public function setDefaultValues() {
219 $this->_defaults
= [];
220 $this->_defaults
['details'] = $this->_details
;
221 return $this->_defaults
;
225 * Validate action input
226 * @param array $fields
227 * Posted fields of the form.
232 * list of errors to be posted back to the form
234 public static function formRule($fields, $files, $self) {
236 if (empty($fields['action'])) {
237 $errors['action'] = ts("Please select Transfer OR Cancel action.");
239 return empty($errors) ?
TRUE : $errors;
243 * Process submit form - based on user selection of action
244 * transfer or cancel the event
248 public function postProcess() {
249 //if selection is cancel, cancel this participant' registration, process refund
250 //if transfer, process form to allow selection of transferree
251 $params = $this->controller
->exportValues($this->_name
);
252 $action = $params['action'];
253 if ($action == "1") {
254 $action = "Transfer Event";
255 $this->transferParticipant($params);
257 elseif ($action == "2") {
258 $action = "Cancel Event";
259 $this->cancelParticipant($params);
264 * Transfer to a new form, allowing selection of a new contact
265 * based on email and name. The Event will be transferred to this new participant
269 public function transferParticipant($params) {
270 CRM_Utils_System
::redirect(CRM_Utils_System
::url(
271 'civicrm/event/selfsvctransfer',
275 'pid' => $this->_participant_id
,
276 'cs' => $this->_userChecksum
,
277 'is_backoffice' => $this->isBackoffice
,
283 * Cancel this participant and finish, send cancellation email. At this point no
284 * auto-cancellation of payment is handled, so payment needs to be manually cancelled
288 public function cancelParticipant($params) {
289 //set participant record status to Cancelled, refund payment if possible
290 // send email to participant and admin, and log Activity
292 $value['id'] = $this->_participant_id
;
293 $cancelledId = array_search('Cancelled',
294 CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Negative'"));
295 $value['status_id'] = $cancelledId;
296 CRM_Event_BAO_Participant
::create($value);
298 $domain = CRM_Core_BAO_Domain
::getDomain();
307 'contact' => CRM_Core_SelectValues
::contactTokens(),
309 foreach ($tokens['domain'] as $token) {
310 $domainValues[$token] = CRM_Utils_Token
::getDomainTokenReplacement($token, $domain);
312 $participantRoles = [];
313 $participantRoles = CRM_Event_PseudoConstant
::participantRole();
314 $participantDetails = [];
315 $query = "SELECT * FROM civicrm_participant WHERE id = {$this->_participant_id}";
316 $dao = CRM_Core_DAO
::executeQuery($query);
317 while ($dao->fetch()) {
318 $participantDetails[$dao->id
] = [
320 'role' => $participantRoles[$dao->role_id
],
321 'is_test' => $dao->is_test
,
322 'event_id' => $dao->event_id
,
323 'status_id' => $dao->status_id
,
324 'fee_amount' => $dao->fee_amount
,
325 'contact_id' => $dao->contact_id
,
326 'register_date' => $dao->register_date
,
327 'registered_by_id' => $dao->registered_by_id
,
331 $eventParams = ['id' => $this->_event_id
];
332 CRM_Event_BAO_Event
::retrieve($eventParams, $eventDetails[$this->_event_id
]);
333 //get default participant role.
334 $eventDetails[$this->_event_id
]['participant_role'] = CRM_Utils_Array
::value($eventDetails[$this->_event_id
]['default_role_id'], $participantRoles);
335 //get the location info
336 $locParams = ['entity_id' => $this->_event_id
, 'entity_table' => 'civicrm_event'];
337 $eventDetails[$this->_event_id
]['location'] = CRM_Core_BAO_Location
::getValues($locParams, TRUE);
338 //get contact details
339 $contactIds[$this->_contact_id
] = $this->_contact_id
;
340 list($currentContactDetails) = CRM_Utils_Token
::getTokenDetails($contactIds, NULL,
341 FALSE, FALSE, NULL, [],
342 'CRM_Event_BAO_Participant'
344 foreach ($currentContactDetails as $contactId => $contactValues) {
345 $contactDetails[$this->_contact_id
] = $contactValues;
347 //send a 'cancelled' email to user, and cc the event's cc_confirm email
348 $mail = CRM_Event_BAO_Participant
::sendTransitionParticipantMail($this->_participant_id
,
349 $participantDetails[$this->_participant_id
],
350 $eventDetails[$this->_event_id
],
351 $contactDetails[$this->_contact_id
],
356 $statusMsg = ts('Event registration information for %1 has been updated.', [1 => $this->_contact_name
]);
357 $statusMsg .= ' ' . ts('A cancellation email has been sent to %1.', [1 => $this->_contact_email
]);
358 CRM_Core_Session
::setStatus($statusMsg, ts('Thanks'), 'success');
359 if (!empty($this->isBackoffice
)) {
362 $url = CRM_Utils_System
::url('civicrm/event/info', "reset=1&id={$this->_event_id}&noFullMsg=true");
363 CRM_Utils_System
::redirect($url);