Merge pull request #14725 from eileenmcnaughton/cust_field4
[civicrm-core.git] / CRM / Event / Form / ManageEvent.php
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 */
36 class 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 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
118 /**
119 * Set variables up before form is built.
120 */
121 public function preProcess() {
122 $config = CRM_Core_Config::singleton();
123 if (in_array('CiviEvent', $config->enableComponents)) {
124 $this->assign('CiviEvent', TRUE);
125 }
126 CRM_Core_Form_RecurringEntity::preProcess('civicrm_event');
127
128 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add', 'REQUEST');
129
130 $this->assign('action', $this->_action);
131
132 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'GET');
133 if ($this->_id) {
134 $this->_isRepeatingEvent = CRM_Core_BAO_RecurringEntity::getParentFor($this->_id, 'civicrm_event');
135 $this->assign('eventId', $this->_id);
136 if (!empty($this->_addBlockName) && empty($this->_addProfileBottom) && empty($this->_addProfileBottomAdd)) {
137 $this->add('hidden', 'id', $this->_id);
138 }
139 $this->_single = TRUE;
140
141 $params = ['id' => $this->_id];
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)) {
146 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
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
174 // Set "Manage Event" Title
175 $title = NULL;
176 if ($this->_id) {
177 if ($this->_isTemplate) {
178 $title = ts('Edit Event Template') . ' - ' . CRM_Utils_Array::value('template_title', $eventInfo);
179 }
180 else {
181 $configureText = $this->_isRepeatingEvent ? ts('Configure Repeating Event') : ts('Configure Event');
182 $title = $configureText . ' - ' . CRM_Utils_Array::value('title', $eventInfo);
183 }
184 }
185 elseif ($this->_action & CRM_Core_Action::ADD) {
186 $title = $this->_isTemplate ? ts('New Event Template') : ts('New Event');
187 }
188 $this->setTitle($title);
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);
201
202 //Is a repeating event
203 if ($this->_isRepeatingEvent) {
204 $isRepeatingEntity = TRUE;
205 $this->assign('isRepeatingEntity', $isRepeatingEntity);
206 }
207
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);
212 $checkPermission = [
213 [
214 'administer CiviCRM',
215 'manage event profiles',
216 ],
217 ];
218 if (CRM_Core_Permission::check($checkPermission) || !empty($ufCreate) || !empty($ufEdit)) {
219 $this->assign('perm', TRUE);
220 }
221
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.
227 $breadCrumb = [];
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 );
238 $breadCrumb = [
239 [
240 'title' => ts('Manage Events'),
241 'url' => $this->_doneUrl,
242 ],
243 ];
244 }
245 }
246 else {
247 $this->_doneUrl = CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1');
248 $breadCrumb = [
249 [
250 'title' => ts('Manage Event Templates'),
251 'url' => $this->_doneUrl,
252 ],
253 ];
254 }
255 CRM_Utils_System::appendBreadCrumb($breadCrumb);
256 }
257
258 /**
259 * Set default values for the form.
260 *
261 * For edit/view mode the default values are retrieved from the database.
262 */
263 public function setDefaultValues() {
264 $defaults = [];
265 if (isset($this->_id)) {
266 $params = ['id' => $this->_id];
267 CRM_Event_BAO_Event::retrieve($params, $defaults);
268
269 $this->_campaignID = CRM_Utils_Array::value('campaign_id', $defaults);
270 }
271 elseif ($this->_templateId) {
272 $params = ['id' => $this->_templateId];
273 CRM_Event_BAO_Event::retrieve($params, $defaults);
274 $defaults['is_template'] = $this->_isTemplate;
275 $defaults['template_id'] = $defaults['id'];
276 unset($defaults['id']);
277 }
278 else {
279 $defaults['is_active'] = 1;
280 $defaults['style'] = 'Inline';
281 }
282
283 return $defaults;
284 }
285
286 /**
287 * Build the form object.
288 */
289 public function buildQuickForm() {
290 $session = CRM_Core_Session::singleton();
291
292 $this->_cancelURL = CRM_Utils_Array::value('cancelURL', $_POST);
293
294 if (!$this->_cancelURL) {
295 if ($this->_isTemplate) {
296 $this->_cancelURL = CRM_Utils_System::url('civicrm/admin/eventTemplate',
297 'reset=1'
298 );
299 }
300 else {
301 $this->_cancelURL = CRM_Utils_System::url('civicrm/event/manage',
302 'reset=1'
303 );
304 }
305 }
306
307 if ($this->_cancelURL) {
308 $this->addElement('hidden', 'cancelURL', $this->_cancelURL);
309 }
310
311 if ($this->_single) {
312 $buttons = [
313 [
314 'type' => 'upload',
315 'name' => ts('Save'),
316 'isDefault' => TRUE,
317 ],
318 [
319 'type' => 'upload',
320 'name' => ts('Save and Done'),
321 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
322 'subName' => 'done',
323 ],
324 [
325 'type' => 'cancel',
326 'name' => ts('Cancel'),
327 ],
328 ];
329 $this->addButtons($buttons);
330 }
331 else {
332 $buttons = [];
333 if (!$this->_first) {
334 $buttons[] = [
335 'type' => 'back',
336 'name' => ts('Previous'),
337 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
338 ];
339 }
340 $buttons[] = [
341 'type' => 'upload',
342 'name' => ts('Continue'),
343 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
344 'isDefault' => TRUE,
345 ];
346 $buttons[] = [
347 'type' => 'cancel',
348 'name' => ts('Cancel'),
349 ];
350
351 $this->addButtons($buttons);
352 }
353 $session->replaceUserContext($this->_cancelURL);
354 $this->add('hidden', 'is_template', $this->_isTemplate);
355 }
356
357 public function endPostProcess() {
358 // make submit buttons keep the current working tab opened.
359 if ($this->_action & CRM_Core_Action::UPDATE) {
360 $className = CRM_Utils_String::getClassName($this->_name);
361
362 // hack for special cases.
363 switch ($className) {
364 case 'Event':
365 $attributes = $this->getVar('_attributes');
366 $subPage = CRM_Utils_Request::retrieveComponent($attributes);
367 break;
368
369 case 'EventInfo':
370 $subPage = 'settings';
371 break;
372
373 case 'ScheduleReminders':
374 $subPage = 'reminder';
375 break;
376
377 default:
378 $subPage = strtolower($className);
379 break;
380 }
381
382 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
383 [1 => CRM_Utils_Array::value('title', CRM_Utils_Array::value($subPage, $this->get('tabHeader')), $className)]
384 ), $this->getTitle(), 'success');
385
386 $config = CRM_Core_Config::singleton();
387 if (in_array('CiviCampaign', $config->enableComponents)) {
388 $values = $this->controller->exportValues($this->_name);
389 $newCampaignID = CRM_Utils_Array::value('campaign_id', $values);
390 $eventID = CRM_Utils_Array::value('id', $values);
391 if ($eventID && $this->_campaignID != $newCampaignID) {
392 CRM_Event_BAO_Event::updateParticipantCampaignID($eventID, $newCampaignID);
393 }
394 }
395 $this->postProcessHook();
396 if ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_done") {
397 if ($this->_isTemplate) {
398 CRM_Core_Session::singleton()
399 ->pushUserContext(CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1'));
400 }
401 else {
402 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/event/manage', 'reset=1'));
403 }
404 }
405 else {
406 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url("civicrm/event/manage/{$subPage}",
407 "action=update&reset=1&id={$this->_id}"
408 ));
409 }
410 }
411 }
412
413 /**
414 * @return string
415 */
416 public function getTemplateFileName() {
417 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 || $this->_action & CRM_Core_Action::DELETE) {
418 return parent::getTemplateFileName();
419 }
420 else {
421 // hack lets suppress the form rendering for now
422 self::$_template->assign('isForm', FALSE);
423 return 'CRM/Event/Form/ManageEvent/Tab.tpl';
424 }
425 }
426
427 /**
428 * Pre-load libraries required by Online Registration Profile fields
429 */
430 public static function addProfileEditScripts() {
431 CRM_UF_Page_ProfileEditor::registerProfileScripts();
432 CRM_UF_Page_ProfileEditor::registerSchemas(['IndividualModel', 'ParticipantModel']);
433 }
434
435 }