Merge pull request #23902 from civicrm/5.51
[civicrm-core.git] / CRM / Event / Form / ManageEvent.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
6a488035 13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
15 */
16
17/**
3bdf1f3a 18 * This class generates form components for processing Event.
6a488035
TO
19 */
20class CRM_Event_Form_ManageEvent extends CRM_Core_Form {
21
22 /**
3bdf1f3a 23 * The id of the event we are processing.
6a488035
TO
24 *
25 * @var int
6a488035
TO
26 */
27 public $_id;
28
29 /**
100fef9d 30 * Is this the first page?
6a488035 31 *
d51c6add 32 * @var bool
6a488035
TO
33 */
34 protected $_first = FALSE;
35
36 /**
100fef9d 37 * Are we in single form mode or wizard mode?
6a488035 38 *
d51c6add 39 * @var bool
6a488035
TO
40 */
41 protected $_single;
42
3a936dab 43 public $_action;
6a488035
TO
44
45 /**
100fef9d 46 * Are we actually managing an event template?
d51c6add 47 * @var bool
6a488035
TO
48 */
49 protected $_isTemplate = FALSE;
50
51 /**
9f266042 52 * Pre-populate fields based on this template event_id.
53 *
54 * @var int
6a488035
TO
55 */
56 protected $_templateId;
57
58 protected $_cancelURL = NULL;
59
49f68a22
DL
60 /**
61 * The campaign id of the existing event, we use this to know if we need to update
62 * the participant records
90b461f1 63 * @var int
49f68a22
DL
64 */
65 protected $_campaignID = NULL;
59d63f8b 66
62933949 67 /**
66f9e52b 68 * Check if repeating event.
90b461f1 69 * @var bool
62933949 70 */
c65ce939 71 public $_isRepeatingEvent;
4d18d9a1 72
4a44fd8a
TM
73 /**
74 * Explicitly declare the entity api name.
75 */
6e62b28c
TM
76 public function getDefaultEntity() {
77 return 'Event';
78 }
49f68a22 79
055e4167
TM
80 /**
81 * Explicitly declare the form context.
82 */
83 public function getDefaultContext() {
84 return 'create';
85 }
86
6a488035 87 /**
66f9e52b 88 * Set variables up before form is built.
6a488035 89 */
00be9182 90 public function preProcess() {
e46f73c7 91 $this->assign('CiviEvent', CRM_Core_Component::isEnabled('CiviEvent'));
b1e18356 92 CRM_Core_Form_RecurringEntity::preProcess('civicrm_event');
59d63f8b 93
6a488035
TO
94 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add', 'REQUEST');
95
96 $this->assign('action', $this->_action);
97
639b6887 98 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'GET');
6a488035 99 if ($this->_id) {
62933949 100 $this->_isRepeatingEvent = CRM_Core_BAO_RecurringEntity::getParentFor($this->_id, 'civicrm_event');
6a488035 101 $this->assign('eventId', $this->_id);
be84b210 102 if (!empty($this->_addBlockName) && empty($this->_addProfileBottom) && empty($this->_addProfileBottomAdd)) {
6a488035
TO
103 $this->add('hidden', 'id', $this->_id);
104 }
105 $this->_single = TRUE;
106
be2fb01f 107 $params = ['id' => $this->_id];
6a488035
TO
108 CRM_Event_BAO_Event::retrieve($params, $eventInfo);
109
110 // its an update mode, do a permission check
111 if (!CRM_Event_BAO_Event::checkPermission($this->_id, CRM_Core_Permission::EDIT)) {
e22ec653 112 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
6a488035
TO
113 }
114
9c1bc317 115 $participantListingID = $eventInfo['participant_listing_id'] ?? NULL;
6a488035
TO
116 //CRM_Core_DAO::getFieldValue( 'CRM_Event_DAO_Event', $this->_id, 'participant_listing_id' );
117 if ($participantListingID) {
118 $participantListingURL = CRM_Utils_System::url('civicrm/event/participant',
119 "reset=1&id={$this->_id}",
120 TRUE, NULL, TRUE, TRUE
121 );
122 $this->assign('participantListingURL', $participantListingURL);
123 }
124
125 $this->assign('isOnlineRegistration', CRM_Utils_Array::value('is_online_registration', $eventInfo));
126
127 $this->assign('id', $this->_id);
128 }
129
130 // figure out whether we’re handling an event or an event template
131 if ($this->_id) {
9c1bc317 132 $this->_isTemplate = $eventInfo['is_template'] ?? NULL;
6a488035
TO
133 }
134 elseif ($this->_action & CRM_Core_Action::ADD) {
135 $this->_isTemplate = CRM_Utils_Request::retrieve('is_template', 'Boolean', $this);
136 }
137
138 $this->assign('isTemplate', $this->_isTemplate);
139
648deec4
MWMC
140 // Set "Manage Event" Title
141 $title = NULL;
6a488035
TO
142 if ($this->_id) {
143 if ($this->_isTemplate) {
648deec4 144 $title = ts('Edit Event Template') . ' - ' . CRM_Utils_Array::value('template_title', $eventInfo);
6a488035
TO
145 }
146 else {
648deec4
MWMC
147 $configureText = $this->_isRepeatingEvent ? ts('Configure Repeating Event') : ts('Configure Event');
148 $title = $configureText . ' - ' . CRM_Utils_Array::value('title', $eventInfo);
6a488035 149 }
6a488035
TO
150 }
151 elseif ($this->_action & CRM_Core_Action::ADD) {
648deec4 152 $title = $this->_isTemplate ? ts('New Event Template') : ts('New Event');
6a488035 153 }
648deec4 154 $this->setTitle($title);
6a488035
TO
155
156 if (CRM_Core_Permission::check('view event participants') &&
157 CRM_Core_Permission::check('view all contacts')
158 ) {
159 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1', 'label');
160 $statusTypesPending = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0', 'label');
161 $findParticipants['statusCounted'] = implode(', ', array_values($statusTypes));
162 $findParticipants['statusNotCounted'] = implode(', ', array_values($statusTypesPending));
163 $this->assign('findParticipants', $findParticipants);
164 }
165
166 $this->_templateId = (int) CRM_Utils_Request::retrieve('template_id', 'Integer', $this);
59d63f8b 167
62933949 168 //Is a repeating event
a50a97b8 169 if ($this->_isRepeatingEvent) {
6266861e 170 $isRepeatingEntity = TRUE;
171 $this->assign('isRepeatingEntity', $isRepeatingEntity);
62933949 172 }
6a488035 173
e4fa553b 174 // CRM-16776 - show edit/copy/create buttons for Profiles if user has required permission.
175 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
176 $ufCreate = CRM_ACL_API::group(CRM_Core_Permission::CREATE, NULL, 'civicrm_uf_group', $ufGroups);
177 $ufEdit = CRM_ACL_API::group(CRM_Core_Permission::EDIT, NULL, 'civicrm_uf_group', $ufGroups);
be2fb01f
CW
178 $checkPermission = [
179 [
755a1835 180 'administer CiviCRM data',
e4fa553b 181 'manage event profiles',
be2fb01f
CW
182 ],
183 ];
e4fa553b 184 if (CRM_Core_Permission::check($checkPermission) || !empty($ufCreate) || !empty($ufEdit)) {
185 $this->assign('perm', TRUE);
186 }
187
6a488035
TO
188 // also set up tabs
189 CRM_Event_Form_ManageEvent_TabHeader::build($this);
190
191 // Set Done button URL and breadcrumb. Templates go back to Manage Templates,
192 // otherwise go to Manage Event for new event or ManageEventEdit if event if exists.
be2fb01f 193 $breadCrumb = [];
6a488035
TO
194 if (!$this->_isTemplate) {
195 if ($this->_id) {
196 $this->_doneUrl = CRM_Utils_System::url(CRM_Utils_System::currentPath(),
197 "action=update&reset=1&id={$this->_id}"
198 );
199 }
200 else {
201 $this->_doneUrl = CRM_Utils_System::url('civicrm/event/manage',
202 'reset=1'
203 );
be2fb01f
CW
204 $breadCrumb = [
205 [
353ffa53 206 'title' => ts('Manage Events'),
6a488035 207 'url' => $this->_doneUrl,
be2fb01f
CW
208 ],
209 ];
6a488035
TO
210 }
211 }
212 else {
213 $this->_doneUrl = CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1');
be2fb01f
CW
214 $breadCrumb = [
215 [
353ffa53 216 'title' => ts('Manage Event Templates'),
6a488035 217 'url' => $this->_doneUrl,
be2fb01f
CW
218 ],
219 ];
6a488035
TO
220 }
221 CRM_Utils_System::appendBreadCrumb($breadCrumb);
222 }
223
224 /**
3bdf1f3a 225 * Set default values for the form.
6a488035 226 *
3bdf1f3a 227 * For edit/view mode the default values are retrieved from the database.
6a488035 228 */
00be9182 229 public function setDefaultValues() {
be2fb01f 230 $defaults = [];
6a488035 231 if (isset($this->_id)) {
be2fb01f 232 $params = ['id' => $this->_id];
6a488035 233 CRM_Event_BAO_Event::retrieve($params, $defaults);
49f68a22 234
9c1bc317 235 $this->_campaignID = $defaults['campaign_id'] ?? NULL;
6a488035
TO
236 }
237 elseif ($this->_templateId) {
be2fb01f 238 $params = ['id' => $this->_templateId];
6a488035
TO
239 CRM_Event_BAO_Event::retrieve($params, $defaults);
240 $defaults['is_template'] = $this->_isTemplate;
241 $defaults['template_id'] = $defaults['id'];
242 unset($defaults['id']);
48c866ee
ML
243 unset($defaults['start_date']);
244 unset($defaults['end_date']);
6a488035
TO
245 }
246 else {
247 $defaults['is_active'] = 1;
248 $defaults['style'] = 'Inline';
249 }
250
251 return $defaults;
252 }
253
254 /**
66f9e52b 255 * Build the form object.
6a488035
TO
256 */
257 public function buildQuickForm() {
258 $session = CRM_Core_Session::singleton();
259
9c1bc317 260 $this->_cancelURL = $_POST['cancelURL'] ?? NULL;
6a488035
TO
261
262 if (!$this->_cancelURL) {
263 if ($this->_isTemplate) {
264 $this->_cancelURL = CRM_Utils_System::url('civicrm/admin/eventTemplate',
265 'reset=1'
266 );
267 }
268 else {
269 $this->_cancelURL = CRM_Utils_System::url('civicrm/event/manage',
270 'reset=1'
271 );
272 }
273 }
274
275 if ($this->_cancelURL) {
276 $this->addElement('hidden', 'cancelURL', $this->_cancelURL);
277 }
278
279 if ($this->_single) {
be2fb01f
CW
280 $buttons = [
281 [
6a488035
TO
282 'type' => 'upload',
283 'name' => ts('Save'),
284 'isDefault' => TRUE,
be2fb01f
CW
285 ],
286 [
6a488035
TO
287 'type' => 'upload',
288 'name' => ts('Save and Done'),
289 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
290 'subName' => 'done',
be2fb01f
CW
291 ],
292 [
6a488035
TO
293 'type' => 'cancel',
294 'name' => ts('Cancel'),
be2fb01f
CW
295 ],
296 ];
6a488035
TO
297 $this->addButtons($buttons);
298 }
299 else {
be2fb01f 300 $buttons = [];
6a488035 301 if (!$this->_first) {
be2fb01f 302 $buttons[] = [
6a488035 303 'type' => 'back',
f212d37d 304 'name' => ts('Previous'),
6a488035 305 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
be2fb01f 306 ];
6a488035 307 }
be2fb01f 308 $buttons[] = [
6a488035 309 'type' => 'upload',
f212d37d 310 'name' => ts('Continue'),
6a488035
TO
311 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
312 'isDefault' => TRUE,
be2fb01f
CW
313 ];
314 $buttons[] = [
6a488035
TO
315 'type' => 'cancel',
316 'name' => ts('Cancel'),
be2fb01f 317 ];
6a488035
TO
318
319 $this->addButtons($buttons);
320 }
321 $session->replaceUserContext($this->_cancelURL);
322 $this->add('hidden', 'is_template', $this->_isTemplate);
323 }
324
00be9182 325 public function endPostProcess() {
6a488035
TO
326 // make submit buttons keep the current working tab opened.
327 if ($this->_action & CRM_Core_Action::UPDATE) {
328 $className = CRM_Utils_String::getClassName($this->_name);
329
330 // hack for special cases.
331 switch ($className) {
332 case 'Event':
333 $attributes = $this->getVar('_attributes');
b90552b7 334 $subPage = CRM_Utils_Request::retrieveComponent($attributes);
6a488035
TO
335 break;
336
337 case 'EventInfo':
338 $subPage = 'settings';
339 break;
340
341 case 'ScheduleReminders':
342 $subPage = 'reminder';
343 break;
344
345 default:
346 $subPage = strtolower($className);
347 break;
348 }
349
350 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
be2fb01f 351 [1 => CRM_Utils_Array::value('title', CRM_Utils_Array::value($subPage, $this->get('tabHeader')), $className)]
648deec4 352 ), $this->getTitle(), 'success');
6a488035 353
e46f73c7 354 if (CRM_Core_Component::isEnabled('CiviCampaign')) {
49f68a22 355 $values = $this->controller->exportValues($this->_name);
9c1bc317
CW
356 $newCampaignID = $values['campaign_id'] ?? NULL;
357 $eventID = $values['id'] ?? NULL;
49f68a22
DL
358 if ($eventID && $this->_campaignID != $newCampaignID) {
359 CRM_Event_BAO_Event::updateParticipantCampaignID($eventID, $newCampaignID);
360 }
361 }
d06bcfa8 362 $this->postProcessHook();
6a488035
TO
363 if ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_done") {
364 if ($this->_isTemplate) {
353ffa53
TO
365 CRM_Core_Session::singleton()
366 ->pushUserContext(CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1'));
6a488035
TO
367 }
368 else {
4b628e67 369 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/event/manage', 'reset=1'));
6a488035
TO
370 }
371 }
372 else {
4b628e67 373 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url("civicrm/event/manage/{$subPage}",
353ffa53
TO
374 "action=update&reset=1&id={$this->_id}"
375 ));
6a488035
TO
376 }
377 }
378 }
379
0cf587a7
EM
380 /**
381 * @return string
382 */
00be9182 383 public function getTemplateFileName() {
cc4f2812 384 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 || $this->_action & CRM_Core_Action::DELETE) {
6a488035
TO
385 return parent::getTemplateFileName();
386 }
387 else {
388 // hack lets suppress the form rendering for now
389 self::$_template->assign('isForm', FALSE);
390 return 'CRM/Event/Form/ManageEvent/Tab.tpl';
391 }
392 }
bfd83a87
CW
393
394 /**
395 * Pre-load libraries required by Online Registration Profile fields
396 */
00be9182 397 public static function addProfileEditScripts() {
bfd83a87 398 CRM_UF_Page_ProfileEditor::registerProfileScripts();
be2fb01f 399 CRM_UF_Page_ProfileEditor::registerSchemas(['IndividualModel', 'ParticipantModel']);
bfd83a87 400 }
96025800 401
6a488035 402}