remove crm_util_api class v1
[civicrm-core.git] / CRM / Event / Form / ManageEvent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for processing Event
38 *
39 */
40 class CRM_Event_Form_ManageEvent extends CRM_Core_Form {
41
42 /**
43 * The id of the event we are proceessing.
44 *
45 * @var int
46 */
47 public $_id;
48
49 /**
50 * Is this the first page?
51 *
52 * @var boolean
53 */
54 protected $_first = FALSE;
55
56 /**
57 * Are we in single form mode or wizard mode?
58 *
59 * @var boolean
60 */
61 protected $_single;
62
63 protected $_action;
64
65 /**
66 * Are we actually managing an event template?
67 * @var boolean
68 */
69 protected $_isTemplate = FALSE;
70
71 /**
72 * Pre-populate fields based on this template event_id
73 * @var integer
74 */
75 protected $_templateId;
76
77 protected $_cancelURL = NULL;
78
79 /**
80 * The campaign id of the existing event, we use this to know if we need to update
81 * the participant records
82 */
83 protected $_campaignID = NULL;
84
85 /**
86 * Check if repeating event.
87 */
88 public $_isRepeatingEvent;
89
90 /**
91 * Explicitly declare the entity api name.
92 */
93 public function getDefaultEntity() {
94 return 'Event';
95 }
96
97 /**
98 * Set variables up before form is built.
99 *
100 * @return void
101 */
102 public function preProcess() {
103 $config = CRM_Core_Config::singleton();
104 if (in_array('CiviEvent', $config->enableComponents)) {
105 $this->assign('CiviEvent', TRUE);
106 }
107 CRM_Core_Form_RecurringEntity::preProcess('civicrm_event');
108
109 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add', 'REQUEST');
110
111 $this->assign('action', $this->_action);
112
113 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, NULL, 'GET');
114 if ($this->_id) {
115 $this->_isRepeatingEvent = CRM_Core_BAO_RecurringEntity::getParentFor($this->_id, 'civicrm_event');
116 $this->assign('eventId', $this->_id);
117 if (!empty($this->_addBlockName) && empty($this->_addProfileBottom) && empty($this->_addProfileBottomAdd)) {
118 $this->add('hidden', 'id', $this->_id);
119 }
120 $this->_single = TRUE;
121
122 $params = array('id' => $this->_id);
123 CRM_Event_BAO_Event::retrieve($params, $eventInfo);
124
125 // its an update mode, do a permission check
126 if (!CRM_Event_BAO_Event::checkPermission($this->_id, CRM_Core_Permission::EDIT)) {
127 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
128 }
129
130 $participantListingID = CRM_Utils_Array::value('participant_listing_id', $eventInfo);
131 //CRM_Core_DAO::getFieldValue( 'CRM_Event_DAO_Event', $this->_id, 'participant_listing_id' );
132 if ($participantListingID) {
133 $participantListingURL = CRM_Utils_System::url('civicrm/event/participant',
134 "reset=1&id={$this->_id}",
135 TRUE, NULL, TRUE, TRUE
136 );
137 $this->assign('participantListingURL', $participantListingURL);
138 }
139
140 $this->assign('isOnlineRegistration', CRM_Utils_Array::value('is_online_registration', $eventInfo));
141
142 $this->assign('id', $this->_id);
143 }
144
145 // figure out whether we’re handling an event or an event template
146 if ($this->_id) {
147 $this->_isTemplate = CRM_Utils_Array::value('is_template', $eventInfo);
148 }
149 elseif ($this->_action & CRM_Core_Action::ADD) {
150 $this->_isTemplate = CRM_Utils_Request::retrieve('is_template', 'Boolean', $this);
151 }
152
153 $this->assign('isTemplate', $this->_isTemplate);
154
155 if ($this->_id) {
156 if ($this->_isTemplate) {
157 $title = CRM_Utils_Array::value('template_title', $eventInfo);
158 CRM_Utils_System::setTitle(ts('Edit Event Template') . " - $title");
159 }
160 else {
161 $configureText = ts('Configure Event');
162 $title = CRM_Utils_Array::value('title', $eventInfo);
163 //If it is a repeating event change title
164 if ($this->_isRepeatingEvent) {
165 $configureText = 'Configure Repeating Event';
166 }
167 CRM_Utils_System::setTitle($configureText . " - $title");
168 }
169 $this->assign('title', $title);
170 }
171 elseif ($this->_action & CRM_Core_Action::ADD) {
172 if ($this->_isTemplate) {
173 $title = ts('New Event Template');
174 CRM_Utils_System::setTitle($title);
175 }
176 else {
177 $title = ts('New Event');
178 CRM_Utils_System::setTitle($title);
179 }
180 $this->assign('title', $title);
181 }
182
183 if (CRM_Core_Permission::check('view event participants') &&
184 CRM_Core_Permission::check('view all contacts')
185 ) {
186 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1', 'label');
187 $statusTypesPending = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0', 'label');
188 $findParticipants['statusCounted'] = implode(', ', array_values($statusTypes));
189 $findParticipants['statusNotCounted'] = implode(', ', array_values($statusTypesPending));
190 $this->assign('findParticipants', $findParticipants);
191 }
192
193 $this->_templateId = (int) CRM_Utils_Request::retrieve('template_id', 'Integer', $this);
194
195 //Is a repeating event
196 if ($this->_isRepeatingEvent) {
197 $isRepeatingEntity = TRUE;
198 $this->assign('isRepeatingEntity', $isRepeatingEntity);
199 }
200
201 // also set up tabs
202 CRM_Event_Form_ManageEvent_TabHeader::build($this);
203
204 // Set Done button URL and breadcrumb. Templates go back to Manage Templates,
205 // otherwise go to Manage Event for new event or ManageEventEdit if event if exists.
206 $breadCrumb = array();
207 if (!$this->_isTemplate) {
208 if ($this->_id) {
209 $this->_doneUrl = CRM_Utils_System::url(CRM_Utils_System::currentPath(),
210 "action=update&reset=1&id={$this->_id}"
211 );
212 }
213 else {
214 $this->_doneUrl = CRM_Utils_System::url('civicrm/event/manage',
215 'reset=1'
216 );
217 $breadCrumb = array(
218 array(
219 'title' => ts('Manage Events'),
220 'url' => $this->_doneUrl,
221 ),
222 );
223 }
224 }
225 else {
226 $this->_doneUrl = CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1');
227 $breadCrumb = array(
228 array(
229 'title' => ts('Manage Event Templates'),
230 'url' => $this->_doneUrl,
231 ),
232 );
233 }
234 CRM_Utils_System::appendBreadCrumb($breadCrumb);
235 }
236
237 /**
238 * Set default values for the form. For edit/view mode
239 * the default values are retrieved from the database
240 *
241 *
242 * @return void
243 */
244 public function setDefaultValues() {
245 $defaults = array();
246 if (isset($this->_id)) {
247 $params = array('id' => $this->_id);
248 CRM_Event_BAO_Event::retrieve($params, $defaults);
249
250 $this->_campaignID = CRM_Utils_Array::value('campaign_id', $defaults);
251 }
252 elseif ($this->_templateId) {
253 $params = array('id' => $this->_templateId);
254 CRM_Event_BAO_Event::retrieve($params, $defaults);
255 $defaults['is_template'] = $this->_isTemplate;
256 $defaults['template_id'] = $defaults['id'];
257 unset($defaults['id']);
258 }
259 else {
260 $defaults['is_active'] = 1;
261 $defaults['style'] = 'Inline';
262 }
263
264 return $defaults;
265 }
266
267 /**
268 * Build the form object.
269 *
270 * @return void
271 */
272 public function buildQuickForm() {
273 $session = CRM_Core_Session::singleton();
274
275 $this->_cancelURL = CRM_Utils_Array::value('cancelURL', $_POST);
276
277 if (!$this->_cancelURL) {
278 if ($this->_isTemplate) {
279 $this->_cancelURL = CRM_Utils_System::url('civicrm/admin/eventTemplate',
280 'reset=1'
281 );
282 }
283 else {
284 $this->_cancelURL = CRM_Utils_System::url('civicrm/event/manage',
285 'reset=1'
286 );
287 }
288 }
289
290 if ($this->_cancelURL) {
291 $this->addElement('hidden', 'cancelURL', $this->_cancelURL);
292 }
293
294 if ($this->_single) {
295 $buttons = array(
296 array(
297 'type' => 'upload',
298 'name' => ts('Save'),
299 'isDefault' => TRUE,
300 ),
301 array(
302 'type' => 'upload',
303 'name' => ts('Save and Done'),
304 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
305 'subName' => 'done',
306 ),
307 array(
308 'type' => 'cancel',
309 'name' => ts('Cancel'),
310 ),
311 );
312 $this->addButtons($buttons);
313 }
314 else {
315 $buttons = array();
316 if (!$this->_first) {
317 $buttons[] = array(
318 'type' => 'back',
319 'name' => ts('Previous'),
320 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
321 );
322 }
323 $buttons[] = array(
324 'type' => 'upload',
325 'name' => ts('Continue'),
326 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
327 'isDefault' => TRUE,
328 );
329 $buttons[] = array(
330 'type' => 'cancel',
331 'name' => ts('Cancel'),
332 );
333
334 $this->addButtons($buttons);
335 }
336 $session->replaceUserContext($this->_cancelURL);
337 $this->add('hidden', 'is_template', $this->_isTemplate);
338 }
339
340 public function endPostProcess() {
341 // make submit buttons keep the current working tab opened.
342 if ($this->_action & CRM_Core_Action::UPDATE) {
343 $className = CRM_Utils_String::getClassName($this->_name);
344
345 // hack for special cases.
346 switch ($className) {
347 case 'Event':
348 $attributes = $this->getVar('_attributes');
349 $subPage = strtolower(basename(CRM_Utils_Array::value('action', $attributes)));
350 break;
351
352 case 'EventInfo':
353 $subPage = 'settings';
354 break;
355
356 case 'ScheduleReminders':
357 $subPage = 'reminder';
358 break;
359
360 default:
361 $subPage = strtolower($className);
362 break;
363 }
364
365 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
366 array(1 => CRM_Utils_Array::value('title', CRM_Utils_Array::value($subPage, $this->get('tabHeader')), $className))
367 ), ts('Saved'), 'success');
368
369 $config = CRM_Core_Config::singleton();
370 if (in_array('CiviCampaign', $config->enableComponents)) {
371 $values = $this->controller->exportValues($this->_name);
372 $newCampaignID = CRM_Utils_Array::value('campaign_id', $values);
373 $eventID = CRM_Utils_Array::value('id', $values);
374 if ($eventID && $this->_campaignID != $newCampaignID) {
375 CRM_Event_BAO_Event::updateParticipantCampaignID($eventID, $newCampaignID);
376 }
377 }
378 $this->postProcessHook();
379 if ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_done") {
380 if ($this->_isTemplate) {
381 CRM_Core_Session::singleton()
382 ->pushUserContext(CRM_Utils_System::url('civicrm/admin/eventTemplate', 'reset=1'));
383 }
384 else {
385 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/event/manage', 'reset=1'));
386 }
387 }
388 else {
389 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url("civicrm/event/manage/{$subPage}",
390 "action=update&reset=1&id={$this->_id}"
391 ));
392 }
393 }
394 }
395
396 /**
397 * @return string
398 */
399 public function getTemplateFileName() {
400 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 || $this->_action & CRM_Core_Action::DELETE) {
401 return parent::getTemplateFileName();
402 }
403 else {
404 // hack lets suppress the form rendering for now
405 self::$_template->assign('isForm', FALSE);
406 return 'CRM/Event/Form/ManageEvent/Tab.tpl';
407 }
408 }
409
410 /**
411 * Pre-load libraries required by Online Registration Profile fields
412 */
413 public static function addProfileEditScripts() {
414 CRM_UF_Page_ProfileEditor::registerProfileScripts();
415 CRM_UF_Page_ProfileEditor::registerSchemas(array('IndividualModel', 'ParticipantModel'));
416 }
417
418 }