Merge pull request #14662 from eileenmcnaughton/activity_pdf_71
[civicrm-core.git] / CRM / Event / Form / SelfSvcTransfer.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21 /**
22 * This class generates form components to transfer an Event to another participant
23 *
24 */
25 class CRM_Event_Form_SelfSvcTransfer extends CRM_Core_Form {
26 /**
27 * from particpant id
28 *
29 * @var string
30 *
31 */
32 protected $_from_participant_id;
33 /**
34 * from contact id
35 *
36 * @var string
37 *
38 */
39 protected $_from_contact_id;
40 /**
41 * last name of the particpant to transfer to
42 *
43 * @var string
44 *
45 */
46 protected $_to_contact_last_name;
47 /**
48 * first name of the particpant to transfer to
49 *
50 * @var string
51 *
52 */
53 protected $_to_contact_first_name;
54 /**
55 * email of participant
56 *
57 *
58 * @var string
59 */
60 protected $_to_contact_email;
61 /**
62 * _to_contact_id
63 *
64 * @var string
65 */
66 protected $_to_contact_id;
67 /**
68 * event to be cancelled/transferred
69 *
70 * @var string
71 */
72 protected $_event_id;
73 /**
74 * event title
75 *
76 * @var string
77 */
78 protected $_event_title;
79 /**
80 * event title
81 *
82 * @var string
83 */
84 protected $_event_start_date;
85 /**
86 * action
87 *
88 * @var string
89 */
90 public $_action;
91 /**
92 * participant object
93 *
94 * @var string
95 */
96 protected $_participant = [];
97 /**
98 * particpant values
99 *
100 * @var string
101 */
102 protected $_part_values;
103 /**
104 * details
105 *
106 * @var array
107 */
108 protected $_details = [];
109 /**
110 * line items
111 *
112 * @var array
113 */
114 protected $_line_items = [];
115 /**
116 * contact_id
117 *
118 * @var int
119 */
120 protected $contact_id;
121
122 /**
123 * Is backoffice form?
124 *
125 * @var bool
126 */
127 protected $isBackoffice = FALSE;
128
129 /**
130 * Get source values for transfer based on participant id in URL. Line items will
131 * be transferred to this participant - at this point no transaction changes processed
132 *
133 * return @void
134 */
135 public function preProcess() {
136 $config = CRM_Core_Config::singleton();
137 $session = CRM_Core_Session::singleton();
138 $this->_userContext = $session->readUserContext();
139 $this->_from_participant_id = CRM_Utils_Request::retrieve('pid', 'Positive', $this, FALSE, NULL, 'REQUEST');
140 $this->_userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this, FALSE, NULL, 'REQUEST');
141 $this->isBackoffice = CRM_Utils_Request::retrieve('is_backoffice', 'String', $this, FALSE, NULL, 'REQUEST');
142 $params = ['id' => $this->_from_participant_id];
143 $participant = $values = [];
144 $this->_participant = CRM_Event_BAO_Participant::getValues($params, $values, $participant);
145 $this->_part_values = $values[$this->_from_participant_id];
146 $this->set('values', $this->_part_values);
147 $this->_event_id = $this->_part_values['event_id'];
148 $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}");
149 $this->_from_contact_id = $this->_part_values['participant_contact_id'];
150 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($this->_from_contact_id, $this->_userChecksum);
151 if (!$validUser && !CRM_Core_Permission::check('edit all events')) {
152 CRM_Core_Error::statusBounce(ts('You do not have sufficient permission to transfer/cancel this participant.'), $url);
153 }
154 $this->assign('action', $this->_action);
155 if ($this->_from_participant_id) {
156 $this->assign('participantId', $this->_from_participant_id);
157 }
158 $event = [];
159 $daoName = 'title';
160 $this->_event_title = CRM_Event_BAO_Event::getFieldValue('CRM_Event_DAO_Event', $this->_event_id, $daoName);
161 $daoName = 'start_date';
162 $this->_event_start_date = CRM_Event_BAO_Event::getFieldValue('CRM_Event_DAO_Event', $this->_event_id, $daoName);
163 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_from_contact_id);
164 $this->_contact_name = $displayName;
165 $this->_contact_email = $email;
166 $details = [];
167 $details = CRM_Event_BAO_Participant::participantDetails($this->_from_participant_id);
168 $optionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'participant_role', 'id', 'name');
169 $query = "
170 SELECT cpst.name as status, cov.name as role, cp.fee_level, cp.fee_amount, cp.register_date, civicrm_event.start_date
171 FROM civicrm_participant cp
172 LEFT JOIN civicrm_participant_status_type cpst ON cpst.id = cp.status_id
173 LEFT JOIN civicrm_option_value cov ON cov.value = cp.role_id and cov.option_group_id = {$optionGroupId}
174 LEFT JOIN civicrm_event ON civicrm_event.id = cp.event_id
175 WHERE cp.id = {$this->_from_participant_id}";
176 $dao = CRM_Core_DAO::executeQuery($query);
177 while ($dao->fetch()) {
178 $details['status'] = $dao->status;
179 $details['role'] = $dao->role;
180 $details['fee_level'] = $dao->fee_level;
181 $details['fee_amount'] = $dao->fee_amount;
182 $details['register_date'] = $dao->register_date;
183 $details['event_start_date'] = $dao->start_date;
184 }
185 $this->assign('details', $details);
186 //This participant row will be cancelled. Get line item(s) to cancel
187 $this->selfsvctransferUrl = CRM_Utils_System::url('civicrm/event/selfsvcupdate',
188 "reset=1&id={$this->_from_participant_id}&id=0");
189 $this->selfsvctransferText = ts('Update');
190 $this->selfsvctransferButtonText = ts('Update');
191 }
192
193 /**
194 * Build form for input of transferree email, name
195 *
196 * return @void
197 */
198 public function buildQuickForm() {
199 // use entityRef select field for contact when this form is used by staff/admin user
200 if ($this->isBackoffice) {
201 $this->addEntityRef("contact_id", ts('Select Contact'), ['create' => TRUE], TRUE);
202 }
203 // for front-end user show and use the basic three fields used to create a contact
204 else {
205 $this->add('text', 'email', ts('To Email'), $this->_contact_email, TRUE);
206 $this->add('text', 'last_name', ts('To Last Name'), $this->_to_contact_last_name, TRUE);
207 $this->add('text', 'first_name', ts('To First Name'), $this->_to_contact_first_name, TRUE);
208 }
209
210 $this->addButtons([
211 [
212 'type' => 'submit',
213 'name' => ts('Transfer Registration'),
214 ],
215 ]);
216 $this->addFormRule(['CRM_Event_Form_SelfSvcTransfer', 'formRule'], $this);
217 parent::buildQuickForm();
218 }
219
220 /**
221 * Set defaults
222 *
223 * return @array _defaults
224 */
225 public function setDefaultValues() {
226 $this->_defaults = [];
227 return $this->_defaults;
228 }
229
230 /**
231 * Validate email and name input
232 *
233 * return array $errors
234 */
235 public static function formRule($fields, $files, $self) {
236 $errors = [];
237 if (!empty($fields['contact_id'])) {
238 $to_contact_id = $fields['contact_id'];
239 }
240 else {
241 //check that either an email or firstname+lastname is included in the form(CRM-9587)
242 $to_contact_id = self::checkProfileComplete($fields, $errors, $self);
243 }
244 //To check if the user is already registered for the event(CRM-2426)
245 if (!empty($to_contact_id)) {
246 self::checkRegistration($fields, $self, $to_contact_id, $errors);
247 }
248 //return parent::formrule($fields, $files, $self);
249 return empty($errors) ? TRUE : $errors;
250 }
251
252 /**
253 * Check whether profile (name, email) is complete
254 *
255 * return $contact_id
256 */
257 public static function checkProfileComplete($fields, &$errors, $self) {
258 $email = '';
259 foreach ($fields as $fieldname => $fieldvalue) {
260 if (substr($fieldname, 0, 5) == 'email' && $fieldvalue) {
261 $email = $fieldvalue;
262 }
263 }
264 if (!$email && !(CRM_Utils_Array::value('first_name', $fields) &&
265 CRM_Utils_Array::value('last_name', $fields))) {
266 $defaults = $params = ['id' => $eventId];
267 CRM_Event_BAO_Event::retrieve($params, $defaults);
268 $message = ts("Mandatory fields (first name and last name, OR email address) are missing from this form.");
269 $errors['_qf_default'] = $message;
270 }
271 $contact = CRM_Contact_BAO_Contact::matchContactOnEmail($email, "");
272 $contact_id = empty($contact->contact_id) ? NULL : $contact->contact_id;
273 if (!CRM_Utils_Rule::email($fields['email'])) {
274 $errors['email'] = ts('Enter valid email address.');
275 }
276 if (empty($errors) && empty($contact_id)) {
277 $params = [
278 'email-Primary' => CRM_Utils_Array::value('email', $fields, NULL),
279 'first_name' => CRM_Utils_Array::value('first_name', $fields, NULL),
280 'last_name' => CRM_Utils_Array::value('last_name', $fields, NULL),
281 'is_deleted' => CRM_Utils_Array::value('is_deleted', $fields, FALSE),
282 ];
283 //create new contact for this name/email pair
284 //if new contact, no need to check for contact already registered
285 $contact_id = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $contact_id);
286 }
287 return $contact_id;
288 }
289
290 /**
291 * Check contact details
292 *
293 * return @void
294 */
295 public static function checkRegistration($fields, $self, $contact_id, &$errors) {
296 // verify whether this contact already registered for this event
297 $contact_details = CRM_Contact_BAO_Contact::getContactDetails($contact_id);
298 $display_name = $contact_details[0];
299 $query = "select event_id from civicrm_participant where contact_id = " . $contact_id;
300 $dao = CRM_Core_DAO::executeQuery($query);
301 while ($dao->fetch()) {
302 $to_event_id[] = $dao->event_id;
303 }
304 if (!empty($to_event_id)) {
305 foreach ($to_event_id as $id) {
306 if ($id == $self->_event_id) {
307 $errors['email'] = $display_name . ts(" is already registered for this event");
308 }
309 }
310 }
311 }
312
313 /**
314 * Process transfer - first add the new participant to the event, then cancel
315 * source participant - send confirmation email to transferee
316 */
317 public function postProcess() {
318 //For transfer, process form to allow selection of transferree
319 $params = $this->controller->exportValues($this->_name);
320 if (!empty($params['contact_id'])) {
321 $contact_id = $params['contact_id'];
322 }
323 else {
324 //cancel 'from' participant row
325 $contact_id_result = civicrm_api3('Contact', 'get', [
326 'sequential' => 1,
327 'return' => ["id"],
328 'email' => $params['email'],
329 'options' => ['limit' => 1],
330 ]);
331 $contact_id_result = $contact_id_result['values'][0];
332 $contact_id = $contact_id_result['contact_id'];
333 $contact_is_deleted = $contact_id_result['contact_is_deleted'];
334 if ($contact_is_deleted || !is_numeric($contact_id)) {
335 CRM_Core_Error::statusBounce(ts('Contact does not exist.'));
336 }
337 }
338 $from_participant = $params = [];
339 $query = "select role_id, source, fee_level, is_test, is_pay_later, fee_amount, discount_id, fee_currency,campaign_id, discount_amount from civicrm_participant where id = " . $this->_from_participant_id;
340 $dao = CRM_Core_DAO::executeQuery($query);
341 $value_to = [];
342 while ($dao->fetch()) {
343 $value_to['role_id'] = $dao->role_id;
344 $value_to['source'] = $dao->source;
345 $value_to['fee_level'] = $dao->fee_level;
346 $value_to['is_test'] = $dao->is_test;
347 $value_to['is_pay_later'] = $dao->is_pay_later;
348 $value_to['fee_amount'] = $dao->fee_amount;
349 }
350 $value_to['contact_id'] = $contact_id;
351 $value_to['event_id'] = $this->_event_id;
352 $value_to['status_id'] = CRM_Core_PseudoConstant::getKey(
353 'CRM_Event_BAO_Participant',
354 'status_id',
355 'Registered'
356 );
357 $value_to['register_date'] = date("Y-m-d");
358 //first create the new participant row -don't set registered_by yet or email won't be sent
359 $participant = CRM_Event_BAO_Participant::create($value_to);
360 //send a confirmation email to the new participant
361 $this->participantTransfer($participant);
362 //now update registered_by_id
363 $query = "UPDATE civicrm_participant cp SET cp.registered_by_id = %1 WHERE cp.id = ({$participant->id})";
364 $params = [1 => [$this->_from_participant_id, 'Integer']];
365 $dao = CRM_Core_DAO::executeQuery($query, $params);
366 //copy line items to new participant
367 $line_items = CRM_Price_BAO_LineItem::getLineItems($this->_from_participant_id);
368 foreach ($line_items as $item) {
369 $item['entity_id'] = $participant->id;
370 $item['id'] = NULL;
371 $item['entity_table'] = "civicrm_participant";
372 $new_item = CRM_Price_BAO_LineItem::create($item);
373 }
374 //now cancel the from participant record, leaving the original line-item(s)
375 $value_from = [];
376 $value_from['id'] = $this->_from_participant_id;
377 $tansferId = array_search('Transferred', CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'"));
378 $value_from['status_id'] = $tansferId;
379 $value_from['transferred_to_contact_id'] = $contact_id;
380 $contact_details = CRM_Contact_BAO_Contact::getContactDetails($contact_id);
381 $display_name = current($contact_details);
382 $this->assign('to_participant', $display_name);
383 CRM_Event_BAO_Participant::create($value_from);
384 $this->sendCancellation();
385 list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contact_id);
386 $statusMsg = ts('Event registration information for %1 has been updated.', [1 => $displayName]);
387 $statusMsg .= ' ' . ts('A confirmation email has been sent to %1.', [1 => $email]);
388 CRM_Core_Session::setStatus($statusMsg, ts('Registration Transferred'), 'success');
389 if ($this->isBackoffice) {
390 return;
391 }
392 $url = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_event_id}");
393 CRM_Utils_System::redirect($url);
394 }
395
396 /**
397 * Based on input, create participant row for transferee and send email
398 *
399 * return @ void
400 */
401 public function participantTransfer($participant) {
402 $contactDetails = [];
403 $contactIds[] = $participant->contact_id;
404 list($currentContactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL,
405 FALSE, FALSE, NULL, [], 'CRM_Event_BAO_Participant');
406 foreach ($currentContactDetails as $contactId => $contactValues) {
407 $contactDetails[$contactId] = $contactValues;
408 }
409 $participantRoles = CRM_Event_PseudoConstant::participantRole();
410 $participantDetails = [];
411 $query = "SELECT * FROM civicrm_participant WHERE id = " . $participant->id;
412 $dao = CRM_Core_DAO::executeQuery($query);
413 while ($dao->fetch()) {
414 $participantDetails[$dao->id] = [
415 'id' => $dao->id,
416 'role' => $participantRoles[$dao->role_id],
417 'is_test' => $dao->is_test,
418 'event_id' => $dao->event_id,
419 'status_id' => $dao->status_id,
420 'fee_amount' => $dao->fee_amount,
421 'contact_id' => $dao->contact_id,
422 'register_date' => $dao->register_date,
423 'registered_by_id' => $dao->registered_by_id,
424 ];
425 }
426 $domainValues = [];
427 if (empty($domainValues)) {
428 $domain = CRM_Core_BAO_Domain::getDomain();
429 $tokens = [
430 'domain' =>
431 [
432 'name',
433 'phone',
434 'address',
435 'email',
436 ],
437 'contact' => CRM_Core_SelectValues::contactTokens(),
438 ];
439 foreach ($tokens['domain'] as $token) {
440 $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain);
441 }
442 }
443 $eventDetails = [];
444 $eventParams = ['id' => $participant->event_id];
445 CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails);
446 //get default participant role.
447 $eventDetails['participant_role'] = CRM_Utils_Array::value($eventDetails['default_role_id'], $participantRoles);
448 //get the location info
449 $locParams = [
450 'entity_id' => $participant->event_id,
451 'entity_table' => 'civicrm_event',
452 ];
453 $eventDetails['location'] = CRM_Core_BAO_Location::getValues($locParams, TRUE);
454 $toEmail = CRM_Utils_Array::value('email', $contactDetails[$participant->contact_id]);
455 if ($toEmail) {
456 //take a receipt from as event else domain.
457 $receiptFrom = $domainValues['name'] . ' <' . $domainValues['email'] . '>';
458 if (!empty($eventDetails['confirm_from_name']) && !empty($eventDetails['confirm_from_email'])) {
459 $receiptFrom = $eventDetails['confirm_from_name'] . ' <' . $eventDetails['confirm_from_email'] . '>';
460 }
461 $participantName = $contactDetails[$participant->contact_id]['display_name'];
462 $tplParams = [
463 'event' => $eventDetails,
464 'participant' => $participantDetails[$participant->id],
465 'participantID' => $participant->id,
466 'participant_status' => 'Registered',
467 ];
468
469 $sendTemplateParams = [
470 'groupName' => 'msg_tpl_workflow_event',
471 'valueName' => 'event_online_receipt',
472 'contactId' => $participantDetails[$participant->id]['contact_id'],
473 'tplParams' => $tplParams,
474 'from' => $receiptFrom,
475 'toName' => $participantName,
476 'toEmail' => $toEmail,
477 'cc' => CRM_Utils_Array::value('cc_confirm', $eventDetails),
478 'bcc' => CRM_Utils_Array::value('bcc_confirm', $eventDetails),
479 ];
480 CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
481 }
482 }
483
484 /**
485 * Send confirmation of cancellation to source participant
486 *
487 * return @ void
488 */
489 public function sendCancellation() {
490 $domainValues = [];
491 $domain = CRM_Core_BAO_Domain::getDomain();
492 $tokens = [
493 'domain' =>
494 [
495 'name',
496 'phone',
497 'address',
498 'email',
499 ],
500 'contact' => CRM_Core_SelectValues::contactTokens(),
501 ];
502 foreach ($tokens['domain'] as $token) {
503 $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain);
504 }
505 $participantRoles = [];
506 $participantRoles = CRM_Event_PseudoConstant::participantRole();
507 $participantDetails = [];
508 $query = "SELECT * FROM civicrm_participant WHERE id = {$this->_from_participant_id}";
509 $dao = CRM_Core_DAO::executeQuery($query);
510 while ($dao->fetch()) {
511 $participantDetails[$dao->id] = [
512 'id' => $dao->id,
513 'role' => $participantRoles[$dao->role_id],
514 'is_test' => $dao->is_test,
515 'event_id' => $dao->event_id,
516 'status_id' => $dao->status_id,
517 'fee_amount' => $dao->fee_amount,
518 'contact_id' => $dao->contact_id,
519 'register_date' => $dao->register_date,
520 'registered_by_id' => $dao->registered_by_id,
521 ];
522 }
523 $eventDetails = [];
524 $eventParams = ['id' => $this->_event_id];
525 CRM_Event_BAO_Event::retrieve($eventParams, $eventDetails[$this->_event_id]);
526 //get default participant role.
527 $eventDetails[$this->_event_id]['participant_role'] = CRM_Utils_Array::value($eventDetails[$this->_event_id]['default_role_id'], $participantRoles);
528 //get the location info
529 $locParams = ['entity_id' => $this->_event_id, 'entity_table' => 'civicrm_event'];
530 $eventDetails[$this->_event_id]['location'] = CRM_Core_BAO_Location::getValues($locParams, TRUE);
531 //get contact details
532 $contactIds[$this->_from_contact_id] = $this->_from_contact_id;
533 list($currentContactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, NULL,
534 FALSE, FALSE, NULL, [],
535 'CRM_Event_BAO_Participant'
536 );
537 foreach ($currentContactDetails as $contactId => $contactValues) {
538 $contactDetails[$this->_from_contact_id] = $contactValues;
539 }
540 //send a 'cancelled' email to user, and cc the event's cc_confirm email
541 $mail = CRM_Event_BAO_Participant::sendTransitionParticipantMail($this->_from_participant_id,
542 $participantDetails[$this->_from_participant_id],
543 $eventDetails[$this->_event_id],
544 $contactDetails[$this->_from_contact_id],
545 $domainValues,
546 "Transferred",
547 ""
548 );
549 $statusMsg = ts('Event registration information for %1 has been updated.', [1 => $this->_contact_name]);
550 $statusMsg .= ' ' . ts('A cancellation email has been sent to %1.', [1 => $this->_contact_email]);
551 CRM_Core_Session::setStatus($statusMsg, ts('Thanks'), 'success');
552 }
553
554 }