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