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