|
<?php defined('SYSPATH') or die('No direct script access.');
|
|
/*
|
|
* This file is part of open source system FreeNetIS
|
|
* and it is release under GPLv3 licence.
|
|
*
|
|
* More info about licence can be found:
|
|
* http://www.gnu.org/licenses/gpl-3.0.html
|
|
*
|
|
* More info about project can be found:
|
|
* http://www.freenetis.org/
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* Port
|
|
*
|
|
* @package Model
|
|
*
|
|
* @property integer $id
|
|
* @property integer $device_id
|
|
* @property Device_Model $device
|
|
* @property integer $segment_id
|
|
* @property Device_Model $segment
|
|
* @property integer $port_nr
|
|
* @property string $name
|
|
* @property ORM_Iterator $vlans
|
|
*/
|
|
class Port_Model extends ORM
|
|
{
|
|
protected $belongs_to = array('segment', 'device');
|
|
protected $has_and_belongs_to_many = array('vlans');
|
|
|
|
/**
|
|
* Gets count of ports of device
|
|
*
|
|
* @param integer $device_id
|
|
* @return integer
|
|
*/
|
|
public function count_ports_of_device($device_id)
|
|
{
|
|
return $this->db->where('device_id', $device_id)->count_records('ports');
|
|
}
|
|
|
|
/**
|
|
* Gets count off all ports
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function count_all_ports()
|
|
{
|
|
return $this->db->count_records('ports');
|
|
}
|
|
|
|
/**
|
|
* Gets all ports
|
|
*
|
|
* @param integer $limit_from
|
|
* @param integer $limit_results
|
|
* @param string $order_by
|
|
* @param string $order_by_direction
|
|
* @return Mysql_Result
|
|
*/
|
|
public function get_all_ports(
|
|
$limit_from = 0, $limit_results = 50,
|
|
$order_by = 'ports.id', $order_by_direction = 'ASC')
|
|
{
|
|
// order by direction check
|
|
if (strtolower($order_by_direction) != 'desc')
|
|
{
|
|
$order_by_direction = 'asc';
|
|
}
|
|
// query
|
|
return $this->db->query("
|
|
SELECT devices.name as device_name,segments.name as segment_name,
|
|
IFNULL(ports_vlans.vlan_count,0) AS vlan_count, ports.id,ports.name,
|
|
ports.port_nr
|
|
FROM ports
|
|
LEFT JOIN devices ON devices.id = ports.device_id
|
|
LEFT JOIN segments ON segments.id = ports.segment_id
|
|
LEFT JOIN (
|
|
SELECT COUNT(*) as vlan_count,port_id
|
|
FROM ports_vlans
|
|
GROUP BY port_id
|
|
) ports_vlans ON ports_vlans.port_id=ports.id
|
|
ORDER BY " . $this->db->escape_column($order_by) . " $order_by_direction
|
|
LIMIT " . intval($limit_from) . ", " . intval($limit_results) . "
|
|
");
|
|
}
|
|
|
|
/**
|
|
* Gets ports of device
|
|
*
|
|
* @param integer $device_id
|
|
* @return integer
|
|
*/
|
|
public function get_ports_of_device($device_id)
|
|
{
|
|
return $this->db->query("
|
|
SELECT ports.id AS port_id, ports.name AS port_name,
|
|
devices.name as device_name,ports.segment_id, segments.name as segment_name,
|
|
IFNULL(ports_vlans.vlan_count,0) AS vlan_count, ports.id,ports.name,
|
|
ports.port_nr
|
|
FROM ports
|
|
LEFT JOIN devices ON devices.id = ports.device_id
|
|
LEFT JOIN segments ON segments.id = ports.segment_id
|
|
LEFT JOIN (
|
|
SELECT COUNT(*) as vlan_count,port_id
|
|
FROM ports_vlans
|
|
GROUP BY port_id
|
|
) ports_vlans ON ports_vlans.port_id=ports.id
|
|
WHERE device_id=?
|
|
ORDER BY port_nr ASC
|
|
", $device_id);
|
|
}
|
|
|
|
/**
|
|
* Function deletes port and vlan relation from pivot table.
|
|
*
|
|
* @param integer $port_id
|
|
*/
|
|
public function delete_from_pivot_table($port_id)
|
|
{
|
|
$this->db->query("
|
|
DELETE FROM ports_vlans
|
|
WHERE port_id = ?
|
|
", $port_id);
|
|
}
|
|
|
|
/**
|
|
* Checks whether exists port number in device
|
|
*
|
|
* @author Michal Kliment
|
|
* @param integer $port_nr
|
|
* @param integer $device_id
|
|
* @return boolean
|
|
*/
|
|
public function port_number_exists ($port_nr, $device_id)
|
|
{
|
|
$result = $this->db->query("
|
|
SELECT COUNT(*) AS total
|
|
FROM ports p
|
|
WHERE p.port_nr = ? AND p.device_id = ?
|
|
", array($port_nr, $device_id));
|
|
|
|
if ($result && $result->current())
|
|
return (bool) $result->current()->total;
|
|
else
|
|
return false;
|
|
}
|
|
}
|