Tables json loading and support for caching

This commit is contained in:
Matteo Gheza 2020-10-18 00:22:11 +02:00
parent 8e9b5efdc2
commit 4455553b0a
4 changed files with 122 additions and 168 deletions

View File

@ -1,96 +0,0 @@
<?php
include_once("../../core.php");
init_class();
$user->requirelogin(false);
$risultato = $database->exec("SELECT * FROM `%PREFIX%_profiles` ORDER BY available DESC, chief DESC, services ASC, availability_minutes ASC, name ASC;", true);
$hidden = $user->hidden();
?>
<style>
th, td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
#href {
outline: none;
cursor: pointer;
text-align: center;
text-decoration: none;
font: bold 12px Arial, Helvetica, sans-serif;
color: #fff;
padding: 10px 20px;
border: solid 1px #0076a3;
background: #0095cd;
}
table {
box-shadow: 2px 2px 25px rgba(0,0,0,0.5);
border-radius: 15px;
margin: auto;
}
</style>
<div style="overflow-x:auto;">
<table style="width: 90%; text-align:center;">
<tr>
<th><?php t("Name"); ?></th>
<th><?php t("Available"); ?></th>
<th><?php t("Driver"); ?></th>
<th><?php t("Call"); ?></th>
<th><?php t("Write"); ?></th>
<th><?php t("Services"); ?></th>
<th><?php t("Availability Minutes"); ?></th>
<th><?php t("Other"); ?></th>
<?php
foreach($risultato as $row){
if(!in_array($row['name'], $hidden) && ($row['hidden'] == 0 && $row['disabled'] == 0)){
echo "<tr>
<td>";
$name = $user->nameById($row["id"]);
$callFunction = ($row['available'] == 1) ? "Deactivate" : "Activate";
$available = $row["available"];
if ($row['chief'] == 1) {
echo "<a onclick='$callFunction(".$row["id"].");'><img src='./resources/images/red_helmet.png' width='20px'>";
} else {
echo "<a onclick='$callFunction(".$row["id"].");'><img src='./resources/images/black_helmet.png' width='20px'>";
}
if((time()-$row["online_time"])<=30){
echo "<u>".$name."</u></a></td><td><a onclick='$callFunction(".$row["id"].");'>";
} else {
echo $name."</a></td><td><a onclick='$callFunction(".$row["id"].");'>";
}
if ($row['available'] == 1) {
echo "<i class='fa fa-check' style='color:green'></i>";
} else {
echo "<i class='fa fa-times' style='color:red'></i>";
};
echo "</a></td>
<td>";
if ($row['autista'] == 1) {
echo "<img src='./resources/images/wheel.png' width='20px'>";
} else {
echo "";
};
echo "</td>
<td><a href='tel:+" . $row['telefono'] . "'><i class='fa fa-phone'></i></a></td><td>";
$nome_url = urlencode($row['name']);
echo " <a href='https://api.whatsapp.com/send?phone=" . $row['telefono'] . "&text=ALLERTA IN CORSO.%20Mettiti%20in%20contatto%20con%20$nome_url'><i class='fa fa-whatsapp' style='color:green'></i></td>";
$services = $row['services'];
$minutes = $row['availability_minutes'];
$u = 'user_details.php?user=' . str_replace(' ', '_', urldecode(strtolower($row["id"])));
echo "<td>$services</td><td>$minutes</td><td><a href='$u'><p>".t("Others infos",false)."</p></a></td></tr>";
}
}
?>
</table>
<br>
<p style="text-align: center;">
<a id='add' href="edit_user.php?add"><?php t("Add user"); ?></a>
</p>
</div>

View File

