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