c2b815497cee7c40422237d80db3c445c5839c1b
[civicrm-core.git] / CRM / Event / Page / ManageEvent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * Page for displaying list of events
38 */
39 class CRM_Event_Page_ManageEvent extends CRM_Core_Page {
40
41 /**
42 * The action links that we need to display for the browse screen
43 *
44 * @var array
45 * @static
46 */
47 static $_actionLinks = NULL;
48
49 static $_links = NULL;
50
51 static $_tabLinks = NULL;
52
53 protected $_pager = NULL;
54
55 protected $_sortByCharacter;
56
57 protected $_isTemplate = FALSE;
58
59 /**
60 * Get action Links
61 *
62 * @return array (reference) of action links
63 */
64 function &links() {
65 if (!(self::$_actionLinks)) {
66 // helper variable for nicer formatting
67 $copyExtra = ts('Are you sure you want to make a copy of this Event?');
68 $deleteExtra = ts('Are you sure you want to delete this Event?');
69
70 self::$_actionLinks = array(
71 CRM_Core_Action::DISABLE => array(
72 'name' => ts('Disable'),
73 'ref' => 'crm-enable-disable',
74 'title' => ts('Disable Event'),
75 ),
76 CRM_Core_Action::ENABLE => array(
77 'name' => ts('Enable'),
78 'ref' => 'crm-enable-disable',
79 'title' => ts('Enable Event'),
80 ),
81 CRM_Core_Action::DELETE => array(
82 'name' => ts('Delete'),
83 'url' => CRM_Utils_System::currentPath(),
84 'qs' => 'action=delete&id=%%id%%',
85 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
86 'title' => ts('Delete Event'),
87 ),
88 CRM_Core_Action::COPY => array(
89 'name' => ts('Copy'),
90 'url' => CRM_Utils_System::currentPath(),
91 'qs' => 'reset=1&action=copy&id=%%id%%',
92 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
93 'title' => ts('Copy Event'),
94 ),
95 );
96 }
97 return self::$_actionLinks;
98 }
99
100 /**
101 * Get tab Links for events
102 *
103 * @return array (reference) of tab links
104 */
105 static function &tabs($enableCart) {
106 $cacheKey = $enableCart ? 1 : 0;
107 if (!(self::$_tabLinks)) {
108 self::$_tabLinks = array();
109 }
110 if (!isset(self::$_tabLinks[$cacheKey])) {
111 self::$_tabLinks[$cacheKey] = array(
112 'settings' => array(
113 'title' => ts('Info and Settings'),
114 'url' => 'civicrm/event/manage/settings',
115 'field' => 'id'
116 ),
117 'location' => array(
118 'title' => ts('Location'),
119 'url' => 'civicrm/event/manage/location',
120 'field' => 'loc_block_id',
121 ),
122 'fee' => array(
123 'title' => ts('Fees'),
124 'url' => 'civicrm/event/manage/fee',
125 'field' => 'is_monetary',
126 ),
127 'registration' => array(
128 'title' => ts('Online Registration'),
129 'url' => 'civicrm/event/manage/registration',
130 'field' => 'is_online_registration',
131 ),
132 'reminder' => array(
133 'title' => ts('Schedule Reminders'),
134 'url' => 'civicrm/event/manage/reminder',
135 'field' => 'reminder',
136 ),
137 'conference' => array(
138 'title' => ts('Conference Slots'),
139 'url' => 'civicrm/event/manage/conference',
140 'field' => 'slot_label_id',
141 ),
142 'friend' => array(
143 'title' => ts('Tell a Friend'),
144 'url' => 'civicrm/event/manage/friend',
145 'field' => 'friend',
146 ),
147 'pcp' => array(
148 'title' => ts('Personal Campaign Pages'),
149 'url' => 'civicrm/event/manage/pcp',
150 'field' => 'is_pcp_enabled',
151 ),
152 );
153 }
154
155 if (!$enableCart) {
156 unset(self::$_tabLinks[$cacheKey]['conference']);
157 }
158
159 CRM_Utils_Hook::tabset('civicrm/event/manage', self::$_tabLinks[$cacheKey], array());
160 return self::$_tabLinks[$cacheKey];
161 }
162
163 /**
164 * Run the page.
165 *
166 * This method is called after the page is created. It checks for the
167 * type of action and executes that action.
168 * Finally it calls the parent's run method.
169 *
170 * @return void
171 * @access public
172 *
173 */
174 function run() {
175 // get the requested action
176 $action = CRM_Utils_Request::retrieve('action', 'String',
177 // default to 'browse'
178 $this, FALSE, 'browse'
179 );
180
181 // assign vars to templates
182 $this->assign('action', $action);
183 $id = CRM_Utils_Request::retrieve('id', 'Positive',
184 $this, FALSE, 0, 'REQUEST'
185 );
186
187 // figure out whether we’re handling an event or an event template
188 if ($id) {
189 $this->_isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $id, 'is_template');
190 }
191 elseif ($action & CRM_Core_Action::ADD) {
192 $this->_isTemplate = CRM_Utils_Request::retrieve('is_template', 'Boolean', $this);
193 }
194
195 if (!$this->_isTemplate && $id) {
196 $breadCrumb = array(array('title' => ts('Manage Events'),
197 'url' => CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'),
198 ));
199 CRM_Utils_System::appendBreadCrumb($breadCrumb);
200 }
201
202 // what action to take ?
203 if ($action & CRM_Core_Action::DELETE) {
204 $session = CRM_Core_Session::singleton();
205 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1&action=browse'));
206 $controller = new CRM_Core_Controller_Simple('CRM_Event_Form_ManageEvent_Delete',
207 'Delete Event',
208 $action
209 );
210 $controller->set('id', $id);
211 $controller->process();
212 return $controller->run();
213 }
214 elseif ($action & CRM_Core_Action::COPY) {
215 $this->copy();
216 }
217
218 // finally browse the custom groups
219 $this->browse();
220
221 // parent run
222 return parent::run();
223 }
224
225 /**
226 * browse all events
227 *
228 * @return void
229 */
230 function browse() {
231 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter',
232 'String',
233 $this
234 );
235 $createdId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
236 if (strtolower($this->_sortByCharacter) == 'all' ||
237 !empty($_POST)
238 ) {
239 $this->_sortByCharacter = '';
240 $this->set('sortByCharacter', '');
241 }
242
243 $this->_force = $this->_searchResult = NULL;
244
245 $this->search();
246
247 $params = array();
248 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean',
249 $this, FALSE
250 );
251 $this->_searchResult = CRM_Utils_Request::retrieve('searchResult', 'Boolean', $this);
252
253 $whereClause = $this->whereClause($params, FALSE, $this->_force);
254 $this->pagerAToZ($whereClause, $params);
255
256 $params = array();
257 $whereClause = $this->whereClause($params, TRUE, $this->_force);
258 // because is_template != 1 would be to simple
259 $whereClause .= ' AND (is_template = 0 OR is_template IS NULL)';
260
261 $this->pager($whereClause, $params);
262
263 list($offset, $rowCount) = $this->_pager->getOffsetAndRowCount();
264
265 // get all custom groups sorted by weight
266 $manageEvent = array();
267
268 $query = "
269 SELECT *
270 FROM civicrm_event
271 WHERE $whereClause
272 ORDER BY start_date desc
273 LIMIT $offset, $rowCount";
274
275 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Event_DAO_Event');
276 $permissions = CRM_Event_BAO_Event::checkPermission();
277
278 //get all campaigns.
279 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
280
281 // get the list of active event pcps
282 $eventPCPS = array();
283
284 $pcpDao = new CRM_PCP_DAO_PCPBlock;
285 $pcpDao->entity_table = 'civicrm_event';
286 $pcpDao->find();
287
288 while ($pcpDao->fetch()) {
289 $eventPCPS[$pcpDao->entity_id] = $pcpDao->entity_id;
290 }
291 // check if we're in shopping cart mode for events
292 $enableCart = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME,
293 'enable_cart'
294 );
295 $this->assign('eventCartEnabled', $enableCart);
296 $mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'civicrm_event', 'id', 'entity_value');
297 $eventType = CRM_Core_OptionGroup::values('event_type');
298 while ($dao->fetch()) {
299 if (in_array($dao->id, $permissions[CRM_Core_Permission::VIEW])) {
300 $manageEvent[$dao->id] = array();
301 CRM_Core_DAO::storeValues($dao, $manageEvent[$dao->id]);
302
303 // form all action links
304 $action = array_sum(array_keys($this->links()));
305
306 if ($dao->is_active) {
307 $action -= CRM_Core_Action::ENABLE;
308 }
309 else {
310 $action -= CRM_Core_Action::DISABLE;
311 }
312
313 if (!in_array($dao->id, $permissions[CRM_Core_Permission::DELETE])) {
314 $action -= CRM_Core_Action::DELETE;
315 }
316 if (!in_array($dao->id, $permissions[CRM_Core_Permission::EDIT])) {
317 $action -= CRM_Core_Action::UPDATE;
318 }
319
320 $manageEvent[$dao->id]['action'] = CRM_Core_Action::formLink(self::links(),
321 $action,
322 array('id' => $dao->id),
323 ts('more'),
324 TRUE,
325 'event.manage.list',
326 'Event',
327 $dao->id
328 );
329
330 $params = array(
331 'entity_id' => $dao->id,
332 'entity_table' => 'civicrm_event',
333 'is_active' => 1,
334 );
335
336 $defaults['location'] = CRM_Core_BAO_Location::getValues($params, TRUE);
337
338 $manageEvent[$dao->id]['friend'] = CRM_Friend_BAO_Friend::getValues($params);
339
340 if (isset($defaults['location']['address'][1]['city'])) {
341 $manageEvent[$dao->id]['city'] = $defaults['location']['address'][1]['city'];
342 }
343 if (isset($defaults['location']['address'][1]['state_province_id'])) {
344 $manageEvent[$dao->id]['state_province'] = CRM_Core_PseudoConstant::stateProvince($defaults['location']['address'][1]['state_province_id']);
345 }
346
347 //show campaigns on selector.
348 $manageEvent[$dao->id]['campaign'] = CRM_Utils_Array::value($dao->campaign_id, $allCampaigns);
349 $manageEvent[$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, $mappingID);
350 $manageEvent[$dao->id]['is_pcp_enabled'] = CRM_Utils_Array::value($dao->id, $eventPCPS);
351 $manageEvent[$dao->id]['event_type'] = CRM_Utils_Array::value($manageEvent[$dao->id]['event_type_id'], $eventType);
352
353 // allow hooks to set 'field' value which allows configuration pop-up to show a tab as enabled/disabled
354 CRM_Utils_Hook::tabset('civicrm/event/manage/rows', $manageEvent, array('event_id' => $dao->id));
355 }
356 }
357
358 $manageEvent['tab'] = self::tabs($enableCart);
359 $this->assign('rows', $manageEvent);
360
361 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
362 $statusTypesPending = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
363 $findParticipants['statusCounted'] = implode(', ', array_values($statusTypes));
364 $findParticipants['statusNotCounted'] = implode(', ', array_values($statusTypesPending));
365 $this->assign('findParticipants', $findParticipants);
366 }
367
368 /**
369 * This function is to make a copy of a Event, including
370 * all the fields in the event wizard
371 *
372 * @return void
373 * @access public
374 */
375 function copy() {
376 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE, 0, 'GET');
377
378 $urlString = 'civicrm/event/manage';
379 $copyEvent = CRM_Event_BAO_Event::copy($id);
380 $urlParams = 'reset=1';
381 // Redirect to Copied Event Configuration
382 if ($copyEvent->id) {
383 $urlString = 'civicrm/event/manage/settings';
384 $urlParams .= '&action=update&id=' . $copyEvent->id;
385 }
386
387 return CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams));
388 }
389
390 function search() {
391 if (isset($this->_action) &
392 (CRM_Core_Action::ADD |
393 CRM_Core_Action::UPDATE |
394 CRM_Core_Action::DELETE
395 )
396 ) {
397 return;
398 }
399
400 $form = new CRM_Core_Controller_Simple('CRM_Event_Form_SearchEvent', ts('Search Events'), CRM_Core_Action::ADD);
401 $form->setEmbedded(TRUE);
402 $form->setParent($this);
403 $form->process();
404 $form->run();
405 }
406
407 function whereClause(&$params, $sortBy = TRUE, $force) {
408 $values = array();
409 $clauses = array();
410 $title = $this->get('title');
411 $createdId = $this->get('cid');
412
413 if ($createdId) {
414 $clauses[] = "(created_id = {$createdId})";
415 }
416
417 if ($title) {
418 $clauses[] = "title LIKE %1";
419 if (strpos($title, '%') !== FALSE) {
420 $params[1] = array(trim($title), 'String', FALSE);
421 }
422 else {
423 $params[1] = array(trim($title), 'String', TRUE);
424 }
425 }
426
427 $value = $this->get('event_type_id');
428 $val = array();
429 if ($value) {
430 if (is_array($value)) {
431 foreach ($value as $k => $v) {
432 if ($v) {
433 $val[$k] = $k;
434 }
435 }
436 $type = implode(',', $val);
437 }
438 $clauses[] = "event_type_id IN ({$type})";
439 }
440
441 $eventsByDates = $this->get('eventsByDates');
442 if ($this->_searchResult) {
443 if ($eventsByDates) {
444
445 $from = $this->get('start_date');
446 if (!CRM_Utils_System::isNull($from)) {
447 $clauses[] = '( start_date >= %3 OR start_date IS NULL )';
448 $params[3] = array($from, 'String');
449 }
450
451 $to = $this->get('end_date');
452 if (!CRM_Utils_System::isNull($to)) {
453 $clauses[] = '( end_date <= %4 OR end_date IS NULL )';
454 $params[4] = array($to, 'String');
455 }
456 }
457 else {
458 $curDate = date('YmdHis');
459 $clauses[5] = "(end_date >= {$curDate} OR end_date IS NULL)";
460 }
461 }
462 else {
463 $curDate = date('YmdHis');
464 $clauses[] = "(end_date >= {$curDate} OR end_date IS NULL)";
465 }
466
467 if ($sortBy &&
468 $this->_sortByCharacter !== NULL
469 ) {
470 $clauses[] = "title LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
471 }
472
473 $campaignIds = $this->get('campaign_id');
474 if (!CRM_Utils_System::isNull($campaignIds)) {
475 if (!is_array($campaignIds)) {
476 $campaignIds = array($campaignIds);
477 }
478 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campaignIds)) . ' ) )';
479 }
480
481 // don't do a the below assignment when doing a
482 // AtoZ pager clause
483 if ($sortBy) {
484 if (count($clauses) > 1 || $eventsByDates) {
485 $this->assign('isSearch', 1);
486 }
487 else {
488 $this->assign('isSearch', 0);
489 }
490 }
491
492 return !empty($clauses) ? implode(' AND ', $clauses) : '(1)';
493 }
494
495 function pager($whereClause, $whereParams) {
496
497 $params['status'] = ts('Event %%StatusMessage%%');
498 $params['csvString'] = NULL;
499 $params['buttonTop'] = 'PagerTopButton';
500 $params['buttonBottom'] = 'PagerBottomButton';
501 $params['rowCount'] = $this->get(CRM_Utils_Pager::PAGE_ROWCOUNT);
502 if (!$params['rowCount']) {
503 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
504 }
505
506 $query = "
507 SELECT count(id)
508 FROM civicrm_event
509 WHERE $whereClause";
510
511 $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams);
512
513 $this->_pager = new CRM_Utils_Pager($params);
514 $this->assign_by_ref('pager', $this->_pager);
515 }
516
517 function pagerAtoZ($whereClause, $whereParams) {
518
519 $query = "
520 SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
521 FROM civicrm_event
522 WHERE $whereClause
523 ORDER BY LEFT(title, 1)
524 ";
525 $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
526
527 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
528 $this->assign('aToZ', $aToZBar);
529 }
530 }
531