raspberrydisplay v2 init
This commit is contained in:
@@ -6,8 +6,12 @@ class RaspberryDisplayModel
|
||||
public $hostname;
|
||||
public $ip_address;
|
||||
public $display_url;
|
||||
public $auto_refresh_enabled;
|
||||
public $margin_hot_fix_enabled;
|
||||
public $group_name;
|
||||
public $group_order;
|
||||
public $monitor_size;
|
||||
public $hdmi_port;
|
||||
public $agent_port;
|
||||
public $custom_style;
|
||||
|
||||
public function __construct($data = [])
|
||||
{
|
||||
@@ -22,7 +26,7 @@ class RaspberryDisplayModel
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select("RaspberryDisplay", "*", "id = $id");
|
||||
$res = $db->select("RaspberryDisplay", "*", "id = " . (int)$id);
|
||||
if ($db->num_rows($res)) {
|
||||
return new RaspberryDisplay($db->fetch_object($res));
|
||||
}
|
||||
@@ -33,28 +37,97 @@ class RaspberryDisplayModel
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select("RaspberryDisplay", "*", "hostname = '$hostname'");
|
||||
//fetch 2 rows
|
||||
$res = $db->select("RaspberryDisplay", "*", "hostname = '" . $db->real_escape_string($hostname) . "'");
|
||||
$items = [];
|
||||
|
||||
if ($db->num_rows($res)) {
|
||||
while ($data = $db->fetch_object($res)) {
|
||||
$items[] = new RaspberryDisplay($data);
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
public static function getByIpAddress($ipAddress)
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select("RaspberryDisplay", "*", "ip_address = '" . $db->real_escape_string($ipAddress) . "'");
|
||||
$items = [];
|
||||
|
||||
if ($db->num_rows($res)) {
|
||||
while ($data = $db->fetch_object($res)) {
|
||||
$items[] = new RaspberryDisplay($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
public static function getByGroup($groupName)
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select(
|
||||
"RaspberryDisplay",
|
||||
"*",
|
||||
"group_name = '" . $db->real_escape_string($groupName) . "'",
|
||||
"group_order ASC"
|
||||
);
|
||||
$items = [];
|
||||
|
||||
if ($db->num_rows($res)) {
|
||||
while ($data = $db->fetch_object($res)) {
|
||||
$items[] = new RaspberryDisplay($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
public static function getGroups()
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->query("SELECT DISTINCT group_name FROM RaspberryDisplay ORDER BY group_name ASC");
|
||||
$groups = [];
|
||||
|
||||
if ($db->num_rows($res)) {
|
||||
while ($data = $db->fetch_object($res)) {
|
||||
$groups[] = $data->group_name;
|
||||
}
|
||||
}
|
||||
return $groups;
|
||||
}
|
||||
|
||||
public static function create(array $data)
|
||||
{
|
||||
$model = new RaspberryDisplay();
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
if (property_exists(get_called_class(), $field)) {
|
||||
$model->$field = $value;
|
||||
$fields = [
|
||||
'display_label', 'hostname', 'ip_address', 'display_url',
|
||||
'group_name', 'group_order', 'monitor_size', 'hdmi_port',
|
||||
'agent_port', 'custom_style'
|
||||
];
|
||||
|
||||
foreach ($fields as $field) {
|
||||
if (isset($data[$field])) {
|
||||
$model->$field = $data[$field];
|
||||
}
|
||||
}
|
||||
|
||||
// Set defaults
|
||||
if ($model->group_order === null) {
|
||||
$model->group_order = 0;
|
||||
}
|
||||
if ($model->monitor_size === null) {
|
||||
$model->monitor_size = '27';
|
||||
}
|
||||
if ($model->hdmi_port === null) {
|
||||
$model->hdmi_port = 0;
|
||||
}
|
||||
if ($model->agent_port === null) {
|
||||
$model->agent_port = 5000;
|
||||
}
|
||||
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
|
||||
@@ -74,14 +147,19 @@ class RaspberryDisplayModel
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->select("RaspberryDisplay", "id, display_label, hostname, ip_address,custom_style, display_url, auto_refresh_enabled, margin_hot_fix_enabled");
|
||||
$res = $db->select(
|
||||
"RaspberryDisplay",
|
||||
"id, display_label, hostname, ip_address, display_url, group_name, group_order, monitor_size, hdmi_port, agent_port, custom_style",
|
||||
"",
|
||||
"group_name ASC, group_order ASC"
|
||||
);
|
||||
|
||||
if ($db->num_rows($res)) {
|
||||
while ($data = $db->fetch_object($res)) {
|
||||
$items[] = new RaspberryDisplay($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
|
||||
}
|
||||
|
||||
public static function save(RaspberryDisplay $model)
|
||||
@@ -91,13 +169,57 @@ class RaspberryDisplayModel
|
||||
$data = $model->data;
|
||||
|
||||
if ($model->id) {
|
||||
$db->update("RaspberryDisplay", $data, "id=" . $model->id);
|
||||
$data['edit'] = time();
|
||||
$db->update("RaspberryDisplay", $data, "id=" . (int)$model->id);
|
||||
} else {
|
||||
$model->create = date("U");
|
||||
$model->edit = date("U");
|
||||
$data['create'] = time();
|
||||
$data['edit'] = time();
|
||||
$model->id = $db->insert("RaspberryDisplay", $data);
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
|
||||
public static function delete($id)
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
$db->delete("RaspberryDisplay", "id = " . (int)$id);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function updateOrder(array $orders)
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
foreach ($orders as $order) {
|
||||
$id = (int)$order['id'];
|
||||
$groupName = $db->real_escape_string($order['group_name']);
|
||||
$groupOrder = (int)$order['group_order'];
|
||||
|
||||
$db->update("RaspberryDisplay", [
|
||||
'group_name' => $groupName,
|
||||
'group_order' => $groupOrder,
|
||||
'edit' => time()
|
||||
], "id = $id");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function getMaxOrderInGroup($groupName)
|
||||
{
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$res = $db->query(
|
||||
"SELECT MAX(group_order) as max_order FROM RaspberryDisplay WHERE group_name = '" .
|
||||
$db->real_escape_string($groupName) . "'"
|
||||
);
|
||||
|
||||
if ($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
return (int)($data->max_order ?? 0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user