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