Files
thetool/application/RaspberryDisplay/RaspberryDisplayModel.php
2026-02-03 15:52:55 +01:00

240 lines
6.5 KiB
PHP

<?php
class RaspberryDisplayModel
{
public $display_label;
public $hostname;
public $ip_address;
public $display_url;
public $group_name;
public $group_order;
public $monitor_size;
public $hdmi_port;
public $agent_port;
public $custom_style;
public function __construct($data = [])
{
foreach ($data as $field => $value) {
if (property_exists(get_called_class(), $field)) {
$this->$field = $value;
}
}
}
public static function get($id)
{
$db = FronkDB::singleton();
$res = $db->select("RaspberryDisplay", "*", "id = " . (int)$id);
if ($db->num_rows($res)) {
return new RaspberryDisplay($db->fetch_object($res));
}
return null;
}
public static function getByHostname($hostname)
{
$db = FronkDB::singleton();
$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;
}
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();
$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];
}
}
if ($model->group_order === null) {
$model->group_order = 0;
}
if (!in_array($model->monitor_size, ['27', '42', '55', '65'])) {
$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();
if ($model->create_by === null) {
$model->create_by = $me->id;
}
if ($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll()
{
$items = [];
$db = FronkDB::singleton();
$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)
{
$db = FronkDB::singleton();
$data = [
'display_label' => $model->display_label,
'hostname' => $model->hostname,
'ip_address' => $model->ip_address,
'display_url' => $model->display_url,
'group_name' => $model->group_name,
'group_order' => (int)$model->group_order,
'monitor_size' => in_array($model->monitor_size, ['27', '42', '55', '65']) ? $model->monitor_size : '27',
'hdmi_port' => (int)$model->hdmi_port,
'agent_port' => (int)$model->agent_port,
'custom_style' => $model->custom_style,
'create_by' => $model->create_by,
'edit_by' => $model->edit_by,
];
$forceStr = ['monitor_size'];
if ($model->id) {
$data['edit'] = time();
$db->update("RaspberryDisplay", $data, "id=" . (int)$model->id, $forceStr);
} else {
$data['create'] = time();
$data['edit'] = time();
$model->id = $db->insert("RaspberryDisplay", $data, $forceStr);
}
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;
}
}