Merge pull request #2 from civicrm/master
[civicrm-core.git] / CRM / Event / Form / SelfSvcUpdate.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
31 * @copyright CiviCRM LLC (c) 2004-2019
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 */
91 public $_action;
92 /**
93 * participant object
94 *
95 * @var string
96 */
97 protected $_participant = [];
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 */
109 protected $_details = [];
110 /**
111 * Is backoffice form?
112 *
113 * @var bool
114 */
115 protected $isBackoffice = FALSE;
116
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();
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);
141 }
142 $this->assign('action', $this->_action);
143 if ($this->_participant_id) {
144 $this->assign('participantId', $this->_participant_id);
145 }
146 $event = [];
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;
154 $details = [];
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);
159 $query = "
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;
174 }
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);
180 }
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;
186 }
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'));
192 }
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'));
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 }
210
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() {
218 $this->add('select', 'action', ts('Transfer or Cancel Registration'), [ts('-select-'), ts('Transfer'), ts('Cancel')], TRUE);
219 $this->addButtons([
220 [
221 'type' => 'submit',
222 'name' => ts('Submit'),
223 ],
224 ]);
225 $this->addFormRule(['CRM_Event_Form_SelfSvcUpdate', 'formRule'], $this);
226 parent::buildQuickForm();
227 }
228
229 /**
230 * Set default values for contact
231 *
232 * return @void
233 */
234 public function setDefaultValues() {
235 $this->_defaults = [];
236 $this->_defaults['details'] = $this->_details;
237 return $this->_defaults;
238 }
239
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) {
251 $errors = [];
252 if (empty($fields['action'])) {
253 $errors['action'] = ts("Please select Transfer OR Cancel action.");
254 }
255 return empty($errors) ? TRUE : $errors;
256 }
257
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 }
278
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) {
286 CRM_Utils_System::redirect(CRM_Utils_System::url(
287 'civicrm/event/selfsvctransfer',
288 [
289 'reset' => 1,
290 'action' => 'add',
291 'pid' => $this->_participant_id,
292 'cs' => $this->_userChecksum,
293 'is_backoffice' => $this->isBackoffice,
294 ]
295 ));
296 }
297
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
307 $value = [];
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);
313 $domainValues = [];
314 $domain = CRM_Core_BAO_Domain::getDomain();
315 $tokens = [
316 'domain' =>
317 [
318 'name',
319 'phone',
320 'address',
321 'email',
322 ],
323 'contact' => CRM_Core_SelectValues::contactTokens(),
324 ];
325 foreach ($tokens['domain'] as $token) {
326 $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain);
327 }
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] = [
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,
344 ];
345 }
346 $eventDetails = [];
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'
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 );
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)) {
376 return;
377 }
378 $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}&noFullMsg=true");
379 CRM_Utils_System::redirect($url);
380 }
381
382 }