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