$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]; } } // 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(); 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 = $model->data; if ($model->id) { $data['edit'] = time(); $db->update("RaspberryDisplay", $data, "id=" . (int)$model->id); } else { $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; } }