Merge pull request #22812 from totten/master-queue-schema
[civicrm-core.git] / CRM / Event / Form / ManageEvent / EventInfo.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * This class generates form components for processing Event.
19 */
20 class CRM_Event_Form_ManageEvent_EventInfo extends CRM_Event_Form_ManageEvent {
21
22 /**
23 * Event type.
24 * @var int
25 */
26 protected $_eventType = NULL;
27
28 /**
29 * Set variables up before form is built.
30 */
31 public function preProcess() {
32 parent::preProcess();
33 $this->setSelectedChild('settings');
34
35 $entityID = $this->_id ?: $this->_templateId;
36 if ($entityID) {
37 $this->assign('entityID', $entityID);
38 $eventType = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
39 $entityID,
40 'event_type_id'
41 );
42 }
43 else {
44 $eventType = 'null';
45 }
46
47 $showLocation = FALSE;
48 // when custom data is included in this page
49 if (!empty($_POST['hidden_custom'])) {
50 $this->set('type', 'Event');
51 $this->set('subType', CRM_Utils_Array::value('event_type_id', $_POST));
52 $this->assign('customDataSubType', CRM_Utils_Array::value('event_type_id', $_POST));
53 $this->set('entityId', $entityID);
54
55 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_eventType, 1, 'Event', $entityID);
56 CRM_Custom_Form_CustomData::buildQuickForm($this);
57 CRM_Custom_Form_CustomData::setDefaultValues($this);
58 }
59 }
60
61 /**
62 * Set default values for the form.
63 *
64 * For edit/view mode he default values are retrieved from the database.
65 */
66 public function setDefaultValues() {
67 $defaults = parent::setDefaultValues();
68
69 // in update mode, we need to set custom data subtype to tpl
70 if (!empty($defaults['event_type_id'])) {
71 $this->assign('customDataSubType', $defaults['event_type_id']);
72 }
73
74 $this->_showHide = new CRM_Core_ShowHideBlocks();
75 // Show waitlist features or event_full_text if max participants set
76 if (!empty($defaults['max_participants'])) {
77 $this->_showHide->addShow('id-waitlist');
78 if (!empty($defaults['has_waitlist'])) {
79 $this->_showHide->addShow('id-waitlist-text');
80 $this->_showHide->addHide('id-event_full');
81 }
82 else {
83 $this->_showHide->addHide('id-waitlist-text');
84 $this->_showHide->addShow('id-event_full');
85 }
86 }
87 else {
88 $this->_showHide->addHide('id-event_full');
89 $this->_showHide->addHide('id-waitlist');
90 $this->_showHide->addHide('id-waitlist-text');
91 }
92
93 $this->_showHide->addToTemplate();
94 $this->assign('elemType', 'table-row');
95
96 $this->assign('description', CRM_Utils_Array::value('description', $defaults));
97
98 // Provide suggested text for event full and waitlist messages if they're empty
99 $defaults['event_full_text'] = CRM_Utils_Array::value('event_full_text', $defaults, ts('This event is currently full.'));
100
101 $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.'));
102 $defaults['template_id'] = $this->_templateId;
103
104 $defaults['event_tz'] = CRM_Utils_Array::value('event_tz', $defaults, CRM_Core_Config::singleton()->userSystem->getTimeZoneString());
105
106 // Convert start and end date defaults to event time zone.
107 if (!empty($defaults['start_date'])) {
108 $defaults['start_date'] = CRM_Utils_Date::convertTimeZone($defaults['start_date'], $defaults['event_tz']);
109 }
110 if (!empty($defaults['end_date'])) {
111 $defaults['end_date'] = CRM_Utils_Date::convertTimeZone($defaults['end_date'], $defaults['event_tz']);
112 }
113
114 return $defaults;
115 }
116
117 /**
118 * Build the form object.
119 */
120 public function buildQuickForm() {
121 //need to assign custom data type and subtype to the template
122 $this->assign('customDataType', 'Event');
123 if ($this->_eventType) {
124 $this->assign('customDataSubType', $this->_eventType);
125 }
126
127 $this->_first = TRUE;
128 $this->applyFilter('__ALL__', 'trim');
129 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
130
131 if ($this->_isTemplate) {
132 $this->add('text', 'template_title', ts('Template Title'), $attributes['template_title'], TRUE);
133 }
134
135 if ($this->_action & CRM_Core_Action::ADD) {
136 $eventTemplates = CRM_Event_PseudoConstant::eventTemplates();
137 if (CRM_Utils_System::isNull($eventTemplates) && !$this->_isTemplate) {
138 $url = CRM_Utils_System::url('civicrm/admin/eventTemplate', ['reset' => 1]);
139 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');
140 }
141 if (!CRM_Utils_System::isNull($eventTemplates)) {
142 $this->add('select', 'template_id', ts('From Template'), ['' => ts('- select -')] + $eventTemplates, FALSE, ['class' => 'crm-select2 huge']);
143 }
144 // Make sure this form redirects properly
145 $this->preventAjaxSubmit();
146 }
147
148 // add event title, make required if this is not a template
149 $this->add('text', 'title', ts('Event Title'), $attributes['event_title'], !$this->_isTemplate);
150
151 $this->addSelect('event_type_id',
152 ['onChange' => "CRM.buildCustomData( 'Event', this.value );"],
153 TRUE
154 );
155
156 //CRM-7362 --add campaigns.
157 $campaignId = NULL;
158 if ($this->_id) {
159 $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'campaign_id');
160 }
161 CRM_Campaign_BAO_Campaign::addCampaign($this, $campaignId);
162
163 $this->addSelect('default_role_id', [], TRUE);
164
165 $this->addSelect('participant_listing_id', ['placeholder' => ts('Disabled'), 'option_url' => NULL]);
166
167 $this->add('textarea', 'summary', ts('Event Summary'), $attributes['summary']);
168 $this->add('wysiwyg', 'description', ts('Complete Description'), $attributes['event_description'] + ['preset' => 'civievent']);
169 $this->addElement('checkbox', 'is_public', ts('Public Event'));
170 $this->addElement('checkbox', 'is_share', ts('Add footer region with Twitter, Facebook and LinkedIn share buttons and scripts?'));
171 $this->addElement('checkbox', 'is_map', ts('Include Map to Event Location'));
172
173 $this->addSelect('event_tz', ['placeholder' => ts('- Select time zone -')], TRUE);
174
175 $this->add('datepicker', 'start_date', ts('Start'), [], !$this->_isTemplate, ['time' => TRUE]);
176 $this->add('datepicker', 'end_date', ts('End'), [], FALSE, ['time' => TRUE]);
177
178 $this->add('number', 'max_participants', ts('Max Number of Participants'),
179 ['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;}"]
180 );
181 $this->addRule('max_participants', ts('Max participants should be a positive number'), 'positiveInteger');
182
183 $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
184 $waitlist = 0;
185 if (in_array('On waitlist', $participantStatuses) and in_array('Pending from waitlist', $participantStatuses)) {
186 $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);"]);
187 $this->add('textarea', 'waitlist_text', ts('Waitlist Message'), $attributes['waitlist_text']);
188 $waitlist = 1;
189 }
190 $this->assign('waitlist', $waitlist);
191
192 $this->add('textarea', 'event_full_text', ts('Message if Event Is Full'), $attributes['event_full_text']);
193
194 $this->addElement('checkbox', 'is_active', ts('Is this Event Active?'));
195
196 $this->addFormRule(['CRM_Event_Form_ManageEvent_EventInfo', 'formRule']);
197
198 parent::buildQuickForm();
199 }
200
201 /**
202 * Global validation rules for the form.
203 *
204 * @param array $values
205 *
206 * @return array
207 * list of errors to be posted back to the form
208 */
209 public static function formRule($values) {
210 $errors = [];
211
212 if (!empty($values['end_date']) && ($values['end_date'] < $values['start_date'])) {
213 $errors['end_date'] = ts('End date should be after Start date.');
214 }
215
216 //CRM-4286
217 if (strstr($values['title'], '/')) {
218 $errors['title'] = ts("Please do not use '/' in Event Title.");
219 }
220
221 return $errors;
222 }
223
224 /**
225 * Process the form submission.
226 */
227 public function postProcess() {
228 $params = array_merge($this->controller->exportValues($this->_name), $this->_submitValues);
229
230 //format params
231 $params['start_date'] = !empty($params['start_date']) ? CRM_Utils_Date::convertTimeZone($params['start_date'], NULL, $params['event_tz'] ?? NULL) : $params['start_date'];
232 $params['end_date'] = !empty($params['end_date']) ? CRM_Utils_Date::convertTimeZone($params['end_date'], NULL, $params['event_tz'] ?? NULL) : $params['end_date'];
233 $params['has_waitlist'] = CRM_Utils_Array::value('has_waitlist', $params, FALSE);
234 $params['is_map'] = CRM_Utils_Array::value('is_map', $params, FALSE);
235 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
236 $params['is_public'] = CRM_Utils_Array::value('is_public', $params, FALSE);
237 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
238 $params['default_role_id'] = CRM_Utils_Array::value('default_role_id', $params, FALSE);
239 $params['id'] = $this->_id;
240
241 //merge params with defaults from templates
242 if (!empty($params['template_id'])) {
243 $params = array_merge(CRM_Event_BAO_Event::getTemplateDefaultValues($params['template_id']), $params);
244 }
245
246 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
247 $this->_id,
248 'Event'
249 );
250
251 // now that we have the event’s id, do some more template-based stuff
252 if (!empty($params['template_id'])) {
253 $event = CRM_Event_BAO_Event::copy($params['template_id'], $params);
254 }
255 else {
256 $event = CRM_Event_BAO_Event::create($params);
257 }
258
259 $this->set('id', $event->id);
260
261 $this->postProcessHook();
262
263 if ($this->_action & CRM_Core_Action::ADD) {
264 $url = 'civicrm/event/manage/location';
265 $urlParams = "action=update&reset=1&id={$event->id}";
266 // special case for 'Save and Done' consistency.
267 if ($this->controller->getButtonName('submit') == '_qf_EventInfo_upload_done') {
268 $url = 'civicrm/event/manage';
269 $urlParams = 'reset=1';
270 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
271 [1 => $this->getTitle()]
272 ), ts('Saved'), 'success');
273 }
274
275 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
276 }
277
278 parent::endPostProcess();
279 }
280
281 /**
282 * Return a descriptive name for the page, used in wizard header.
283 *
284 * @return string
285 */
286 public function getTitle() {
287 return ts('Event Information and Settings');
288 }
289
290 }