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