-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingular-event.php
288 lines (249 loc) · 10.5 KB
/
singular-event.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
<?php
/**
* Apocrypha Theme Single Event Template
* Andrew Clayton
* Version 1.0.1
* 2-20-2014
*/
// Get some information about the current user and the event
$apoc = apocrypha();
$user = $apoc->user;
$user_id = $user->ID;
$post = $apoc->queried_object;
$post_id = $post->ID;
// Get the calendars flagged for this event
$calendars = wp_get_post_terms( $post_id , 'calendar' );
// Does the user have access to any calendar?
$can_view = false;
foreach ( $calendars as $calendar ) {
// Is it a group calendar?
$term_id = $calendar->term_id;
$slug = $calendar->slug;
if ( is_group_calendar( $term_id ) ) :
$group_id = groups_get_id( $slug );
$can_view = groups_is_user_member( $user_id , $group_id ) ? true : false;
else :
$can_view = true;
endif;
// If we find a calendar for which the user is authorized, go ahead and display it
if( true == $can_view ) :
global $allowed_calendar;
$header = ( 'entropy-rising' == $slug ) ? 'entropy_rising_header' : 'get_header';
$sidebar = ( 'entropy-rising' == $slug ) ? 'entropy_rising_sidebar' : 'apoc_primary_sidebar';
$allowed_calendar = $calendar;
break;
endif;
}
// If the user is not authorized to view any calendar, redirect them
if ( !$can_view ) :
$default_slug = $calendars[0]->slug;
$redirect = ( 'entropy-rising' == $slug ) ? SITEURL . '/entropy-rising/' : SITEURL . '/groups/' . trailingslashit( $slug );
bp_core_add_message( 'You cannot access events on this calendar.' , 'error' );
bp_core_redirect( $redirect );
endif;
// Get event information
$action_url = get_permalink();
$title = $post->post_title;
$content = $post->post_content;
$capacity = get_post_meta( $post_id , 'event_capacity' , true );
$capacity = ( '' == $capacity ) ? 9999 : $capacity;
$cap_label = ( 9999 == $capacity ) ? '∞' : $capacity;
$req_rsvp = get_post_meta( $post_id , 'event_require_rsvp' , true );
$req_role = get_post_meta( $post_id , 'event_require_role' , true );
$url = get_post_permalink();
$rsvps = get_post_meta( $post_id , 'event_rsvps' , true );
// Check if the form was submitted and update the responses
if ( isset ( $_POST['submit'] ) && wp_verify_nonce( $_POST['event_rsvp_nonce'] , 'event-rsvp' ) ) :
$new_rsvp = array();
// Clean each field individually
if( !isset ( $_POST['attendance'] ) )
$error = 'You must select your expected attendance!';
else
$new_rsvp['rsvp'] = $_POST['attendance'];
if( $req_role && empty( $_POST['rsvp-role'] ) && $_POST['attendance'] != 'no' )
$error = 'You must select your preferred role!';
elseif( $req_role )
$new_rsvp['role'] = $_POST['rsvp-role'];
if( isset ( $_POST['rsvp-comment'] ) )
$new_rsvp['comment'] = sanitize_text_field( $_POST['rsvp-comment'] );
// If there are no errors, we can save the stuff
if ( !$error ) :
// Update the postmeta
$rsvps[$user_id] = $new_rsvp;
update_post_meta( $post_id, 'event_rsvps' , $rsvps );
bp_core_add_message( 'Thank you for responding!' );
// Clear notifications on each group calendar
foreach ( $calendars as $calendar ) {
if ( is_group_calendar( $calendar->term_id ) ) :
global $bp;
$group_id = groups_get_id( $calendar->slug );
// Clear notifications
bp_notifications_delete_notifications_by_item_id( $user_id , $group_id , $bp->groups->id , 'new_calendar_event' , $post_id );
endif;
}
// Otherwise, throw the error message
else :
bp_core_add_message( $error , 'error' );
endif;
endif;
// Sort responses alphabetically by name
if( !empty( $rsvps ) ) :
$names = array();
// Get user information
foreach ($rsvps as $uid => $info) {
$name = bp_core_get_user_displayname( $uid );
$link = bp_core_get_userlink( $uid );
$rsvps[$uid]['id'] = $uid;
$rsvps[$uid]['name'] = $name;
$rsvps[$uid]['link'] = $link;
$names[$uid] = strtolower( $name );
}
// Sort the array alphabetically
array_multisort($names, SORT_STRING, $rsvps);
// Restore the user_id keys
$temp = array();
for ( $i = 0; $i < count( $rsvps ); $i++ )
$temp[$rsvps[$i]['id']] = $rsvps[$i];
$rsvps = $temp;
endif;
// Count Attendance
$confirmed = 0;
$maybe = 0;
$declined = 0;
foreach ( $rsvps as $response ) {
if ( 'yes' == $response['rsvp'] )
$confirmed++;
elseif ( 'maybe' == $response['rsvp'] )
$maybe++;
elseif ( 'no' == $response['rsvp'] )
$declined++;
}
// User Response
if ( isset( $rsvps[$user_ID] ) ) :
switch ( $rsvps[$user_id]['rsvp'] ) {
case "yes" :
$rsvp = "Attending";
break;
case "no" :
$rsvp = "Absent";
break;
case "maybe" :
$rsvp = "Maybe";
break;
}
else :
$rsvp = "RSVP";
endif;
// Date and Time
$event_date = get_post_meta( $post_id , 'event_date' , true );
$fulldate = date('l F j, Y', strtotime( $event_date ) );
$start = get_post_meta( $post_id , 'event_start' , true );
$end = get_post_meta( $post_id , 'event_end' , true );
$fulltime = date('g:i', strtotime( $start ) ) . ' - ' . date('g:ia' , strtotime( $end ) ) . ' EST';
// Is the Event Over?
$end_time = strtotime( $end ) > strtotime( $start ) ? strtotime( $end ) : strtotime ( $end . ' tomorrow' );
$is_past = ( $end_time < strtotime( '-5 hours' ) ) ? true : false;
$header(); // Load the header contextually ?>
<div id="content" role="main">
<?php apoc_breadcrumbs(); ?>
<header class="entry-header <?php post_header_class(); ?>">
<h1 class="entry-title"><?php entry_header_title(); ?></h1>
<p class="entry-byline">This event is scheduled on the <?php echo $allowed_calendar->name; ?> calendar.</p>
</header>
<div class="entry-content">
<div class="event-details">
<h2 class="event-time double-border bottom">
<?php echo $fulldate . ' from ' . $fulltime; ?>
</h2>
<div class="event-meta">
<span class="event-attendance"><?php echo $confirmed . '/' . $cap_label; ?></span>
<div class="event-response <?php echo $rsvps[$user_id]['rsvp']; ?>">
<span class="event-rsvp"><?php echo $rsvp; ?></span>
</div>
</div>
<div class="event-content">
<div class="event-description"><?php echo wpautop( $content ); ?></div>
</div>
</div>
<div class="event-respondents">
<?php if ( $confirmed > 0 ) : ?>
<h2 class="calendar-header">Attending (<?php echo $confirmed; ?>)</h2>
<ul class="respondent-list attending">
<?php foreach ( $rsvps as $uid => $response ) :
if ( 'yes' == $response['rsvp'] ) :
$role = isset( $response['role'] ) ? "(" . $response['role'] . ") " : "";
echo '<li class="event-respondent">' . implode( ' - ' , array( $response['link'] , $role . stripslashes( $response['comment'] ) ) ) . '</li>';
endif;
endforeach; ?>
</ul>
<?php endif; ?>
<?php if ( $maybe > 0 ) : ?>
<h2 class="calendar-header">Maybe (<?php echo $maybe; ?>)</h2>
<ul class="respondent-list attending">
<?php foreach ( $rsvps as $responder => $response ) :
if ( 'maybe' == $response['rsvp'] ) :
$role = isset( $response['role'] ) ? "(" . $response['role'] . ") " : "";
echo '<li class="event-respondent">' . implode( ' - ' , array( $response['link'] , $role . stripslashes( $response['comment'] ) ) ) . '</li>';
endif;
endforeach; ?>
</ul>
<?php endif; ?>
<?php if ( $declined > 0 ) : ?>
<h2 class="calendar-header">Not Attending (<?php echo $declined; ?>)</h2>
<ul class="respondent-list attending">
<?php foreach ( $rsvps as $responder => $response ) :
if ( 'no' == $response['rsvp'] ) :
echo '<li class="event-respondent">' . implode( ' - ' , array( $response['link'] , stripslashes( $response['comment'] ) ) ) . '</li>';
endif;
endforeach; ?>
</ul>
<?php endif; ?>
<?php if ( 0 == count( $rsvps ) ) : ?>
<h2 class="calendar-header">No Responses Yet!</h2>
<?php endif; ?>
</div>
</div>
<?php if ( !$is_past ) : ?>
<form action="<?php echo $action_url; ?>" name="calendar-rsvp-form" id="calendar-rsvp-form" method="post">
<?php do_action( 'template_notices' ); ?>
<h2 class="calendar-header">Respond to this Event</h2>
<ol class="form">
<li class="form-field radio">
<label for="attendance">Expected Attendance : ☆</label>
<ul class="radio-options-list">
<?php if ( $confirmed < $capacity || ( isset( $rsvps[$user_ID] ) && 'yes' == $rsvps[$user_ID]['rsvp'] ) ) : ?>
<li><input type="radio" name="attendance" value="yes" <?php if ( isset( $rsvps[$user_ID] ) ) checked( $rsvps[$user_ID]['rsvp'] , 'yes' ); ?>/><label for="attendance">Yes</label></li>
<li><input type="radio" name="attendance" value="no" <?php if ( isset( $rsvps[$user_ID] ) ) checked( $rsvps[$user_ID]['rsvp'] , 'no' ); ?>/><label for="playstyle">No</label></li>
<li><input type="radio" name="attendance" value="maybe" <?php if ( isset( $rsvps[$user_ID] ) ) checked( $rsvps[$user_ID]['rsvp'] , 'maybe' ); ?>/><label for="attendance">Maybe</label></li>
<?php else : ?>
<li><input type="radio" name="attendance" value="maybe" <?php if ( isset( $rsvps[$user_ID] ) ) checked( $rsvps[$user_ID]['rsvp'] , 'maybe' ); ?>/><label for="attendance">Standby</label></li>
<li><input type="radio" name="attendance" value="no" <?php if ( isset( $rsvps[$user_ID] ) ) checked( $rsvps[$user_ID]['rsvp'] , 'no' ); ?>/><label for="playstyle">No</label></li>
<?php endif; ?>
</ul>
</li>
<?php if ( $req_role ) : ?>
<li class="form-field select">
<label for="rsvp-role">Preferred Role : ☆</label>
<select name="rsvp-role">
<option></option>
<option value="tank" <?php if ( isset( $rsvps[$user_ID] ) ) selected( $rsvps[$user_ID]['role'] , 'tank' ); ?>>Tank</option>
<option value="healer" <?php if ( isset( $rsvps[$user_ID] ) ) selected( $rsvps[$user_ID]['role'] , 'healer' ); ?>>Healer</option>
<option value="dps" <?php if ( isset( $rsvps[$user_ID] ) ) selected( $rsvps[$user_ID]['role'] , 'dps' ); ?>>DPS</option>
<option value="control" <?php if ( isset( $rsvps[$user_ID] ) ) selected( $rsvps[$user_ID]['role'] , 'control' ); ?>>Control</option>
</select>
</li>
<?php endif; ?>
<li class="form-field textarea">
<label for="rsvp-comment">Comment:</label><br/>
<textarea type="textarea" name="rsvp-comment" value="" rows="2" ><?php if ( isset( $rsvps[$user_ID] ) ) echo stripslashes( $rsvps[$user_ID]['comment'] ); ?></textarea>
</li>
<li class="form-field submit">
<?php wp_nonce_field( 'event-rsvp' , 'event_rsvp_nonce' ) ?>
<input type="submit" name="submit" value="Submit">
</li>
</ol>
</form>
<?php endif; ?>
</div><!-- #content -->
<?php $sidebar(); // Load the sidebar contextually ?>
<?php get_footer(); // Load the footer