Fix sorting room, fix form sending after sorting and add column Historie

This commit is contained in:
Haitem 2021-01-05 12:19:30 +01:00
parent 64b50ead9f
commit b5b2bb95a7
9 changed files with 58 additions and 26 deletions

View File

@ -19,7 +19,7 @@ $router->any('/logout', 'Logout');
$router->any('/automation', 'Automation');
$router->any('/setting', 'Setting');
$router->any('/device', 'Device');
$router->get('/device/{sortBy}/{sortType}', 'Device');
$router->any('/device/{sortBy}/{sortType}', 'Device');
$router->any('/plugins', 'Plugins');
$router->any('/ajax', 'Ajax');
$router->any('/oauth', 'Oauth');

View File

@ -7,11 +7,9 @@ class DevicesApi extends ApiController{
$response = [];
// TODO: process the request
$this->response($response);
}
public function getDevicesByRoom($roomId){
}
}

View File

@ -1,6 +1,7 @@
<?php
if (!empty ($_POST)){
$deviceManager = new DeviceManager ();
$subDeviceManager = new SubDeviceManager ();
if (!empty ($_FILES['deviceFirmware']) && !empty ($_FILES['deviceFirmware']['tmp_name']) && !empty ($_POST['deviceId'])) {
$file = $_FILES['deviceFirmware'];
$deviceMac = $deviceManager->getDeviceById ($_POST['deviceId'])['mac'];
@ -27,6 +28,9 @@ if (!empty ($_POST)){
if (!empty ($_POST['deviceName']) && !empty ($_POST['deviceId'])) {
$deviceManager->edit ($_POST['deviceId'], array ('name' => $_POST['deviceName']));
}
header('Location: ./device');
if (isset ($_POST['deviceHistory']) && !empty ($_POST['deviceId'])) {
$subDeviceManager->editSubDevicesByDevice($_POST['deviceId'], array ('history' => $_POST['deviceHistory']));
}
header('Location: ' . BASEURL . str_replace(BASEDIR, "", $_SERVER['REQUEST_URI']));
die();
}

View File

@ -187,20 +187,31 @@ class Utilities
* @param string $operator ('asc'/'desc')
* @return void
*/
static function sortArrayByKey($data = [], $key, $operator = "asc"){
if ($operator == "asc")
{
uasort($data, function($a, $b) use ($key){
if ($a[$key] == $b[$key]) return 0;
return ($a[$key] < $b[$key]) ? -1 : 1;
static function sortArrayByKey($data = [], $key, $operator = "asc")
{
if ($operator == "asc") {
uasort($data, function ($a, $b) use ($key) {
$SortA = $a[$key];
$SortB = $b[$key];
if ($key == "room_id") {
$SortA = RoomManager::getRoomName($SortA);
$SortB = RoomManager::getRoomName($SortB);
}
if ($SortA == $SortB) return 0;
return ($SortA < $SortB) ? -1 : 1;
});
} else{
uasort($data, function($a, $b) use ($key){
if ($a[$key] == $b[$key]) return 0;
return ($a[$key] > $b[$key]) ? -1 : 1;
} else {
uasort($data, function ($a, $b) use ($key) {
$SortA = $a[$key];
$SortB = $b[$key];
if ($key == "room_id") {
$SortA = RoomManager::getRoomName($SortA);
$SortB = RoomManager::getRoomName($SortB);
}
if ($SortA == $SortB) return 0;
return ($SortA > $SortB) ? -1 : 1;
});
}
return $data;
}
}

View File

@ -19,6 +19,12 @@ class RoomManager{
return $allRoom;
}
public static function getRoomName ($room_id) {
//TODO: ignore Widgets withoud data
$allRoom = Db::loadAlone ("SELECT name FROM rooms WHERE room_id=?", array ($room_id));
return $allRoom;
}
public static function create ($name) {
$room = array (
'name' => $name,

View File

@ -66,6 +66,10 @@ class SubDeviceManager
}
}
public static function editSubDevicesByDevice ($deviceId, $subDeviceParameters) {
DB::edit('subdevices', $subDeviceParameters, 'WHERE device_id=?', array ($deviceId));
}
public static function remove($subDeviceId)
{
RecordManager::cleanSubdeviceRecords($subDeviceId);
@ -103,7 +107,7 @@ class SubDeviceManager
public static function getSubdeviceDetailById($subDeviceId){
if (empty($subDeviceId)) return NULL;
$rows = Db::loadOne("SELECT d.room_id, d.sleep_time, sd.subdevice_id, sd.type, sd.device_id FROM subdevices sd
$rows = Db::loadOne("SELECT d.room_id, d.sleep_time, sd.subdevice_id, sd.type, sd.device_id FROM subdevices sd
JOIN devices d ON sd.device_id = d.device_id
WHERE sd.subdevice_id = ? ", [$subDeviceId]);

View File

@ -28,7 +28,8 @@ class Device extends Template
"token" => "token",
"signal" => "signal",
"firmware" => "firmware_hash",
"icon" => "icon"
"icon" => "icon",
"history" => "history",
];
$sortIcons = [
@ -47,18 +48,23 @@ class Device extends Template
$sortBy = "id";
$sortType = "DESC";
}
$template->prepare('sortIcon', [$sortBy => $sortIcons[$sortType]]);
foreach ($devices as $key => $device) {
//Signal Stenght
$subdevice = $subDeviceManager->getSubDeviceByMasterAndType($device['device_id'], "wifi");
$subdeviceLocal = $subDeviceManager->getSubDeviceByMaster($device['device_id']);
if (!empty ($subdeviceLocal)) {
$devices[$key]['history'] = (!empty ($subdeviceLocal['history']) ? $subdeviceLocal['history'] : 0);
} else {
unset($devices[$key]['history']);
}
$devices[$key]['signal'] = "";
if (!empty($subdevice['subdevice_id'])) {
$record = $recordManager->getLastRecord($subdevice['subdevice_id']);
if (!empty($record)) {
$devices[$key]['signal'] = $record['value'] . " " . $subdevice['unit'];
}
}
}
//Firmware Status

View File

@ -36,6 +36,7 @@
<a href="device/mac/<?php echo $SORTTYPE; ?>">(Mac)</a><i class="fa"><?php echo (!empty($SORTICON['mac']) ? $SORTICON['mac'] : ""); ?></i><br>
<a href="device/token/<?php echo $SORTTYPE; ?>">Token</a><i class="fa"><?php echo (!empty($SORTICON['token']) ? $SORTICON['token'] : ""); ?></i>
</th>
<th><a href="device/history/<?php echo $SORTTYPE; ?>">Historie</a><i class="fa"><?php echo (!empty($SORTICON['history']) ? $SORTICON['history'] : ""); ?></i></th>
<th>Action
<form method="post" action="">
<button class="fa custom-file-input" type="submit" name="deviceCommand" value="reset" title="Reset All"><b>&#xf01e;</b></button>
@ -100,6 +101,14 @@
<td><?php echo (!empty($device['mac']) ? $device['mac'] : ""); ?><br>
<?php echo (!empty($device['ip_address']) ? $device['ip_address'] : ""); ?><br>
<?php echo (!empty($device['token']) ? $device['token'] : ""); ?></td>
<td>
<?php if (isset($device['history'])): ?>
<form method="post" action="">
<input type="hidden" name="deviceId" value="<?php echo (!empty($device['device_id']) ? $device['device_id'] : ""); ?>">
<input class="input" type="number" onchange="this.form.submit();" name="deviceHistory" value="<?php echo (!empty($device['history']) ? $device['history'] : 0); ?>">
</form>
<?php endif; ?>
</td>
<td>
<?php if (!empty($device['mac'])) : ?>
<form method="post" action="">
@ -122,12 +131,6 @@
$partial->render();
//TODO js do main.js
?>
<script>
$(function() {
$("#sortable").sortable();
$("#sortable").disableSelection();
});
</script>
</body>
</html>

View File

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $this->title ?></title>
<title><?php echo $this->title; ?></title>
</head>
<body>
<?php echo $this->content(); ?>