Projekt

Obecné

Profil

<?php defined('SYSPATH') or die('No direct script access.');
/*
* This file is part of open source system FreeNetIS
* and it is released 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/
*
*/

/**
*
* @package Controller
*/
class Vlan_ifaces_Controller extends Controller
{
/**
* Index redirects to show all
*/
public function index()
{
url::redirect(url_lang::base() . 'vlan_ifaces/show_all');
}

/**
* Function shows all vlan interfaces.
*
* @param integer $limit_results
* @param string $order_by
* @param string $order_by_direction
* @param integer $page_word
* @param integer $page
*/
public function show_all(
$limit_results = 50, $order_by = 'id', $order_by_direction = 'asc',
$page_word = null, $page = 1)
{
if (!$this->acl_check_view('Devices_Controller', 'vlan_iface'))
Controller::error(ACCESS);

// get new selector
if (is_numeric($this->input->get('record_per_page')))
$limit_results = (int) $this->input->get('record_per_page');

$vlan_iface_model = new Vlan_iface_Model();
$total_vlan_ifaces = $vlan_iface_model->count_all_vlan_ifaces();
if (($sql_offset = ($page - 1) * $limit_results) > $total_vlan_ifaces)
$sql_offset = 0;
$query = $vlan_iface_model->select(array
(
'ifaces.name as iface_name', 'vlans.name as vlan_name',
'vlan_ifaces.*'
))->join('ifaces', 'ifaces.id', 'vlan_ifaces.iface_id')
->join('vlans', 'vlans.id', 'vlan_ifaces.vlan_id')
->orderby($order_by, $order_by_direction)
->limit($limit_results, $sql_offset)
->find_all();

$grid = new Grid(url_lang::base() . 'vlan_ifaces', null, array
(
'current' => $limit_results,
'selector_increace' => 10,
'selector_min' => 10,
'selector_max_multiplier' => 5,
'base_url' => Config::get('lang') . '/vlan_ifaces/show_all/'
. $limit_results . '/' . $order_by . '/'
. $order_by_direction,
'uri_segment' => 'page',
'total_items' => $total_vlan_ifaces,
'items_per_page' => $limit_results,
'style' => 'classic',
'order_by' => $order_by,
'order_by_direction' => $order_by_direction,
'limit_results' => $limit_results
));

if ($this->acl_check_new('Devices_Controller', 'vlan_iface'))
{
$grid->add_new_button(
url_lang::base() . 'vlan_ifaces/add',
__('Add new VLAN interface')
);
}
$grid->order_field('id')
->label('ID')
->class('center');
$grid->order_field('name')
->label(__('name'));
$grid->order_field('iface_name')
->label(__('Interface name'));
$grid->order_field('vlan_name')
->label(__('VLAN name'));
$actions = $grid->grouped_action_field();
if ($this->acl_check_view('Devices_Controller', 'vlan_iface'))
{
$actions->add_action()
->icon_action('show')
->url('vlan_ifaces/show');
}
if ($this->acl_check_edit('Devices_Controller', 'vlan_iface'))
{
$actions->add_action()
->icon_action('edit')
->url('vlan_ifaces/edit');
}
if ($this->acl_check_delete('Devices_Controller', 'vlan_iface'))
{
$actions->add_action()
->icon_action('delete')
->url('vlan_ifaces/delete')
->class('delete_link');
}
$grid->datasource($query);
$this->template->content = $grid;

$view = new View('main');
$view->title = __('VLAN interfaces list');
$view->breadcrumbs = __('VLAN interfaces');
$view->content = new View('show_all');
$view->content->table = $this->template->content;
$view->content->headline = __('VLAN interfaces list');
$view->render(TRUE);
} // end of show_all

/**
* Function shows vlan interface.
* @param $vlan_iface_id
* @return unknown_type
*/
public function show($vlan_iface_id = null)
{
if (!$vlan_iface_id)
{
Controller::warning(PARAMETER);
}
$vlan_iface = new Vlan_iface_Model($vlan_iface_id);
if (!$vlan_iface->id)
{
Controller::warning(ACCESS);
}

$member_id = $vlan_iface->iface->device->user->member_id;
if (!$this->acl_check_view('Devices_Controller', 'vlan_iface', $member_id))
Controller::error(ACCESS);

// list of IP ADDRESSES
$ip_model = new Ip_address_Model();

$query = $ip_model->select(array
(
'ip_addresses.id', 'ip_addresses.ip_address',
'subnets.name as subnet_name'
))->join('subnets', 'subnets.id', 'ip_addresses.subnet_id')
->orderby('id', 'ASC')
->where('vlan_iface_id', $vlan_iface_id)
->find_all();
$grid_ip_addresses = new Grid(url_lang::base() . 'vlan_ifaces', null, array
(
'use_paginator' => false,
'use_selector' => false
));

if ($this->acl_check_new('Devices_Controller', 'ip_address'))
{
$grid_ip_addresses->add_new_button(
url_lang::base() . 'ip_addresses/add_to_vlan_iface/' .
$vlan_iface_id, __('Add new IP address')
);
}
$grid_ip_addresses->field('id')
->label('ID')
->class('center');
$grid_ip_addresses->field('ip_address')
->label(__('IP address'));
$grid_ip_addresses->field('subnet_name')
->label(__('Subnet name'));
$actions = $grid_ip_addresses->grouped_action_field();
if ($this->acl_check_view('Devices_Controller', 'ip_address'))
{
if (url_lang::current(TRUE) == 'devices')
$url = 'devices/show_ip_address';
else
$url = 'ip_addresses/show';
$actions->add_action()
->icon_action('show')
->url($url);
}
if ($this->acl_check_edit('Devices_Controller', 'ip_address'))
{
$actions->add_action()
->icon_action('edit')
->url('ip_addresses/edit');
}
if ($this->acl_check_delete('Devices_Controller', 'ip_address'))
{
$actions->add_action()
->icon_action('delete')
->url('ip_addresses/delete')
->class('delete_link');
}
$grid_ip_addresses->datasource($query);
if (url_lang::current(TRUE) == 'devices')
{
if ($vlan_iface->iface->name != '')
$iface_name = $vlan_iface->iface->name." (".$vlan_iface->iface->mac.")";
else
$iface_name = $vlan_iface->iface->mac;
$breadcrumbs = breadcrumbs::add()
->link(
'members/show_all',
'Members',
$this->acl_check_view('Members_Controller','members')
)
->link(
'members/show/'.$vlan_iface->iface->device->user->member_id,
'ID '.$vlan_iface->iface->device->user->member->id.' - '.$vlan_iface->iface->device->user->member->name,
$this->acl_check_view(
'Members_Controller',
'members',
$vlan_iface->iface->device->user->member_id
)
)
->link(
'users/show_by_member/'.$vlan_iface->iface->device->user->member_id,
"Users",
$this->acl_check_view(
'Users_Controller',
'users',$vlan_iface->iface->device->user->member_id
)
)
->link(
'users/show/'.$vlan_iface->iface->device->user_id,
$vlan_iface->iface->device->user->name.' '.$vlan_iface->iface->device->user->surname.' ('.$vlan_iface->iface->device->user->login.')',
$this->acl_check_view(
'Users_Controller',
'users',
$vlan_iface->iface->device->user->member_id
)
)
->link(
'devices/show_by_user/'.$vlan_iface->iface->device->user_id,
'Devices',
$this->acl_check_view(
'Devices_Controller',
'devices',
$vlan_iface->iface->device->user->member_id
)
)
->link(
'devices/show/'.$vlan_iface->iface->device_id,
$vlan_iface->iface->device->name,
$this->acl_check_view(
'Devices_Controller',
'devices',
$vlan_iface->iface->device->user->member_id
)
)
->link(
'devices/show_iface/'.$vlan_iface->iface->id,
$iface_name,
$this->acl_check_view(
'Devices_Controller',
'iface',
$vlan_iface->iface->device->user->member_id
)
);
}
else
{
$breadcrumbs = breadcrumbs::add()
->link('vlan_ifaces/show_all', 'VLAN interfaces');
}
$breadcrumbs->disable_translation()
->text($vlan_iface->name);

$view = new View('main');
$view->title = __('VLAN interface detail') . ' - ' . $vlan_iface->name;
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('show_vlan_iface');
$view->content->vlan_iface = $vlan_iface;
$view->content->headline = __('VLAN interface detail') . ' - ' . $vlan_iface->name;
$view->content->table_ip_addresses = $grid_ip_addresses;
$view->render(TRUE);
} // end of show

/**
* Function adds vlan interface.
*
* @param integer $iface_id
* @param integer $device_id
*/
public function add($iface_id = NULL, $device_id = NULL)
{
if (!$this->acl_check_new('Devices_Controller', 'vlan_iface'))
Controller::error(ACCESS);
$arr_ifaces = array();

if ($iface_id && $iface_id != 0)
{
if (!is_numeric($iface_id))
Controller::warning(PARAMETER);

$iface = new Iface_Model($iface_id);

if (!$iface->id)
Controller::error(RECORD);

$arr_ifaces[$iface->id] = $iface->name;

$title = __('Add new VLAN interface to interface');
if ($iface->name != '')
$name = "$iface->name ($iface->mac)";
else
$name = $iface->mac;
// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link(
'ifaces/show_all',
'Interfaces',
$this->acl_check_view('Devices_Controller','iface')
)
->link(
'ifaces/show/'.$iface->id,
$name,
$this->acl_check_view(
'Devices_Controller',
'iface',
$iface->device->user->member->id
)
)
->text('Add new VLAN interface to interface');
}
else if ($device_id)
{
if (!is_numeric($device_id))
Controller::warning(PARAMETER);

$device = new Device_Model($device_id);

if (!$device->id)
Controller::error(RECORD);

foreach ($device->ifaces as $iface)
$arr_ifaces[$iface->id] = $iface->name;

$title = __('Add new VLAN interface to device') . ' ' . $device->name;
// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link(
'members/show_all',
'Members',
$this->acl_check_view('Members_Controller','members')
)
->link(
'members/show/'.$iface->device->user->member_id,
'ID '.$iface->device->user->member->id.
' - '.$iface->device->user->member->name,
$this->acl_check_view(
'Members_Controller',
'members',
$iface->device->user->member_id
)
)
->link(
'users/show_by_member/'.$iface->device->user->member_id,
"Users",
$this->acl_check_view(
'Users_Controller',
'users',$iface->device->user->member_id
)
)
->link(
'users/show/'.$iface->device->user_id,
$iface->device->user->name.' '.$iface->device->user->surname.
' ('.$iface->device->user->login.')',
$this->acl_check_view(
'Users_Controller',
'users',
$iface->device->user->member_id
)
)
->link(
'devices/show_by_user/'.$iface->device->user_id,
'Devices',
$this->acl_check_view(
'Devices_Controller',
'devices',
$iface->device->user->member_id
)
)
->link(
'devices/show/'.$iface->device_id . '#vlan_interfaces',
$iface->device->name,
$this->acl_check_view(
'Devices_Controller',
'devices',
$iface->device->user->member_id
)
)
->text('Add new VLAN interface to device');
}
else
{
$arr_ifaces = ORM::factory('iface')->select_list_with_device();

$title = __('Add new VLAN interface');
// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link('vlan_ifaces/show_all', 'VLAN interfaces',
$this->acl_check_view('Devices_Controller', 'vlan_iface'))
->text('Add');
}

