bcast-bridge/web/include/db_Hlsw.class.php

93 lines
2.0 KiB
PHP

<?php
class Hlsw {
private $inDB = false;
public $id;
public $vlanid;
public $flags;
public $ip = "0.0.0.0";
public $name;
public $rrdpath;
private function fromRow($row) {
$retval = new Hlsw();
if ($row) {
$retval->inDB = true;
$retval->id = (int)$row['id'];
$retval->vlanid = (int)$row['vlanid'];
$retval->flags = (int)$row['flags'];
$retval->ip = (string)$row['ip'];
$retval->name = (string)$row['name'];
$retval->rrdpath = (string)$row['rrdpath'];
}
return $retval;
}
private function toSql() {
return "vlanid = '{$this->vlanid}',
flags = '{$this->flags}',
ip = INET_ATON('".mysql_real_escape_string($this->ip)."'),
name = '".mysql_real_escape_string($this->name)."'";
}
function load($id) {
$dbh = Database::getInstance();
$sql = "SELECT h.id, h.vlanid, h.flags, INET_NTOA(h.ip) AS ip, h.name,
CONCAT('bridge-', b.id, '/hlsw-', h.id, '.rrd') AS rrdpath
FROM hlsw h, vlan v, trunk t, bridge b
WHERE h.id = '{$id}'
AND v.id = h.vlanid
AND t.id = v.trunkid
AND b.id = t.bridgeid";
$dbh->query($sql);
return self::fromRow($dbh->fetch_assoc());
}
function save() {
$dbh = Database::getInstance();
if ($this->inDB) {
$sql = "UPDATE hlsw SET ".$this->toSql()."
WHERE id = '{$this->id}'";
$dbh->query($sql);
} else {
$sql = "INSERT hlsw SET ".$this->toSql();
$dbh->query($sql);
$this->iNDB = true;
$this->id = $dbh->insert_id();
}
}
function delete($id) {
$dbh = Database::getInstance();
$sql = "DELETE FROM hlsw
WHERE id = '{$id}'";
$dbh->query($sql);
}
function getAll() {
$dbh = Database::getInstance();
$sql = "SELECT h.id, h.vlanid, h.flags, INET_NTOA(h.ip) AS ip, h.name,
CONCAT('bridge-', b.id, '/hlsw-', h.id, '.rrd') AS rrdpath
FROM hlsw h, vlan v, trunk t, bridge b
WHERE v.id = h.vlanid
AND t.id = v.trunkid
AND b.id = t.bridgeid
ORDER BY h.ip";
$dbh->query($sql);
$retval = array();
while ($row = $dbh->fetch_assoc())
$retval[$row['id']] = self::fromRow($row);
return $retval;
}
}
?>