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