File: //home/awaldron/_accomods/api.accomods.com/admin/users_edit.php
<?php
include '_helpers.php';
$update_result = 0;
$insert_result = 0;
$update_password = 0;
// CREATE NEW USER ********************************************************************************************/
if (isset($_POST['newuser'])) {
$insert_result = create_user( $_POST );
if ($insert_result == 1) {
$uid = get_last_uid();
header('Location: /admin/users_edit.php?uid='.$uid);
}
}
// UPDATING THE USER ********************************************************************************************/
if (isset($_POST['updateuser'])) {
$update_result = update_user( $_POST );
}
// UPDATING USER PASSWORD ********************************************************************************************/
if (isset($_POST['updatepw'])) {
$update_password = update_password( $_POST );
}
// GET THE USER DATA ********************************************************************************************/
if ($_GET['uid'] !== 'new') {
$u = get_user($_GET['uid']);
} else {
$u = [ 'id' => 'new', 'email' => '', 'fname' => '', 'lname' => '', 'phone' => '', 'school_code' => '', 'school_name' => '', 'expires' => '', 'access' => 1 ];
}
if ($update_result === -1 || $insert_result == -1) {
$u = $_POST;
}
// GET SCHOOLS DATA ********************************************************************************************/
$schools = get_schools();
include '_header.php';
//echo '<pre>', print_r($_POST), '</pre>';
?>
<style>
#schoolNameField, #accountExpires { display: none; }
</style>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1><?=($_GET['uid'] === 'new' ? 'Create New ' : 'Update ')?>User</h1>
<p><a href="/admin">Admin Panel</a> / <a href="/admin/users.php">Manage Users</a> </p>
<hr/>
<?php
if ($update_result === 1) { echo '<div class="alert alert-success"><b>Update Successful</b></div>'; }
if ($update_result === -1) { echo '<div class="alert alert-danger"><b>Update Failed - Please Try Again</b></div>'; }
if ($insert_result === -1) { echo '<div class="alert alert-danger"><b>User Create Failed - Please Try Again</b></div>'; }
if ($update_password === 1) { echo '<div class="alert alert-success"><b>Password Update Successful</b></div>'; }
if ($update_password === -1) { echo '<div class="alert alert-danger"><b>Password Update Failed - Please Try Again</b></div>'; }
?>
<form action="users_edit.php?uid=<?=$u['id']?>" method="post">
<input type="hidden" name="id" value="<?=$u['id']?>" />
<table class="table table-borderless" style="font-size:14px;">
<tbody>
<tr>
<td width="150">First Name</td>
<td><input type="text" name="fname" value="<?=$u['fname']?>" class="form-control" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lname" value="<?=$u['lname']?>" class="form-control" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?=$u['email']?>" class="form-control" /></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone" value="<?=$u['phone']?>" class="form-control" /></td>
</tr>
<tr>
<td>School (License)</td>
<td>
<select name="school" id="licenseSelect" class="form-control" id="">
<option value="1">--- No License ---</option>
<?php foreach ($schools as $s) {
echo '<option name="school" value="'.$s[0].'||'.$s[1].'" '.($s[1]==$u["school_code"] ? 'selected="selected"' : '').'>'.$s[0].'</option>';
}?>
</select>
</td>
</tr>
<tr id="schoolNameField">
<td>School Name</td>
<td><input type="text" name="school_name" value="<?=$u['school_name']?>" class="form-control" /></td>
</tr>
<tr id="accountExpires">
<td>Account Expires</td>
<td><input type="text" name="expires" value="<?=$u['expires']?>" class="form-control" placeholder="YYYY-MM-DD" /></td>
</tr>
<?php if ($_GET['uid'] === 'new'): ?>
<tr id="password">
<td>Set Password</td>
<td><input type="text" name="pass" value="" class="form-control" /></td>
</tr>
<?php endif; ?>
<tr>
<td> </td>
<td><input type="submit" name="<?=($_GET['uid']=='new' ? 'newuser' : 'updateuser')?>" value="Submit <?=($_GET['uid']=='new' ? 'New User' : 'Edits')?>" class="btn btn-success" /></td>
</tr>
</tbody>
</table>
</form>
<?php if ($_GET['uid'] !== 'new'): ?>
<br/><br/><br/><br/>
<h3>Set new Password</h3>
<hr/>
<form action="users_edit.php?uid=<?=$u['id']?>" method="post">
<input type="hidden" name="id" value="<?=$u['id']?>" />
<table class="table table-borderless" style="font-size:14px;">
<tbody>
<tr id="password">
<td width="150">New Password</td>
<td><input type="text" name="pass" value="" class="form-control" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="updatepw" value="Set New Password" class="btn btn-success" /></td>
</tr>
</tbody>
</table>
</form>
<?php endif; ?>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#licenseSelect').each(function () {
let val = $(this).val();
if (val == 1) { $('#schoolNameField, #accountExpires').show(); }
else { $('#schoolNameField, #accountExpires').hide(); }
}).change(function () {
let val = $(this).val();
if (val == 1) { $('#schoolNameField, #accountExpires').show(); }
else { $('#schoolNameField, #accountExpires').hide(); }
});
});
</script>
<?php
$db->close();
include '_footer.php';
?>