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