-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.php
356 lines (318 loc) · 13.9 KB
/
ajax.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
<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );
$POST = preg_replace( "/[^a-zA-Z0-9\s\p{P}]/", '', $_POST );
global $wpdb;
// List Members
if($POST['action'] == "listMembers"){
$members = $wpdb->get_results("SELECT * FROM wp_cap_roles");
?> <div id="memberList"> <?php
foreach($members as $member){
$member_id = $member->user_id;
$role = $member->cap_role;
$member_info = $wpdb->get_results("SELECT * FROM wp_users WHERE ID=$member_id");
$email = $member_info[0]->user_email;
$firstName = get_user_meta($member_id, "FirstName", true);
$lastName = get_user_meta($member_id, "LastName", true);
?>
<div class="memberListItem">
<div id="name-<?php echo $member_id; ?>" class="memberName"><h3><?php echo $firstName . " " . $lastName; ?></h3></div>
<div id="role-<?php echo $member_id; ?>" class="memberRole"><p><?php echo $role; ?></p></div>
<div id="email-<?php echo $member_id; ?>" class="memberEmail"><p><?php echo $email;?></p></div>
<div id="tools-<?php echo $member_id; ?>" class="memberTools">
<span class="dashicons dashicons-edit" onClick="editMember(<?php echo $member_id; ?>, '<?php echo $firstName; ?>', '<?php echo $lastName; ?>', '<?php echo $role; ?>', '<?php echo $email ?>')"></span>
<span class="dashicons dashicons-trash" onClick="deleteMember(<?php echo $member_id; ?>)"></span>
</div>
</div>
<?php
}
?></div><?php
}
// End List Members
// Add New Member Form
if($POST['action'] == 'memberForm'){
?>
<div class="reformed-form">
<form method="post" name="AddMember" id="AddMember">
<dl>
<dt>
<label for="memberFirstName">First Name</label>
</dt>
<dd><input type="text" id="memberFirstName" class="required" name="memberFirstName" /></dd>
</dl>
<dl>
<dt>
<label for="memberLastName">Last Name</label>
</dt>
<dd><input type="text" id="memberLastName" class="required" name="memberLastName" /></dd>
</dl>
<dl>
<dt>
<label for="memberEmail">E-Mail</label>
</dt>
<dd><input type="text" id="memberEmail" class="required email" name="memberEmail" /></dd>
</dl>
<dl>
<dt>
<label for="memberRole">Role</label>
</dt>
<dd>
<select size="1" name="memberRole" id="memberRole" class="required">
<option value="Cadet">Cadet</option>
<option value="Cadet Staff">Cadet Staff</option>
<option value="Senior Member">Senior Member</option>
<option value="Senior Staff">Senior Staff</option>
</select>
</dd>
</dl>
<div id="submit_buttons">
<button type="submit" onClick="submitMemberForm()">Submit</button>
</div>
</form>
</div>
<?php
}
// End Add New Member Form
// Submit New Member Form
if($POST['action'] == 'submitMemberForm'){
$firstName = $POST['fName'];
$lastName = $POST['lName'];
$email = $POST['email'];
$role = $POST['role'];
$username = substr($firstName, 0, 1) . $lastName;
$i=1;
while(username_exists($username)){
$username = substr($firstName, 0, $i++) . $lastName;
}
if(email_exists($email)){
echo "A user with that email already exists!";
} else {
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
$user_id = wp_create_user( $username, $random_password, $email );
add_user_meta( $user_id, "FirstName", $firstName);
add_user_meta( $user_id, "LastName", $lastName);
//wp_new_user_notification($user_id, null, 'both');
$wpdb->query("INSERT INTO wp_cap_roles (user_id, cap_role) VALUES ($user_id, '$role')");
echo "INSERT INTO wp_cap_roles (user_id, cap_role) VALUES ($user_id, '$role')";
}
}
// End Submit New Member Form
// Update Member Info
if($POST['action'] == 'updateMember'){
$id = $POST['id'];
$firstName = $POST['fName'];
$lastName = $POST['lName'];
$email = $POST['email'];
$role = $POST['role'];
update_user_meta($id, "FirstName", $firstName);
update_user_meta($id, "LastName", $lastName);
wp_update_user( array ('ID' => $id, 'user_email' => $email) );
$wpdb->query("UPDATE wp_cap_roles SET cap_role='$role' WHERE user_id=$id");
}
// End Update Member Info
// Delete Member
if($POST['action'] == 'deleteMember'){
$id = $POST['id'];
require_once(ABSPATH.'wp-admin/includes/user.php' );
wp_delete_user($id);
$wpdb->query("DELETE FROM wp_cap_roles WHERE user_id=$id");
}
// End Delete Member
// Weekly Check-In Form
if($POST['action'] == 'checkinForm'){
global $wpdb;
date_default_timezone_set('America/New_York');
$current_date = strtotime("today");
$today_dow = date('D');
$user_id = get_current_user_id();
$firstName = get_user_meta($user_id, "FirstName", true);
$lastName = get_user_meta($user_id, "LastName", true);
if($today_dow == 'Mon'){
$meeting_date = date('d F Y');
$meeting_timestamp = strtotime("today");
} else {
$meeting_date = date('d F Y', strtotime('this monday', strtotime('tomorrow')));
$meeting_timestamp = strtotime('this monday', strtotime('tomorrow'));
}
$last_checkin = $wpdb->get_results("SELECT date FROM wp_cap_attendance WHERE user_id=$user_id ORDER BY date DESC LIMIT 1");
$last_checkin_date = $last_checkin[0]->date;
if($last_checkin_date == $meeting_timestamp) {
?> <h2><?php echo $firstName . " " . $lastName; ?> has already checked in for this meeting.</h2> <?php
} else {
?>
<form class="form-horizontal">
<fieldset>
<!-- Form Name -->
<legend>Check-In</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Your Name</label>
<div class="col-md-4">
<input id="name" name="name" type="text" placeholder="<?php echo $firstName . " " . $lastName; ?>" class="form-control input-md">
<span class="help-block">Cannot be changed</span>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="date">Meeting Date</label>
<div class="col-md-4">
<input id="date" name="date" type="text" placeholder="<?php echo $meeting_date; ?>" class="form-control input-md">
<span class="help-block">Cannot be changed</span>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="attendance">Attending?</label>
<div class="col-md-4">
<select id="attendance" name="attendance" class="form-control">
<option value="Yes">Yes</option>
<option value="Late">Late</option>
<option value="No">No</option>
</select>
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="reason">Reason (Late/No):</label>
<div class="col-md-4">
<textarea class="form-control" id="reason" name="reason" disabled="disabled" rows="4"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button type="button" id="submit" name="submit" class="btn btn-success" onClick="submitCheckIn('<?php echo $user_id; ?>', '<?php echo $meeting_timestamp; ?>')">Check-In</button>
</div>
</div>
</fieldset>
</form>
<script>
jQuery( "#attendance" ).change(function() {
var attending = jQuery('#attendance').val();
if(attending == "Late" || attending == "No"){
jQuery('#reason').removeAttr("disabled");
} else {
jQuery('#reason').attr("disabled", "disabled");
}
});
</script>
<?php
}
}
// End Weekly Check-In Form
// Check-In
if($POST['action'] == 'checkIn'){
global $wpdb;
$user_id = $POST['id'];
$date = $POST['date'];
$attendance = $POST['attendance'];
$reason = $POST['reason'];
$wpdb->query("INSERT INTO wp_cap_attendance (user_id, date, attendance, reason) VALUES ($user_id, $date, '$attendance', '$reason')");
}
// End Check-In
// List Cadet Attendance
if($POST['action'] == "listCadetAttendance"){
global $wpdb;
$cadets = $wpdb->get_results("SELECT * FROM wp_cap_roles WHERE cap_role='Cadet' OR cap_role='Cadet Staff'");
$today_dow = date('D');
if($today_dow == 'Mon'){
$meeting_date = date('d F Y');
$meeting_timestamp = strtotime("today");
} else {
$meeting_date = date('d F Y', strtotime('this monday', strtotime('tomorrow')));
$meeting_timestamp = strtotime('this monday', strtotime('tomorrow'));
}
?>
<table id="senior-attend" class="tablesorter">
<caption>Cadet Check-Ins for <?php echo $meeting_date; ?></caption>
<thead>
<tr>
<th class="table2">Last Name</th>
<th class="table2">First Name</th>
<th class="table1">Attending?</th>
<th class="table4">Reason</th>
</tr>
</thead>
<tbody>
<?php
foreach($cadets as $cadet){
$firstName = get_user_meta($cadet->user_id, "FirstName", true);
$lastName = get_user_meta($cadet->user_id, "LastName", true);
$attendance = $wpdb->get_results("SELECT * FROM wp_cap_attendance WHERE user_id=$cadet->user_id ORDER BY date DESC LIMIT 1");
$lastCheckInDate = $attendance[0]->date;
$lastCheckInStatus = $attendance[0]->attendance;
$lastCheckInReason = $attendance[0]->reason;
?>
<tr>
<td><?php echo $lastName; ?></td>
<td><?php echo $firstName; ?></td>
<?php if(date('d F Y', $meeting_timestamp) == date('d F Y', $lastCheckInDate)){ ?>
<td><?php echo $lastCheckInStatus; ?></td>
<td colspan="4"><?php echo $lastCheckInReason; ?></td>
<?php } else { ?>
<td>Not Checked In</td>
<td colspan="4"></td>
<?php } ?>
</tr>
<?php
}
?>
</tbody>
</table>
<script>jQuery("#senior-attend").tablesorter({sortList: [[0,0]]});</script>
<?php
}
// End List Cadet Attendance
// List Senior Attendance
if($POST['action'] == "listSeniorAttendance"){
global $wpdb;
$today_dow = date('D');
if($today_dow == 'Mon'){
$meeting_date = date('d F Y');
$meeting_timestamp = strtotime("today");
} else {
$meeting_date = date('d F Y', strtotime('this monday', strtotime('tomorrow')));
$meeting_timestamp = strtotime('this monday', strtotime('tomorrow'));
}
?>
<table id="senior-attend" class="tablesorter">
<caption>Senior Check-Ins for <?php echo $meeting_date; ?></caption>
<thead>
<tr>
<th class="table2">Last Name</th>
<th class="table2">First Name</th>
<th class="table1">Attending?</th>
<th class="table4">Reason</th>
</tr>
</thead>
<tbody>
<?php
$seniors = $wpdb->get_results("SELECT * FROM wp_cap_roles WHERE cap_role='Senior Member' OR cap_role='Senior Staff'");
foreach($seniors as $senior){
$firstName = get_user_meta($senior->user_id, "FirstName", true);
$lastName = get_user_meta($senior->user_id, "LastName", true);
$attendance = $wpdb->get_results("SELECT * FROM wp_cap_attendance WHERE user_id=$senior->user_id ORDER BY date DESC LIMIT 1");
$lastCheckInDate = $attendance[0]->date;
$lastCheckInStatus = $attendance[0]->attendance;
$lastCheckInReason = $attendance[0]->reason;
?>
<tr>
<td><?php echo $lastName; ?></td>
<td><?php echo $firstName; ?></td>
<?php if(date('d F Y', $meeting_timestamp) == date('d F Y', $lastCheckInDate)){ ?>
<td><?php echo $lastCheckInStatus; ?></td>
<td colspan="4"><?php echo $lastCheckInReason; ?></td>
<?php } else { ?>
<td>Not Checked In</td>
<td colspan="4"></td>
<?php } ?>
</tr>
<?php
}
?>
</tbody>
</table>
<script>jQuery("#senior-attend").tablesorter({sortList: [[0,0]]});</script>
<?php
}
// End List Senior Attendance