Merge pull request #7022 from davecivicrm/CRM-17428
[civicrm-core.git] / CRM / Event / Form / ManageEvent / EventInfo.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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/**
29 *
30 *
31 * @package CRM
e7112fa7 32 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
33 * $Id$
34 *
35 */
36
37/**
38 * This class generates form components for processing Event
39 *
40 */
41class CRM_Event_Form_ManageEvent_EventInfo extends CRM_Event_Form_ManageEvent {
42
43 /**
66f9e52b 44 * Event type.
6a488035
TO
45 */
46 protected $_eventType = NULL;
47
48 /**
66f9e52b 49 * Set variables up before form is built.
6a488035
TO
50 *
51 * @return void
6a488035 52 */
00be9182 53 public function preProcess() {
2a4e505f 54 parent::preProcess();
6a488035
TO
55
56 if ($this->_id) {
57 $this->assign('entityID', $this->_id);
58 $eventType = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
59 $this->_id,
60 'event_type_id'
61 );
62 }
63 else {
64 $eventType = 'null';
65 }
66
67 $showLocation = FALSE;
68 // when custom data is included in this page
a7488080 69 if (!empty($_POST['hidden_custom'])) {
6a488035
TO
70 $this->set('type', 'Event');
71 $this->set('subType', CRM_Utils_Array::value('event_type_id', $_POST));
72 $this->set('entityId', $this->_id);
73
f19dc468 74 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_eventType, 1, 'Event', $this->_id);
6a488035
TO
75 CRM_Custom_Form_CustomData::buildQuickForm($this);
76 CRM_Custom_Form_CustomData::setDefaultValues($this);
77 }
78 }
79
80 /**
c490a46a 81 * Set default values for the form. For edit/view mode
6a488035
TO
82 * the default values are retrieved from the database
83 *
6a488035 84 *
355ba699 85 * @return void
6a488035 86 */
00be9182 87 public function setDefaultValues() {
6a488035
TO
88 $defaults = parent::setDefaultValues();
89
90 // in update mode, we need to set custom data subtype to tpl
a7488080 91 if (!empty($defaults['event_type_id'])) {
6a488035
TO
92 $this->assign('customDataSubType', $defaults['event_type_id']);
93 }
94
95 $this->_showHide = new CRM_Core_ShowHideBlocks();
96 // Show waitlist features or event_full_text if max participants set
a7488080 97 if (!empty($defaults['max_participants'])) {
6a488035 98 $this->_showHide->addShow('id-waitlist');
a7488080 99 if (!empty($defaults['has_waitlist'])) {
6a488035
TO
100 $this->_showHide->addShow('id-waitlist-text');
101 $this->_showHide->addHide('id-event_full');
102 }
103 else {
104 $this->_showHide->addHide('id-waitlist-text');
105 $this->_showHide->addShow('id-event_full');
106 }
107 }
108 else {
109 $this->_showHide->addHide('id-event_full');
110 $this->_showHide->addHide('id-waitlist');
111 $this->_showHide->addHide('id-waitlist-text');
112 }
113
114 $this->_showHide->addToTemplate();
115 $this->assign('elemType', 'table-row');
116
117 $this->assign('description', CRM_Utils_Array::value('description', $defaults));
118
119 // Provide suggested text for event full and waitlist messages if they're empty
120 $defaults['event_full_text'] = CRM_Utils_Array::value('event_full_text', $defaults, ts('This event is currently full.'));
121
122 $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.'));
123 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults(CRM_Utils_Array::value('start_date', $defaults), 'activityDateTime');
124
a7488080 125 if (!empty($defaults['end_date'])) {
6a488035
TO
126 list($defaults['end_date'], $defaults['end_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['end_date'], 'activityDateTime');
127 }
128 return $defaults;
129 }
130
131 /**
66f9e52b 132 * Build the form object.
6a488035 133 *
355ba699 134 * @return void
6a488035
TO
135 */
136 public function buildQuickForm() {
6a488035
TO
137 //need to assign custom data type and subtype to the template
138 $this->assign('customDataType', 'Event');
139 if ($this->_eventType) {
140 $this->assign('customDataSubType', $this->_eventType);
141 }
142 $this->assign('entityId', $this->_id);
143
144 $this->_first = TRUE;
145 $this->applyFilter('__ALL__', 'trim');
146 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
147
148 if ($this->_isTemplate) {
149 $this->add('text', 'template_title', ts('Template Title'), $attributes['template_title'], TRUE);
150 }
151
152 if ($this->_action & CRM_Core_Action::ADD) {
153 $eventTemplates = CRM_Event_PseudoConstant::eventTemplates();
154 if (CRM_Utils_System::isNull($eventTemplates) && !$this->_isTemplate) {
155 $url = CRM_Utils_System::url('civicrm/admin/eventTemplate', array('reset' => 1));
156 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');
157 }
158 if (!CRM_Utils_System::isNull($eventTemplates)) {
a2015e41 159 $this->add('select', 'template_id', ts('From Template'), array('' => ts('- select -')) + $eventTemplates, FALSE, array('class' => 'crm-select2 huge'));
6a488035 160 }
bfd83a87
CW
161 // Make sure this form redirects properly
162 $this->preventAjaxSubmit();
6a488035
TO
163 }
164
165 // add event title, make required if this is not a template
166 $this->add('text', 'title', ts('Event Title'), $attributes['event_title'], !$this->_isTemplate);
167
e1462487
CW
168 $this->addSelect('event_type_id',
169 array('onChange' => "CRM.buildCustomData( 'Event', this.value );"),
170 TRUE
6a488035
TO
171 );
172
173 //CRM-7362 --add campaigns.
174 $campaignId = NULL;
175 if ($this->_id) {
176 $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'campaign_id');
177 }
178 CRM_Campaign_BAO_Campaign::addCampaign($this, $campaignId);
179
e1462487 180 $this->addSelect('default_role_id', array(), TRUE);
6a488035 181
3a6b5d94 182 $this->addSelect('participant_listing_id', array('placeholder' => ts('Disabled'), 'option_url' => NULL));
6a488035
TO
183
184 $this->add('textarea', 'summary', ts('Event Summary'), $attributes['summary']);
5d51a2f9 185 $this->add('wysiwyg', 'description', ts('Complete Description'), $attributes['event_description']);
0781df9b 186 $this->addElement('checkbox', 'is_public', ts('Public Event'));
6a488035 187 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
0781df9b 188 $this->addElement('checkbox', 'is_map', ts('Include Map to Event Location'));
6a488035
TO
189
190 $this->addDateTime('start_date', ts('Start Date'), FALSE, array('formatType' => 'activityDateTime'));
191 $this->addDateTime('end_date', ts('End Date / Time'), FALSE, array('formatType' => 'activityDateTime'));
192
193 $this->add('text', 'max_participants', ts('Max Number of Participants'),
194 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;}")
195 );
196 $this->addRule('max_participants', ts('Max participants should be a positive number'), 'positiveInteger');
197
198 $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
199 $waitlist = 0;
200 if (in_array('On waitlist', $participantStatuses) and in_array('Pending from waitlist', $participantStatuses)) {
201 $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);"));
202 $this->add('textarea', 'waitlist_text', ts('Waitlist Message'), $attributes['waitlist_text']);
203 $waitlist = 1;
204 }
205 $this->assign('waitlist', $waitlist);
206
207 $this->add('textarea', 'event_full_text', ts('Message if Event Is Full'), $attributes['event_full_text']);
208
209 $this->addElement('checkbox', 'is_active', ts('Is this Event Active?'));
210
211 $this->addFormRule(array('CRM_Event_Form_ManageEvent_EventInfo', 'formRule'));
212
213 parent::buildQuickForm();
214 }
215
216 /**
66f9e52b 217 * Global validation rules for the form.
6a488035 218 *
c490a46a 219 * @param array $values
6a488035 220 *
a6c01b45
CW
221 * @return array
222 * list of errors to be posted back to the form
6a488035 223 */
00be9182 224 public static function formRule($values) {
6a488035
TO
225 $errors = array();
226
227 if (!$values['is_template']) {
228 if (CRM_Utils_System::isNull($values['start_date'])) {
229 $errors['start_date'] = ts('Start Date and Time are required fields');
230 }
231 else {
232 $start = CRM_Utils_Date::processDate($values['start_date']);
233 $end = CRM_Utils_Date::processDate($values['end_date']);
234 if (($end < $start) && ($end != 0)) {
9a0187a0 235 $errors['end_date'] = ts('End date should be after Start date.');
6a488035
TO
236 }
237 }
238 }
239
240 //CRM-4286
241 if (strstr($values['title'], '/')) {
242 $errors['title'] = ts("Please do not use '/' in Event Title.");
243 }
244
245 return $errors;
246 }
247
248 /**
66f9e52b 249 * Process the form submission.
6a488035 250 *
6a488035 251 *
355ba699 252 * @return void
6a488035
TO
253 */
254 public function postProcess() {
255 $params = $this->controller->exportValues($this->_name);
256
257 //format params
258 $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time']);
259 $params['end_date'] = CRM_Utils_Date::processDate(CRM_Utils_Array::value('end_date', $params),
260 CRM_Utils_Array::value('end_date_time', $params),
261 TRUE
262 );
263 $params['has_waitlist'] = CRM_Utils_Array::value('has_waitlist', $params, FALSE);
264 $params['is_map'] = CRM_Utils_Array::value('is_map', $params, FALSE);
265 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
266 $params['is_public'] = CRM_Utils_Array::value('is_public', $params, FALSE);
267 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
268 $params['default_role_id'] = CRM_Utils_Array::value('default_role_id', $params, FALSE);
269 $params['id'] = $this->_id;
270
271 $customFields = CRM_Core_BAO_CustomField::getFields('Event', FALSE, FALSE,
272 CRM_Utils_Array::value('event_type_id', $params)
273 );
274 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
6a488035
TO
275 $this->_id,
276 'Event'
277 );
278
279 //merge params with defaults from templates
a7488080 280 if (!empty($params['template_id'])) {
6a488035
TO
281 $params = array_merge(CRM_Event_BAO_Event::getTemplateDefaultValues($params['template_id']), $params);
282 }
283
284 $event = CRM_Event_BAO_Event::create($params);
285
286 // now that we have the event’s id, do some more template-based stuff
a7488080 287 if (!empty($params['template_id'])) {
6a488035 288 CRM_Event_BAO_Event::copy($params['template_id'], $event, TRUE);
6a488035 289 }
03e04002 290
6a488035
TO
291 $this->set('id', $event->id);
292
c654c119 293 $this->postProcessHook();
294
6a488035
TO
295 if ($this->_action & CRM_Core_Action::ADD) {
296 $url = 'civicrm/event/manage/location';
297 $urlParams = "action=update&reset=1&id={$event->id}";
298 // special case for 'Save and Done' consistency.
299 if ($this->controller->getButtonName('submit') == '_qf_EventInfo_upload_done') {
300 $url = 'civicrm/event/manage';
301 $urlParams = 'reset=1';
302 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
353ffa53
TO
303 array(1 => $this->getTitle())
304 ), ts('Saved'), 'success');
6a488035
TO
305 }
306
307 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
308 }
309
310 parent::endPostProcess();
311 }
6a488035
TO
312
313 /**
314 * Return a descriptive name for the page, used in wizard header
315 *
316 * @return string
6a488035
TO
317 */
318 public function getTitle() {
319 return ts('Event Information and Settings');
320 }
321
0cf587a7 322 /**
66f9e52b 323 * Retrieve event template custom data values.
c490a46a
CW
324 * and set as default values for current new event.
325 *
d4dd1e85
TO
326 * @param int $templateId
327 * Event template id.
0cf587a7 328 *
a6c01b45 329 * @return array
16b10e64 330 * Array of custom data defaults.
0cf587a7 331 */
6a488035
TO
332 public function templateCustomDataValues($templateId) {
333 $defaults = array();
334 if (!$templateId) {
335 return $defaults;
336 }
337
338 // pull template custom data as a default for event, CRM-5596
353ffa53
TO
339 $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_type, $this, $templateId, NULL, $this->_subType);
340 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $this->_groupCount, $this);
6a488035
TO
341 $customValues = array();
342 CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $customValues);
343 foreach ($customValues as $key => $val) {
344 if ($fieldKey = CRM_Core_BAO_CustomField::getKeyID($key)) {
345 $defaults["custom_{$fieldKey}_-1"] = $val;
346 }
347 }
348
349 return $defaults;
350 }
96025800 351
6a488035 352}