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