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