Revize 1409
Přidáno uživatelem Ondřej Fibich před asi 12 roky(ů)
freenetis/branches/network/application/models/ports_vlan.php | ||
---|---|---|
<?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/
|
||
*
|
||
*/
|
||
|
||
/**
|
||
* Ports vlan pivot table
|
||
*
|
||
* @package Model
|
||
*
|
||
* @property integer $id
|
||
* @property integer $port_id
|
||
* @property Port_Model $port
|
||
* @property integer $vlan_id
|
||
* @property Vlan_Model $vlan
|
||
*/
|
||
class Ports_vlan_Model extends ORM
|
||
{
|
||
protected $belongs_to = array('port', 'vlan');
|
||
}
|
freenetis/branches/network/application/models/vlan.php | ||
---|---|---|
{
|
||
// order by direction check
|
||
$order_by_direction = strtolower($order_by_direction);
|
||
|
||
if ($order_by_direction != 'desc')
|
||
{
|
||
$order_by_direction = 'asc';
|
||
}
|
||
|
||
// query
|
||
return $this->db->query("
|
||
SELECT v.*, COUNT(d.id) AS devices_count,
|
||
GROUP_CONCAT(d.name SEPARATOR ', \n') AS devices
|
||
SELECT v.*, COUNT(DISTINCT d.id) AS devices_count,
|
||
GROUP_CONCAT(DISTINCT d.name SEPARATOR ', \n') AS devices
|
||
FROM vlans v
|
||
LEFT JOIN
|
||
(
|
||
SELECT i.device_id, vi.vlan_id
|
||
FROM vlan_ifaces vi
|
||
JOIN ifaces i ON vi.iface_id = i.id
|
||
UNION
|
||
SELECT p.device_id, pv.vlan_id
|
||
FROM ports_vlans pv
|
||
JOIN ports p ON pv.port_id = p.id
|
||
) i ON i.vlan_id = v.id
|
||
LEFT JOIN ifaces_vlans iv ON iv.vlan_id = v.id
|
||
LEFT JOIN ifaces i ON iv.iface_id = i.id
|
||
LEFT JOIN devices d ON i.device_id = d.id
|
||
GROUP BY v.id
|
||
ORDER BY $order_by $order_by_direction
|
||
... | ... | |
* @param integer $vlan_id
|
||
* @return Mysql_Result
|
||
*/
|
||
public function get_devices_of_vlan ($vlan_id = NULL)
|
||
public function get_devices_of_vlan($vlan_id = NULL)
|
||
{
|
||
if (!$vlan_id && isset($this))
|
||
{
|
||
$vlan_id = $this->id;
|
||
}
|
||
|
||
return $this->db->query("
|
||
SELECT
|
||
d.id, d.name, IFNULL(p.ports_count,0) AS ports_count, p.ports,
|
||
IFNULL(d.ip_address, IFNULL(ip1.ip_address,ip2.ip_address)) AS ip_address,
|
||
IFNULL(d.ip_address_id, IFNULL(ip1.id,ip2.id)) AS ip_address_id
|
||
FROM
|
||
(
|
||
SELECT d.*,
|
||
IFNULL(ip1.ip_address,ip2.ip_address) AS ip_address,
|
||
IFNULL(ip1.id,ip2.id) AS ip_address_id
|
||
FROM devices d
|
||
LEFT JOIN ifaces i ON i.device_id = d.id
|
||
LEFT JOIN vlan_ifaces vi ON vi.iface_id = i.id
|
||
LEFT JOIN ip_addresses ip1 ON ip1.vlan_iface_id = vi.id
|
||
LEFT JOIN ip_addresses ip2 ON ip2.iface_id = i.id
|
||
LEFT JOIN ports p ON p.device_id = d.id
|
||
LEFT JOIN ports_vlans pv ON pv.port_id = p.id
|
||
WHERE vi.vlan_id = ? OR pv.vlan_id = ?
|
||
GROUP BY d.id
|
||
) d
|
||
SELECT d.id, d.name, IFNULL(p.ports_count, 0) AS ports_count,
|
||
p.ports, ip.ip_address AS ip_address, ip.id AS ip_address_id
|
||
FROM devices d
|
||
LEFT JOIN ifaces i ON i.device_id = d.id
|
||
LEFT JOIN vlan_ifaces vi ON vi.iface_id = i.id
|
||
LEFT JOIN ip_addresses ip1 ON ip1.iface_id = i.id
|
||
LEFT JOIN ip_addresses ip2 ON ip2.vlan_iface_id = vi.id
|
||
LEFT JOIN ifaces_vlans iv ON iv.iface_id = i.id
|
||
LEFT JOIN ip_addresses ip ON ip.iface_id = iv.iface_id
|
||
LEFT JOIN
|
||
(
|
||
SELECT
|
||
p.device_id, COUNT(*) AS ports_count,
|
||
GROUP_CONCAT(IF(p.port_nr IS NOT NULL, CONCAT(?, p.port_nr), p.name) ORDER BY p.port_nr SEPARATOR ', \n') AS ports
|
||
FROM ports p
|
||
JOIN ports_vlans pv ON pv.port_id = p.id
|
||
WHERE pv.vlan_id = ?
|
||
GROUP BY p.device_id
|
||
SELECT COUNT(*) AS ports_count, i.device_id, GROUP_CONCAT(
|
||
IF(i.number IS NOT NULL, CONCAT(?, i.number), i.name)
|
||
ORDER BY i.number SEPARATOR ', \n'
|
||
) AS ports
|
||
FROM ifaces i
|
||
JOIN ifaces_vlans iv2 ON iv2.iface_id = i.id
|
||
WHERE i.type = ? AND iv2.vlan_id = ?
|
||
GROUP BY i.device_id
|
||
) p ON p.device_id = d.id
|
||
WHERE iv.vlan_id = ?
|
||
GROUP BY d.id
|
||
ORDER BY INET_ATON(IFNULL(d.ip_address, IFNULL(ip1.ip_address,ip2.ip_address)))
|
||
", array($vlan_id, $vlan_id, __('Port').' ', $vlan_id));
|
||
ORDER BY INET_ATON(ip.ip_address)
|
||
", array( __('Port') . ' ', Iface_Model::TYPE_PORT, $vlan_id, $vlan_id));
|
||
}
|
||
|
||
/**
|
||
* Returns all VLAN ports belongs to port
|
||
*
|
||
* @author Michal Kliment
|
||
* @param type $port_id
|
||
* @return type
|
||
* @param integer $port_id
|
||
* @return Mysql_Result
|
||
*/
|
||
public function get_all_vlans_by_port ($port_id)
|
||
public function get_all_vlans_by_port($port_id)
|
||
{
|
||
return $this->db->query("
|
||
SELECT v.*, pv.tagged
|
||
SELECT v.*, iv.tagged
|
||
FROM vlans v
|
||
JOIN ports_vlans pv ON pv.vlan_id = v.id
|
||
JOIN ports p ON pv.port_id = p.id
|
||
WHERE p.id = ?
|
||
", $port_id);
|
||
JOIN ifaces_vlans iv ON iv.vlan_id = v.id
|
||
JOIN ifaces i ON iv.iface_id = i.id
|
||
WHERE i.id = ? AND i.type = ?
|
||
", $port_id, Iface_Model::TYPE_PORT);
|
||
}
|
||
}
|
freenetis/branches/network/application/controllers/vlans.php | ||
---|---|---|
|
||
if ($this->acl_check_new('Devices_Controller', 'vlan'))
|
||
{
|
||
$grid->add_new_button('vlans/add', __('Add new vlan'));
|
||
$grid->add_new_button('vlans/add', 'Add new vlan');
|
||
}
|
||
|
||
$grid->order_field('id')
|
||
... | ... | |
->class('center');
|
||
|
||
$grid->order_field('name')
|
||
->label(__('Vlan name'));
|
||
->label('Vlan name');
|
||
|
||
$grid->order_field('tag_802_1q');
|
||
|
||
$grid->order_callback_field('devices_count')
|
||
->label(__('Devices count'))
|
||
->label('Devices count')
|
||
->callback('callback::devices_field');
|
||
|
||
$actions = $grid->grouped_action_field();
|
||
... | ... | |
}
|
||
|
||
$vlan = new Vlan_Model($vlan_id);
|
||
$ports_vlan_model = new Ports_vlan_Model();
|
||
|
||
if (!$vlan->id)
|
||
{
|
||
... | ... | |
->class('center');
|
||
|
||
$grid->field('name')
|
||
->label(__('Device name'));
|
||
->label('Device name');
|
||
|
||
$grid->callback_field('ports_count')
|
||
->label(__('Ports count'))
|
||
->label('Ports count')
|
||
->callback('callback::ports_field');
|
||
|
||
$grid->callback_field('ip_address')
|
||
... | ... | |
$form->group('Basic data');
|
||
|
||
$form->input('name')
|
||
->label(__('name') . ':')
|
||
->rules('required|length[3,250]');
|
||
|
||
$form->input('tag_802_1q')
|
||
->label(__('tag_802_1q') . ':')
|
||
->rules('required|valid_digit')
|
||
->callback(array($this, 'valid_tag_802_1q'));
|
||
|
||
$form->textarea('comment')
|
||
->label(__('comment') . ':')
|
||
->rules('length[0,254]')
|
||
->cols('20')
|
||
->rows('5');
|
||
... | ... | |
if (!$this->acl_check_delete('Devices_Controller', 'vlan'))
|
||
Controller::error(ACCESS);
|
||
|
||
foreach ($vlan->vlan_ifaces as $vlan_iface)
|
||
$vlan_iface->delete();
|
||
|
||
$ports = ORM::factory('ports_vlan')->where('vlan_id', $vlan->id)
|
||
->find_all();
|
||
|
||
foreach ($ports as $port)
|
||
$port->delete();
|
||
|
||
if ($vlan->delete())
|
||
status::success ('Vlan has been successfully deleted.');
|
||
else
|
||
status::error ('Vlan hasn\t been deleted');
|
||
status::error ('Vlan hasn\'t been deleted');
|
||
|
||
url::redirect('vlans/show_all');
|
||
}
|
||
... | ... | |
|
||
if (!$this->_vlan_id)
|
||
{
|
||
$vlan = $vlan_model
|
||
->where('tag_802_1q', $input->value)
|
||
->find();
|
||
$vlan = $vlan_model->where('tag_802_1q', $input->value)->find();
|
||
}
|
||
else
|
||
{
|
||
$vlan = $vlan_model
|
||
->where(array
|
||
(
|
||
'tag_802_1q' => $input->value,
|
||
'id <>' => $this->_vlan_id
|
||
))->find();
|
||
$vlan = $vlan_model->where(array
|
||
(
|
||
'tag_802_1q' => $input->value,
|
||
'id <>' => $this->_vlan_id
|
||
))->find();
|
||
}
|
||
|
||
if ($vlan->id)
|
||
if ($vlan->id && $input)
|
||
{
|
||
$input->add_error('required', __('There is already some VLAN with this tag.'));
|
||
}
|
freenetis/branches/network/application/libraries/Grid.php | ||
---|---|---|
public function add_new_button($uri, $label, $options = array(), $help = '')
|
||
{
|
||
$this->buttons[] = html::anchor(
|
||
$uri, $label, $options
|
||
$uri, __($label), $options
|
||
) . (($help == '') ? '' : ' ' . $help);
|
||
}
|
||
|
Také k dispozici: Unified diff
Upravy:
- uprava VLANu pro #185