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