Files
crm_php/.hta_slug/admin/users.php
Kar l5 13133d2d2a mig
2024-05-06 16:29:00 +05:30

306 lines
14 KiB
PHP

<?php
require_once('.hta_config/crm_config.php');
require_once('.htac_header.php');
require_once('.htac_nav.php');
try {
$conn = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && $_POST['name'] && $_POST['pass'] && $_POST['email']) {
$mysqli = new mysqli($mariaServer, $mariaUser, $mariaPass, $mariaDb);
if ($mysqli->connect_error) die("Connection failed: " . $mysqli->connect_error);
$result = $mysqli->query("SELECT bv FROM business_verticals");
if ($result) {
$access_array = array();
while ($row = $result->fetch_assoc()) {
$access_array[] = $row['bv'];
}
// $access_array = ['beanstalk', 'inhouse', 'teenybeans', 'iimtt', 'buzzapp', 'atheneum', 'teenybeans_curriculum'];
$access_values_array = [];
foreach ($access_array as $access) {
if (isset($_POST[$access])) {
$access_values_array[] = $_POST[$access];
}
}
$selected_access = implode(',', $access_values_array);
$result->free();
// print_r($access_array);
} else { echo "Error: " . $mysqli->error; }
$mysqli->close();
$apiUrl = 'https://api.siliconpin.com/v3/list/country/state/?country=IN';
$handle = fopen($apiUrl, 'rb');
if ($handle === false) {
echo 'Unable to open URL';
} else {
$jsonResponse = stream_get_contents($handle);
fclose($handle);
$states = json_decode($jsonResponse, true);
if ($states === null) {
echo 'Error decoding JSON from API';
} else {
$state_values_array = [];
if (isset($_POST['international'])) $state_values_array[] ='international';
foreach ($states as $state) {
if (isset($_POST[$state['iso2']])) $state_values_array[] = $_POST[$state['iso2']];
}
$selected_states = implode(',', $state_values_array);
}
}
try {
$pass=md5($_POST['pass']);
$stmt = $conn->prepare("INSERT INTO users (name, email, mobile, access, pass, states) VALUES (:name, :email, :mobile, :access, :pass, :states)");
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':mobile', $_POST['phone']);
$stmt->bindParam(':access', $selected_access);
$stmt->bindParam(':pass', $pass);
$stmt->bindParam(':states', $selected_states); // Bind selected states
$stmt->execute();
echo "A new user added.";
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
?>
<div>
<button onclick="toggleDisplay();" style="background: linear-gradient(90deg, rgba(111, 107, 255, 0.91) 0%, rgba(68, 55, 128, 0.91) 100%); text-align: center; padding: 10px 20px 10px 20px; color: #fff; border-radius: 6px; float: right; margin-top: 100px; margin-bottom: 30px;">Add User</button>
<!-- <a href="/admin/add-user" class="" style="background: linear-gradient(90deg, rgba(111, 107, 255, 0.91) 0%, rgba(68, 55, 128, 0.91) 100%); text-align: center; padding: 10px 20px 10px 20px; color: #fff; border-radius: 6px; float: right; margin-top: 30px;">Add User</a> -->
</div>
<table class="bg-[#fff] text-[#3F4254] rounded-lg font-bold">
<thead>
<tr>
<th class="text-center border-b-2 p-3">Name</th>
<th class="text-center border-b-2 p-3">Email</th>
<th class="text-center border-b-2 p-3">Mobile</th>
<th class="text-center border-b-2 p-3">Time</th>
<th class="text-center border-b-2 p-3">Access</th>
<th class="text-center border-b-2 p-3">Access by State</th>
<th class="text-center border-b-2 p-3">Action</th>
</tr>
</thead>
<tbody>
<?php
try {
$conn = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM users");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
?>
<tr>
<td class="border-y-2 p-2"><?php echo $row['name']; ?></td>
<td class="border-y-2 p-2"><?php echo $row['email']; ?></td>
<td class="border-y-2 p-2"><?php echo $row['mobile']; ?></td>
<td class="border-y-2 p-2"><?php echo $row['time']; ?></td>
<td class="border-y-2 p-2"><?php echo $row['access']; ?></td>
<td class="border-y-2 p-2"><?php echo $row['states']; ?></td>
<td class="border-y-2 p-2">
<div class="flex flex-row justify-center">
<a href="/admin/edit-user/?id=<?php echo $row['id']; ?>">Edit</a> &nbsp; | &nbsp;
<a href="/admin/delete-user/?id=<?php echo $row['id']; ?>">Delete</a>
</div>
</td>
</tr>
<?php
}
$num_rows = $stmt->rowCount();
} catch (PDOException $e) {
echo "<p class='text-danger'>Error: " . $e->getMessage() . "</p>";
}
?>
</tbody>
</table>
<div class="add-user-form" id="add-user-form">
<form action="" method="post" style="">
<div class="flex flex-row place-content-between" style="border-bottom: 2px solid #464E5F; border-style: dashed;">
<p style="font-size: 25px; font-weight: bold;">Add new user</p>
<p onclick="toggleDisplay();" class="rounded-lg px-1.5 text-[#464E5F] text-[25px] cursor-pointer" style="border: 2px solid #464E5F; border-radius: 50%;">&#10008;</p>
</div>
<div style="display: flex; flex-direction: column;">
<label for="name">Name:</label>
<input type="text" name="name" id="name" style="border: 1px solid #363650; border-radius: 5px; padding: 6px;" />
</div>
<div class="flex flex-row space-x-2">
<div style="display: flex; flex-direction: column; width: 100%;">
<label for="email">E-mail:</label>
<input type="text" name="email" id="email" style="border: 1px solid #363650; border-radius: 5px; padding: 6px;" />
</div>
<div style="display: flex; flex-direction: column; width: 100%;">
<label for="phone">Phone:</label>
<input type="text" name="phone" id="phone" style="border: 1px solid #363650; border-radius: 5px; padding: 6px;" />
</div>
</div>
<div style="display: flex; flex-direction: column; border-bottom: 1px solid #7E8299;">
<p>Access:</p>
<div class="bv-list">
<?php
try {
$stmt = $conn->prepare("SELECT * FROM business_verticals");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num_rows = $stmt->rowCount();
if ($num_rows >= 1) {
foreach($rows as $row){ ?>
<div class="flex flex-row">
<input type="checkbox" name="<?php echo $row['bv']; ?>" id="<?php echo $row['bv']; ?>" value="<?php echo $row['bv']; ?>" />&nbsp;
<label for="<?php echo $row['bv']; ?>" style="text-transform: uppercase;"><?php echo $row['bv']; ?></label>
</div>
<?php
}
} else{
echo "<p class='text-danger'>Not Found any Data</p>";
}
} catch (PDOException $e) {
echo "<p class='text-danger'>Error: " . $e->getMessage() . "</p>";
}
?>
</div>
</div>
<hr> <br>
<div style="display: flex; flex-direction: column;">
<p style="font-size: 16px; font-weight: bold; margin-top: 10px;"> Responsible Area:</p>
<div class="flex flex-row">
<input type="checkbox" name="international" id="international" value="international" /> &nbsp;&nbsp;
<label for="international" style="text-transform: uppercase;">international</label>
</div>
<hr><br>
<div style="height:200px; overflow:scroll;white-space: nowrap; overflow-x: visible;">
<?php
$apiUrl = 'https://api.siliconpin.com/v3/list/country/state/?country=IN';
$handle = fopen($apiUrl, 'rb');
if ($handle === false) {
echo 'Unable to open URL';
} else {
$jsonResponse = stream_get_contents($handle);
fclose($handle);
$states = json_decode($jsonResponse, true);
if ($states === null) {
echo 'Error decoding JSON from API';
} else {
foreach ($states as $state) {
echo '<input type="checkbox" id="'.$state["iso2"].'" name="'.$state["iso2"].'"'. ' value="' . $state["iso2"] . '" />';
echo '<label for="' . $state["iso2"] . '">' . $state["name"] . '</label><br>';
}
}
}
?>
</div>
</div>
<div style="display: flex; flex-direction: column;">
<label for="pass">Password:</label>
<input type="text" name="pass" id="pass" style="border: 1px solid #363650; border-radius: 5px; padding: 6px;" />
</div>
<div style="display: flex; flex-direction: column;">
<input type="submit" name="submit" id="submit" value="Submit" style="background: linear-gradient(90deg, rgba(111, 107, 255, 0.91) 0%, rgba(68, 55, 128, 0.91) 100%); text-align: center; padding: 10px 20px 10px 20px; color: #fff; border-radius: 6px; float: right; margin-top: 30px;" />
</div>
</form>
</div>
<script>
// JavaScript code to fetch data and populate checkboxes
// fetch(`https://api.siliconpin.com/v3/list/country/state/?country=IN`)
// .then(res => res.json())
// .then(data => {
// const stateList = document.getElementById('stateList');
// data.forEach(state => {
// const checkbox = document.createElement('input');
// checkbox.type = 'checkbox';
// checkbox.value = state.iso2;
// checkbox.name = 'selectedStates[]'; // Adjust name as per your form's needs
// const label = document.createElement('label');
// label.appendChild(checkbox);
// label.appendChild(document.createTextNode('\u00A0' + state.name)); // Adding a non-breaking space before the state name
// stateList.appendChild(label);
// });
// });
function toggleDisplay() {
var element = document.getElementById('add-user-form');
if (element.style.display === 'block') {
element.style.display = 'none';
} else {
element.style.display = 'block';
element.classList.add('slide-in-animation'); // Add class to trigger animation
}
}
// Event listener for the Esc key
document.addEventListener('keydown', function(event) {
if (event.keyCode === 27) {
var modal = document.getElementById('add-user-form');
if (modal.style.display === 'block') {
modal.style.display = 'none';
}
}
});
</script>
<style>
/* For small screens */
@media screen and (max-width: 767px) {
#stateList, .bv-list {
display: grid;
grid-template-columns: repeat(2, 1fr); /* 2 columns */
gap: 10px; /* Adjust the gap as needed */
}
}
/* For large screens */
@media screen and (min-width: 768px) {
#stateList, .bv-list {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 columns */
gap: 10px; /* Adjust the gap as needed */
}
}
.add-user-form {
background-color: #F8F8F8;
display: none;
position: fixed;
top: 0%;
right: 0%;
transform: translateX(100%);
/* transform: translate(-0%, -50%); */
width: 100%;
max-width: 700px;
box-shadow: 0px 0px 20px 0px #443780;
border-radius: 2px;
z-index: 100;
height: 100%;
padding: 10px 20px 10px 20px;
animation: slideInRight 0.5s forwards; /* Animation to slide in */
overflow-y: scroll;
font-size: 15px;
}
@keyframes slideInRight {
from {
transform: translateX(100%);
}
to {
transform: translateX(0%);
}
}
</style>