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