@ -6,55 +6,56 @@ $user->requirelogin(false);
$risultato = $database->exec("SELECT * FROM `%PREFIX%_profiles` ORDER BY available DESC, chief DESC, services ASC, availability_minutes ASC, name ASC;", true);
$hidden = $user->hidden();
?>
<style>
th, td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
#href {
outline: none;
cursor: pointer;
text-align: center;
text-decoration: none;
font: bold 12px Arial, Helvetica, sans-serif;
color: #fff;
padding: 10px 20px;
border: solid 1px #0076a3;
background: #0095cd;
}
table {
box-shadow: 2px 2px 25px rgba(0,0,0,0.5);
border-radius: 15px;
margin: auto;
}
</style>
<div style="overflow-x:auto;">
<table style="width: 90%; text-align:center;">
<tr>
<th><?php t("Name"); ?></th>
<th><?php t("Available"); ?></th>
<?php
foreach($risultato as $row){
if(!in_array($row['name'], $hidden) && ($row['hidden'] == 0 && $row['disabled'] == 0)){
echo "<tr><td>";
if ($row['chief'] == 1) {echo "<img src='./resources/images/red_helmet.png' width='20px'> ";} else{echo "<img src='./resources/images/black_helmet.png' width='20px'> ";}
if((time()-$row["online_time"])<=30){
echo "<u>".$user->nameById($row["id"])."</u></td><td>";
$response = [];
foreach($risultato as $row){
if(!in_array($row['name'], $hidden) && ($row['hidden'] == 0 && $row['disabled'] == 0)){
if(isset($_GET["only_data"])){
if($user->requireRole(Role::FULL_VIEWER)){
$response[] = [
"id" => $row["id"],
"available" => $row["available"],
"chief" => $row['chief'],
"online" => (time()-$row["online_time"])<=30 ? 1 : 0,
"driver" => $row['autista'],
"phone" => $row['telefono'],
"services" => $row['services'],
"availability_minutes" => $row['availability_minutes']
];
} else {
echo "".$user->nameById($row["id"])."</td><td>";
$response[] = [
"id" => $row["id"],
"available" => $row["available"],
"online" => (time()-$row["online_time"])<=30 ? 1 : 0
];
}
if ($row['available'] == 1) {echo "<i class='fa fa-check' style='color:green'></i>";} else{echo "<i class='fa fa-times' style='color:red'></i>";};
echo "</td></tr>";
} else {
if($user->requireRole(Role::FULL_VIEWER)){
$name = $user->nameById($row["id"]);
$name_encoded = urlencode($name);
$functionName = $row["available"] ? "Deactivate" : "Activate";
$firstCell = $row["chief"] ? "<a onclick='$functionName(".$row["id"].");'><img src='./resources/images/red_helmet.png' width='20px'>$name</a>" : "<a onclick='$callFunction(".$row["id"].");'><img src='./resources/images/black_helmet.png' width='20px'>$name</a>";
$secondCell = $row["available"] ? "<a onclick='$functionName(".$row["id"].");'><i class='fa fa-check' style='color:green'></i></a>" : "<a onclick='$callFunction(".$row["id"].");'><i class='fa fa-times' style='color:red'></i></a>";
$response[] = [
(time()-$row["online_time"])<=30 ? "<u>".$firstCell."</u>" : $firstCell,
$secondCell,
$row['autista'] ? "<img src='./resources/images/wheel.png' width='20px'>" : "",
$row['telefono'] ? "<a href='tel:+".$row['telefono']."'><i class='fa fa-phone'></i></a>" : "",
$row['telefono'] ? "<a href='https://api.whatsapp.com/send?phone=+".$row['telefono']."text=ALLERTA IN CORSO.%20Mettiti%20in%20contatto%20con%20$name_encoded'><i class='fa fa-whatsapp' style='color:green'></i></a>" : "",
$row['services'],
$row['availability_minutes'],
"<a href='user_details.php?user=".$row['id']."'><p>".t("Altri dettagli", false)."</p></a>"
];
} else {
$response[] = [
"id" => $row["id"],
"available" => $row["available"],
"online" => (time()-$row["online_time"])<=30 ? 1 : 0
];
}
}
?>
</table>
</div>
}
}
}
header("Content-type: application/json");
print(json_encode($response));
?>

View File

@ -6,6 +6,13 @@ import './font-awesome.scss';
import '../node_modules/bootstrap-cookie-alert/cookiealert.css'; // TODO: migrate to Bootstrap Italia
import pickadate from 'pickadate'
$( document ).ajaxError(function(event, xhr, settings, error) {
console.error("Error requesting content: "+error+" - status code "+xhr.status);
console.log(event);
console.log(xhr);
console.log(settings);
});
$( document ).ready(function() {
// From https://github.com/Wruczek/Bootstrap-Cookie-Alert/blob/gh-pages/cookiealert.js
var cookieAlert = document.querySelector(".cookiealert");
@ -58,4 +65,21 @@ if ('serviceWorker' in navigator) {
console.log('SW registration failed: ', registrationError);
});
});
}
}
function loadTable(table_page){
$.getJSON( "resources/ajax/ajax_"+table_page+".php", function( data, status, xhr ) {
$("#table_body").empty();
$.each(data, function(num, item) {
var row = document.createElement("tr");
$.each(item, function(num, i) {
var cell = document.createElement("td");
cell.innerHTML = i;
row.appendChild(cell);
});
document.getElementById("table_body").appendChild(row);
});
caches.open('static-1').then((cache) => { cache.put('/table_'+table_page+'.json', new Response(xhr.responseText)) });
});
}
window.loadTable = loadTable;

View File

@ -13,26 +13,7 @@
<img alt="VVF" src="./resources/images/owner.png" width="150" style="display: block; margin-left: auto; margin-right: auto;">
<br>
<br>
<div id="list"></div>
{% if user.full_viewer == false %}
<script>
$(document).ready(function() {
$( "#list" ).load("resources/ajax/ajax_list.php?"+Math.random());
setInterval(function() {
$( "#list" ).load("resources/ajax/ajax_list.php?"+Math.random());
}, 10000);
});
</script>
{% elseif user.full_viewer == true %}
<script>
$(document).ready(function() {
$( "#list" ).load("resources/ajax/ajax_admin_list.php?"+Math.random());
setInterval(function() {
$( "#list" ).load("resources/ajax/ajax_admin_list.php?"+Math.random());
}, 10000);
});
</script>
<style>
<style>
#add {
outline: none;
cursor: pointer;
@ -56,8 +37,52 @@ $(document).ready(function() {
border: solid 1px #0076a3;
background: #0095cd;
}
th, td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table {
box-shadow: 2px 2px 25px rgba(0,0,0,0.5);
border-radius: 15px;
margin: auto;
}
</style>
{% endif %}
<br>
<br>
<div style="overflow-x:auto;">
<table style="width: 90%; text-align:center;">
<thead>
<tr>
<th>{{ 'Name'|t }}</th>
<th>{{ 'Available'|t }}</th>
{% if user.full_viewer %}
<th>{{ 'Driver'|t }}</th>
<th>{{ 'Call'|t }}</th>
<th>{{ 'Write'|t }}</th>
<th>{{ 'Services'|t }}</th>
<th>{{ 'Availability Minutes'|t }}</th>
<th>{{ 'Other'|t }}</th>
{% endif %}
</tr>
</thead>
<tbody id="table_body">
</tbody>
</table>
<script>
$( document ).ready(function() {
window.loadTable("list");
setInterval(function() {
window.loadTable("list");
}, 10000);
});
</script>
</div>
<br><br>
<p style="text-align: center;">
<a id='add' href="edit_user.php?add">{{ 'Add user'|t }}</a>
</p>
<br>
<br>
{% endblock %}