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