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