Merge pull request #22977 from eileenmcnaughton/issetit
[civicrm-core.git] / CRM / Event / Form / Registration / ParticipantConfirm.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19 /**
20 * This class generates form components for processing Event
21 *
22 */
23 class CRM_Event_Form_Registration_ParticipantConfirm extends CRM_Event_Form_Registration {
24
25 /**
26 * Optional credit card return status code
27 * @var string
28 * @see https://issues.civicrm.org/jira/browse/CRM-6060
29 */
30 protected $_cc = NULL;
31
32 /**
33 * Set variables up before form is built.
34 *
35 * @return void
36 */
37 public function preProcess() {
38 $this->_participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
39
40 $this->_cc = CRM_Utils_Request::retrieve('cc', 'String', $this);
41
42 //get the contact and event id and assing to session.
43 $values = [];
44 $csContactID = NULL;
45 if ($this->_participantId) {
46 $params = ['id' => $this->_participantId];
47 CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Participant', $params, $values,
48 ['contact_id', 'event_id', 'status_id']
49 );
50 }
51
52 $this->_participantStatusId = $values['status_id'] ?? NULL;
53 $this->_eventId = $values['event_id'] ?? NULL;
54 $csContactId = $values['contact_id'] ?? NULL;
55
56 // make sure we have right permission to edit this user
57 $this->_csContactID = NULL;
58 if ($csContactId && $this->_eventId) {
59 $session = CRM_Core_Session::singleton();
60 if ($csContactId == $session->get('userID')) {
61 $this->_csContactID = $csContactId;
62 }
63 else {
64 if (CRM_Contact_BAO_Contact_Permission::validateChecksumContact($csContactId, $this)) {
65 //since we have landing page so get this contact
66 //id in session if user really want to walk wizard.
67 $this->_csContactID = $csContactId;
68 }
69 }
70 }
71
72 if (!$this->_csContactID) {
73 $config = CRM_Core_Config::singleton();
74 CRM_Core_Error::statusBounce(ts('You do not have permission to access this event registration. Contact the site administrator if you need assistance.'), $config->userFrameworkBaseURL);
75 }
76 }
77
78 /**
79 * Build the form object.
80 *
81 * @return void
82 */
83 public function buildQuickForm() {
84 $params = ['id' => $this->_eventId];
85 $values = [];
86 CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event', $params, $values,
87 ['title']
88 );
89
90 $buttons = [];
91 // only pending status class family able to confirm.
92
93 $statusMsg = NULL;
94 if (array_key_exists($this->_participantStatusId,
95 CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'")
96 )) {
97
98 //need to confirm that though participant confirming
99 //registration - but is there enough space to confirm.
100 $emptySeats = CRM_Event_BAO_Participant::eventFull($this->_eventId, TRUE, FALSE, TRUE, FALSE, TRUE);
101 $additonalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_participantId);
102 $requireSpace = 1 + count($additonalIds);
103 if ($emptySeats !== NULL && ($requireSpace > $emptySeats)) {
104 $statusMsg = ts("Unfortunately there are currently no available spaces for the %1 event.", [1 => $values['title']]);
105 }
106 else {
107 if ($this->_cc == 'fail') {
108 $statusMsg = '<div class="bold">' . ts('Your Credit Card transaction was not successful. No money has yet been charged to your card.') . '</div><div><br />' . ts('Click the "Confirm Registration" button to complete your registration in %1, or click "Cancel Registration" if you are no longer interested in attending this event.', [
109 1 => $values['title'],
110 ]) . '</div>';
111 }
112 else {
113 $statusMsg = '<div class="bold">' . ts('Confirm your registration for %1.', [
114 1 => $values['title'],
115 ]) . '</div><div><br />' . ts('Click the "Confirm Registration" button to begin, or click "Cancel Registration" if you are no longer interested in attending this event.') . '</div>';
116 }
117 $buttons = array_merge($buttons, [
118 [
119 'type' => 'next',
120 'name' => ts('Confirm Registration'),
121 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
122 'isDefault' => TRUE,
123 ],
124 ]);
125 }
126 }
127
128 // status class other than Negative should be able to cancel registration.
129 if (array_key_exists($this->_participantStatusId,
130 CRM_Event_PseudoConstant::participantStatus(NULL, "class != 'Negative'")
131 )) {
132 $cancelConfirm = ts('Are you sure you want to cancel your registration for this event?');
133 $buttons = array_merge($buttons, [
134 [
135 'type' => 'submit',
136 'name' => ts('Cancel Registration'),
137 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
138 'js' => ['onclick' => 'return confirm(\'' . $cancelConfirm . '\');'],
139 ],
140 ]);
141 if (!$statusMsg) {
142 $statusMsg = ts('You can cancel your registration for %1 by clicking "Cancel Registration".', [1 => $values['title']]);
143 }
144 }
145 if (!$statusMsg) {
146 $statusMsg = ts("Your registration for %1 has already been cancelled. No further action is needed.",
147 [1 => $values['title']]
148 );
149 }
150 $this->assign('statusMsg', $statusMsg);
151
152 $this->addButtons($buttons);
153 }
154
155 /**
156 * Process the form submission.
157 *
158 *
159 * @return void
160 */
161 public function postProcess() {
162 //get the button.
163 $buttonName = $this->controller->getButtonName();
164 $participantId = $this->_participantId;
165
166 if ($buttonName == '_qf_ParticipantConfirm_next') {
167 //lets get contact id in session.
168 $session = CRM_Core_Session::singleton();
169 $session->set('userID', $this->_csContactID);
170
171 $this->postProcessHook();
172
173 //check user registration status is from pending class
174 $url = CRM_Utils_System::url('civicrm/event/register',
175 "reset=1&id={$this->_eventId}&participantId={$participantId}"
176 );
177 CRM_Utils_System::redirect($url);
178 }
179 elseif ($buttonName == '_qf_ParticipantConfirm_submit') {
180 //need to registration status to 'cancelled'.
181
182 $cancelledId = array_search('Cancelled', CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'"));
183 $additionalParticipantIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($participantId);
184
185 $participantIds = array_merge([$participantId], $additionalParticipantIds);
186 $results = CRM_Event_BAO_Participant::transitionParticipants($participantIds, $cancelledId, NULL, TRUE);
187
188 if (count($participantIds) > 1) {
189 $statusMessage = ts("%1 Event registration(s) have been cancelled.", [1 => count($participantIds)]);
190 }
191 else {
192 $statusMessage = ts("Your Event Registration has been cancelled.");
193 }
194
195 if (!empty($results['mailedParticipants'])) {
196 foreach ($results['mailedParticipants'] as $key => $displayName) {
197 $statusMessage .= "<br />" . ts("Email has been sent to : %1", [1 => $displayName]);
198 }
199 }
200
201 $this->postProcessHook();
202 CRM_Core_Session::setStatus($statusMessage);
203 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/info',
204 "reset=1&id={$this->_eventId}&noFullMsg=1",
205 FALSE, NULL, FALSE, TRUE
206 )
207 );
208 }
209 }
210
211 }