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('/automation', 'Automation');
$router->any('/setting', 'Setting'); $router->any('/setting', 'Setting');
$router->any('/device', 'Device'); $router->any('/device', 'Device');
$router->get('/device/{sortBy}/{sortType}', 'Device'); $router->any('/device/{sortBy}/{sortType}', 'Device');
$router->any('/plugins', 'Plugins'); $router->any('/plugins', 'Plugins');
$router->any('/ajax', 'Ajax'); $router->any('/ajax', 'Ajax');
$router->any('/oauth', 'Oauth'); $router->any('/oauth', 'Oauth');

View File

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

View File

@ -1,6 +1,7 @@
<?php <?php
if (!empty ($_POST)){ if (!empty ($_POST)){
$deviceManager = new DeviceManager (); $deviceManager = new DeviceManager ();
$subDeviceManager = new SubDeviceManager ();
if (!empty ($_FILES['deviceFirmware']) && !empty ($_FILES['deviceFirmware']['tmp_name']) && !empty ($_POST['deviceId'])) { if (!empty ($_FILES['deviceFirmware']) && !empty ($_FILES['deviceFirmware']['tmp_name']) && !empty ($_POST['deviceId'])) {
$file = $_FILES['deviceFirmware']; $file = $_FILES['deviceFirmware'];
$deviceMac = $deviceManager->getDeviceById ($_POST['deviceId'])['mac']; $deviceMac = $deviceManager->getDeviceById ($_POST['deviceId'])['mac'];
@ -27,6 +28,9 @@ if (!empty ($_POST)){
if (!empty ($_POST['deviceName']) && !empty ($_POST['deviceId'])) { if (!empty ($_POST['deviceName']) && !empty ($_POST['deviceId'])) {
$deviceManager->edit ($_POST['deviceId'], array ('name' => $_POST['deviceName'])); $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(); die();
} }

View File

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

View File

@ -19,6 +19,12 @@ class RoomManager{
return $allRoom; 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) { public static function create ($name) {
$room = array ( $room = array (
'name' => $name, '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) public static function remove($subDeviceId)
{ {
RecordManager::cleanSubdeviceRecords($subDeviceId); RecordManager::cleanSubdeviceRecords($subDeviceId);

View File

@ -28,7 +28,8 @@ class Device extends Template
"token" => "token", "token" => "token",
"signal" => "signal", "signal" => "signal",
"firmware" => "firmware_hash", "firmware" => "firmware_hash",
"icon" => "icon" "icon" => "icon",
"history" => "history",
]; ];
$sortIcons = [ $sortIcons = [
@ -47,12 +48,17 @@ class Device extends Template
$sortBy = "id"; $sortBy = "id";
$sortType = "DESC"; $sortType = "DESC";
} }
$template->prepare('sortIcon', [$sortBy => $sortIcons[$sortType]]); $template->prepare('sortIcon', [$sortBy => $sortIcons[$sortType]]);
foreach ($devices as $key => $device) { foreach ($devices as $key => $device) {
//Signal Stenght //Signal Stenght
$subdevice = $subDeviceManager->getSubDeviceByMasterAndType($device['device_id'], "wifi"); $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'] = ""; $devices[$key]['signal'] = "";
if (!empty($subdevice['subdevice_id'])) { if (!empty($subdevice['subdevice_id'])) {
$record = $recordManager->getLastRecord($subdevice['subdevice_id']); $record = $recordManager->getLastRecord($subdevice['subdevice_id']);

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/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> <a href="device/token/<?php echo $SORTTYPE; ?>">Token</a><i class="fa"><?php echo (!empty($SORTICON['token']) ? $SORTICON['token'] : ""); ?></i>
</th> </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 <th>Action
<form method="post" action=""> <form method="post" action="">
<button class="fa custom-file-input" type="submit" name="deviceCommand" value="reset" title="Reset All"><b>&#xf01e;</b></button> <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> <td><?php echo (!empty($device['mac']) ? $device['mac'] : ""); ?><br>
<?php echo (!empty($device['ip_address']) ? $device['ip_address'] : ""); ?><br> <?php echo (!empty($device['ip_address']) ? $device['ip_address'] : ""); ?><br>
<?php echo (!empty($device['token']) ? $device['token'] : ""); ?></td> <?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> <td>
<?php if (!empty($device['mac'])) : ?> <?php if (!empty($device['mac'])) : ?>
<form method="post" action=""> <form method="post" action="">
@ -122,12 +131,6 @@
$partial->render(); $partial->render();
//TODO js do main.js //TODO js do main.js
?> ?>
<script>
$(function() {
$("#sortable").sortable();
$("#sortable").disableSelection();
});
</script>
</body> </body>
</html> </html>

View File

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