$form = new Forge(
url_lang::base() . 'vlan_ifaces/add/' . $iface_id, '',
'POST', array('id' => 'article_form')
);
$form->set_attr('class', 'form_class')
->set_attr('method', 'post');
$form->group('')
->label(__('Basic data'));
$form->input('name')
->label(__('VLAN interface name') . ':')
->rules('required|length[3,250]');

$form->dropdown('iface_id')
->label(__('Interface name'))
->options($arr_ifaces)
->rules('required');
$arr_vlans = array
(
NULL => '----- ' . __('select vlan') . ' -----'
) + ORM::factory('vlan')->select_list();
$form->dropdown('vlan_id')
->label(__('VLAN name'))
->options($arr_vlans)
->rules('required')
->add_button('vlans', '{tag_802_1q} - {name}');
$form->submit('submit')
->value(__('Save'));
special::required_forge_style($form, ' *', 'required');
if ($form->validate())
{
$form_data = $form->as_array();

foreach ($form_data as $key => $value)
{
$form_data[$key] = htmlspecialchars($value);
}

$vlan_iface = new Vlan_iface_Model();

$vlan_iface->name = $form_data['name'];
$vlan_iface->iface_id = $form_data['iface_id'];
$vlan_iface->vlan_id = $form_data['vlan_id'];

unset($form_data);

if ($vlan_iface->save())
{
status::success('VLAN interface has been successfully saved.');
url::redirect(
url_lang::base() . 'vlan_ifaces/show/' . $vlan_iface->id
);
exit;
}
}

