Fix unreleased regression on batch merge through the UI
[civicrm-core.git] / CRM / Event / Form / ManageEvent / EventInfo.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
6a488035 29 * @package CRM
6b83d5bd 30 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
31 */
32
33/**
3bdf1f3a 34 * This class generates form components for processing Event.
6a488035
TO
35 */
36class CRM_Event_Form_ManageEvent_EventInfo extends CRM_Event_Form_ManageEvent {
37
38 /**
66f9e52b 39 * Event type.
90b461f1 40 * @var int
6a488035
TO
41 */
42 protected $_eventType = NULL;
43
44 /**
66f9e52b 45 * Set variables up before form is built.
6a488035 46 */
00be9182 47 public function preProcess() {
2a4e505f 48 parent::preProcess();
e4b857f8 49 $this->setSelectedChild('settings');
6a488035 50
8c605c27
MD
51 $entityID = $this->_id ?: $this->_templateId;
52 if ($entityID) {
53 $this->assign('entityID', $entityID);
6a488035 54 $eventType = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
8c605c27 55 $entityID,
6a488035
TO
56 'event_type_id'
57 );
58 }
59 else {
60 $eventType = 'null';
61 }
62
63 $showLocation = FALSE;
64 // when custom data is included in this page
a7488080 65 if (!empty($_POST['hidden_custom'])) {
6a488035
TO
66 $this->set('type', 'Event');
67 $this->set('subType', CRM_Utils_Array::value('event_type_id', $_POST));
d6dd7344 68 $this->assign('customDataSubType', CRM_Utils_Array::value('event_type_id', $_POST));
6a488035
TO
69 $this->set('entityId', $this->_id);
70
f19dc468 71 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_eventType, 1, 'Event', $this->_id);
6a488035
TO
72 CRM_Custom_Form_CustomData::buildQuickForm($this);
73 CRM_Custom_Form_CustomData::setDefaultValues($this);
74 }
75 }
76
77 /**
3bdf1f3a 78 * Set default values for the form.
6a488035 79 *
3bdf1f3a 80 * For edit/view mode he default values are retrieved from the database.
6a488035 81 */
00be9182 82 public function setDefaultValues() {
6a488035
TO
83 $defaults = parent::setDefaultValues();
84
85 // in update mode, we need to set custom data subtype to tpl
a7488080 86 if (!empty($defaults['event_type_id'])) {
6a488035
TO
87 $this->assign('customDataSubType', $defaults['event_type_id']);
88 }
89
90 $this->_showHide = new CRM_Core_ShowHideBlocks();
91 // Show waitlist features or event_full_text if max participants set
a7488080 92 if (!empty($defaults['max_participants'])) {
6a488035 93 $this->_showHide->addShow('id-waitlist');
a7488080 94 if (!empty($defaults['has_waitlist'])) {
6a488035
TO
95 $this->_showHide->addShow('id-waitlist-text');
96 $this->_showHide->addHide('id-event_full');
97 }
98 else {
99 $this->_showHide->addHide('id-waitlist-text');
100 $this->_showHide->addShow('id-event_full');
101 }
102 }
103 else {
104 $this->_showHide->addHide('id-event_full');
105 $this->_showHide->addHide('id-waitlist');
106 $this->_showHide->addHide('id-waitlist-text');
107 }
108
109 $this->_showHide->addToTemplate();
110 $this->assign('elemType', 'table-row');
111
112 $this->assign('description', CRM_Utils_Array::value('description', $defaults));
113
114 // Provide suggested text for event full and waitlist messages if they're empty
115 $defaults['event_full_text'] = CRM_Utils_Array::value('event_full_text', $defaults, ts('This event is currently full.'));
116
117 $defaults['waitlist_text'] = CRM_Utils_Array::value('waitlist_text', $defaults, ts('This event is currently full. However you can register now and get added to a waiting list. You will be notified if spaces become available.'));
10672453 118 $defaults['template_id'] = $this->_templateId;
6a488035
TO
119 return $defaults;
120 }
121
122 /**
66f9e52b 123 * Build the form object.
6a488035
TO
124 */
125 public function buildQuickForm() {
6a488035
TO
126 //need to assign custom data type and subtype to the template
127 $this->assign('customDataType', 'Event');
128 if ($this->_eventType) {
129 $this->assign('customDataSubType', $this->_eventType);
130 }
6a488035
TO
131
132 $this->_first = TRUE;
133 $this->applyFilter('__ALL__', 'trim');
134 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
135
136 if ($this->_isTemplate) {
137 $this->add('text', 'template_title', ts('Template Title'), $attributes['template_title'], TRUE);
138 }
139
140 if ($this->_action & CRM_Core_Action::ADD) {
141 $eventTemplates = CRM_Event_PseudoConstant::eventTemplates();
142 if (CRM_Utils_System::isNull($eventTemplates) && !$this->_isTemplate) {
be2fb01f
CW
143 $url = CRM_Utils_System::url('civicrm/admin/eventTemplate', ['reset' => 1]);
144 CRM_Core_Session::setStatus(ts('If you find that you are creating multiple events with similar settings, you may want to use the <a href="%1">Event Templates</a> feature to streamline your workflow.', [1 => $url]), ts('Tip'), 'info');
6a488035
TO
145 }
146 if (!CRM_Utils_System::isNull($eventTemplates)) {
be2fb01f 147 $this->add('select', 'template_id', ts('From Template'), ['' => ts('- select -')] + $eventTemplates, FALSE, ['class' => 'crm-select2 huge']);
6a488035 148 }
bfd83a87
CW
149 // Make sure this form redirects properly
150 $this->preventAjaxSubmit();
6a488035
TO
151 }
152
153 // add event title, make required if this is not a template
154 $this->add('text', 'title', ts('Event Title'), $attributes['event_title'], !$this->_isTemplate);
155
e1462487 156 $this->addSelect('event_type_id',
be2fb01f 157 ['onChange' => "CRM.buildCustomData( 'Event', this.value );"],
e1462487 158 TRUE
6a488035
TO
159 );
160
161 //CRM-7362 --add campaigns.
162 $campaignId = NULL;
163 if ($this->_id) {
164 $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'campaign_id');
165 }
166 CRM_Campaign_BAO_Campaign::addCampaign($this, $campaignId);
167
be2fb01f 168 $this->addSelect('default_role_id', [], TRUE);
6a488035 169
be2fb01f 170 $this->addSelect('participant_listing_id', ['placeholder' => ts('Disabled'), 'option_url' => NULL]);
6a488035
TO
171
172 $this->add('textarea', 'summary', ts('Event Summary'), $attributes['summary']);
be2fb01f 173 $this->add('wysiwyg', 'description', ts('Complete Description'), $attributes['event_description'] + ['preset' => 'civievent']);
0781df9b 174 $this->addElement('checkbox', 'is_public', ts('Public Event'));
6a488035 175 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
0781df9b 176 $this->addElement('checkbox', 'is_map', ts('Include Map to Event Location'));
6a488035 177
67cc69d7 178 $this->add('datepicker', 'start_date', ts('Start'), [], !$this->_isTemplate, ['time' => TRUE]);
8b827219 179 $this->add('datepicker', 'end_date', ts('End'), [], FALSE, ['time' => TRUE]);
6a488035 180
d70ada2a 181 $this->add('number', 'max_participants', ts('Max Number of Participants'),
be2fb01f 182 ['onchange' => "if (this.value != '') {cj('#id-waitlist').show(); showHideByValue('has_waitlist','0','id-waitlist-text','table-row','radio',false); showHideByValue('has_waitlist','0','id-event_full','table-row','radio',true); return;} else {cj('#id-event_full, #id-waitlist, #id-waitlist-text').hide(); return;}"]
6a488035
TO
183 );
184 $this->addRule('max_participants', ts('Max participants should be a positive number'), 'positiveInteger');
185
186 $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
187 $waitlist = 0;
188 if (in_array('On waitlist', $participantStatuses) and in_array('Pending from waitlist', $participantStatuses)) {
be2fb01f 189 $this->addElement('checkbox', 'has_waitlist', ts('Offer a Waitlist?'), NULL, ['onclick' => "showHideByValue('has_waitlist','0','id-event_full','table-row','radio',true); showHideByValue('has_waitlist','0','id-waitlist-text','table-row','radio',false);"]);
6a488035
TO
190 $this->add('textarea', 'waitlist_text', ts('Waitlist Message'), $attributes['waitlist_text']);
191 $waitlist = 1;
192 }
193 $this->assign('waitlist', $waitlist);
194
195 $this->add('textarea', 'event_full_text', ts('Message if Event Is Full'), $attributes['event_full_text']);
196
197 $this->addElement('checkbox', 'is_active', ts('Is this Event Active?'));
198
be2fb01f 199 $this->addFormRule(['CRM_Event_Form_ManageEvent_EventInfo', 'formRule']);
6a488035
TO
200
201 parent::buildQuickForm();
202 }
203
204 /**
66f9e52b 205 * Global validation rules for the form.
6a488035 206 *
c490a46a 207 * @param array $values
6a488035 208 *
a6c01b45
CW
209 * @return array
210 * list of errors to be posted back to the form
6a488035 211 */
00be9182 212 public static function formRule($values) {
be2fb01f 213 $errors = [];
6a488035 214
67cc69d7
J
215 if (!empty($values['end_date']) && ($values['end_date'] < $values['start_date'])) {
216 $errors['end_date'] = ts('End date should be after Start date.');
6a488035
TO
217 }
218
219 //CRM-4286
220 if (strstr($values['title'], '/')) {
221 $errors['title'] = ts("Please do not use '/' in Event Title.");
222 }
223
224 return $errors;
225 }
226
227 /**
66f9e52b 228 * Process the form submission.
6a488035
TO
229 */
230 public function postProcess() {
8c605c27 231 $params = array_merge($this->controller->exportValues($this->_name), $this->_submitValues);
6a488035
TO
232
233 //format params
c71da91d
MWMC
234 $params['start_date'] = CRM_Utils_Array::value('start_date', $params);
235 $params['end_date'] = CRM_Utils_Array::value('end_date', $params);
6a488035
TO
236 $params['has_waitlist'] = CRM_Utils_Array::value('has_waitlist', $params, FALSE);
237 $params['is_map'] = CRM_Utils_Array::value('is_map', $params, FALSE);
238 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
239 $params['is_public'] = CRM_Utils_Array::value('is_public', $params, FALSE);
240 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
241 $params['default_role_id'] = CRM_Utils_Array::value('default_role_id', $params, FALSE);
242 $params['id'] = $this->_id;
243
6a488035 244 //merge params with defaults from templates
a7488080 245 if (!empty($params['template_id'])) {
6a488035 246 $params = array_merge(CRM_Event_BAO_Event::getTemplateDefaultValues($params['template_id']), $params);
8c605c27
MD
247 foreach ($params as $key => $value) {
248 $customFieldInfo = CRM_Core_BAO_CustomField::getKeyID($key, TRUE);
249 if (!empty($customFieldInfo[1])) {
250 $params[str_replace($customFieldInfo[1], '-' . $customFieldInfo[1], $key)] = $value;
251 unset($params[$key]);
252 }
253 }
6a488035
TO
254 }
255
8c605c27
MD
256 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
257 $this->_id,
258 'Event'
259 );
260
6a488035 261 // now that we have the event’s id, do some more template-based stuff
a7488080 262 if (!empty($params['template_id'])) {
2e45b2f8 263 $event = CRM_Event_BAO_Event::copy($params['template_id'], $params);
ae70f47e
MWMC
264 }
265 else {
266 $event = CRM_Event_BAO_Event::create($params);
6a488035 267 }
03e04002 268
6a488035
TO
269 $this->set('id', $event->id);
270
c654c119 271 $this->postProcessHook();
272
6a488035
TO
273 if ($this->_action & CRM_Core_Action::ADD) {
274 $url = 'civicrm/event/manage/location';
275 $urlParams = "action=update&reset=1&id={$event->id}";
276 // special case for 'Save and Done' consistency.
277 if ($this->controller->getButtonName('submit') == '_qf_EventInfo_upload_done') {
278 $url = 'civicrm/event/manage';
279 $urlParams = 'reset=1';
280 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
be2fb01f 281 [1 => $this->getTitle()]
353ffa53 282 ), ts('Saved'), 'success');
6a488035
TO
283 }
284
285 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
286 }
287
288 parent::endPostProcess();
289 }
6a488035
TO
290
291 /**
3bdf1f3a 292 * Return a descriptive name for the page, used in wizard header.
6a488035
TO
293 *
294 * @return string
6a488035
TO
295 */
296 public function getTitle() {
297 return ts('Event Information and Settings');
298 }
299
6a488035 300}