Merge pull request #4726 from atif-shaikh/CRM-5039
[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 * @protected
47 */
48 public $_id;
49
50 /**
51 * Is this the first page?
52 *
53 * @var boolean
54 * @access protected
55 */
56 protected $_first = FALSE;
57
58 /**
59 * Are we in single form mode or wizard mode?
60 *
61 * @var boolean
62 * @access protected
63 */
64 protected $_single;
65
66 protected $_action;
67
68 /**
69 * Are we actually managing an event template?
70 * @var boolean
71 */
72 protected $_isTemplate = FALSE;
73
74 /**
75 * Pre-populate fields based on this template event_id
76 * @var integer
77 */
78 protected $_templateId;
79
80 protected $_cancelURL = NULL;
81
82 /**
83 * The campaign id of the existing event, we use this to know if we need to update
84 * the participant records
85 */
86 protected $_campaignID = NULL;
87
88 /**
89 * Check if repeating event
90 */
91 protected $_isRepeatingEvent;
92
93 /**
94 * Set variables up before form is built
95 *
96 * @return void
97 * @access public
98 */
99 function preProcess() {
100 $config = CRM_Core_Config::singleton();
101 if (in_array('CiviEvent', $config->enableComponents)) {
102 $this->assign('CiviEvent', TRUE);
103 }
104
105 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add', 'REQUEST');
106
107 $this->assign('action', $this->_action);
108
109 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'GET');
110 if ($this->_id) {
111 $this->_isRepeatingEvent = CRM_Core_BAO_RecurringEntity::getParentFor($this->_id, 'civicrm_event');
112 $this->assign('eventId', $this->_id);
113 if (!empty($this->_addBlockName) && empty($this->_addProfileBottom) && empty($this->_addProfileBottomAdd)) {
114 $this->add('hidden', 'id', $this->_id);
115 }
116 $this->_single = TRUE;
117
118 $params = array('id' => $this->_id);
119 CRM_Event_BAO_Event::retrieve($params, $eventInfo);
120
121 // its an update mode, do a permission check
122 if (!CRM_Event_BAO_Event::checkPermission($this->_id, CRM_Core_Permission::EDIT)) {
123 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
124 }
125
126 $participantListingID = CRM_Utils_Array::value('participant_listing_id', $eventInfo);
127 //CRM_Core_DAO::getFieldValue( 'CRM_Event_DAO_Event', $this->_id, 'participant_listing_id' );
128 if ($participantListingID) {
129 $participantListingURL = CRM_Utils_System::url('civicrm/event/participant',
130 "reset=1&id={$this->_id}",
131 TRUE, NULL, TRUE, TRUE
132 );
133 $this->assign('participantListingURL', $participantListingURL);
134 }
135
136 $this->assign('isOnlineRegistration', CRM_Utils_Array::value('is_online_registration', $eventInfo));
137
138 $this->assign('id', $this->_id);
139 }
140
141 // figure out whether we’re handling an event or an event template
142 if ($this->_id) {
143 $this->_isTemplate = CRM_Utils_Array::value('is_template', $eventInfo);
144 }
145 elseif ($this->_action & CRM_Core_Action::ADD) {
146 $this->_isTemplate = CRM_Utils_Request::retrieve('is_template', 'Boolean', $this);
147 }
148
149 $this->assign('isTemplate', $this->_isTemplate);
150
151 if ($this->_id) {
152 if ($this->_isTemplate) {
153 $title = CRM_Utils_Array::value('template_title', $eventInfo);
154 CRM_Utils_System::setTitle(ts('Edit Event Template') . " - $title");
155 }
156 else {
157 $configureText = ts('Configure Event');
158 $title = CRM_Utils_Array::value('title', $eventInfo);
159 //If it is a repeating event change title
160 if ($this->_isRepeatingEvent) {
161 $configureText = 'Configure Repeating Event';
162 }
163 CRM_Utils_System::setTitle($configureText . " - $title");
164 }
165 $this->assign('title', $title);
166 }
167 elseif ($this->_action & CRM_Core_Action::ADD) {
168 if ($this->_isTemplate) {
169 $title = ts('New Event Template');
170 CRM_Utils_System::setTitle($title);
171 }
172 else {
173 $title = ts('New Event');
174 CRM_Utils_System::setTitle($title);
175 }
176 $this->assign('title', $title);
177 }
178
179 if (CRM_Core_Permission::check('view event participants') &&
180 CRM_Core_Permission::check('view all contacts')
181 ) {
182 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1', 'label');
183 $statusTypesPending = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0', 'label');
184 $findParticipants['statusCounted'] = implode(', ', array_values($statusTypes));
185 $findParticipants['statusNotCounted'] = implode(', ', array_values($statusTypesPending));
186 $this->assign('findParticipants', $findParticipants);
187 }
188
189 $this->_templateId = (int) CRM_Utils_Request::retrieve('template_id', 'Integer', $this);
190
191 //Is a repeating event
192 if ($this->_isRepeatingEvent) {
193 $isRepeatingEntity = TRUE;
194 $this->assign('isRepeatingEntity', $isRepeatingEntity);
195 }
196
197 // also set up tabs
198 CRM_Event_Form_ManageEvent_TabHeader::build($this);
199
200 // Set Done button URL and breadcrumb. Templates go back to Manage Templates,
201 // otherwise go to Manage Event for new event or ManageEventEdit if event if exists.
202 $breadCrumb = array();
203 if (!$this->_isTemplate) {
204 if ($this->_id) {
205 $this->_doneUrl = CRM_Utils_System::url(CRM_Utils_System::currentPath(),
206 "action=update&reset=1&id={$this->_id}"
207 );
208 }
209 else {
210 $this->_doneUrl = CRM_Utils_System::url('civicrm/event/manage',
211 'reset=1'
212 );
213 $breadCrumb = array(array('title' => ts('Manage Events'),
214 'url' => $this->_doneUrl,
215 ));
216 }
217 }
218 else {
219 $this->_doneUrl = CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1');
220 $breadCrumb = array(array('title' => ts('Manage Event Templates'),
221 'url' => $this->_doneUrl,
222 ));
223 }
224 CRM_Utils_System::appendBreadCrumb($breadCrumb);
225 }
226
227 /**
228 * Set default values for the form. For edit/view mode
229 * the default values are retrieved from the database
230 *
231 * @access public
232 *
233 * @return void
234 */
235 function setDefaultValues() {
236 $defaults = array();
237 if (isset($this->_id)) {
238 $params = array('id' => $this->_id);
239 CRM_Event_BAO_Event::retrieve($params, $defaults);
240
241 $this->_campaignID = CRM_Utils_Array::value('campaign_id', $defaults);
242 }
243 elseif ($this->_templateId) {
244 $params = array('id' => $this->_templateId);
245 CRM_Event_BAO_Event::retrieve($params, $defaults);
246 $defaults['is_template'] = $this->_isTemplate;
247 $defaults['template_id'] = $defaults['id'];
248 unset($defaults['id']);
249 }
250 else {
251 $defaults['is_active'] = 1;
252 $defaults['style'] = 'Inline';
253 }
254
255 return $defaults;
256 }
257
258 /**
259 * Build the form object
260 *
261 * @return void
262 * @access public
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',
311 'name' => ts('<< Previous'),
312 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
313 );
314 }
315 $buttons[] = array(
316 'type' => 'upload',
317 'name' => ts('Continue >>'),
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
332 function endPostProcess() {
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.",
358 array(1 => CRM_Utils_Array::value('title', CRM_Utils_Array::value($subPage, $this->get('tabHeader')), $className))
359 ), ts('Saved'), 'success');
360
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 }
370 $this->postProcessHook();
371 if ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_done") {
372 if ($this->_isTemplate) {
373 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1'));
374 }
375 else {
376 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/event/manage', 'reset=1'));
377 }
378 }
379 else {
380 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url("civicrm/event/manage/{$subPage}",
381 "action=update&reset=1&id={$this->_id}"
382 ));
383 }
384 }
385 }
386
387 /**
388 * @return string
389 */
390 function getTemplateFileName() {
391 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 || $this->_action & CRM_Core_Action::DELETE) {
392 return parent::getTemplateFileName();
393 }
394 else {
395 // hack lets suppress the form rendering for now
396 self::$_template->assign('isForm', FALSE);
397 return 'CRM/Event/Form/ManageEvent/Tab.tpl';
398 }
399 }
400
401 /**
402 * Pre-load libraries required by Online Registration Profile fields
403 */
404 static function addProfileEditScripts() {
405 CRM_UF_Page_ProfileEditor::registerProfileScripts();
406 CRM_UF_Page_ProfileEditor::registerSchemas(array('IndividualModel', 'ParticipantModel'));
407 }
408 }
409