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