Merge pull request #7015 from seamuslee001/CRM-17303-futher
[civicrm-core.git] / CRM / Event / Page / ManageEvent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 */
46 static $_actionLinks = NULL;
47
48 static $_links = NULL;
49
50 static $_tabLinks = NULL;
51
52 protected $_pager = NULL;
53
54 protected $_sortByCharacter;
55
56 protected $_isTemplate = FALSE;
57
58 /**
59 * Get action Links.
60 *
61 * @return array
62 * (reference) of action links
63 */
64 public 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
106 * (reference) of tab links
107 */
108 public static function &tabs($enableCart) {
109 $cacheKey = $enableCart ? 1 : 0;
110 if (!(self::$_tabLinks)) {
111 self::$_tabLinks = array();
112 }
113 if (!isset(self::$_tabLinks[$cacheKey])) {
114 self::$_tabLinks[$cacheKey]['settings']
115 = array(
116 'title' => ts('Info and Settings'),
117 'url' => 'civicrm/event/manage/settings',
118 'field' => 'id',
119 );
120 self::$_tabLinks[$cacheKey]['location']
121 = array(
122 'title' => ts('Location'),
123 'url' => 'civicrm/event/manage/location',
124 'field' => 'loc_block_id',
125 );
126
127 self::$_tabLinks[$cacheKey]['fee']
128 = array(
129 'title' => ts('Fees'),
130 'url' => 'civicrm/event/manage/fee',
131 'field' => 'is_monetary',
132 );
133 self::$_tabLinks[$cacheKey]['registration']
134 = array(
135 'title' => ts('Online Registration'),
136 'url' => 'civicrm/event/manage/registration',
137 'field' => 'is_online_registration',
138 );
139
140 if (CRM_Core_Permission::check('administer CiviCRM') || CRM_Event_BAO_Event::checkPermission(NULL, CRM_Core_Permission::EDIT)) {
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 if (!$enableCart) {
175 unset(self::$_tabLinks[$cacheKey]['conference']);
176 }
177
178 CRM_Utils_Hook::tabset('civicrm/event/manage', self::$_tabLinks[$cacheKey], array());
179 return self::$_tabLinks[$cacheKey];
180 }
181
182 /**
183 * Run the page.
184 *
185 * This method is called after the page is created. It checks for the
186 * type of action and executes that action.
187 * Finally it calls the parent's run method.
188 *
189 * @return void
190 */
191 public function run() {
192 // get the requested action
193 $action = CRM_Utils_Request::retrieve('action', 'String',
194 // default to 'browse'
195 $this, FALSE, 'browse'
196 );
197
198 // assign vars to templates
199 $this->assign('action', $action);
200 $id = CRM_Utils_Request::retrieve('id', 'Positive',
201 $this, FALSE, 0, 'REQUEST'
202 );
203
204 // figure out whether we’re handling an event or an event template
205 if ($id) {
206 $this->_isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $id, 'is_template');
207 }
208 elseif ($action & CRM_Core_Action::ADD) {
209 $this->_isTemplate = CRM_Utils_Request::retrieve('is_template', 'Boolean', $this);
210 }
211
212 if (!$this->_isTemplate && $id) {
213 $breadCrumb = array(
214 array(
215 'title' => ts('Manage Events'),
216 'url' => CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'),
217 ),
218 );
219 CRM_Utils_System::appendBreadCrumb($breadCrumb);
220 }
221
222 // what action to take ?
223 if ($action & CRM_Core_Action::DELETE) {
224 $session = CRM_Core_Session::singleton();
225 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1&action=browse'));
226 $controller = new CRM_Core_Controller_Simple('CRM_Event_Form_ManageEvent_Delete',
227 'Delete Event',
228 $action
229 );
230 $controller->set('id', $id);
231 $controller->process();
232 return $controller->run();
233 }
234 elseif ($action & CRM_Core_Action::COPY) {
235 $this->copy();
236 }
237
238 // finally browse the custom groups
239 $this->browse();
240
241 // parent run
242 return parent::run();
243 }
244
245 /**
246 * Browse all events.
247 *
248 * @return void
249 */
250 public function browse() {
251 Civi::resources()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
252
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 $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array(
319 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID,
320 )));
321 $eventType = CRM_Core_OptionGroup::values('event_type');
322 while ($dao->fetch()) {
323 if (in_array($dao->id, $permissions[CRM_Core_Permission::VIEW])) {
324 $manageEvent[$dao->id] = array();
325 $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($dao->id, 'civicrm_event');
326 $manageEvent[$dao->id]['repeat'] = '';
327 if ($repeat) {
328 $manageEvent[$dao->id]['repeat'] = ts('Repeating (%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1]));
329 }
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'),
353 TRUE,
354 'event.manage.list',
355 'Event',
356 $dao->id
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);
378 $manageEvent[$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, $mapping->getId());
379 $manageEvent[$dao->id]['is_pcp_enabled'] = CRM_Utils_Array::value($dao->id, $eventPCPS);
380 $manageEvent[$dao->id]['event_type'] = CRM_Utils_Array::value($manageEvent[$dao->id]['event_type_id'], $eventType);
381 $manageEvent[$dao->id]['is_repeating_event'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_RecurringEntity', $dao->id, 'parent_id', 'entity_id');
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));
384 }
385 }
386
387 $manageEvent['tab'] = self::tabs($enableCart);
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);
395 }
396
397 /**
398 * make a copy of a Event, including
399 * all the fields in the event wizard
400 *
401 * @return void
402 */
403 public function copy() {
404 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE, 0, 'GET');
405
406 $urlString = 'civicrm/event/manage';
407 $copyEvent = CRM_Event_BAO_Event::copy($id);
408 $urlParams = 'reset=1';
409 // Redirect to Copied Event Configuration
410 if ($copyEvent->id) {
411 $urlString = 'civicrm/event/manage/settings';
412 $urlParams .= '&action=update&id=' . $copyEvent->id;
413 }
414
415 return CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams));
416 }
417
418 public function search() {
419 if (isset($this->_action) & (CRM_Core_Action::ADD |
420 CRM_Core_Action::UPDATE |
421 CRM_Core_Action::DELETE
422 )
423 ) {
424 return;
425 }
426
427 $form = new CRM_Core_Controller_Simple('CRM_Event_Form_SearchEvent', ts('Search Events'), CRM_Core_Action::ADD);
428 $form->setEmbedded(TRUE);
429 $form->setParent($this);
430 $form->process();
431 $form->run();
432 }
433
434 /**
435 * @param array $params
436 * @param bool $sortBy
437 * @param $force
438 *
439 * @return string
440 */
441 public function whereClause(&$params, $sortBy = TRUE, $force) {
442 $values = array();
443 $clauses = array();
444 $title = $this->get('title');
445 $createdId = $this->get('cid');
446
447 if ($createdId) {
448 $clauses[] = "(created_id = {$createdId})";
449 }
450
451 if ($title) {
452 $clauses[] = "title LIKE %1";
453 if (strpos($title, '%') !== FALSE) {
454 $params[1] = array(trim($title), 'String', FALSE);
455 }
456 else {
457 $params[1] = array(trim($title), 'String', TRUE);
458 }
459 }
460
461 $value = $this->get('event_type_id');
462 if ($value) {
463 if (is_array($value)) {
464 $type = implode(',', $value);
465 }
466 $clauses[] = "event_type_id IN ({$type})";
467 }
468
469 $eventsByDates = $this->get('eventsByDates');
470 if ($this->_searchResult) {
471 if ($eventsByDates) {
472
473 $from = $this->get('start_date');
474 if (!CRM_Utils_System::isNull($from)) {
475 $clauses[] = '( start_date >= %3 OR start_date IS NULL )';
476 $params[3] = array($from, 'String');
477 }
478
479 $to = $this->get('end_date');
480 if (!CRM_Utils_System::isNull($to)) {
481 $clauses[] = '( end_date <= %4 OR end_date IS NULL )';
482 $params[4] = array($to, 'String');
483 }
484 }
485 else {
486 $curDate = date('YmdHis');
487 $clauses[5] = "(end_date >= {$curDate} OR end_date IS NULL)";
488 }
489 }
490 else {
491 $curDate = date('YmdHis');
492 $clauses[] = "(end_date >= {$curDate} OR end_date IS NULL)";
493 }
494
495 if ($sortBy &&
496 $this->_sortByCharacter !== NULL
497 ) {
498 $clauses[] = "title LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
499 }
500
501 $campaignIds = $this->get('campaign_id');
502 if (!CRM_Utils_System::isNull($campaignIds)) {
503 if (!is_array($campaignIds)) {
504 $campaignIds = array($campaignIds);
505 }
506 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campaignIds)) . ' ) )';
507 }
508
509 // don't do a the below assignment when doing a
510 // AtoZ pager clause
511 if ($sortBy) {
512 if (count($clauses) > 1 || $eventsByDates) {
513 $this->assign('isSearch', 1);
514 }
515 else {
516 $this->assign('isSearch', 0);
517 }
518 }
519
520 return !empty($clauses) ? implode(' AND ', $clauses) : '(1)';
521 }
522
523 /**
524 * @param $whereClause
525 * @param array $whereParams
526 */
527 public function pager($whereClause, $whereParams) {
528
529 $params['status'] = ts('Event %%StatusMessage%%');
530 $params['csvString'] = NULL;
531 $params['buttonTop'] = 'PagerTopButton';
532 $params['buttonBottom'] = 'PagerBottomButton';
533 $params['rowCount'] = $this->get(CRM_Utils_Pager::PAGE_ROWCOUNT);
534 if (!$params['rowCount']) {
535 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
536 }
537
538 $query = "
539 SELECT count(id)
540 FROM civicrm_event
541 WHERE $whereClause";
542
543 $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams);
544
545 $this->_pager = new CRM_Utils_Pager($params);
546 $this->assign_by_ref('pager', $this->_pager);
547 }
548
549 /**
550 * @param $whereClause
551 * @param array $whereParams
552 */
553 public function pagerAtoZ($whereClause, $whereParams) {
554
555 $query = "
556 SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
557 FROM civicrm_event
558 WHERE $whereClause
559 ORDER BY LEFT(title, 1)
560 ";
561 $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
562
563 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
564 $this->assign('aToZ', $aToZBar);
565 }
566
567 }