3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
37 * This class generates form components to allow an Event to be cancelled or transferred from an email link
40 class CRM_Event_Form_SelfSvcUpdate
extends CRM_Core_Form
{
47 protected $_participant_id;
54 protected $_contact_id;
56 * name of the particpant
61 protected $_contact_name;
63 * email of participant
67 protected $_contact_email;
69 * event to be cancelled/transferred
79 protected $_event_title;
85 protected $_event_start_date;
97 protected $_participant = [];
103 protected $_part_values;
105 * details of event registration values
109 protected $_details = [];
111 * Is backoffice form?
115 protected $isBackoffice = FALSE;
118 * Set variables up before form is built based on participant ID from URL
122 public function preProcess() {
123 $config = CRM_Core_Config
::singleton();
124 $session = CRM_Core_Session
::singleton();
125 $this->_userContext
= $session->readUserContext();
126 $participant = $values = [];
127 $this->_participant_id
= CRM_Utils_Request
::retrieve('pid', 'Positive', $this, FALSE, NULL, 'REQUEST');
128 $this->_userChecksum
= CRM_Utils_Request
::retrieve('cs', 'String', $this, FALSE, NULL, 'REQUEST');
129 $this->isBackoffice
= CRM_Utils_Request
::retrieve('is_backoffice', 'String', $this, FALSE, NULL, 'REQUEST');
130 $params = ['id' => $this->_participant_id
];
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'];
136 $url = CRM_Utils_System
::url('civicrm/event/info', "reset=1&id={$this->_event_id}");
137 $this->_contact_id
= $this->_part_values
['participant_contact_id'];
138 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($this->_contact_id
, $this->_userChecksum
);
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);
142 $this->assign('action', $this->_action
);
143 if ($this->_participant_id
) {
144 $this->assign('participantId', $this->_participant_id
);
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;
155 $details = CRM_Event_BAO_Participant
::participantDetails($this->_participant_id
);
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);
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
161 FROM civicrm_participant cp
162 LEFT JOIN civicrm_participant_status_type cpst ON cpst.id = cp.status_id
163 LEFT JOIN civicrm_option_value cov ON cov.value = cp.role_id and cov.option_group_id = {$optionGroupId}
164 LEFT JOIN civicrm_event ON civicrm_event.id = cp.event_id
165 WHERE cp.id = {$this->_participant_id}";
166 $dao = CRM_Core_DAO
::executeQuery($query);
167 while ($dao->fetch()) {
168 $details['status'] = $dao->status
;
169 $details['role'] = $dao->role
;
170 $details['fee_level'] = trim($dao->fee_level
, CRM_Core_DAO
::VALUE_SEPARATOR
);
171 $details['fee_amount'] = $dao->fee_amount
;
172 $details['register_date'] = $dao->register_date
;
173 $details['event_start_date'] = $dao->start_date
;
175 //verify participant status is still Registered
176 if ($details['status'] != "Registered") {
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');
179 CRM_Utils_System
::redirect($url);
181 $query = "select start_date as start, selfcancelxfer_time as time from civicrm_event where id = " . $this->_event_id
;
182 $dao = CRM_Core_DAO
::executeQuery($query);
183 while ($dao->fetch()) {
184 $time_limit = $dao->time
;
185 $start_date = $dao->start
;
187 $start_time = new Datetime($start_date);
188 $timenow = new Datetime();
189 if (!$this->isBackoffice
&& !empty($start_time) && $start_time < $timenow) {
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'));
193 if (!$this->isBackoffice
&& !empty($time_limit) && $time_limit > 0) {
194 $interval = $timenow->diff($start_time);
195 $days = $interval->format('%d');
196 $hours = $interval->format('%h');
197 if ($hours <= $time_limit && $days < 1) {
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]);
199 CRM_Core_Error
::statusBounce($status, $url, ts('Sorry'));
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
212 * buildQuickForm -populate input variables for source Event
213 * to cancel or transfer to another person
217 public function buildQuickForm() {
218 $this->add('select', 'action', ts('Transfer or Cancel Registration'), [ts('-select-'), ts('Transfer'), ts('Cancel')], TRUE);
222 'name' => ts('Submit'),
225 $this->addFormRule(['CRM_Event_Form_SelfSvcUpdate', 'formRule'], $this);
226 parent
::buildQuickForm();
230 * Set default values for contact
234 public function setDefaultValues() {
235 $this->_defaults
= [];
236 $this->_defaults
['details'] = $this->_details
;
237 return $this->_defaults
;
241 * Validate action input
242 * @param array $fields
243 * Posted fields of the form.
248 * list of errors to be posted back to the form
250 public static function formRule($fields, $files, $self) {
252 if (empty($fields['action'])) {
253 $errors['action'] = ts("Please select Transfer OR Cancel action.");
255 return empty($errors) ?
TRUE : $errors;
259 * Process submit form - based on user selection of action
260 * transfer or cancel the event
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);
273 elseif ($action == "2") {
274 $action = "Cancel Event";
275 $this->cancelParticipant($params);
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
285 public function transferParticipant($params) {
286 $isBackOfficeArg = $this->isBackoffice ?
'&is_backoffice=1' : '';
287 CRM_Utils_System
::redirect(CRM_Utils_System
::url(
288 'civicrm/event/selfsvctransfer',
292 'pid' => $this->_participant_id
,
293 'cs' => $this->_userChecksum
,
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
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
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);
314 $domain = CRM_Core_BAO_Domain
::getDomain();
323 'contact' => CRM_Core_SelectValues
::contactTokens(),
325 foreach ($tokens['domain'] as $token) {
326 $domainValues[$token] = CRM_Utils_Token
::getDomainTokenReplacement($token, $domain);
328 $participantRoles = [];
329 $participantRoles = CRM_Event_PseudoConstant
::participantRole();
330 $participantDetails = [];
331 $query = "SELECT * FROM civicrm_participant WHERE id = {$this->_participant_id}";
332 $dao = CRM_Core_DAO
::executeQuery($query);
333 while ($dao->fetch()) {
334 $participantDetails[$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
,
347 $eventParams = ['id' => $this->_event_id
];
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
352 $locParams = ['entity_id' => $this->_event_id
, 'entity_table' => 'civicrm_event'];
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,
357 FALSE, FALSE, NULL, [],
358 'CRM_Event_BAO_Participant'
360 foreach ($currentContactDetails as $contactId => $contactValues) {
361 $contactDetails[$this->_contact_id
] = $contactValues;
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
],
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
]);
374 CRM_Core_Session
::setStatus($statusMsg, ts('Thanks'), 'success');
375 if (!empty($this->isBackoffice
)) {
378 $url = CRM_Utils_System
::url('civicrm/event/info', "reset=1&id={$this->_event_id}&noFullMsg=true");
379 CRM_Utils_System
::redirect($url);