Merge pull request #12018 from civicrm/5.1
[civicrm-core.git] / CRM / Event / Form / SelfSvcUpdate.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 = array();
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 = array();
110 /**
111 * Set variables up before form is built based on participant ID from URL
112 *
113 * @return void
114 */
115 public function preProcess() {
116 $config = CRM_Core_Config::singleton();
117 $session = CRM_Core_Session::singleton();
118 $this->_userContext = $session->readUserContext();
119 $participant = $values = array();
120 $this->_participant_id = CRM_Utils_Request::retrieve('pid', 'Positive', $this, FALSE, NULL, 'REQUEST');
121 $this->_userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this, FALSE, NULL, 'REQUEST');
122 $params = array('id' => $this->_participant_id);
123 $this->_participant = CRM_Event_BAO_Participant::getValues($params, $values, $participant);
124 $this->_part_values = $values[$this->_participant_id];
125 $this->set('values', $this->_part_values);
126 //fetch Event by event_id, verify that this event can still be xferred/cancelled
127 $this->_event_id = $this->_part_values['event_id'];
128 $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}");
129 $this->_contact_id = $this->_part_values['participant_contact_id'];
130 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($this->_contact_id, $this->_userChecksum);
131 if (!$validUser && !CRM_Core_Permission::check('edit all events')) {
132 CRM_Core_Error::statusBounce(ts('You do not have sufficient permission to transfer/cancel this participant.'), $url);
133 }
134 $this->assign('action', $this->_action);
135 if ($this->_participant_id) {
136 $this->assign('participantId', $this->_participant_id);
137 }
138 $event = array();
139 $daoName = 'title';
140 $this->_event_title = CRM_Event_BAO_Event::getFieldValue('CRM_Event_DAO_Event', $this->_event_id, $daoName);
141 $daoName = 'start_date';
142 $this->_event_start_date = CRM_Event_BAO_Event::getFieldValue('CRM_Event_DAO_Event', $this->_event_id, $daoName);
143 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contact_id);
144 $this->_contact_name = $displayName;
145 $this->_contact_email = $email;
146 $details = array();
147 $details = CRM_Event_BAO_Participant::participantDetails($this->_participant_id);
148 $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'participant_role', 'id', 'name');
149 $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_participant_id, 'contribution_id', 'participant_id');
150 $this->assign('contributionId', $contributionId);
151 $query = "
152 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
153 FROM civicrm_participant cp
154 LEFT JOIN civicrm_participant_status_type cpst ON cpst.id = cp.status_id
155 LEFT JOIN civicrm_option_value cov ON cov.value = cp.role_id and cov.option_group_id = {$optionGroupId}
156 LEFT JOIN civicrm_event ON civicrm_event.id = cp.event_id
157 WHERE cp.id = {$this->_participant_id}";
158 $dao = CRM_Core_DAO::executeQuery($query);
159 while ($dao->fetch()) {
160 $details['status'] = $dao->status;
161 $details['role'] = $dao->role;
162 $details['fee_level'] = $dao->fee_level;
163 $details['fee_amount'] = $dao->fee_amount;
164 $details['register_date'] = $dao->register_date;
165 $details['event_start_date'] = $dao->start_date;
166 }
167 //verify participant status is still Registered
168 if ($details['status'] != "Registered") {
169 $status = "You cannot transfer or cancel your registration for " . $this->_event_title . ' as you are not currently registered for this event.';
170 CRM_Core_Session::setStatus($status, ts('Sorry'), 'alert');
171 CRM_Utils_System::redirect($url);
172 }
173 $query = "select start_date as start, selfcancelxfer_time as time from civicrm_event where id = " . $this->_event_id;
174 $dao = CRM_Core_DAO::executeQuery($query);
175 while ($dao->fetch()) {
176 $time_limit = $dao->time;
177 $start_date = $dao->start;
178 }
179 $start_time = new Datetime($start_date);
180 $timenow = new Datetime();
181 if (!empty($start_time) && $start_time < $timenow) {
182 $status = ts("Registration for this event cannot be cancelled or transferred once the event has begun. Contact the event organizer if you have questions.");
183 CRM_Core_Error::statusBounce($status, $url, ts('Sorry'));
184 }
185 if (!empty($time_limit) && $time_limit > 0) {
186 $interval = $timenow->diff($start_time);
187 $days = $interval->format('%d');
188 $hours = $interval->format('%h');
189 if ($hours <= $time_limit && $days < 1) {
190 $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.", array(1 => $time_limit));
191 CRM_Core_Error::statusBounce($status, $url, ts('Sorry'));
192 }
193 }
194 $this->assign('details', $details);
195 $this->selfsvcupdateUrl = CRM_Utils_System::url('civicrm/event/selfsvcupdate', "reset=1&id={$this->_participant_id}&id=0");
196 $this->selfsvcupdateText = ts('Update');
197 $this->selfsvcupdateButtonText = ts('Update');
198 // Based on those ids retrieve event and verify it is eligible
199 // for self update (event.start_date > today, event can be 'self_updated'
200 // retrieve contact name and email, and let user verify his/her identity
201 }
202 /**
203 * buildQuickForm -populate input variables for source Event
204 * to cancel or transfer to another person
205 *
206 * return @void
207 */
208 public function buildQuickForm() {
209 $this->add('select', 'action', ts('Transfer or Cancel Registration'), array(ts('-select-'), ts('Transfer'), ts('Cancel')), TRUE);
210 $this->addButtons(array(
211 array(
212 'type' => 'submit',
213 'name' => ts('Submit'),
214 ),
215 ));
216 $this->addFormRule(array('CRM_Event_Form_SelfSvcUpdate', 'formRule'), $this);
217 parent::buildQuickForm();
218 }
219
220 /**
221 * Set default values for contact
222 *
223 * return @void
224 */
225 public function setDefaultValues() {
226 $this->_defaults = array();
227 $this->_defaults['details'] = $this->_details;
228 return $this->_defaults;
229 }
230
231 /**
232 * Validate action input
233 * @param array $fields
234 * Posted fields of the form.
235 * @param $files
236 * @param $self
237 *
238 * @return array
239 * list of errors to be posted back to the form
240 */
241 public static function formRule($fields, $files, $self) {
242 $errors = array();
243 if (empty($fields['action'])) {
244 $errors['action'] = ts("Please select Transfer OR Cancel action.");
245 }
246 return empty($errors) ? TRUE : $errors;
247 }
248
249 /**
250 * Process submit form - based on user selection of action
251 * transfer or cancel the event
252 *
253 * return @void
254 */
255 public function postProcess() {
256 //if selection is cancel, cancel this participant' registration, process refund
257 //if transfer, process form to allow selection of transferree
258 $params = $this->controller->exportValues($this->_name);
259 $action = $params['action'];
260 if ($action == "1") {
261 $action = "Transfer Event";
262 $this->transferParticipant($params);
263 }
264 elseif ($action == "2") {
265 $action = "Cancel Event";
266 $this->cancelParticipant($params);
267 }
268 }
269
270 /**
271 * Transfer to a new form, allowing selection of a new contact
272 * based on email and name. The Event will be transferred to this new participant
273 *
274 * return @void
275 */
276 public function transferParticipant($params) {
277 $transferUrl = 'civicrm/event/form/selfsvctransfer';
278 $url = CRM_Utils_System::url('civicrm/event/selfsvctransfer', 'reset=1&action=add&pid=' . $this->_participant_id . '&cs=' . $this->_userChecksum);
279 $this->controller->setDestination($url);
280 $session = CRM_Core_Session::singleton();
281 $session->replaceUserContext($url);
282 }
283
284 /**
285 * Cancel this participant and finish, send cancellation email. At this point no
286 * auto-cancellation of payment is handled, so payment needs to be manually cancelled
287 *
288 * return @void
289 */
290 public function cancelParticipant($params) {
291 //set participant record status to Cancelled, refund payment if possible
292 // send email to participant and admin, and log Activity
293 $value = array();
294 $value['id'] = $this->_participant_id;
295 $cancelledId = array_search('Cancelled',
296 CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'"));
297 $value['status_id'] = $cancelledId;
298 CRM_Event_BAO_Participant::create($value);
299 $domainValues = array();
300 $domain = CRM_Core_BAO_Domain::getDomain();
301 $tokens = array(
302 'domain' =>
303 array(
304 'name',
305 'phone',
306 'address',
307 'email',
308 ),
309 'contact' => CRM_Core_SelectValues::contactTokens(),
310 );
311 foreach ($tokens['domain'] as $token) {
312 $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain);
313 }
314 $participantRoles = array();
315 $participantRoles = CRM_Event_PseudoConstant::participantRole();
316 $participantDetails = array();
317 $query = "SELECT * FROM civicrm_participant WHERE id = {$this->_participant_id}";
318 $dao = CRM_Core_DAO::executeQuery($query);
319 while ($dao->fetch()) {
320 $participantDetails[$dao->id] = array(
321 'id' => $dao->id,
322 'role' => $participantRoles[$dao->role_id],
323 'is_test' => $dao->is_test,
324 'event_id' => $dao->event_id,
325 'status_id' => $dao->status_id,
326 'fee_amount' => $dao->fee_amount,
327 'contact_id' => $dao->contact_id,
328 'register_date' => $dao->register_date,
329 'registered_by_id' => $dao->registered_by_id,
330 );
331 }
332 $eventDetails = array();
333 $eventParams = array('id' => $this->_event_id);
334 CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails[$this->_event_id]);
335 //get default participant role.
336 $eventDetails[$this->_event_id]['participant_role'] = CRM_Utils_Array::value($eventDetails[$this->_event_id]['default_role_id'], $participantRoles);
337 //get the location info
338 $locParams = array('entity_id' => $this->_event_id, 'entity_table' => 'civicrm_event');
339 $eventDetails[$this->_event_id]['location'] = CRM_Core_BAO_Location::getValues($locParams, TRUE);
340 //get contact details
341 $contactIds[$this->_contact_id] = $this->_contact_id;
342 list($currentContactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL,
343 FALSE, FALSE, NULL, array(),
344 'CRM_Event_BAO_Participant'
345 );
346 foreach ($currentContactDetails as $contactId => $contactValues) {
347 $contactDetails[$this->_contact_id] = $contactValues;
348 }
349 //send a 'cancelled' email to user, and cc the event's cc_confirm email
350 $mail = CRM_Event_BAO_Participant::sendTransitionParticipantMail($this->_participant_id,
351 $participantDetails[$this->_participant_id],
352 $eventDetails[$this->_event_id],
353 $contactDetails[$this->_contact_id],
354 $domainValues,
355 "Cancelled",
356 ""
357 );
358 $statusMsg = ts('Event registration information for %1 has been updated.', array(1 => $this->_contact_name));
359 $statusMsg .= ' ' . ts('A cancellation email has been sent to %1.', array(1 => $this->_contact_email));
360 CRM_Core_Session::setStatus($statusMsg, ts('Thanks'), 'success');
361 $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}&noFullMsg=true");
362 CRM_Utils_System::redirect($url);
363 }
364
365 }