bcast-bridge/web/include/db_Trunk.class.php
2006-12-11 21:36:44 +01:00

86 lines
1.6 KiB
PHP

<?php
class Trunk {
private $inDB = false;
public $id;
public $bridgeid;
public $flags;
public $name;
private function fromRow($row) {
$retval = new Trunk();
if ($row) {
$retval->inDB = true;
$retval->id = (int)$row['id'];
$retval->bridgeid = (int)$row['bridgeid'];
$retval->flags = (int)$row['flags'];
$retval->name = (string)$row['name'];
}
return $retval;
}
private function toSql() {
return "bridgeid = '{$this->bridgeid}',
flags = '{$this->flags}',
name = '".mysql_real_escape_string($this->name)."'";
}
function load($id) {
$dbh = Database::getInstance();
$sql = "SELECT id, bridgeid, flags, name
FROM trunk
WHERE id = '{$id}'";
$dbh->query($sql);
return self::fromRow($dbh->fetch_assoc());
}
function save() {
$dbh = Database::getInstance();
if ($this->inDB) {
$sql = "UPDATE trunk SET ".$this->toSql()."
WHERE id = '{$this->id}'";
$dbh->query($sql);
} else {
$sql = "INSERT trunk SET ".$this->toSql();
$dbh->query($sql);
$this->iNDB = true;
$this->id = $dbh->insert_id();
}
}
function delete($id) {
$dbh = Database::getInstance();
$sql = "DELETE FROM trunk
WHERE id = '{$id}'";
$dbh->query($sql);
$vlans = Vlan::getAll($id);
foreach ($vlans as $vlan)
Vlan::delete($vlan->id);
}
function getAll($bridgeid = false) {
$dbh = Database::getInstance();
$sql = "SELECT id, bridgeid, flags, name
FROM trunk
".($bridgeid ? "WHERE bridgeid = '{$bridgeid}'" : "")."
ORDER BY name";
$dbh->query($sql);
$retval = array();
while ($row = $dbh->fetch_assoc())
$retval[$row['id']] = self::fromRow($row);
return $retval;
}
}
?>