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