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