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