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