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

91 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2006-12-11 21:36:44 +01:00
<?php
class Trunk {
private $inDB = false;
public $id;
public $bridgeid;
public $flags;
public $name;
2006-12-22 00:06:05 +01:00
public $fullname;
2006-12-11 21:36:44 +01:00
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'];
2006-12-22 00:06:05 +01:00
$retval->fullname = (string)$row['fullname'];
2006-12-11 21:36:44 +01:00
}
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();
2006-12-22 00:06:05 +01:00
$sql = "SELECT t.id, t.bridgeid, t.flags, t.name,
CONCAT(b.name, ': ', t.name) AS fullname
FROM trunk t, bridge b
WHERE id = '{$id}'
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 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();
2006-12-22 00:06:05 +01:00
$sql = "SELECT t.id, t.bridgeid, t.flags, t.name,
CONCAT(b.name, ': ', t.name) AS fullname
FROM trunk t, bridge b
WHERE ".($bridgeid ? "bridgeid = '{$bridgeid}'" : "1")."
AND b.id = t.bridgeid
ORDER BY b.name, t.name";
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;
}
}
?>