4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 * This class generates form components to transfer an Event to another participant
25 class CRM_Event_Form_SelfSvcTransfer
extends CRM_Core_Form
{
32 protected $_from_participant_id;
39 protected $_from_contact_id;
41 * last name of the particpant to transfer to
46 protected $_to_contact_last_name;
48 * first name of the particpant to transfer to
53 protected $_to_contact_first_name;
55 * email of participant
60 protected $_to_contact_email;
66 protected $_to_contact_id;
68 * event to be cancelled/transferred
78 protected $_event_title;
84 protected $_event_start_date;
96 protected $_participant = [];
102 protected $_part_values;
108 protected $_details = [];
114 protected $_line_items = [];
120 protected $contact_id;
123 * Is backoffice form?
127 protected $isBackoffice = FALSE;
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
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);
154 $this->assign('action', $this->_action
);
155 if ($this->_from_participant_id
) {
156 $this->assign('participantId', $this->_from_participant_id
);
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;
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');
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
;
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');
194 * Build form for input of transferree email, name
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);
203 // for front-end user show and use the basic three fields used to create a contact
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);
213 'name' => ts('Transfer Registration'),
216 $this->addFormRule(['CRM_Event_Form_SelfSvcTransfer', 'formRule'], $this);
217 parent
::buildQuickForm();
223 * return @array _defaults
225 public function setDefaultValues() {
226 $this->_defaults
= [];
227 return $this->_defaults
;
231 * Validate email and name input
233 * return array $errors
235 public static function formRule($fields, $files, $self) {
237 if (!empty($fields['contact_id'])) {
238 $to_contact_id = $fields['contact_id'];
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);
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);
248 //return parent::formrule($fields, $files, $self);
249 return empty($errors) ?
TRUE : $errors;
253 * Check whether profile (name, email) is complete
257 public static function checkProfileComplete($fields, &$errors, $self) {
259 foreach ($fields as $fieldname => $fieldvalue) {
260 if (substr($fieldname, 0, 5) == 'email' && $fieldvalue) {
261 $email = $fieldvalue;
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;
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.');
276 if (empty($errors) && empty($contact_id)) {
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),
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);
291 * Check contact details
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
;
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");
314 * Process transfer - first add the new participant to the event, then cancel
315 * source participant - send confirmation email to transferee
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'];
324 //cancel 'from' participant row
325 $contact_id_result = civicrm_api3('Contact', 'get', [
328 'email' => $params['email'],
329 'options' => ['limit' => 1],
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.'));
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);
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
;
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',
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
;
371 $item['entity_table'] = "civicrm_participant";
372 $new_item = CRM_Price_BAO_LineItem
::create($item);
374 //now cancel the from participant record, leaving the original line-item(s)
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
) {
392 $url = CRM_Utils_System
::url('civicrm/event/info', "reset=1&id={$this->_event_id}");
393 CRM_Utils_System
::redirect($url);
397 * Based on input, create participant row for transferee and send email
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;
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
] = [
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
,
427 if (empty($domainValues)) {
428 $domain = CRM_Core_BAO_Domain
::getDomain();
437 'contact' => CRM_Core_SelectValues
::contactTokens(),
439 foreach ($tokens['domain'] as $token) {
440 $domainValues[$token] = CRM_Utils_Token
::getDomainTokenReplacement($token, $domain);
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
450 'entity_id' => $participant->event_id
,
451 'entity_table' => 'civicrm_event',
453 $eventDetails['location'] = CRM_Core_BAO_Location
::getValues($locParams, TRUE);
454 $toEmail = CRM_Utils_Array
::value('email', $contactDetails[$participant->contact_id
]);
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'] . '>';
461 $participantName = $contactDetails[$participant->contact_id
]['display_name'];
463 'event' => $eventDetails,
464 'participant' => $participantDetails[$participant->id
],
465 'participantID' => $participant->id
,
466 'participant_status' => 'Registered',
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),
480 CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
485 * Send confirmation of cancellation to source participant
489 public function sendCancellation() {
491 $domain = CRM_Core_BAO_Domain
::getDomain();
500 'contact' => CRM_Core_SelectValues
::contactTokens(),
502 foreach ($tokens['domain'] as $token) {
503 $domainValues[$token] = CRM_Utils_Token
::getDomainTokenReplacement($token, $domain);
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
] = [
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
,
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'
537 foreach ($currentContactDetails as $contactId => $contactValues) {
538 $contactDetails[$this->_from_contact_id
] = $contactValues;
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
],
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');