Static class Fix

This commit is contained in:
JonatanRek 2020-04-21 19:44:30 +02:00
parent ba836c9573
commit ee65ea43c4
8 changed files with 66 additions and 66 deletions

View File

@ -3,21 +3,21 @@
class AutomationManager{
public static $automation;
public function remove($automationId) {
public static function remove($automationId) {
return Db::command ('DELETE FROM automation WHERE automation_id=?', array ($automationId));
}
public function deactive($automationId) {
public static function deactive($automationId) {
$automation = Db::loadOne ("SELECT * FROM automation WHERE automation_id=?" , array ($automationId));
$flipedValue = ($automation['active'] == 1 ? 0 : 1);
return Db::command ('UPDATE automation SET active = ? WHERE automation_id=?', array ($flipedValue,$automationId));
}
public function restart($automationId) {
public static function restart($automationId) {
return Db::command ('UPDATE automation SET executed = 0 WHERE automation_id=?', array ($automationId));
}
public function create ($name, $onDays, $doCode, $ifCode, $automationId = "") {
public static function create ($name, $onDays, $doCode, $ifCode, $automationId = "") {
$userId = UserManager::getUserData('user_id');
$scene = array (
'name' => $name,
@ -38,12 +38,12 @@ class AutomationManager{
}
}
public function getAll(){
public static function getAll(){
return Db::loadAll ("SELECT * FROM automation");
}
public function executeAll(){
public static function executeAll(){
global $logManager;
$automations = Db::loadAll ("SELECT * FROM automation");

View File

@ -3,19 +3,19 @@ class DashboardManager{
public static $devices;
function getAllDashboards ($userId) {
static function getAllDashboards ($userId) {
return Db::loadAll ("SELECT * FROM dashboard WHERE user_id=?", array($userId));
}
function getAllSubDevices ($userId) {
static function getAllSubDevices ($userId) {
return Db::loadAll ("SELECT * FROM subdevices WHERE subdevice_id IN (SELECT subdevice_id FROM dashboard WHERE user_id=?)", array($userId));
}
function getSubDevice ($userId, $subDeviceId) {
static function getSubDevice ($userId, $subDeviceId) {
return Db::loadOne ("SELECT * FROM subdevices WHERE subdevice_id = (SELECT subdevice_id FROM dashboard WHERE user_id=? AND subdevice_id = ? )", array($userId, $subDeviceId));
}
function Add ($subDeviceId) {
static function Add ($subDeviceId) {
if (self::getSubDevice(UserManager::getUserData('user_id'), $subDeviceId) == null){
// to do: pokud existuje nepridej
@ -34,7 +34,7 @@ class DashboardManager{
}
}
function Remove ($subDeviceId){
static function Remove ($subDeviceId){
$userId = UserManager::getUserData('user_id');
Db::command ('DELETE FROM dashboard WHERE subdevice_id=? AND user_id = ?', array ($subDeviceId, $userId));
}

View File

@ -2,31 +2,31 @@
class DeviceManager{
public static $devices;
function getAllDevices () {
static function getAllDevices () {
return Db::loadAll ("SELECT * FROM devices WHERE approved != ?", Array(2));
}
function getAllDevicesInRoom ($roomId = "") {
static function getAllDevicesInRoom ($roomId = "") {
return Db::loadAll ("SELECT * FROM devices WHERE room_id = ? AND approved != ?", Array($roomId, 2));
}
function getOtherDevices(){
static function getOtherDevices(){
return Db::loadAll ("SELECT * FROM devices WHERE room_id IS NULL ");
}
function getDeviceByToken($deviceToken) {
static function getDeviceByToken($deviceToken) {
return Db::loadOne("SELECT * FROM devices WHERE token = ?", array($deviceToken));
}
function getDeviceByMac($deviceMac) {
static function getDeviceByMac($deviceMac) {
return Db::loadOne("SELECT * FROM devices WHERE mac = ?", array($deviceMac));
}
function getDeviceById($deviceId) {
static function getDeviceById($deviceId) {
return Db::loadOne("SELECT * FROM devices WHERE device_id = ?", array($deviceId));
}
public function create ($name, $token) {
public static function create ($name, $token) {
$defaultRoom = RoomManager::getDefaultRoomId();
$device = array (
'name' => $name,
@ -42,7 +42,7 @@ class DeviceManager{
}
}
public function edit ($deviceId, $values = []) {
public static function edit ($deviceId, $values = []) {
try {
Db::edit ('devices', $values, 'WHERE device_id = ?', array($deviceId));
} catch(PDOException $error) {
@ -51,7 +51,7 @@ class DeviceManager{
}
}
public function editByToken ($token, $values = []) {
public static function editByToken ($token, $values = []) {
try {
Db::edit ('devices', $values, 'WHERE token = ?', array($token));
} catch(PDOException $error) {
@ -65,7 +65,7 @@ class DeviceManager{
* @param [type] $roomId [číslo místnosti do kter se zařízení přiřadit]
* @param [type] $deviceId [Číslo zařízení které chcete přiřadit do místnosti]
*/
public function assignRoom ($roomId, $deviceId) {
public static function assignRoom ($roomId, $deviceId) {
$device = array (
'room_id' => $roomId,
);
@ -81,15 +81,15 @@ class DeviceManager{
* [delete Smazání zařízení]
* @param [type] $deviceId [Id zařízení ke smazání]
*/
public function delete ($deviceId) {
public static function delete ($deviceId) {
Db::command ('DELETE FROM devices WHERE device_id=?', array ($deviceId));
}
public function registeret ($deviceToken) {
public static function registeret ($deviceToken) {
return (count(Db::loadAll ("SELECT * FROM devices WHERE token=?", array($deviceToken))) == 1 ? true : false);
}
public function approved ($deviceToken) {
public static function approved ($deviceToken) {
return (count(Db::loadAll ("SELECT * FROM devices WHERE token=? AND approved = ?", array($deviceToken, 1))) == 1 ? true : false);
}
}

View File

@ -2,17 +2,17 @@
class RoomManager{
public static $rooms;
function getDefaultRoomId() {
static function getDefaultRoomId() {
$defaultRoom = Db::loadOne("SELECT room_id FROM rooms WHERE 'default' = 1");
return $defaultRoom['room_id'];
}
function getAllRooms () {
static function getAllRooms () {
$allRoom = Db::loadAll ("SELECT rooms.*, COUNT(devices.device_id) as device_count FROM rooms LEFT JOIN devices ON (devices.room_id=rooms.room_id) GROUP BY rooms.room_id");
return $allRoom;
}
public function create ($name) {
public static function create ($name) {
$room = array (
'name' => $name,
);
@ -24,7 +24,7 @@ class RoomManager{
}
}
public function delete ($roomId) {
public static function delete ($roomId) {
Db::command ('DELETE FROM rooms WHERE room_id=?', array ($roomId));
}
}

View File

@ -2,7 +2,7 @@
class SceneManager{
public static $scenes;
public function create ($icon, $name, $doCode) {
public static function create ($icon, $name, $doCode) {
$scene = array (
'icon' => $icon,
'name' => $name,
@ -16,15 +16,15 @@ class SceneManager{
}
}
public function getAllScenes () {
public static function getAllScenes () {
return Db::loadAll ("SELECT * FROM scenes");
}
public function getScene ($sceneId) {
public static function getScene ($sceneId) {
return Db::loadOne("SELECT * FROM scenes WHERE scene_id = ?", array($sceneId));
}
public function execScene ($sceneId) {
public static function execScene ($sceneId) {
$sceneData = SceneManager::getScene($sceneId);
$sceneDoJson = $sceneData['do_something'];
$sceneDoArray = json_decode($sceneDoJson);
@ -34,7 +34,7 @@ class SceneManager{
return true;
}
public function delete($sceneId){
public static function delete($sceneId){
Db::command ('DELETE FROM scenes WHERE scene_id=?', array ($sceneId));
}
}

View File

@ -3,17 +3,17 @@ class SubDeviceManager
{
public static $devices;
public function getAllSubDevices($deviceId)
public static function getAllSubDevices($deviceId)
{
return Db::loadAll("SELECT * FROM subdevices WHERE device_id = ?", array($deviceId));
}
public function getSubDeviceMaster($subDeviceId)
public static function getSubDeviceMaster($subDeviceId)
{
return Db::loadOne("SELECT * FROM devices WHERE device_id = (SELECT device_id FROM subdevices WHERE subdevice_id = ?)", array($subDeviceId));
}
public function getSubDeviceByMaster($deviceId, $subDeviceType = null)
public static function getSubDeviceByMaster($deviceId, $subDeviceType = null)
{
if ($subDeviceType == null) {
return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ?;", array($deviceId));
@ -22,7 +22,7 @@ class SubDeviceManager
}
}
public function getSubDeviceByMasterAndType($deviceId, $subDeviceType = null)
public static function getSubDeviceByMasterAndType($deviceId, $subDeviceType = null)
{
if (!empty($subDeviceType)) {
return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ?;", array($deviceId));
@ -31,12 +31,12 @@ class SubDeviceManager
}
}
public function getSubDevice($subDeviceId)
public static function getSubDevice($subDeviceId)
{
return Db::loadOne("SELECT * FROM subdevices WHERE subdevice_id = ?;", array($subDeviceId));
}
public function getSubDevicesTypeForMater($deviceId)
public static function getSubDevicesTypeForMater($deviceId)
{
$parsedTypes = [];
$types = Db::loadAll("SELECT type FROM subdevices WHERE device_id = ?;", array($deviceId));
@ -48,7 +48,7 @@ class SubDeviceManager
//check if dubdevice exist
public function create($deviceId, $type, $unit)
public static function create($deviceId, $type, $unit)
{
$record = array(
'device_id' => $deviceId,
@ -63,7 +63,7 @@ class SubDeviceManager
}
}
public function remove($subDeviceId)
public static function remove($subDeviceId)
{
RecordManager::cleanSubdeviceRecords($subDeviceId);
return Db::loadAll("DELETE FROM subdevices WHERE subdevice_id = ?", array($subDeviceId));

View File

@ -1,7 +1,7 @@
<?php
class UserManager
{
public function getUsers () {
public static function getUsers () {
try {
$allUsers = Db::loadAll ("SELECT user_id, username, at_home, ota FROM users");
return $allUsers;
@ -11,7 +11,7 @@ class UserManager
}
}
public function getUser ($userName) {
public static function getUser ($userName) {
try {
$user = Db::loadOne ("SELECT * FROM users WHERE username = ?", [$userName]);
return $user;
@ -21,7 +21,7 @@ class UserManager
}
}
public function getUserId ($userId) {
public static function getUserId ($userId) {
try {
$user = Db::loadOne ("SELECT * FROM users WHERE user_id = ?", [$userId]);
return $user;
@ -31,7 +31,7 @@ class UserManager
}
}
public function getAvatarUrl($userId = null){
public static function getAvatarUrl($userId = null){
$email = self::getUserData('email');
if ($userId != null){
$email = self::getUserData('email',$userId);
@ -39,12 +39,12 @@ class UserManager
return 'https://secure.gravatar.com/avatar/' . md5( strtolower( trim( $email ) ) );
}
public function login ($username, $password, $rememberMe) {
public static function login ($username, $password, $rememberMe) {
try {
if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?)', array ($username))) {
if ($user['password'] == UserManager::getHashPassword($password)) {
if (isset($rememberMe) && $rememberMe == 'true') {
setcookie ("rememberMe", $this->setEncryptedCookie($user['username']), time () + (30 * 24 * 60 * 60 * 1000), BASEDIR, $_SERVER['HTTP_HOST'], 1);
setcookie ("rememberMe", self::setEncryptedCookie($user['username']), time () + (30 * 24 * 60 * 60 * 1000), BASEDIR, $_SERVER['HTTP_HOST'], 1);
}
$_SESSION['user']['id'] = $user['user_id'];
$page = "";
@ -65,7 +65,7 @@ class UserManager
}
}
public function loginNew ($username, $password) {
public static function loginNew ($username, $password) {
try {
if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?)', array ($username))) {
if ($user['password'] == UserManager::getHashPassword($password)) {
@ -83,12 +83,12 @@ class UserManager
}
}
public function isLogin () {
public static function isLogin () {
if (isset ($_SESSION['user']) && isset($_SESSION['user']['id'])) {
return true;
} else {
if (isset ($_COOKIE['rememberMe'])){
if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?)', array ($this->getDecryptedCookie($_COOKIE['rememberMe'])))) {
if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?)', array (self::getDecryptedCookie($_COOKIE['rememberMe'])))) {
$_SESSION['user']['id'] = $user['user_id'];
return true;
}
@ -97,7 +97,7 @@ class UserManager
return false;
}
public function logout () {
public static function logout () {
unset($_SESSION['user']);
session_destroy();
if (isset($_COOKIE['rememberMe'])){
@ -106,7 +106,7 @@ class UserManager
}
}
public function setEncryptedCookie($value){
public static function setEncryptedCookie($value){
$first_key = base64_decode(FIRSTKEY);
$second_key = base64_decode(SECONDKEY);
@ -119,7 +119,7 @@ class UserManager
return $newvalue;
}
public function getDecryptedCookie($value){
public static function getDecryptedCookie($value){
$first_key = base64_decode(FIRSTKEY);
$second_key = base64_decode(SECONDKEY);
@ -145,19 +145,19 @@ class UserManager
return $user[$type];
}
public function setUserData ($type, $value) {
public static function setUserData ($type, $value) {
if (isset ($_SESSION['user']['id'])) {
Db::command ('UPDATE users SET ' . $type . '=? WHERE user_id=?', array ($value, $_SESSION['user']['id']));
}
}
public function getHashPassword ($password) {
public static function getHashPassword ($password) {
$salt = "s0mRIdlKvI";
$hashPassword = hash('sha512', ($password . $salt));
return $hashPassword;
}
public function atHome($userId, $atHome){
public static function atHome($userId, $atHome){
try {
Db::edit ('users', ['at_home' => $atHome], 'WHERE user_id = ?', array($userId));
} catch(PDOException $error) {
@ -166,7 +166,7 @@ class UserManager
}
}
public function changePassword($oldPassword, $newPassword, $newPassword2){
public static function changePassword($oldPassword, $newPassword, $newPassword2){
if ($newPassword == $newPassword2) {
//Password Criteria
$oldPasswordSaved = self::getUserData('password');
@ -180,7 +180,7 @@ class UserManager
}
}
public function createUser($userName, $password){
public static function createUser($userName, $password){
$userId = Db::loadOne('SELECT * FROM users WHERE username = ?;', array($userName))['user_id'];
if ($userId != null) {
return false;
@ -197,8 +197,8 @@ class UserManager
}
}
public function haveOtaEnabled($userName){
$ota = $this->getUser($userName)['ota'];
public static function haveOtaEnabled($userName){
$ota = self::getUser($userName)['ota'];
if ($ota != ''){
return ($ota != '' ? $ota : false);

View File

@ -4,7 +4,7 @@
*/
class Utilities
{
function cleanString($text) {
static function cleanString($text) {
$utf8 = array(
'/[áàâãªä]/u' => 'a',
'/[ÁÀÂÃÄ]/u' => 'A',
@ -36,7 +36,7 @@ class Utilities
return preg_replace(array_keys($utf8), array_values($utf8), $text);
}
function stringInsert($str,$insertstr,$pos)
static function stringInsert($str,$insertstr,$pos)
{
$str = substr($str, 0, $pos) . $insertstr . substr($str, $pos);
return $str;
@ -50,7 +50,7 @@ class Utilities
* @return [type] [description]
*/
function generateGraphJson(string $type = 'line', array $data = [], array $options = []){
static function generateGraphJson(string $type = 'line', array $data = [], array $options = []){
$array = [
'type' => $type,
'data' => [
@ -94,7 +94,7 @@ class Utilities
return json_encode($array, JSON_PRETTY_PRINT);
}
function ago( $datetime )
static function ago( $datetime )
{
$interval = date_create('now')->diff( $datetime );
$suffix = ( $interval->invert ? ' ago' : '' );
@ -105,12 +105,12 @@ class Utilities
return self::pluralize( $interval->s, 'second' ) . $suffix;
}
function pluralize( $count, $text )
static function pluralize( $count, $text )
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
function checkOperator($value1, $operator, $value2) {
static function checkOperator($value1, $operator, $value2) {
switch ($operator) {
case '<': // Less than
return $value1 < $value2;