$view = new View('main');
$view->title = $title;
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('form');
$view->content->form = $form->html();
$view->content->headline = $title;
$view->render(TRUE);
} // end of add

/**
* Function edits vlan interface.
*
* @param integer $vlan_iface_id
*/
public function edit($vlan_iface_id = null)
{
if (!$vlan_iface_id)
Controller::warning(PARAMETER);

$vlan_iface = new Vlan_iface_Model($vlan_iface_id);

if (!$vlan_iface->id)
Controller::error(RECORD);

if (!$this->acl_check_edit(
'Devices_Controller', 'vlan_iface',
$vlan_iface->iface->device->user->member_id))
{
Controller::error(ACCESS);
}

$form = new Forge(
url_lang::base() . 'vlan_ifaces/edit/' . $vlan_iface_id, '',
'POST', array('id' => 'article_form')
);
$form->set_attr('class', 'form_class')
->set_attr('method', 'post');

$form->group('')
->label(__('Basic data'));

$form->input('name')
->label(__('name') . ':')
->rules('required|length[3,250]')
->value($vlan_iface->name);

$iface_model = new Iface_Model();
$ifaces = $iface_model->where('id', $vlan_iface->iface_id)->find_all();

foreach ($ifaces as $iface)
{
$arr_ifaces[$iface->id] = $iface->name;
}
$form->dropdown('iface_id')
->label(__('Interface name'))
->options($arr_ifaces)
->rules('required')
->selected($vlan_iface->iface_id);

$arr_vlans = array
(
NULL => '----- ' . __('select vlan') . ' -----'
) + ORM::factory('vlan')->select_list();
$form->dropdown('vlan_id')
->label(__('VLAN name'))
->options($arr_vlans)
->rules('required')
->selected($vlan_iface->vlan_id)
->add_button('vlans', '{tag_802_1q} - {name}');


$form->submit('submit')
->value(__('update'));
special::required_forge_style($form, ' *', 'required');


// validate form and save data
if ($form->validate())
{

$form_data = $form->as_array();

foreach ($form_data as $key => $value)
{
$form_data[$key] = htmlspecialchars($value);
}

$vlan_iface = new Vlan_iface_Model($vlan_iface_id);

$vlan_iface->name = $form_data['name'];
$vlan_iface->iface_id = $form_data['iface_id'];
$vlan_iface->vlan_id = $form_data['vlan_id'];

unset($form_data);

if ($vlan_iface->save())
{
status::success('Vlan interface has been successfully updated.');
if (Path::instance()->previous()->uri()->current(0,1) == 'devices')
url::redirect(
url_lang::base() . 'devices/show_vlan_iface/'.$vlan_iface->id
);
else
url::redirect(
url_lang::base() . 'vlan_ifaces/show/' . $vlan_iface->id
);
exit;
}
}
// end validate
if (Path::instance()->previous()->uri()->current(0,1) == 'devices')
{
if ($vlan_iface->iface->name != '')
$iface_name = $vlan_iface->iface->name." (".$vlan_iface->iface->mac.")";
else
$iface_name = $vlan_iface->iface->mac;
$breadcrumbs = breadcrumbs::add()
->link(
'members/show_all',
'Members',
$this->acl_check_view('Members_Controller','members')
)
->link(
'members/show/'.$vlan_iface->iface->device->user->member_id,
'ID '.$vlan_iface->iface->device->user->member->id.' - '.$vlan_iface->iface->device->user->member->name,
$this->acl_check_view(
'Members_Controller',
'members',
$vlan_iface->iface->device->user->member_id
)
)
->link(
'users/show_by_member/'.$vlan_iface->iface->device->user->member_id,
"Users",
$this->acl_check_view(
'Users_Controller',
'users',$vlan_iface->iface->device->user->member_id
)
)
->link(
'users/show/'.$vlan_iface->iface->device->user_id,
$vlan_iface->iface->device->user->name.' '.$vlan_iface->iface->device->user->surname.' ('.$vlan_iface->iface->device->user->login.')',
$this->acl_check_view(
'Users_Controller',
'users',
$vlan_iface->iface->device->user->member_id
)
)
->link(
'devices/show_by_user/'.$vlan_iface->iface->device->user_id,
'Devices',
$this->acl_check_view(
'Devices_Controller',
'devices',
$vlan_iface->iface->device->user->member_id
)
)
->link(
'devices/show/'.$vlan_iface->iface->device_id,
$vlan_iface->iface->device->name,
$this->acl_check_view(
'Devices_Controller',
'devices',
$vlan_iface->iface->device->user->member_id
)
)
->link(
'devices/show_iface/'.$vlan_iface->iface->id,
$iface_name,
$this->acl_check_view(
'Devices_Controller',
'iface',
$vlan_iface->iface->device->user->member_id
)
);
}
else
{
$breadcrumbs = breadcrumbs::add()
->link('vlan_ifaces/show_all', 'VLAN interfaces',
$this->acl_check_view('Devices_Controller', 'vlan_iface'));
}
$breadcrumbs->disable_translation()
->link('vlan_ifaces/show/' . $vlan_iface->id, $vlan_iface->name,
$this->acl_check_view(
'Devices_Controller', 'vlan_iface',
$vlan_iface->iface->device->user->member_id
)
)
->enable_translation()
->text('Edit');

$view = new View('main');
$view->title = __('Edit vlan interface') . ' - ' . $vlan_iface->name;
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('form');
$view->content->form = $form->html();
$view->content->headline = __('Edit vlan interface') . ' - ' . $vlan_iface->name;
$view->render(TRUE);
} // end of edit

/**
* Function deletes vlan interface.
*
* @param integer $vlan_iface_id
*/
public function delete($vlan_iface_id = null)
{
if (!isset($vlan_iface_id))
Controller::warning(PARAMETER);
$vlan_iface = new Vlan_iface_Model($vlan_iface_id);

if (!$vlan_iface->id)
Controller::error(RECORD);

$link_back = url_lang::base() . 'ifaces/show/' . $vlan_iface->iface_id;

if (!$this->acl_check_delete(
'Devices_Controller', 'vlan_iface',
$vlan_iface->iface->device->user->member_id
))
{
Controller::error(ACCESS);
}

// find ip addresses of interface, in this relation 1:n ORM works
$ips = $vlan_iface->ip_addresses;

if (count($ips) == 0)
{
if ($vlan_iface->delete())
{
status::success('VLAN interface has been successfully deleted.');
}
else
{
status::error('Error - cant delete VLAN interface.');
}
}
else
{
status::warning('VLAN interface still has at least one ip address.');
}
url::redirect($link_back);
}

}
(68-68/76)