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

105 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2006-12-11 21:36:44 +01:00
<?php
class Vlan {
private $inDB = false;
public $id;
public $trunkid;
public $flags;
public $vlannum;
public $bandwidth;
public $mac = "00:00:00:00:00:00";
2006-12-22 00:06:05 +01:00
public $rrdpath;
public $fullname;
2006-12-11 21:36:44 +01:00
private function fromRow($row) {
$retval = new Vlan();
if ($row) {
$retval->inDB = true;
$retval->id = (int)$row['id'];
$retval->trunkid = (int)$row['trunkid'];
$retval->flags = (int)$row['flags'];
$retval->vlannum = (int)$row['vlannum'];
$retval->bandwidth = (int)$row['bandwidth'];
$retval->mac = (string)$row['mac'];
2006-12-22 00:06:05 +01:00
$retval->rrdpath = (string)$row['rrdpath'];
$retval->fullname = (string)$row['fullname'];
2006-12-11 21:36:44 +01:00
}
return $retval;
}
private function toSql() {
return "trunkid = '{$this->trunkid}',
flags = '{$this->flags}',
vlannum = '{$this->vlannum}',
bandwidth = '{$this->bandwidth}',
mac = '".mysql_real_escape_string($this->mac)."'";
}
function load($id) {
$dbh = Database::getInstance();
2006-12-22 00:06:05 +01:00
$sql = "SELECT v.id, v.trunkid, v.flags, v.vlannum, v.bandwidth, v.mac,
IF (v.vlannum = 1,
CONCAT('bridge-', b.id, '/vlan-', t.name, '.rrd'),
CONCAT('bridge-', b.id, '/vlan-', t.name, '.', v.vlannum, '.rrd')
) AS rrdpath,
IF (v.vlannum = 1,
CONCAT(b.name, ': ', t.name),
CONCAT(b.name, ': ', t.name, '.', v.vlannum)
) AS fullname
FROM vlan v, trunk t, bridge b
WHERE v.id = '{$id}'
AND t.id = v.trunkid
AND b.id = t.bridgeid";
2006-12-11 21:36:44 +01:00
$dbh->query($sql);
return self::fromRow($dbh->fetch_assoc());
}
function save() {
$dbh = Database::getInstance();
if ($this->inDB) {
$sql = "UPDATE vlan SET ".$this->toSql()."
WHERE id = '{$this->id}'";
$dbh->query($sql);
} else {
$sql = "INSERT vlan SET ".$this->toSql();
$dbh->query($sql);
$this->iNDB = true;
$this->id = $dbh->insert_id();
}
}
function delete($id) {
$dbh = Database::getInstance();
$sql = "DELETE FROM vlan
WHERE id = '{$id}'";
$dbh->query($sql);
}
function getAll($trunkid = false) {
$dbh = Database::getInstance();
2006-12-22 00:06:05 +01:00
$sql = "SELECT v.id, v.trunkid, v.flags, v.vlannum, v.bandwidth, v.mac,
CONCAT('bridge-', b.id, '/vlan-', t.name, '.', v.vlannum, '.rrd') as rrdpath,
CONCAT(t.name, '.', v.vlannum) as fullname
FROM vlan v, trunk t, bridge b
WHERE ".($trunkid ? "trunkid = '{$trunkid}'" : "1")."
AND t.id = v.trunkid
AND b.id = t.bridgeid
ORDER BY b.name, t.name, v.vlannum";
2006-12-11 21:36:44 +01:00
$dbh->query($sql);
$retval = array();
while ($row = $dbh->fetch_assoc())
$retval[$row['id']] = self::fromRow($row);
return $retval;
}
}
?>