Revize 1494
Přidáno uživatelem Ondřej Fibich před asi 12 roky(ů)
freenetis/branches/network/application/models/device.php | ||
---|---|---|
->get_all_dependent_subnets_by_device($device_id, $recursive);
|
||
}
|
||
|
||
/**
|
||
* Checks if port number exists on given device
|
||
*
|
||
* @param integer $port_number
|
||
* @param integer $device_id
|
||
* @param integer $iface_id Iface for edit
|
||
* @return boolean
|
||
*/
|
||
public function port_number_exists($port_number, $device_id = NULL, $iface_id = NULL)
|
||
{
|
||
if ($device_id === NULL && $this)
|
||
{
|
||
$device_id = $this->id;
|
||
}
|
||
|
||
$where = '';
|
||
|
||
if (intval($iface_id))
|
||
{
|
||
$where = 'AND i.id <> ' . intval($iface_id);
|
||
}
|
||
|
||
return $this->db->query("
|
||
SELECT COUNT(*) AS total
|
||
FROM ifaces i
|
||
WHERE i.device_id = ? AND i.type = ? AND i.number = ? $where
|
||
", $device_id, Iface_Model::TYPE_PORT, $port_number)->current()->total > 0;
|
||
}
|
||
|
||
}
|
freenetis/branches/network/application/models/vlan.php | ||
---|---|---|
*/
|
||
class Vlan_Model extends ORM
|
||
{
|
||
/**
|
||
* Const for default VLAN
|
||
*/
|
||
const DEFAULT_VLAN_TAG = 1;
|
||
|
||
// relationship
|
||
protected $has_many = array('ifaces_vlan');
|
||
|
||
/**
|
||
* Returns default VLAN
|
||
*
|
||
* @author Michal Kliment
|
||
* @return Vlan_Model object
|
||
*/
|
||
public function get_default_vlan()
|
||
{
|
||
return $this->where('tag_802_1q', self::DEFAULT_VLAN_TAG)->find();
|
||
}
|
||
|
||
/**
|
||
* Get count of vlans
|
||
*
|
||
* @return integer
|
freenetis/branches/network/application/controllers/ifaces.php | ||
---|---|---|
class Ifaces_Controller extends Controller
|
||
{
|
||
/**
|
||
* Device for validators
|
||
*
|
||
* @var integer
|
||
*/
|
||
private $device_id = NULL;
|
||
|
||
/**
|
||
* Interface for validators
|
||
*
|
||
* @var integer
|
||
... | ... | |
Controller::error(RECORD);
|
||
}
|
||
|
||
$this->device_id = $device_id;
|
||
$this->iface_type = $type;
|
||
|
||
$form = new Forge();
|
||
... | ... | |
NULL => '------- '.__('Select interface').' -------'
|
||
) + $parent_ifaces;
|
||
|
||
$vlans = array
|
||
$vlan_ifaces = array
|
||
(
|
||
NULL => '------- '.__('Select VLAN').' -------'
|
||
) + ORM::factory('vlan')->select_list();
|
||
... | ... | |
|
||
$vlan_form->dropdown('vlan_id')
|
||
->label('VLAN')
|
||
->options($vlans)
|
||
->options($vlan_ifaces)
|
||
->rules('required')
|
||
->style('width:200px');
|
||
|
||
... | ... | |
(
|
||
NULL => '------- '.__('Select mode').' -------'
|
||
) + Iface_Model::get_port_modes();
|
||
|
||
$vlan_model = new Vlan_Model();
|
||
$arr_vlans = $vlan_model->select_list();
|
||
$default_vlan = $vlan_model->get_default_vlan();
|
||
|
||
$default_vlans = array
|
||
(
|
||
NULL => '----- ' . __('Select VLAN') . ' -----'
|
||
) + $arr_vlans;
|
||
|
||
$port_form = $form->group('Port setting');
|
||
|
||
$port_form->input('number')
|
||
->class('increase_decrease_buttons');
|
||
->class('increase_decrease_buttons')
|
||
->callback(array($this, 'valid_port_nr'));
|
||
|
||
$port_form->dropdown('port_mode')
|
||
->options($modes)
|
||
->rules('required')
|
||
->style('width:200px');
|
||
}
|
||
|
||
$form->dropdown('port_vlan_id')
|
||
->options($default_vlans)
|
||
->selected($default_vlan->id)
|
||
->rules('required')
|
||
->label('Port VLAN')
|
||
->help('port_vlan')
|
||
->callback(array($this, 'valid_port_vlan'))
|
||
->style('width:200px')
|
||
->add_button('vlans');
|
||
|
||
$form->dropdown('tagged_vlan_id[]')
|
||
->label('Tagged VLANs')
|
||
->options($arr_vlans)
|
||
->multiple('multiple')
|
||
->size(30)
|
||
->add_button('vlans');
|
||
|
||
$form->dropdown('untagged_vlan_id[]')
|
||
->label('Untagged VLANs')
|
||
->options($arr_vlans)
|
||
->selected($default_vlan->id)
|
||
->callback(array($this, 'valid_vlans'))
|
||
->multiple('multiple')
|
||
->size(30)
|
||
->add_button('vlans');
|
||
}
|
||
|
||
// print wireless fields only if type is wireless
|
||
if ($type == Iface_Model::TYPE_WIRELESS)
|
||
{
|
||
... | ... | |
$iface_relationship->save_throwable();
|
||
}
|
||
}
|
||
else if ($iface->type == Iface_Model::TYPE_PORT)
|
||
{
|
||
$vlan_ifaces = new Ifaces_vlan_Model();
|
||
|
||
if ($iface->port_mode == Iface_Model::PORT_MODE_TRUNK ||
|
||
$iface->port_mode == Iface_Model::PORT_MODE_HYBRID)
|
||
{
|
||
foreach ((array) $form_data['tagged_vlan_id'] as $vlan_id)
|
||
{
|
||
if (intval($vlan_id))
|
||
{
|
||
$vlan_ifaces->clear();
|
||
$vlan_ifaces->vlan_id = $vlan_id;
|
||
$vlan_ifaces->iface_id = $iface->id;
|
||
$vlan_ifaces->tagged = TRUE;
|
||
$vlan_ifaces->port_vlan = ($vlan_id == $form_data['port_vlan_id']);
|
||
$vlan_ifaces->save_throwable();
|
||
}
|
||
}
|
||
}
|
||
|
||
if ($iface->port_mode == Iface_Model::PORT_MODE_ACCESS ||
|
||
$iface->port_mode == Iface_Model::PORT_MODE_HYBRID)
|
||
{
|
||
foreach ((array) $form_data['untagged_vlan_id'] as $vlan_id)
|
||
{
|
||
if (intval($vlan_id))
|
||
{
|
||
$vlan_ifaces->clear();
|
||
$vlan_ifaces->vlan_id = $vlan_id;
|
||
$vlan_ifaces->iface_id = $iface->id;
|
||
$vlan_ifaces->tagged = FALSE;
|
||
$vlan_ifaces->port_vlan = ($vlan_id == $form_data['port_vlan_id']);
|
||
$vlan_ifaces->save_throwable();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
$iface->transaction_commit();
|
||
|
||
status::success('Interface has been successfully saved.');
|
||
... | ... | |
if (!is_numeric($device_id))
|
||
{
|
||
$form->dropdown('device_id')
|
||
->label('Device')
|
||
->options($devices)
|
||
->rules('required')
|
||
->style('width: 300px');
|
||
->label('Device')
|
||
->options($devices)
|
||
->rules('required')
|
||
->style('width: 300px');
|
||
}
|
||
else
|
||
{
|
||
$form->hidden('device_id')
|
||
->value($device_id);
|
||
->value($device_id);
|
||
}
|
||
|
||
$form->dropdown('type')
|
||
... | ... | |
{
|
||
Controller::error(RECORD);
|
||
}
|
||
|
||
|
||
$this->iface_type = 0;
|
||
|
||
$this->iface_id = $iface->id;
|
||
$this->device_id = $iface->device_id;
|
||
$member_id = $iface->device->user->member_id;
|
||
|
||
// access control
|
||
... | ... | |
|
||
$port_form->input('number')
|
||
->class('increase_decrease_buttons')
|
||
->value($iface->number);
|
||
->value($iface->number)
|
||
->callback(array($this, 'valid_port_nr'));
|
||
|
||
$port_form->dropdown('port_mode')
|
||
->options($modes)
|
||
... | ... | |
$input->add_error('required', 'Link is required');
|
||
}
|
||
|
||
$itype = $this->input->post('itype');
|
||
$mediums = Iface_Model::get_type_has_link_with_medium($this->iface_type);
|
||
|
||
if (!in_array($link->medium, $mediums))
|
||
... | ... | |
}
|
||
|
||
/**
|
||
* Callback function to validate segment
|
||
* Callback function to validate link
|
||
*
|
||
* @author Michal Kliment
|
||
* @param Form_Field $input
|
||
... | ... | |
}
|
||
}
|
||
|
||
/**
|
||
* Callback function to validate tagged and untagged VLANs
|
||
*
|
||
* @author Michal Kliment
|
||
* @param Form_Field $input
|
||
*/
|
||
public function valid_vlans($input = NULL)
|
||
{
|
||
// validators cannot be accessed
|
||
if (empty($input) || !is_object($input))
|
||
{
|
||
self::error(PAGE);
|
||
}
|
||
|
||
$tagged_vlans = (array) @$_POST['tagged_vlan_id'];
|
||
$untagged_vlans = (array) @$_POST['untagged_vlan_id'];
|
||
|
||
// VLAN can be only tagged or untagged
|
||
if (count(array_intersect($untagged_vlans, $tagged_vlans)) > 0)
|
||
{
|
||
$input->add_error('required', __('VLAN can be only tagged or untagged.'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Callback function to validate port VLAN
|
||
*
|
||
* @author Michal Kliment
|
||
* @param Form_Field $input
|
||
*/
|
||
public function valid_port_vlan($input = NULL)
|
||
{
|
||
// validators cannot be accessed
|
||
if (empty($input) || !is_object($input))
|
||
{
|
||
self::error(PAGE);
|
||
}
|
||
|
||
$port_vlan_id = $input->value;
|
||
$tagged_vlans = (array) @$_POST['tagged_vlan_id'];
|
||
$untagged_vlans = (array) @$_POST['untagged_vlan_id'];
|
||
$port_mode = $this->input->post('port_mode');
|
||
|
||
switch($port_mode)
|
||
{
|
||
// port is in access mode
|
||
case Iface_Model::PORT_MODE_ACCESS:
|
||
|
||
if (!in_array($port_vlan_id, $untagged_vlans))
|
||
{
|
||
$input->add_error('required', __('Port VLAN has to be in untagged VLANs.'));
|
||
}
|
||
break;
|
||
|
||
// port is in trunk mode
|
||
case Iface_Model::PORT_MODE_TRUNK:
|
||
|
||
if (!in_array($port_vlan_id, $tagged_vlans))
|
||
{
|
||
$input->add_error('required', __('Port VLAN has to be in tagged VLANs.'));
|
||
}
|
||
|
||
break;
|
||
|
||
// port is in hybrid mode
|
||
case Iface_Model::PORT_MODE_HYBRID:
|
||
|
||
if (!in_array($port_vlan_id, $untagged_vlans))
|
||
{
|
||
$input->add_error('required', __('Port VLAN has to be in untagged VLANs.'));
|
||
}
|
||
|
||
break;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Callback function to validate number of port
|
||
*
|
||
* @author Michal Kliment
|
||
* @param Form_Field $input
|
||
*/
|
||
public function valid_port_nr($input = NULL)
|
||
{
|
||
// validators cannot be accessed
|
||
if (empty($input) || !is_object($input))
|
||
{
|
||
self::error(PAGE);
|
||
}
|
||
|
||
$value = trim($input->value);
|
||
$device = new Device_Model($this->device_id);
|
||
|
||
if (!empty($value) && $device && $device->id)
|
||
{
|
||
if ($device->port_number_exists($value, $device->id, $this->iface_id))
|
||
{
|
||
$input->add_error('required', __('Port number already exist.'));
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
freenetis/branches/network/application/views/js/ifaces_add.php | ||
---|---|---|
<?php
|
||
/**
|
||
* Javascript functionality for adding/edditing of interfaces.
|
||
*
|
||
* @author Michal Kliment, Ondřej Fibich
|
||
*/
|
||
... | ... | |
|
||
?>
|
||
|
||
<?php if (!empty($itype)): ?>
|
||
/**
|
||
* Updates options - options are stored in global variable located in
|
||
* js/base view.
|
||
*/
|
||
function update_options(id)
|
||
{
|
||
select_multiple[id] = new Array();
|
||
|
||
$("#"+id).children().each(function ()
|
||
{
|
||
select_multiple[id].push({
|
||
'key': $(this).attr('value'),
|
||
'value': $(this).html()
|
||
});
|
||
});
|
||
}
|
||
|
||
// on add of new item to one of multiple selectboxes for port - vlan relationship
|
||
$('#port_vlan_id, #tagged_vlan_id, #untagged_vlan_id').live('addOption', function (e, new_option_id)
|
||
{
|
||
switch ($(this).attr('id'))
|
||
{
|
||
case 'port_vlan_id':
|
||
$(this).val(new_option_id);
|
||
multiple_select_add_option('tagged_vlan_id', new_option_id);
|
||
update_options('tagged_vlan_id_options');
|
||
multiple_select_add_option('untagged_vlan_id', new_option_id);
|
||
update_options('untagged_vlan_id_options');
|
||
break;
|
||
|
||
case 'tagged_vlan_id':
|
||
multiple_select_add_option('untagged_vlan_id', new_option_id);
|
||
update_options('untagged_vlan_id_options');
|
||
var port_vlan_id = $('#port_vlan_id').val();
|
||
reload_element('#port_vlan_id', '<?php echo url_lang::base().url_lang::current(0,1) ?>');
|
||
$('#port_vlan_id').val(port_vlan_id);
|
||
update_options('tagged_vlan_id');
|
||
break;
|
||
|
||
case 'untagged_vlan_id':
|
||
multiple_select_add_option('tagged_vlan_id', new_option_id);
|
||
update_options('tagged_vlan_id_options');
|
||
reload_element('#port_vlan_id', '<?php echo url_lang::base().url_lang::current(0,1) ?>');
|
||
var port_vlan_id = $("#port_vlan_id").val();
|
||
reload_element('#port_vlan_id', '<?php echo url_lang::base().url_lang::current(0,1) ?>');
|
||
$('#port_vlan_id').val(port_vlan_id);
|
||
update_options('untagged_vlan_id');
|
||
break;
|
||
}
|
||
|
||
});
|
||
|
||
// on change of port mode - set vlan options accoding to mode
|
||
$('#port_mode').live('change', function ()
|
||
{
|
||
switch ($(this).val())
|
||
{
|
||
case '<?php echo Iface_Model::PORT_MODE_ACCESS ?>':
|
||
$('#tagged_vlan_id').parent().parent().parent().parent().parent().parent().addClass('dispNone');
|
||
$('#untagged_vlan_id').parent().parent().parent().parent().parent().parent().removeClass('dispNone');
|
||
break;
|
||
|
||
case '<?php echo Iface_Model::PORT_MODE_TRUNK ?>':
|
||
$('#tagged_vlan_id').parent().parent().parent().parent().parent().parent().removeClass('dispNone');
|
||
$('#untagged_vlan_id').parent().parent().parent().parent().parent().parent().addClass('dispNone');
|
||
break;
|
||
|
||
case '<?php echo Iface_Model::PORT_MODE_HYBRID ?>':
|
||
$('#tagged_vlan_id').parent().parent().parent().parent().parent().parent().removeClass('dispNone');
|
||
$('#untagged_vlan_id').parent().parent().parent().parent().parent().parent().removeClass('dispNone');
|
||
break;
|
||
|
||
default: // if empty => hide
|
||
$('#tagged_vlan_id').parent().parent().parent().parent().parent().parent().addClass('dispNone');
|
||
$('#untagged_vlan_id').parent().parent().parent().parent().parent().parent().addClass('dispNone');
|
||
break;
|
||
}
|
||
}).trigger('change');
|
||
|
||
// on change of connected to
|
||
$('#connected_to').change(function ()
|
||
{
|
||
... | ... | |
// add require fields at start
|
||
$('#link_id').trigger('change');
|
||
<?php endif ?>
|
||
|
||
<?php endif ?>
|
||
|
Také k dispozici: Unified diff
Upravy:
- pridavani portu (#185)