Merge pull request #6273 from rohankatkar/CRM-16857
[civicrm-core.git] / CRM / Event / Form / ManageEvent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * Set variables up before form is built.
92 *
93 * @return void
94 */
95 public function preProcess() {
96 $config = CRM_Core_Config::singleton();
97 if (in_array('CiviEvent', $config->enableComponents)) {
98 $this->assign('CiviEvent', TRUE);
99 }
100 CRM_Core_Form_RecurringEntity::preProcess('civicrm_event');
101
102 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add', 'REQUEST');
103
104 $this->assign('action', $this->_action);
105
106 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'GET');
107 if ($this->_id) {
108 $this->_isRepeatingEvent = CRM_Core_BAO_RecurringEntity::getParentFor($this->_id, 'civicrm_event');
109 $this->assign('eventId', $this->_id);
110 if (!empty($this->_addBlockName) && empty($this->_addProfileBottom) && empty($this->_addProfileBottomAdd)) {
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)) {
120 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
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 {
154 $configureText = ts('Configure Event');
155 $title = CRM_Utils_Array::value('title', $eventInfo);
156 //If it is a repeating event change title
157 if ($this->_isRepeatingEvent) {
158 $configureText = 'Configure Repeating Event';
159 }
160 CRM_Utils_System::setTitle($configureText . " - $title");
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);
187
188 //Is a repeating event
189 if ($this->_isRepeatingEvent) {
190 $isRepeatingEntity = TRUE;
191 $this->assign('isRepeatingEntity', $isRepeatingEntity);
192 }
193
194 // CRM-16776 - show edit/copy/create buttons for Profiles if user has required permission.
195 $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
196 $ufCreate = CRM_ACL_API::group(CRM_Core_Permission::CREATE, NULL, 'civicrm_uf_group', $ufGroups);
197 $ufEdit = CRM_ACL_API::group(CRM_Core_Permission::EDIT, NULL, 'civicrm_uf_group', $ufGroups);
198 $checkPermission = array(
199 array(
200 'administer CiviCRM',
201 'manage event profiles',
202 ),
203 );
204 if (CRM_Core_Permission::check($checkPermission) || !empty($ufCreate) || !empty($ufEdit)) {
205 $this->assign('perm', TRUE);
206 }
207
208 // also set up tabs
209 CRM_Event_Form_ManageEvent_TabHeader::build($this);
210
211 // Set Done button URL and breadcrumb. Templates go back to Manage Templates,
212 // otherwise go to Manage Event for new event or ManageEventEdit if event if exists.
213 $breadCrumb = array();
214 if (!$this->_isTemplate) {
215 if ($this->_id) {
216 $this->_doneUrl = CRM_Utils_System::url(CRM_Utils_System::currentPath(),
217 "action=update&reset=1&id={$this->_id}"
218 );
219 }
220 else {
221 $this->_doneUrl = CRM_Utils_System::url('civicrm/event/manage',
222 'reset=1'
223 );
224 $breadCrumb = array(
225 array(
226 'title' => ts('Manage Events'),
227 'url' => $this->_doneUrl,
228 ),
229 );
230 }
231 }
232 else {
233 $this->_doneUrl = CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1');
234 $breadCrumb = array(
235 array(
236 'title' => ts('Manage Event Templates'),
237 'url' => $this->_doneUrl,
238 ),
239 );
240 }
241 CRM_Utils_System::appendBreadCrumb($breadCrumb);
242 }
243
244 /**
245 * Set default values for the form. For edit/view mode
246 * the default values are retrieved from the database
247 *
248 *
249 * @return void
250 */
251 public function setDefaultValues() {
252 $defaults = array();
253 if (isset($this->_id)) {
254 $params = array('id' => $this->_id);
255 CRM_Event_BAO_Event::retrieve($params, $defaults);
256
257 $this->_campaignID = CRM_Utils_Array::value('campaign_id', $defaults);
258 }
259 elseif ($this->_templateId) {
260 $params = array('id' => $this->_templateId);
261 CRM_Event_BAO_Event::retrieve($params, $defaults);
262 $defaults['is_template'] = $this->_isTemplate;
263 $defaults['template_id'] = $defaults['id'];
264 unset($defaults['id']);
265 }
266 else {
267 $defaults['is_active'] = 1;
268 $defaults['style'] = 'Inline';
269 }
270
271 return $defaults;
272 }
273
274 /**
275 * Build the form object.
276 *
277 * @return void
278 */
279 public function buildQuickForm() {
280 $session = CRM_Core_Session::singleton();
281
282 $this->_cancelURL = CRM_Utils_Array::value('cancelURL', $_POST);
283
284 if (!$this->_cancelURL) {
285 if ($this->_isTemplate) {
286 $this->_cancelURL = CRM_Utils_System::url('civicrm/admin/eventTemplate',
287 'reset=1'
288 );
289 }
290 else {
291 $this->_cancelURL = CRM_Utils_System::url('civicrm/event/manage',
292 'reset=1'
293 );
294 }
295 }
296
297 if ($this->_cancelURL) {
298 $this->addElement('hidden', 'cancelURL', $this->_cancelURL);
299 }
300
301 if ($this->_single) {
302 $buttons = array(
303 array(
304 'type' => 'upload',
305 'name' => ts('Save'),
306 'isDefault' => TRUE,
307 ),
308 array(
309 'type' => 'upload',
310 'name' => ts('Save and Done'),
311 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
312 'subName' => 'done',
313 ),
314 array(
315 'type' => 'cancel',
316 'name' => ts('Cancel'),
317 ),
318 );
319 $this->addButtons($buttons);
320 }
321 else {
322 $buttons = array();
323 if (!$this->_first) {
324 $buttons[] = array(
325 'type' => 'back',
326 'name' => ts('Previous'),
327 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
328 );
329 }
330 $buttons[] = array(
331 'type' => 'upload',
332 'name' => ts('Continue'),
333 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
334 'isDefault' => TRUE,
335 );
336 $buttons[] = array(
337 'type' => 'cancel',
338 'name' => ts('Cancel'),
339 );
340
341 $this->addButtons($buttons);
342 }
343 $session->replaceUserContext($this->_cancelURL);
344 $this->add('hidden', 'is_template', $this->_isTemplate);
345 }
346
347 public function endPostProcess() {
348 // make submit buttons keep the current working tab opened.
349 if ($this->_action & CRM_Core_Action::UPDATE) {
350 $className = CRM_Utils_String::getClassName($this->_name);
351
352 // hack for special cases.
353 switch ($className) {
354 case 'Event':
355 $attributes = $this->getVar('_attributes');
356 $subPage = strtolower(basename(CRM_Utils_Array::value('action', $attributes)));
357 break;
358
359 case 'EventInfo':
360 $subPage = 'settings';
361 break;
362
363 case 'ScheduleReminders':
364 $subPage = 'reminder';
365 break;
366
367 default:
368 $subPage = strtolower($className);
369 break;
370 }
371
372 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
373 array(1 => CRM_Utils_Array::value('title', CRM_Utils_Array::value($subPage, $this->get('tabHeader')), $className))
374 ), ts('Saved'), 'success');
375
376 $config = CRM_Core_Config::singleton();
377 if (in_array('CiviCampaign', $config->enableComponents)) {
378 $values = $this->controller->exportValues($this->_name);
379 $newCampaignID = CRM_Utils_Array::value('campaign_id', $values);
380 $eventID = CRM_Utils_Array::value('id', $values);
381 if ($eventID && $this->_campaignID != $newCampaignID) {
382 CRM_Event_BAO_Event::updateParticipantCampaignID($eventID, $newCampaignID);
383 }
384 }
385 $this->postProcessHook();
386 if ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_done") {
387 if ($this->_isTemplate) {
388 CRM_Core_Session::singleton()
389 ->pushUserContext(CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1'));
390 }
391 else {
392 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/event/manage', 'reset=1'));
393 }
394 }
395 else {
396 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url("civicrm/event/manage/{$subPage}",
397 "action=update&reset=1&id={$this->_id}"
398 ));
399 }
400 }
401 }
402
403 /**
404 * @return string
405 */
406 public function getTemplateFileName() {
407 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 || $this->_action & CRM_Core_Action::DELETE) {
408 return parent::getTemplateFileName();
409 }
410 else {
411 // hack lets suppress the form rendering for now
412 self::$_template->assign('isForm', FALSE);
413 return 'CRM/Event/Form/ManageEvent/Tab.tpl';
414 }
415 }
416
417 /**
418 * Pre-load libraries required by Online Registration Profile fields
419 */
420 public static function addProfileEditScripts() {
421 CRM_UF_Page_ProfileEditor::registerProfileScripts();
422 CRM_UF_Page_ProfileEditor::registerSchemas(array('IndividualModel', 'ParticipantModel'));
423 }
424
425 }