-
Notifications
You must be signed in to change notification settings - Fork 7
/
em-template-tags.php
514 lines (487 loc) · 17.2 KB
/
em-template-tags.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/*
* Template Tags
* If you know what you're doing, you're probably better off using the EM Objects directly.
*/
/*
* ---------------------------------------------------------------------
* Displaying Functions - Displays Lists, Page Links/URLs, etc.
* ---------------------------------------------------------------------
*/
/**
* Returns a html list of events filtered by the array or query-string of arguments supplied.
* @param array|string $args
* @return string
*/
function em_get_events( $args = array() ){
if ( is_string($args) && strpos ( $args, "=" )) {
// allows the use of arguments without breaking the legacy code
$defaults = EM_Events::get_default_search();
$args = wp_parse_args ( $args, $defaults );
}
return EM_Events::output( $args );
}
/**
* Prints out a list of events, takes same arguments as em_get_events.
* @param array|string $args
* @uses em_get_events()
*/
function em_events( $args = array() ){ echo em_get_events($args); }
/**
* Returns a html list of locations filtered by the array or query-string of arguments supplied.
* @param array|string $args
* @return string
*/
function em_get_locations( $args = array() ){
if (strpos ( $args, "=" )) {
// allows the use of arguments without breaking the legacy code
$defaults = EM_Locations::get_default_search();
$args = wp_parse_args ( $args, $defaults );
}
return EM_Locations::output( $args );
}
/**
* Prints out a list of locations, takes same arguments as em_get_locations.
* @param array|string $args
* @uses em_get_locations()
*/
function em_locations( $args = array() ){ echo em_get_locations($args); }
/**
* Returns an html calendar of events filtered by the array or query-string of arguments supplied.
* @param array|string $args
* @return string
*/
function em_get_calendar( $args = array() ){
if ( !is_array($args) && strpos ( $args, "=" )) {
// allows the use of arguments without breaking the legacy code
$defaults = EM_Calendar::get_default_search();
$args = wp_parse_args ( $args, $defaults );
}
return EM_Calendar::output($args);
}
/**
* Prints out an html calendar, takes same arguments as em_get_calendar.
* @param array|string $args
* @uses em_get_calendar()
*/
function em_calendar( $args = array() ){ echo em_get_calendar($args); }
/**
* Generate a grouped list of events by year, month, week or day.
* @since 4.213
* @param array $args
* @param string $format
* @return string
*/
function em_get_events_list_grouped($args, $format=''){
//Reset some args to include pagination for if pagination is requested.
$args['limit'] = (!empty($args['limit']) && is_numeric($args['limit']) )? $args['limit'] : false;
$args['page'] = (!empty($args['page']) && is_numeric($args['page']) )? $args['page'] : 1;
$args['page'] = (!empty($_REQUEST['page']) && is_numeric($_REQUEST['page']) )? $_REQUEST['page'] : $args['page'];
$args['offset'] = ($args['page']-1) * $args['limit'];
$args['orderby'] = 'event_start_date,event_start_time,event_name'; // must override this to display events in right cronology.
if( !empty($format) ){ $args['format'] = html_entity_decode($format); } //accept formats
//Reset some vars for counting events and displaying set arrays of events
$atts = (array) $args;
$atts['pagination'] = false;
$atts['limit'] = false;
$atts['page'] = false;
$atts['offset'] = false;
//decide what form of dates to show
$EM_Events = EM_Events::get($args);
$events_count = EM_Events::count($atts);
ob_start();
switch ( $args['mode'] ){
case 'yearly':
//go through the events and put them into a monthly array
$format = (!empty($args['date_format'])) ? $args['date_format']:'Y';
$events_dates = array();
foreach($EM_Events as $EM_Event){
$events_dates[date_i18n($format,$EM_Event->start)][] = $EM_Event;
}
foreach ($events_dates as $year => $events){
echo '<h2>'.$year.'</h2>';
echo EM_Events::output($events, $atts);
}
break;
case 'monthly':
//go through the events and put them into a monthly array
$format = (!empty($args['date_format'])) ? $args['date_format']:'M Y';
$events_dates = array();
foreach($EM_Events as $EM_Event){
$events_dates[date_i18n($format,$EM_Event->start)][] = $EM_Event;
}
foreach ($events_dates as $month => $events){
echo '<h2>'.$month.'</h2>';
echo EM_Events::output($events, $atts);
}
break;
case 'weekly':
$format = (!empty($args['date_format'])) ? $args['date_format']:get_option('date_format');
$events_dates = array();
foreach($EM_Events as $EM_Event){
$start_of_week = get_option('start_of_week');
$day_of_week = date('w',$EM_Event->start);
$day_of_week = date('w',$EM_Event->start);
$offset = $day_of_week - $start_of_week;
if($offset<0){ $offset += 7; }
$offset = $offset * 60*60*24; //days in seconds
$start_day = strtotime($EM_Event->start_date);
$events_dates[$start_day - $offset][] = $EM_Event;
}
foreach ($events_dates as $event_day_ts => $events){
echo '<h2>'.date_i18n($format,$event_day_ts).' - '.date_i18n($format,$event_day_ts+(60*60*24*6)).'</h2>';
echo EM_Events::output($events, $atts);
}
break;
default: //daily
//go through the events and put them into a daily array
$format = (!empty($args['date_format'])) ? $args['date_format']:get_option('date_format');
$events_dates = array();
foreach($EM_Events as $EM_Event){
$events_dates[strtotime($EM_Event->start_date)][] = $EM_Event;
}
foreach ($events_dates as $event_day_ts => $events){
echo '<h2>'.date_i18n($format,$event_day_ts).'</h2>';
echo EM_Events::output($events, $atts);
}
break;
}
if( !empty($args['limit']) && $events_count > $args['limit'] && (!empty($args['pagination']) || !isset($args['pagination'])) ){
//Show the pagination links (unless there's less than $limit events)
$page_link_template = add_query_arg(array('page'=>'%PAGE%'));
echo em_paginate( $page_link_template, $events_count, $args['limit'], $args['page']);
}
return ob_get_clean();
}
/**
* Print a grouped list of events by year, month, week or day.
* @since 4.213
* @param array $args
* @param string $format
* @return string
*/
function em_events_list_grouped($args, $format=''){ echo em_get_events_list_grouped($args, $format); }
/**
* Creates an html link to the events page.
* @param string $text
* @return string
*/
function em_get_link( $text = '' ) {
$text = ($text == '') ? get_option ( "dbem_events_page_title" ) : $text;
$text = ($text == '') ? __('Events','dbem') : $text; //In case options aren't there....
return "<a href='".EM_URI."' title='$text'>$text</a>";
}
/**
* Prints the result of em_get_link()
* @param string $text
* @uses em_get_link()
*/
function em_link($text = ''){ echo em_get_link($text); }
/**
* Creates an html link to the RSS feed
* @param string $text
* @return string
*/
function em_get_rss_link($text = "RSS") {
$text = ($text == '') ? 'RSS' : $text;
return "<a href='".EM_RSS_URI."'>$text</a>";
}
/**
* Prints the result of em_get_rss_link()
* @param string $text
* @uses em_get_rss_link()
*/
function em_rss_link($text = "RSS"){ echo em_get_rss_link($text); }
/*
* ---------------------------------------------------------------------
* User Interfaces - Forms, Tables etc.
* ---------------------------------------------------------------------
*/
//Event Forms
/**
* Outputs the event submission form for guests and members.
* @param array $args
*/
function em_event_form($args = array()){
global $EM_Event;
if( !is_user_logged_in() && get_option('dbem_events_anonymous_submissions') && em_locate_template('forms/event-editor-guest.php') ){
em_locate_template('forms/event-editor-guest.php',true, array('args'=>$args));
}else{
if( empty($EM_Event->event_id) ){
$EM_Event = ( is_object($EM_Event) && get_class($EM_Event) == 'EM_Event') ? $EM_Event : new EM_Event();
//Give a default location & category
$default_cat = get_option('dbem_default_category');
$default_loc = get_option('dbem_default_location');
if( is_numeric($default_cat) && $default_cat > 0 && !empty($EM_Event->get_categories->categories) ){
$EM_Category = new EM_Category($default_cat);
$EM_Event->get_categories()->categories[] = $EM_Category;
}
if( is_numeric($default_loc) && $default_loc > 0 && ( empty($EM_Event->get_location()->location_id) && empty($EM_Event->get_location()->location_name) && empty($EM_Event->get_location()->location_address) && empty($EM_Event->get_location()->location_town) ) ){
$EM_Event->location_id = $default_loc;
$EM_Event->location = new EM_Location($default_loc);
}
}
em_locate_template('forms/event-editor.php',true, array('args'=>$args));
}
}
/**
* Retreives the event submission form for guests and members.
* @param array $args
*/
function em_get_event_form( $args = array() ){
ob_start();
em_event_form($args);
return ob_get_clean();
}
/**
* Outputs table of events belonging to user
* @param array $args
*/
function em_events_admin($args = array()){
global $EM_Event, $bp;
if( is_user_logged_in() && current_user_can('edit_events') ){
if( !empty($_GET['action']) && $_GET['action']=='edit' ){
if( empty($_REQUEST['redirect_to']) ){
$_REQUEST['redirect_to'] = em_add_get_params($_SERVER['REQUEST_URI'], array('action'=>null, 'event_id'=>null));
}
em_event_form();
}else{
$limit = ( !empty($_REQUEST['limit']) ) ? $_REQUEST['limit'] : 20;//Default limit
$page = ( !empty($_REQUEST['pno']) ) ? $_REQUEST['pno']:1;
$offset = ( $page > 1 ) ? ($page-1)*$limit : 0;
$order = ( !empty($_REQUEST ['order']) ) ? $_REQUEST ['order']:'ASC';
$scope_names = em_get_scopes();
$scope = ( !empty($_REQUEST ['scope']) && array_key_exists($_REQUEST ['scope'], $scope_names) ) ? $_REQUEST ['scope']:'future';
if( array_key_exists('status', $_REQUEST) ){
$status = ($_REQUEST['status']) ? 1:0;
}else{
$status = false;
}
$search = ( !empty($_REQUEST['em_search']) ) ? $_REQUEST['em_search']:'';
$args = array( 'scope' => $scope, 'order' => $order, 'search' => $search, 'owner' => get_current_user_id(),'status' => $status);
$events_count = EM_Events::count( $args ); //count events without limits for pagination
$args['limit'] = $limit;
$args['offset'] = $offset;
$EM_Events = EM_Events::get( $args );
if($scope != 'future'){
$future_count = EM_Events::count( array('status'=>1, 'owner' =>get_current_user_id(), 'scope' => 'future'));
}else{
$future_count = $events_count;
}
$pending_count = EM_Events::count( array('status'=>0, 'owner' =>get_current_user_id(), 'scope' => 'all') );
em_locate_template('tables/events.php',true, array(
'args'=>$args,
'EM_Events'=>$EM_Events,
'events_count'=>$events_count,
'future_count'=>$future_count,
'pending_count'=>$pending_count,
'page' => $page,
'limit' => $limit,
'offset' => $offset,
'show_add_new' => true
));
}
}else{
echo __("You must log in to view and manage your events.",'dbem');
}
}
/**
* Retreives table of events belonging to user
* @param array $args
*/
function em_get_events_admin( $args = array() ){
ob_start();
em_events_admin($args);
return ob_get_clean();
}
/**
* Outputs the event search form.
* @param array $args
*/
function em_event_search_form($args = array()){
em_locate_template('templates/events-search.php',true, array('args'=>$args));
}
/**
* Retreives the event search form.
* @param array $args
*/
function em_get_event_search_form( $args = array() ){
ob_start();
em_event_search_form($args);
return ob_get_clean();
}
//Location Forms
/**
* Outputs the location submission form for guests and members.
* @param array $args
*/
function em_location_form($args = array()){
global $EM_Location;
$EM_Location = ( is_object($EM_Location) && get_class($EM_Location) == 'EM_Location') ? $EM_Location : new EM_Location();
em_locate_template('forms/location-editor.php',true);
}
/**
* Retreives the location submission form for guests and members.
* @param array $args
*/
function em_get_location_form( $args = array() ){
ob_start();
em_location_form($args);
return ob_get_clean();
}
/**
* Outputs table of locations belonging to user
* @param array $args
*/
function em_locations_admin($args = array()){
global $EM_Location;
if( is_user_logged_in() && current_user_can('edit_locations') ){
if( !empty($_GET['action']) && $_GET['action']=='edit' ){
if( empty($_REQUEST['redirect_to']) ){
$_REQUEST['redirect_to'] = em_add_get_params($_SERVER['REQUEST_URI'], array('action'=>null, 'location_id'=>null));
}
em_location_form();
}else{
$url = empty($url) ? $_SERVER['REQUEST_URI']:$url; //url to this page
$limit = ( !empty($_REQUEST['limit']) ) ? $_REQUEST['limit'] : 20;//Default limit
$page = ( !empty($_REQUEST['pno']) ) ? $_REQUEST['pno']:1;
$offset = ( $page > 1 ) ? ($page-1)*$limit : 0;
$args = array('limit'=>$limit, 'offset'=>$offset, 'status'=>false, 'blog'=>false);
if( !empty($_REQUEST['view']) && $_REQUEST['view'] == 'others' && current_user_can('read_others_locations') ){
$locations = EM_Locations::get($args);
$locations_count = EM_Locations::count(array('status'=>false, 'blog'=>false, 'owner'=>false));
}else{
$locations = EM_Locations::get( array_merge($args, array('owner'=>get_current_user_id())) );
$locations_count = EM_Locations::count(array('status'=>false, 'blog'=>false, 'owner'=>get_current_user_id()));
}
$locations_mine_count = EM_Locations::count( array('owner'=>get_current_user_id(), 'blog'=>false, 'status'=>false) );
$locations_all_count = current_user_can('read_others_locations') ? EM_Locations::count(array('blog'=>false, 'status'=>false)):0;
em_locate_template('tables/locations.php',true, array(
'args'=>$args,
'locations'=>$locations,
'locations_count'=>$locations_count,
'locations_mine_count'=>$locations_mine_count,
'locations_all_count'=>$locations_all_count,
'url' => $url,
'page' => $page,
'limit' => $limit,
'offset' => $offset,
'show_add_new' => true
));
}
}else{
echo __("You must log in to view and manage your locations.",'dbem');
}
}
/**
* Retreives table of locations belonging to user
* @param array $args
*/
function em_get_locations_admin( $args = array() ){
ob_start();
em_locations_admin($args);
return ob_get_clean();
}
//Bookings Pages
function em_bookings_admin(){
if( is_user_logged_in() && current_user_can('manage_bookings') ){
em_locate_template('buddypress/my-bookings.php', true);
}else{
echo __("You must log in to view and manage your bookings.",'dbem');
}
}
function em_get_bookings_admin(){
ob_start();
em_bookings_admin();
return ob_get_clean();
}
function em_my_bookings(){
em_locate_template('templates/my-bookings.php', true);
}
function em_get_my_bookings(){
ob_start();
em_my_bookings();
return ob_get_clean();
}
/*
* ---------------------------------------------------------------------
* Conditionals - Yes/No functions
* ---------------------------------------------------------------------
*/
/**
* Returns true if there are any events that exist in the given scope (default is future events).
* @param string $scope
* @return boolean
*/
function em_are_events_available($scope = "future") {
$scope = ($scope == "") ? "future":$scope;
$events = EM_Events::get( array('limit'=>1, 'scope'=>$scope) );
return ( count($events) > 0 );
}
/**
* Returns true if the page is the events page. this is now only an events page, before v4.0.83 this would be true for any multiple page (e.g. locations)
* @return boolean
*/
function em_is_events_page() {
global $post;
return em_get_page_type() == 'events';
}
/**
* Is this a a single event page?
* @return boolean
*/
function em_is_event_page(){
return em_get_page_type() == 'event';
}
/**
* Is this a a single calendar day page?
* @return boolean
*/
function em_is_calendar_day_page(){
return em_get_page_type() == 'calendar_day';
}
/**
* Is this a a single category page?
* @return boolean
*/
function em_is_category_page(){
return em_get_page_type() == 'category';
}
/**
* Is this a categories list page?
* @return boolean
*/
function em_is_categories_page(){
return em_get_page_type() == 'categories';
}
/**
* Is this a a single location page?
* @return boolean
*/
function em_is_location_page(){
return em_get_page_type() == 'location';
}
/**
* Is this a locations list page?
* @return boolean
*/
function em_is_locations_page(){
return em_get_page_type() == 'locations';
}
/**
* Is this my bookings page?
* @return boolean
*/
function em_is_my_bookings_page(){
return em_get_page_type() == 'my_bookings';
}
/**
* Returns true if this is a single events page and the event is RSVPable
* @return boolean
*/
function em_is_event_rsvpable() {
//We assume that we're on a single event (or recurring event) page here, so $EM_Event must be loaded
global $EM_Event;
return ( em_is_event_page() && $EM_Event->rsvp );
}
?>