REF - Switch to using new `CRM_Core_Component::isEnabled()`
[civicrm-core.git] / CRM / Event / Form / ManageEvent.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 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * This class generates form components for processing Event.
19 */
20 class CRM_Event_Form_ManageEvent extends CRM_Core_Form {
21
22 /**
23 * The id of the event we are processing.
24 *
25 * @var int
26 */
27 public $_id;
28
29 /**
30 * Is this the first page?
31 *
32 * @var bool
33 */
34 protected $_first = FALSE;
35
36 /**
37 * Are we in single form mode or wizard mode?
38 *
39 * @var bool
40 */
41 protected $_single;
42
43 public $_action;
44
45 /**
46 * Are we actually managing an event template?
47 * @var bool
48 */
49 protected $_isTemplate = FALSE;
50
51 /**
52 * Pre-populate fields based on this template event_id.
53 *
54 * @var int
55 */
56 protected $_templateId;
57
58 protected $_cancelURL = NULL;
59
60 /**
61 * The campaign id of the existing event, we use this to know if we need to update
62 * the participant records
63 * @var int
64 */
65 protected $_campaignID = NULL;
66
67 /**
68 * Check if repeating event.
69 * @var bool
70 */
71 public $_isRepeatingEvent;
72
73 /**
74 * Explicitly declare the entity api name.
75 */
76 public function getDefaultEntity() {
77 return 'Event';
78 }
79
80 /**
81 * Explicitly declare the form context.
82 */
83 public function getDefaultContext() {
84 return 'create';
85 }
86
87 /**
88 * Set variables up before form is built.
89 */
90 public function preProcess() {
91 $this->assign('CiviEvent', CRM_Core_Component::isEnabled('CiviEvent'));
92 CRM_Core_Form_RecurringEntity::preProcess('civicrm_event');
93
94 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add', 'REQUEST');
95
96 $this->assign('action', $this->_action);
97
98 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'GET');
99 if ($this->_id) {
100 $this->_isRepeatingEvent = CRM_Core_BAO_RecurringEntity::getParentFor($this->_id, 'civicrm_event');
101 $this->assign('eventId', $this->_id);
102 if (!empty($this->_addBlockName) && empty($this->_addProfileBottom) && empty($this->_addProfileBottomAdd)) {
103 $this->add('hidden', 'id', $this->_id);
104 }
105 $this->_single = TRUE;
106
107 $params = ['id' => $this->_id];
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)) {
112 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
113 }
114
115 $participantListingID = $eventInfo['participant_listing_id'] ?? NULL;
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) {
132 $this->_isTemplate = $eventInfo['is_template'] ?? NULL;
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
140 // Set "Manage Event" Title
141 $title = NULL;
142 if ($this->_id) {
143 if ($this->_isTemplate) {
144 $title = ts('Edit Event Template') . ' - ' . CRM_Utils_Array::value('template_title', $eventInfo);
145 }
146 else {
147 $configureText = $this->_isRepeatingEvent ? ts('Configure Repeating Event') : ts('Configure Event');
148 $title = $configureText . ' - ' . CRM_Utils_Array::value('title', $eventInfo);
149 }
150 }
151 elseif ($this->_action & CRM_Core_Action::ADD) {
152 $title = $this->_isTemplate ? ts('New Event Template') : ts('New Event');
153 }
154 $this->setTitle($title);
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);
167
168 //Is a repeating event
169 if ($this->_isRepeatingEvent) {
170 $isRepeatingEntity = TRUE;
171 $this->assign('isRepeatingEntity', $isRepeatingEntity);
172 }
173
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);
178 $checkPermission = [
179 [
180 'administer CiviCRM data',
181 'manage event profiles',
182 ],
183 ];
184 if (CRM_Core_Permission::check($checkPermission) || !empty($ufCreate) || !empty($ufEdit)) {
185 $this->assign('perm', TRUE);
186 }
187
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.
193 $breadCrumb = [];
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 );
204 $breadCrumb = [
205 [
206 'title' => ts('Manage Events'),
207 'url' => $this->_doneUrl,
208 ],
209 ];
210 }
211 }
212 else {
213 $this->_doneUrl = CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1');
214 $breadCrumb = [
215 [
216 'title' => ts('Manage Event Templates'),
217 'url' => $this->_doneUrl,
218 ],
219 ];
220 }
221 CRM_Utils_System::appendBreadCrumb($breadCrumb);
222 }
223
224 /**
225 * Set default values for the form.
226 *
227 * For edit/view mode the default values are retrieved from the database.
228 */
229 public function setDefaultValues() {
230 $defaults = [];
231 if (isset($this->_id)) {
232 $params = ['id' => $this->_id];
233 CRM_Event_BAO_Event::retrieve($params, $defaults);
234
235 $this->_campaignID = $defaults['campaign_id'] ?? NULL;
236 }
237 elseif ($this->_templateId) {
238 $params = ['id' => $this->_templateId];
239 CRM_Event_BAO_Event::retrieve($params, $defaults);
240 $defaults['is_template'] = $this->_isTemplate;
241 $defaults['template_id'] = $defaults['id'];
242 unset($defaults['id']);
243 unset($defaults['start_date']);
244 unset($defaults['end_date']);
245 }
246 else {
247 $defaults['is_active'] = 1;
248 $defaults['style'] = 'Inline';
249 }
250
251 return $defaults;
252 }
253
254 /**
255 * Build the form object.
256 */
257 public function buildQuickForm() {
258 $session = CRM_Core_Session::singleton();
259
260 $this->_cancelURL = $_POST['cancelURL'] ?? NULL;
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) {
280 $buttons = [
281 [
282 'type' => 'upload',
283 'name' => ts('Save'),
284 'isDefault' => TRUE,
285 ],
286 [
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',
291 ],
292 [
293 'type' => 'cancel',
294 'name' => ts('Cancel'),
295 ],
296 ];
297 $this->addButtons($buttons);
298 }
299 else {
300 $buttons = [];
301 if (!$this->_first) {
302 $buttons[] = [
303 'type' => 'back',
304 'name' => ts('Previous'),
305 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
306 ];
307 }
308 $buttons[] = [
309 'type' => 'upload',
310 'name' => ts('Continue'),
311 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
312 'isDefault' => TRUE,
313 ];
314 $buttons[] = [
315 'type' => 'cancel',
316 'name' => ts('Cancel'),
317 ];
318
319 $this->addButtons($buttons);
320 }
321 $session->replaceUserContext($this->_cancelURL);
322 $this->add('hidden', 'is_template', $this->_isTemplate);
323 }
324
325 public function endPostProcess() {
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');
334 $subPage = CRM_Utils_Request::retrieveComponent($attributes);
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.",
351 [1 => CRM_Utils_Array::value('title', CRM_Utils_Array::value($subPage, $this->get('tabHeader')), $className)]
352 ), $this->getTitle(), 'success');
353
354 if (CRM_Core_Component::isEnabled('CiviCampaign')) {
355 $values = $this->controller->exportValues($this->_name);
356 $newCampaignID = $values['campaign_id'] ?? NULL;
357 $eventID = $values['id'] ?? NULL;
358 if ($eventID && $this->_campaignID != $newCampaignID) {
359 CRM_Event_BAO_Event::updateParticipantCampaignID($eventID, $newCampaignID);
360 }
361 }
362 $this->postProcessHook();
363 if ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_done") {
364 if ($this->_isTemplate) {
365 CRM_Core_Session::singleton()
366 ->pushUserContext(CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1'));
367 }
368 else {
369 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/event/manage', 'reset=1'));
370 }
371 }
372 else {
373 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url("civicrm/event/manage/{$subPage}",
374 "action=update&reset=1&id={$this->_id}"
375 ));
376 }
377 }
378 }
379
380 /**
381 * @return string
382 */
383 public function getTemplateFileName() {
384 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 || $this->_action & CRM_Core_Action::DELETE) {
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 }
393
394 /**
395 * Pre-load libraries required by Online Registration Profile fields
396 */
397 public static function addProfileEditScripts() {
398 CRM_UF_Page_ProfileEditor::registerProfileScripts();
399 CRM_UF_Page_ProfileEditor::registerSchemas(['IndividualModel', 'ParticipantModel']);
400 }
401
402 }