Files
thetool/application/RaspberryDisplay/RaspberryDisplayModel.php

103 lines
2.5 KiB
PHP

<?php
class RaspberryDisplayModel
{
public $display_label;
public $hostname;
public $ip_address;
public $display_url;
public $auto_refresh_enabled;
public $margin_hot_fix_enabled;
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 = $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 = '$hostname'");
//fetch 2 rows
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new RaspberryDisplay($data);
}
return $items;
}
}
public static function create(array $data)
{
$model = new RaspberryDisplay();
foreach ($data as $field => $value) {
if (property_exists(get_called_class(), $field)) {
$model->$field = $value;
}
}
$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,custom_style, display_url, auto_refresh_enabled, margin_hot_fix_enabled");
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) {
$db->update("RaspberryDisplay", $data, "id=" . $model->id);
} else {
$model->create = date("U");
$model->edit = date("U");
$model->id = $db->insert("RaspberryDisplay", $data);
}
return $model;
}
}