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/
*
*/

/**
* Controller performs devices actions.
*
* @package Controller
*/
class Devices_Controller extends Controller
{
/**
* Index redirects to show all
*/
public function index()
{
url::redirect(url_lang::base().'devices/show_all');
}
/**
* Function shows all devices.
*
* @param integer $limit_results devices per page
* @param string $order_by sorting column
* @param string $order_by_direction sorting direction
*/
public function show_all(
$limit_results = 500, $order_by = 'device_id',
$order_by_direction = 'ASC', $page_word = null, $page = 1)
{
// access control
if (!$this->acl_check_view(get_class($this),'devices'))
Controller::error(ACCESS);
$filter_form = new Filter_form('d');
$filter_form->add('name')
->callback('json/device_name');
$filter_form->add('type')
->type('select')
->values(ORM::factory('Enum_type')
->get_values(Enum_type_model::$device_type_id));
$filter_form->add('trade_name')
->callback('json/device_trade_name');
$filter_form->add('user_name')
->type('combo')
->label('Firstname of user')
->callback('json/user_name');
$filter_form->add('user_surname')
->type('combo')
->label('Surname of user')
->callback('json/user_surname');
$filter_form->add('member_name')
->callback('json/member_name');
$filter_form->add('login')
->label('Username')
->callback('json/device_login');
$filter_form->add('password')
->callback('json/device_password');
$filter_form->add('price')
->type('number');
$filter_form->add('payment_rate')
->label('monthly payment rate')
->type('number');
$filter_form->add('buy_date')
->type('date');
$filter_form->add('town')
->type('combo')
->callback('json/town_name');
$filter_form->add('street')
->type('combo')
->callback('json/street_name');
$filter_form->add('street_number')
->type('number');
$filter_form->add('comment');
// get new selector
if (is_numeric($this->input->get('record_per_page')))
$limit_results = (int) $this->input->get('record_per_page');
$device_model = new Device_Model;
$total_devices = $device_model->count_all_devices($filter_form->as_sql());
// limit check
if (($sql_offset = ($page - 1) * $limit_results) > $total_devices)
$sql_offset = 0;
// query
$devices = $device_model->get_all_devices(
$sql_offset, (int)$limit_results, $order_by, $order_by_direction,
NULL, $filter_form->as_sql()
);
// headline
$headline = __('Devices list');
// grid of devices
$grid = new Grid(url_lang::base().'devices', null, array(
'current' => $limit_results,
'selector_increace' => 500,
'selector_min' => 500,
'selector_max_multiplier' => 20,
'base_url' => Config::get('lang').'/devices/show_all/'.
$limit_results.'/'.$order_by.'/'.$order_by_direction ,
'uri_segment' => 'page',
'total_items' => $total_devices,
'items_per_page' => $limit_results,
'style' => 'classic',
'order_by' => $order_by,
'order_by_direction' => $order_by_direction,
'limit_results' => $limit_results,
'filter' => $filter_form
));
if ($this->acl_check_new(get_class($this),'devices'))
{
$grid->add_new_button(
url_lang::base().'devices/add_whole',
__('Add new whole device')
);
}
if ($this->acl_check_new(get_class($this),'devices'))
{
$grid->add_new_button(
url_lang::base().'devices/add',
__('Add new device')
);
}

$grid->order_field('device_id')
->label('ID')
->class('center');
$grid->order_callback_field('device_name')
->label(__('Name'))
->callback('callback::device_field');
$grid->order_field('type_name')
->label(__('Type'));
$grid->order_callback_field('user_login')
->label(__('User'))
->callback('callback::user_login_field');
$actions = $grid->grouped_action_field();
if ($this->acl_check_edit(get_class($this),'devices'))
{
$actions->add_action('device_id')
->icon_action('edit')
->url('devices/edit');
}
if ($this->acl_check_delete('Devices_Controller', 'devices'))
{
$actions->add_action('device_id')
->icon_action('delete')
->url('devices/delete')
->class('delete_link');
}
$grid->datasource($devices);
// view
$view = new View('main');
$view->title = $headline;
$view->breadcrumbs = __('Devices');
$view->content = new View('show_all');
$view->content->headline = $headline;
$view->content->table = $grid;
$view->render(TRUE);
} // end of show_all function

/**
* Function shows all devices of user.
*/
public function show_by_user(
$user_id = null, $limit_results = 10, $order_by = 'id',
$order_by_direction = 'asc', $page_word = 'page', $page = 1)
{
// bad parameter
if (!$user_id || !is_numeric ($user_id))
Controller::warning(PARAMETER);

$user = new User_Model($user_id);

// user doesn't exist
if (!$user->id)
Controller::error(RECORD);

// access control
if (!$this->acl_check_view(get_class($this),'devices',$user->member_id))
Controller::error(ACCESS);

$device_model = new Device_Model();
$enum_type_model = new Enum_type_Model();
$port_model = new Port_Model();
$iface_model = new Iface_Model();
$vlan_iface_model = new Vlan_iface_Model();

$total_devices = $device_model->count_devices_of_user($user->id);

$allowed_order_by = array
(
'id' => __('Device ID'),
'name' => __('Device name') ,
'type' => __('Device type'),
'mac' => __('MAC address'),
'ip_address' => __('IP address')
);

$allowed_order_by_direction = array
(
'asc' => __('Ascending'),
'desc' => __('Descending'),
);

if (!in_array($order_by, array_keys($allowed_order_by)))
$order_by = 'id';

if (!in_array(strtolower($order_by_direction), array_keys($allowed_order_by_direction)))
$order_by_direction = 'asc';

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

if (($sql_offset = ($page - 1) * $limit_results) > $total_devices)
$sql_offset = 0;

$order_form = new Forge(
url::base(TRUE).url::current(TRUE).'#user-devices-advanced-grid','',
'POST', array('id' => 'article_form')
);
$order_form->set_attr('class', 'form_class')
->set_attr('method', 'post');
$order_form->dropdown('order_by')
->label(__('Order by').':')
->options($allowed_order_by)
->rules('required')->selected($order_by);
$order_form->dropdown('order_by_direction')
->label(__('Direction').':')
->options($allowed_order_by_direction)->rules('required')
->selected($order_by_direction);
$order_form->submit('submit')
->value(__('Send'));

if ($order_form->validate())
{
$form_data = $order_form->as_array();
url::redirect(
url_lang::base()."devices/show_by_user/$user_id/$limit_results/".
$form_data['order_by']."/".$form_data['order_by_direction'].
"/$page_word/$page"
);
}
$devices = $device_model->get_devices_of_user($user_id);
$base_grid = new Grid(url_lang::base().'devices', null, array
(
'use_paginator' => false,
'use_selector' => false,
'total_items' => $total_devices
));
$base_grid->field('id');
$base_grid->field('name');
$base_grid->field('type');
$base_grid->callback_field('mac')
->callback('callback::mac_address_field');
$base_grid->callback_field('segment_id')
->label('Segment')
->callback('callback::segment_field');
$base_grid->callback_field('ip_address')
->callback('callback::ip_address_field');
$base_grid->callback_field('subnet_id')
->label('Subnet')
->callback('callback::subnet_field');
$actions = $base_grid->grouped_action_field();
if ($this->acl_check_view(get_class($this),'devices',$user->member_id))
{
$actions->add_action('id')
->icon_action('show')
->url('devices/show');
}
if ($this->acl_check_edit(get_class($this),'devices',$user->member_id))
{
$actions->add_action('id')
->icon_action('edit')
->url('devices/edit');
}

if ($this->acl_check_delete(get_class($this),'devices',$user->member_id))
{
$actions->add_action('id')
->icon_action('delete')
->url('devices/delete_whole')
->class('delete_link');
}
$base_grid->datasource($devices);
$devices = $device_model->get_devices_of_user(
$user_id, $sql_offset, $limit_results,
$order_by, $order_by_direction
);

$this->selector = new Selector (array(
'selector_max_multiplier' => 20,
'current' => $limit_results,
'base_url' => Config::get('lang').'/devices/show_by_user/'.$user->id.'/'.
$limit_results.'/'.$order_by.'/'.$order_by_direction.'/'.
$page_word.'/'.$page.'#user-devices-advanced-grid'
));

$this->pagination = new Pagination (array(
'base_url' => Config::get('lang').'/devices/show_by_user/'.$user->id.'/'.
$limit_results.'/'.$order_by.'/'.$order_by_direction.'/'.
$page_word.'/'.$page.'#user-devices-advanced-grid',
'total_items' => $total_devices,
'items_per_page' => $limit_results,
'uri_segment' => 'page'
));

$arr_devices = array();
foreach ($devices as $device)
{
$arr_devices[$device->id] = array(
'type' => $device->type,
'name' => $device->name,
'buy_date' => $device->buy_date,
'grids' => array()
);

$ifaces = $iface_model->get_all_ifaces_of_device($device->id, TRUE);

$grid = new Grid(url_lang::base().'devices', null, array
(
'use_paginator' => false,
'use_selector' => false,
'total_items' => count($ifaces)
));
$grid->callback_field('id')
->label('ID')
->class('center')
->callback('callback::iface_id_field');
$grid->field('name')
->label(__('Name'));;
$grid->field('mac')
->label(__('MAC'));
$grid->callback_field('segment_id')
->label(__('Segment'))
->callback('callback::segment_field');
$grid->callback_field('ip_address')
->label(__('IP address'))
->callback('callback::ip_address_field', TRUE);
$grid->callback_field('subnet_id')
->label(__('Subnet'))
->callback('callback::subnet_field');
$actions = $grid->grouped_action_field();
if ($this->acl_check_edit(get_class($this),'iface',$user->member_id))
{
$actions->add_action()
->icon_action('edit')
->url('ifaces/edit');
}
if ($this->acl_check_delete(get_class($this), 'iface', $user->member_id))
{
$actions->add_action()
->icon_action('delete')
->url('ifaces/delete')
->class('delete_link');
}
$grid->datasource($ifaces);

$arr_devices[$device->id]['grids']['interfaces'] = $grid;

$ports = $port_model->get_ports_of_device($device->id);

if (count($ports) > 0)
{
$grid = new Grid(url_lang::base().'devices', null, array
(
'use_paginator' => false,
'use_selector' => false,
'total_items' => count($ports)
));
$grid->field('id')
->label('ID')
->class('center');
$grid->callback_field('name')
->label(__('Name'))
->class('center')
->callback('callback::port_field');
$grid->callback_field('segment_id')
->label(__('Segment'))
->callback('callback::segment_field');
$grid->field('vlan_count')
->label(__('VLAN count'))
->class('center');
$actions = $grid->grouped_action_field();
if ($this->acl_check_edit(get_class($this),'port',$user->member_id))
{
$actions->add_action()
->icon_action('edit')
->url('ports/edit');
}
if ($this->acl_check_delete(get_class($this), 'port', $user->member_id))
{
$actions->add_action()
->icon_action('delete')
->url('ports/delete')
->class('delete_link');
}
$grid->datasource($ports);

$arr_devices[$device->id]['grids']['ports'] = $grid;
}

$vlan_ifaces = $vlan_iface_model->get_all_vlan_ifaces_with_ip_addresses_by_device_id($device->id);

if (count($vlan_ifaces))
{
$grid = new Grid(url_lang::base().'devices', null,array(
'use_paginator' => false,
'use_selector' => false,
'total_items' => count($vlan_ifaces)
));
$grid->field('id')
->label('ID')
->class('center');
$grid->callback_field('vlan_iface_id')
->label(__('Name'))
->callback('callback::vlan_iface_field');
$grid->callback_field('vlan_id')
->label(__('VLAN name'))
->callback('callback::vlan_field');
$grid->field('tag_802_1q')
->label(__('tag_802_1q'));
$grid->field('iface_name')
->label(__('Interface'));
$grid->callback_field('ip_address')
->label(__('IP address'))
->callback('callback::ip_address_field', TRUE);
$actions = $grid->grouped_action_field();
if ($this->acl_check_edit(get_class($this),'vlan_iface',$user->member_id))
{
$actions->add_action()
->icon_action('edit')
->url('vlan_ifaces/edit');
}
if ($this->acl_check_delete(get_class($this), 'vlan_iface', $user->member_id))
{
$actions->add_action()
->icon_action('delete')
->url('vlan_ifaces/delete')
->class('delete_link');
}
$grid->datasource($vlan_ifaces);

$arr_devices[$device->id]['grids']['vlan interfaces'] = $grid;
}
}

$headline = __('Device list of user');

// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link('members/show_all', 'Members',
$this->acl_check_view('Members_Controller','members'))
->disable_translation()
->link('members/show/' . $user->member->id,
'ID ' . $user->member->id . ' - ' . $user->member->name,
$this->acl_check_view('Members_Controller','members', $user->member->id))
->enable_translation()
->link('users/show_by_member/' . $user->member_id, 'Users',
$this->acl_check_view('Users_Controller', 'users', $user->member_id))
->disable_translation()
->link('users/show/' . $user->id,
$user->name . ' ' . $user->surname . ' (' . $user->login . ')',
$this->acl_check_view('Users_Controller', 'users',$user->member_id))
->enable_translation()
->text('Devices');

// view
$view = new View('main');
$view->title = $headline;
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('devices_show_by_user');
$view->content->base_grid = $base_grid;
$view->content->user_id = $user->id;
$view->content->member_id = $user->member_id;
$view->content->total_devices = $total_devices;
$view->content->order_form = $order_form->html();
$view->content->headline = $headline;
$view->content->devices = $arr_devices;
$view->render(TRUE);
}
/**
* Function shows device.
*
* @param integer $device_id
*/
public function show($device_id = null)
{
if (!isset($device_id))
{
Controller::warning(PARAMETER);
}
$device = new Device_Model($device_id);
if ($device->id == 0)
{
Controller::error(RECORD);
}
$member_id = $device->user->member_id;
if (!$this->acl_check_view(get_class($this),'devices',$member_id))
Controller::error(ACCESS);
$enum_type_model = new Enum_type_Model();
$device_type = $enum_type_model->get_value($device->type);
$this->session->set('ssDevice_id',$device->id);
$user_model = new User_Model;
$iface_model = new Iface_Model;
$ifaces = $iface_model->where('device_id', $device->id)->find_all();
$ip_model = new Ip_address_Model;
$subnet_model = new Subnet_Model;
$i = 0;
$ip_iface = '';
$arr_ip = array();
foreach ($ifaces as $iface)
{
$ip_iface = $ip_model->where('iface_id', $iface->id)->find_all();
foreach ($ip_iface as $ip)
{
$arr_ip[$i]['iface_id'] = $iface->id;
$arr_ip[$i]['ip_address'] = $ip->ip_address;
$subnet = $subnet_model->where('id', $ip->subnet_id)->find();
$arr_ip[$i]['name'] = $subnet->name;
$arr_ip[$i]['network_address'] = $subnet->network_address;
$arr_ip[$i]['netmask'] = $subnet->netmask;
$i++;
}
}

// device engineers
$device_engineer_model = new Device_engineer_Model();
$de = $device_engineer_model->get_device_engineers($device_id);
$grid_device_engineers = new Grid(url_lang::base().'devices', null, array
(
'use_paginator' => false,
'use_selector' => false,
'total_items' => count($de)
));
if ($this->acl_check_new('Devices_Controller', 'engineer', $member_id))
{
$grid_device_engineers->add_new_button(
url_lang::base()."device_engineers/add/$device_id",
__('Add new device engineer')
);
}
$grid_device_engineers->field('name')
->label(__('Name'));
$grid_device_engineers->field('surname')
->label(__('Surname'));
$grid_device_engineers->field('login')
->label(__('Username'));
if ($this->acl_check_delete('Devices_Controller', 'engineer', $member_id))
{
$grid_device_engineers->grouped_action_field()
->add_action('id')
->icon_action('delete')
->url('device_engineers/delete')
->class('delete_link');
}
$grid_device_engineers->datasource($de);
// device admins
$device_admin_model = new Device_admin_Model();
$da = $device_admin_model->get_device_admins($device_id);
$grid_device_admins = new Grid(url_lang::base().'devices', null, array
(
'use_paginator' => false,
'use_selector' => false,
'total_items' => count($da)
));
if ($this->acl_check_edit(get_class($this), 'admin', $member_id))
{
$grid_device_admins->add_new_button(
url_lang::base()."device_admins/edit/$device_id",
__('Edit device admins')
);
}
$grid_device_admins->field('name')
->label(__('name'));
$grid_device_admins->field('surname')
->label(__('surname'));
$grid_device_admins->field('login')
->label(__('username'));
if ($this->acl_check_delete('Devices_Controller', 'engineer', $member_id))
{
$grid_device_admins->grouped_action_field()
->add_action('id')
->icon_action('delete')
->url('device_admins/delete')
->class('delete_link');
}
$grid_device_admins->datasource($da);
// interfaces of device
$iface_model = new Iface_Model();
$ifaces = $iface_model->get_all_ifaces_of_device($device_id);
// grid
$grid_ifaces = new Grid(url_lang::base().'devices', null, array
(
'use_paginator' => false,
'use_selector' => false,
'total_items' => count($ifaces)
));
if ($this->acl_check_new(get_class($this),'iface',$member_id))
{
$grid_ifaces->add_new_button(
url_lang::base().'ifaces/add/'.$device_id,
__('Add new interface')
);
}
$grid_ifaces->field('id')
->label('ID')
->class('center');
$grid_ifaces->field('name')
->label(__('name'));
$grid_ifaces->field('mac')
->label(__('MAC'));
$grid_ifaces->callback_field('segment_name')
->label(__('Segment name'))
->callback('callback::segment_field');
$actions = $grid_ifaces->grouped_action_field();
if ($this->acl_check_view(get_class($this),'iface',$member_id))
{
$actions->add_action('id')
->icon_action('show')
->url('devices/show_iface');
}
if ($this->acl_check_edit(get_class($this),'iface',$member_id))
{
$actions->add_action('id')
->icon_action('edit')
->url('ifaces/edit');
}
if ($this->acl_check_delete('Devices_Controller', 'iface', $member_id))
{
$actions->add_action('id')
->icon_action('delete')
->url('ifaces/delete')
->class('delete_link');
}
$grid_ifaces->datasource($ifaces);

// vlans
$vlan_iface_model = new Vlan_iface_Model();
$vlan_ifaces = $vlan_iface_model->get_all_vlan_ifaces_by_device_id($device->id);

$grid_vlan_ifaces = new Grid(url_lang::base().'devices', null, array
(
'use_paginator' => false,
'use_selector' => false,
'total_items' => count($vlan_ifaces)
));

if ($this->acl_check_new(get_class($this),'vlan_iface',$member_id))
{
$grid_vlan_ifaces->add_new_button(
url_lang::base().'vlan_ifaces/add/0/'.$device_id,
__('Add new VLAN interface')
);
}

$grid_vlan_ifaces->field('vlan_iface_id')
->label('ID')
->class('center');
$grid_vlan_ifaces->callback_field('vlan_iface_name')
->label(__('Name'))
->callback('callback::vlan_iface_field');
$grid_vlan_ifaces->callback_field('vlan_name')
->label(__('VLAN name'))
->callback('callback::vlan_field');
$grid_vlan_ifaces->field('tag_802_1q')
->label(__('tag_802_1q'));
$grid_vlan_ifaces->field('iface_name')
->label(__('Interface'));
$actions = $grid_vlan_ifaces->grouped_action_field();

if ($this->acl_check_edit(get_class($this),'vlan_iface',$member_id))
{
$actions->add_action('vlan_iface_id')
->icon_action('edit')
->url('vlan_ifaces/edit');
}
if ($this->acl_check_delete(get_class($this),'vlan_iface',$member_id))
{
$actions->add_action('vlan_iface_id')
->icon_action('delete')
->url('vlan_ifaces/delete')
->class('delete_link');
}
$grid_vlan_ifaces->datasource($vlan_ifaces);
// ip addresses of device
$ip_address_model = new Ip_address_Model();
$ips = $ip_address_model->get_ip_addresses_of_device($device_id);
$grid_ips = new Grid(url_lang::base().'devices', null, array
(
'use_paginator' => false,
'use_selector' => false,
'total_items' => count($ips)
));

if ($this->acl_check_new(get_class($this), 'ip_address', $member_id))
{
$grid_ips->add_new_button(
url_lang::base().'ip_addresses/add/'.$device_id,
__('Add new ip address')
);
}

$grid_ips->field('id')
->label('ID');
$grid_ips->callback_field('ip_address')
->label(__('IP address'))
->callback('callback::ip_address_field', TRUE);
$grid_ips->callback_field('subnet_name')
->label(__('Subnet name'))
->callback('callback::subnet_field');
$grid_ips->callback_field('id')
->label(__('Interface').' / '.__('VLAN Interface'))
->callback('callback::iface_vlan_iface_field');
$actions = $grid_ips->grouped_action_field();
if ($this->acl_check_edit('Devices_Controller', 'ip_address', $member_id))
{
$actions->add_action('id')
->icon_action('edit')
->url('ip_addresses/edit');
}
if ($this->acl_check_delete('Devices_Controller', 'ip_address', $member_id))
{
$actions->add_action('id')
->icon_action('delete')
->url('ip_addresses/delete')
->class('delete_link');
}
$grid_ips->datasource($ips);
// ports of device
$port_model = new Port_Model();
$ports = $port_model->get_ports_of_device($device_id);
$grid_ports = new Grid(url_lang::base().'devices', null, array
(
'use_paginator' => false,
'use_selector' => false,
'total_items' => count($ports)
));
if ($this->acl_check_new(get_class($this),'port',$member_id))
{
$grid_ports->add_new_button(
url_lang::base().'ports/add/'.$device_id,
__('Add new port')
);
}
$grid_ports->field('id')
->label('ID')
->class('center');
$grid_ports->field('name')
->label(__('Port name'));
$grid_ports->callback_field('segment_id')
->label(__('Segment name'))
->callback('callback::segment_field');
$grid_ports->field('vlan_count')
->label(__('Vlans'))
->class('center');
$actions = $grid_ports->grouped_action_field();
if ($this->acl_check_view(get_class($this),'port',$member_id))
{
$actions->add_action('id')
->icon_action('show')
->url('devices/show_port');
}
if ($this->acl_check_edit(get_class($this), 'port', $member_id))
{
$actions->add_action('id')
->icon_action('edit')
->url('ports/edit');
}
if ($this->acl_check_delete('Devices_Controller', 'port', $member_id))
{
$actions->add_action('id')
->icon_action('delete')
->url('ports/delete')
->class('delete_link');
}
$grid_ports->datasource($ports);

$gps = "";

if (!empty($device->address_point->gps))
{
$gps_result = ORM::factory('address_point')->get_gps_coordinates(
$device->address_point->id
);

if (! empty($gps_result))
{
$gps = gps::degrees($gps_result->gpsx, $gps_result->gpsy, true);
}
}

$sections = array
(
'interfaces' => __('Interfaces'),
'vlan_interfaces' => __('Vlan interfaces'),
'ip_addresses' => __('Ip addresses'),
'ports' => __('Ports'),
'engineers' => __('Engineers'),
'admins' => __('Admins'),
);

$sec_links = array();
foreach ($sections as $anchor => $label)
$sec_links[] = html::anchor(url::base(TRUE).url::current(TRUE).'#'.$anchor, $label);
// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link('members/show_all', 'Members',
$this->acl_check_view('Members_Controller','members'))
->disable_translation()
->link('members/show/' . $device->user->member->id,
'ID ' . $device->user->member->id . ' - ' . $device->user->member->name,
$this->acl_check_view('Members_Controller','members', $device->user->member->id))
->enable_translation()
->link('users/show_by_member/' . $device->user->member_id, 'Users',
$this->acl_check_view('Users_Controller', 'users', $device->user->member_id))
->disable_translation()
->link('users/show/' . $device->user->id,
$device->user->name . ' ' . $device->user->surname . ' (' . $device->user->login . ')',
$this->acl_check_view('Users_Controller', 'users', $device->user->member_id))
->enable_translation()
->link('devices/show_by_user/' . $device->user->id, 'Devices',
$this->acl_check_view(
get_class($this), 'devices',
$device->user->member_id
)
)->disable_translation()
->text(($device->name != '') ? $device->name : $device_type);
// view
$view = new View('main');
$view->title = __('Device').' '.$device->name;
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('devices_show');
$view->content->device = $device;
$view->content->device_type = $device_type;
$view->content->count_ifaces = $iface_model->count_ifaces_of_device($device->id);
$view->content->count_vlan_ifaces = count($vlan_ifaces);
$view->content->count_ports = count($ports);
$view->content->count_ip_addresses = count($ips);
$view->content->count_engineers = count($de);
$view->content->count_admins = count($da);
$view->content->ifaces = $ifaces;
$view->content->ip_iface = $ip_iface;
$view->content->table_device_engineers = $grid_device_engineers;
$view->content->table_device_admins = $grid_device_admins;
$view->content->table_ifaces = $grid_ifaces;
$view->content->table_vlan_ifaces = $grid_vlan_ifaces;
$view->content->table_ports = $grid_ports;
$view->content->table_ip_addresses = $grid_ips;
$view->content->gps = $gps;
$view->content->gpsx = !empty($gps) ? $gps_result->gpsx : '';
$view->content->gpsy = !empty($gps) ? $gps_result->gpsy : '';
$view->content->lang = Config::get('lang');
$view->content->sec_links = implode(' | ', $sec_links);
$view->render(TRUE);
} // end of show
/**
* Function adds new device only.
*
* @param integer $user_id
*/
public function add($user_id = null)
{
$user_model = new User_Model();
// if the device is added to given user, then only one user will be to select
$selected_engineer = $this->session->get('user_id');
$gpsx = '';
$gpsy = '';
if(isset($user_id))
{
$user = new User_Model($user_id);
if ($user->id == 0)
Controller::error(RECORD);
if (!$this->acl_check_new('Devices_Controller', 'devices', $user->member_id))
Controller::error(ACCESS);
$selected = $user->id;
$selected_country_id = $user->member->address_point->country_id;
$selected_street_id = $user->member->address_point->street_id;
$selected_street_number = $user->member->address_point->street_number;
$selected_town_id = $user->member->address_point->town_id;
$gps = $user->member->address_point->get_gps_coordinates();
$device_engineer_model = new Device_engineer_Model();
$found_engineer = $device_engineer_model->get_engineer_of_user($user->id);
if ($gps)
{
$gpsx = gps::real2degrees($gps->gpsx, FALSE);
$gpsy = gps::real2degrees($gps->gpsy, FALSE);
}
if ($found_engineer)
$selected_engineer = $found_engineer->id;
$arr_users[$user->id] = $user->surname.' '.$user->name.' - '.$user->login;
}
else
{
if (!$this->acl_check_new('Devices_Controller', 'devices'))
Controller::error(ACCESS);
$selected = 0;
$selected_country_id = Settings::get('default_country');
$selected_street_id = 0;
$selected_street_number = '';
$selected_town_id = 0;
$arr_users_first = array
(
NULL => '----- '.__('select user').' -----'
);
$arr_users = $arr_users_first + $user_model->select_list_grouped();
}
// list of engineers
if ($this->acl_check_edit('Devices_Controller', 'main_engineer'))
{
$arr_engineers = $user_model->select_list_grouped();
}
else
{
$engineer = new User_Model($this->session->get('user_id'));
$arr_engineers[$engineer->id] = $engineer->surname . ' ' .
$engineer->name . ' - ' . $engineer->login;
}
// types of device
$enum_type_model = new Enum_type_Model();
$types = $enum_type_model->get_values(Enum_type_Model::$device_type_id);
$types[0] = '----- '.__('select type').' -----';
asort($types);
// country
$arr_countries = ORM::factory('country')->select_list('id', 'country_name');
// streets
$street_model = new Street_Model();
$streets_first = array
(
NULL => '----- '.__('select street').' -----'
);
$arr_streets = $streets_first + $street_model->select_list('id', 'street');
// towns
$town_model = new Town_Model();
$towns_first = array
(
NULL => '----- '.__('select town').' -----'
);
$arr_towns = $towns_first + $town_model->select_list_with_quater();

// forge form
$form = new Forge(
url::base(TRUE).url::current(TRUE),'',
'POST', array('id' => 'article_form')
);
$form->set_attr('class', 'form_class')
->set_attr('method', 'post');
$form->group('')
->label(__('Basic data'));
$form->dropdown('user_id')
->label(__('User'))
->options($arr_users)
->rules('required')
->selected($selected);
// name is not required, it is useful to name routers, access points, etc. only
$form->input('name')
->label(__('Device name').':')
->rules('length[1,200]')
->style('width: 500px');
$form->input('trade_name')
->label(__('Trade name').':')
->rules('length[1,50]')
->autocomplete('json/device_trade_name');
$form->dropdown('type')
->label(__('Type').':')
->options($types)
->rules('required');
$form->checkbox('PPPoE_logging_in')
->label('PPPoE')
->value('1');
if ($this->acl_check_new(get_class($this),'login'))
{
$form->input('login')
->label(__('username').':')
->rules('length[0,30]')
->autocomplete('json/device_login');
}
if ($this->acl_check_new(get_class($this),'password'))
{
$form->input('login_password')
->label(__('password').':')
->rules('length[0,30]')
->autocomplete('json/device_password');
}
$form->textarea('comment')
->label(__('Comment').':')
->rules('length[0,254]');
$form->dropdown('first_engineer_id')
->label(__('Engineer').':')
->options($arr_engineers)
->rules('required')
->selected($selected_engineer);
$form->group('')
->label(__('Device repayments'));
$form->input('price')
->label(__('Price').':')
->rules('valid_numeric');
$form->input('payment_rate')
->label(__('Monthly payment rate').':')
->rules('valid_numeric');
$form->date('buy_date')
->label(__('Buy date').':')
->years(date('Y')-100, date('Y'));
$form->group('')
->label(__('Address'));
$form->dropdown('street_id')
->label(__('Street').':')
->options($arr_streets)
->selected($selected_street_id)
->add_button('streets');
$form->input('street_number')
->label(__('Street number').':')
->rules('length[1,50]|valid_numeric')
->value($selected_street_number);
$form->dropdown('town_id')
->label(__('Town').':')
->rules('required')
->options($arr_towns)
->selected($selected_town_id)
->add_button('towns');
$form->dropdown('country_id')
->label(__('country').':')
->rules('required')
->options($arr_countries)
->selected($selected_country_id);
$form->input('gpsx')
->label(__('GPS').'&nbsp;X:&nbsp;'.help::hint('gps_coordinates'))
->rules('gps')
->value($gpsx);
$form->input('gpsy')
->label(__('GPS').'&nbsp;Y:&nbsp;'.help::hint('gps_coordinates'))
->rules('gps')
->value($gpsy);
$form->submit('submit')
->value(__('Save'));
special::required_forge_style($form, ' *', 'required');
// validation
if($form->validate())
{
$form_data = $form->as_array();
// gps
$gpsx = NULL;
$gpsy = NULL;
if (!empty($form_data['gpsx']) && !empty($form_data['gpsy']))
{
$gpsx = doubleval($form_data["gpsx"]);
$gpsy = doubleval($form_data["gpsy"]);

if (gps::is_valid_degrees_coordinate($form->gpsx->value))
{
$gpsx = gps::degrees2real($form->gpsx->value);
}

if (gps::is_valid_degrees_coordinate($form->gpsy->value))
{
$gpsy = gps::degrees2real($form->gpsy->value);
}
}

// device
$device_model = new Device_Model();
$device_model->user_id = htmlspecialchars($form_data["user_id"]);
// access rights
if (!isset($user_id))
$user = new User_Model($device_model->user_id);
if (!$this->acl_check_new(get_class($this), 'devices', $user->member_id))
Controller::error(ACCESS);
if (htmlspecialchars($form_data["name"]) == '')
$device_model->name = $user->login.'_'.$types[htmlspecialchars($form_data["type"])];
else
$device_model->name = htmlspecialchars($form_data["name"]);
$device_model->trade_name = htmlspecialchars($form_data["trade_name"]);
$device_model->type = htmlspecialchars($form_data["type"]);
$device_model->PPPoE_logging_in = htmlspecialchars($form_data["PPPoE_logging_in"]);
if ($this->acl_check_new(get_class($this),'login'))
$device_model->login = htmlspecialchars($form_data["login"]);
if ($this->acl_check_new(get_class($this),'password'))
$device_model->password = htmlspecialchars($form_data["login_password"]);
$device_model->price = htmlspecialchars($form_data["price"]);
$device_model->payment_rate = htmlspecialchars($form_data["payment_rate"]);
$device_model->buy_date = date('Y-m-d', htmlspecialchars($form_data["buy_date"]));
$device_model->comment = htmlspecialchars($form_data["comment"]);

$address_point_model = new Address_point_Model();
$address_point = $address_point_model->get_address_point(
$form_data["country_id"], $form_data["town_id"],
$form_data["street_id"], $form_data["street_number"],
$gpsx, $gpsy
);

// add address point if there is no such
if (!$address_point->id)
{
$address_point->save();
}
// add GPS
if (!empty($gpsx) && !empty($gpsy))
{ // save
$address_point->update_gps_coordinates($address_point->id, $gpsx, $gpsy);
}
else
{ // delete gps
$address_point->gps = '';
$address_point->save();
}

$device_model->address_point_id = $address_point->id;

$device_saved = $device_model->save();
// device engineer
$device_engineer_model = new Device_engineer_Model();
$device_engineer_model->device_id = $device_model->id;
$device_engineer_model->user_id = $form_data['first_engineer_id'];
$device_engineer_saved = $device_engineer_model->save();
unset($form_data);
if ($device_saved && $device_engineer_saved)
{
status::success('Device has been successfully saved.');
}
if (!$this->popup)
{
// classic adding
url::redirect(url_lang::base().'devices/show/'.$device_model->id);
}
}
if (isset($user_id))
{
$headline = __('Add new device for user').
' '.$user->name.' '.$user->surname.' ('.__('Member').
' '.$user->member_id.' '.$user->member->name.')';
// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link('members/show_all', 'Members',
$this->acl_check_view('Members_Controller', 'members'))
->disable_translation()
->link('members/show/' . $user->member->id,
'ID ' . $user->member->id . ' - ' . $user->member->name,
$this->acl_check_view('Members_Controller', 'members', $user->member->id))
->enable_translation()
->link('users/show_by_member/' . $user->member_id, 'Users',
$this->acl_check_view('Users_Controller', 'users', $user->member_id))
->disable_translation()
->link('users/show/' . $user->id,
$user->name . ' ' . $user->surname . ' (' . $user->login . ')',
$this->acl_check_view('Users_Controller', 'users', $user->member_id))
->enable_translation()
->link('devices/show_by_user/' . $user->id, 'Devices',
$this->acl_check_view('Devices_Controller', 'devices', $user->member_id))
->text('Add new device');
}
else
{
// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link('devices/show_all', 'Devices',
$this->acl_check_view('Devices_Controller','devices'))
->text('Add new device');
$headline = __('Add new device');
}
// view
$view = new View('main');
$view->title = __('Add new device');
$view->breadcrumbs = $breadcrumbs->html();
$view->device = isset($device_model) && $device_model->id ? $device_model : NULL;
$view->content = new View('form');
$view->content->form = $form->html();
$view->content->headline = $headline;
$view->render(TRUE);
} // end of add

/**
* Adds whole device. It means it creates new device, new interface assigned to this device
* and new ip address assigned to this interface.
*
* @param integer $user_id
*/
public function add_whole($user_id = null)
{
$user_model = new User_Model();
// if the device is added to given user, then only one user will be to select
$selected_segment = 0;
$selected_subnet = 0;
$selected_engineer = $this->session->get('user_id');
$gpsx = '';
$gpsy = '';
if (isset($user_id))
{
$user = new User_Model($user_id);
if ($user->id == 0)
Controller::error(RECORD);
if (!$this->acl_check_new('Devices_Controller', 'devices', $user->member_id))
Controller::error(ACCESS);
$selected = $user->id;
$selected_country_id = $user->member->address_point->country_id;
$selected_street_id = $user->member->address_point->street_id;
$selected_street_number = $user->member->address_point->street_number;
$selected_town_id = $user->member->address_point->town_id;
$gps = $user->member->address_point->get_gps_coordinates();
$segment_model = new Segment_Model();
$found_segment = $segment_model->get_segment_of_user($user->id);
if ($gps)
{
$gpsx = gps::real2degrees($gps->gpsx, FALSE);
$gpsy = gps::real2degrees($gps->gpsy, FALSE);
}
if ($found_segment)
{
$selected_segment = $found_segment->id;
}
$subnet_model = new Subnet_Model();
$found_subnet = $subnet_model->get_subnet_of_user($user->id);
if ($found_subnet)
$selected_subnet = $found_subnet->id;
$device_engineer_model = new Device_engineer_Model();
$found_engineer = $device_engineer_model->get_engineer_of_user($user->id);
if ($found_engineer)
$selected_engineer = $found_engineer->id;
$arr_users[$user->id] = $user->surname.' '.$user->name.' - '.$user->login;
}
else
{
if (!$this->acl_check_new('Devices_Controller', 'devices'))
Controller::error(ACCESS);
$selected = 0;
$selected_country_id = Settings::get('default_country');
$selected_street_id = 0;
$selected_street_number = '';
$selected_town_id = 0;
$arr_users_first = array(NULL => '----- '.__('select user').' -----');
$arr_users = $arr_users_first + $user_model->select_list_grouped();
}
// enum types for device
$enum_type_model = new Enum_type_Model();
$types = $enum_type_model->get_values(Enum_type_Model::$device_type_id);
$types[0] = '----- '.__('select type').' -----';
asort($types);
// enum types for wireless setting
$arr_modes = $enum_type_model->get_values(Enum_type_Model::$mode_type_id);
$arr_modes[0] = '----- '.__('select').' -----';
asort($arr_modes);
$arr_norms = $enum_type_model->get_values(Enum_type_Model::$norm_type_id);
$arr_norms[0] = '----- '.__('select').' -----';
asort($arr_norms);
$arr_antennas = $enum_type_model->get_values(Enum_type_Model::$antenna_type_id);
$arr_antennas[0] = '----- '.__('select').' -----';
asort($arr_antennas);
$arr_polarizations = $enum_type_model->get_values(Enum_type_Model::$polarization_type_id);
$arr_polarizations[0] = '----- '.__('select').' -----';
asort($arr_polarizations);
// subnets
$subnet_model = new Subnet_Model();
$subnets = $subnet_model->get_all_subnets_order_by_net();
$arr_subnets_netnames[] = $arr_subnets_names[] = '----- '.__('select subnet').' -----';
$arr_subnets_ids[] = $arr_subnets_masks[] = $arr_subnets_nets[]=0;
foreach ($subnets as $subnet)
{
$arr_subnets_names[$subnet->id] = $subnet->net_str."/".$subnet->mask.": ".$subnet->name;
$this->arr_mask_by_id[$subnet->id]=$subnet->mask;
$this->arr_net_by_id[$subnet->id]=$subnet->net;
$arr_subnets_nets[] = $subnet->net;
$arr_subnets_masks[] = $subnet->mask;
$arr_subnets_netnames[] = $subnet->net_str."/".$subnet->mask.": ".$subnet->name ;
$arr_subnets_ids[]=$subnet->id;
}
array_multisort($arr_subnets_nets, SORT_NUMERIC, SORT_ASC,
$arr_subnets_masks, SORT_NUMERIC, SORT_ASC,
$arr_subnets_ids, SORT_NUMERIC, SORT_ASC,
$arr_subnets_netnames, SORT_STRING, SORT_ASC);
// segments
$segment_model = new Segment_Model();
$segment_first = array
(
NULL => '----- '.__('select segment').' -----'
);
$arr_segments = $segment_first + $segment_model->select_list('id', 'name');
// country
$arr_countries = ORM::factory('country')->select_list('id', 'country_name');
// streets
$street_model = new Street_Model();
$streets_first = array
(
NULL => '----- '.__('select street').' -----'
);
$arr_streets = $streets_first + $street_model->select_list('id', 'street');
// towns
$town_model = new Town_Model();
$towns_first = array
(
NULL => '----- '.__('select town').' -----'
);
$arr_towns = $towns_first + $town_model->select_list_with_quater();

// list of engineers
if ($this->acl_check_edit('Devices_Controller', 'main_engineer'))
{
$arr_engineers = $user_model->select_list_grouped();
}
else
{
$engineer = new User_Model($this->session->get('user_id'));
$arr_engineers[$engineer->id] = $engineer->surname.' '.$engineer->name.' - '.$engineer->login;
}
// forge form
$form = new Forge(
url_lang::base().'devices/add_whole', '',
'POST', array('id' => 'article_form')
);
$form->set_attr('class', 'form_class')
->set_attr('method', 'post');
$form->group('')
->label(__('Device'));
$form->dropdown('user_id')
->label(__('user').':')
->rules('required')
->options($arr_users)
->selected($selected);
// name is not required, it is useful to name routers, access points, etc. only
$form->input('name')
->label(__('Device name').':')
->rules('length[2,200]')
->style('width: 500px');;
$form->input('trade_name')
->label(__('Trade name').':')
->rules('length[2,50]')
->autocomplete('json/device_trade_name');
$form->dropdown('type')
->label(__('Type').':')
->options($types)
->rules('required');
$form->checkbox('PPPoE_logging_in')
->label(__('PPPoE').':')
->value('1');
if ($this->acl_check_new(get_class($this),'login'))
{
$form->input('login')
->label(__('username').':')
->rules('length[0,30]')
->autocomplete('json/device_login');
}
if ($this->acl_check_new(get_class($this),'password'))
{
$form->input('login_password')
->label(__('password').':')
->rules('length[0,30]')
->autocomplete('json/device_password');
}
$form->textarea('comment')
->label(__('comment').':')
->rules('length[0,254]');
$form->dropdown('first_engineer_id')
->label(__('engineer').':')
->options($arr_engineers)
->rules('required')
->selected($selected_engineer);
$form->group('')
->label(__('Device repayments'));
$form->input('price')
->label(__('Price').':')
->rules('valid_numeric');
$form->input('payment_rate')
->label(__('Monthly payment rate').':')
->rules('valid_numeric');
$form->date('buy_date')
->label(__('Buy date').':')
->years(date('Y')-100, date('Y'));
$form->group('')
->label(__('Address'));
$form->dropdown('street_id')
->label(__('street').':')
->options($arr_streets)
->selected($selected_street_id)
->add_button('streets');
$form->input('street_number')
->label(__('street number').':')
->rules('length[1,50]|valid_numeric')
->value($selected_street_number);
$form->dropdown('town_id')
->label(__('town').':')
->rules('required')
->options($arr_towns)
->selected($selected_town_id)
->add_button('towns');
$form->dropdown('country_id')
->label(__('country').':')
->rules('required')
->options($arr_countries)
->selected(Settings::get('default_country'));
$form->input('gpsx')
->label(__('GPS').'&nbsp;X:&nbsp;'.help::hint('gps_coordinates'))
->rules('gps')
->value($gpsx);
$form->input('gpsy')
->label(__('GPS').'&nbsp;Y:&nbsp;'.help::hint('gps_coordinates'))
->rules('gps')
->value($gpsy);

// adding interface
$form->group('')
->label(__('Interface'));
$form->input('mac')
->label(__('MAC').':')
->rules('required|valid_mac_address');
$form->dropdown('segment_id')
->label(__('Segment name').':')
->options($arr_segments)
->rules('required')
->selected($selected_segment)
->add_button('segments');

// adding ip address
$form->group('')
->label(__('IP address'));
$form->input('ip_address')
->label(__('IP address').':')
->rules('required|valid_ip_address')
->callback(array($this, 'valid_ip'));
$form->dropdown('subnet_id')
->label(__('Select subnet name').':')
->rules('required')
->options($arr_subnets_names)
->selected($selected_subnet)
->add_button('subnets');

// submit button
$form->submit('submit')
->value(__('Save'));
special::required_forge_style($form, ' *', 'required');
// validates form and saves data
if($form->validate())
{
$form_data = $form->as_array();
// gps
$gpsx = NULL;
$gpsy = NULL;
if (!empty($form_data['gpsx']) && !empty($form_data['gpsy']))
{
$gpsx = doubleval($form_data["gpsx"]);
$gpsy = doubleval($form_data["gpsy"]);

if (gps::is_valid_degrees_coordinate($form->gpsx->value))
{
$gpsx = gps::degrees2real($form->gpsx->value);
}

if (gps::is_valid_degrees_coordinate($form->gpsy->value))
{
$gpsy = gps::degrees2real($form->gpsy->value);
}
}
// device model
$device_model = new Device_Model();
$device_model->user_id = $form_data["user_id"];
// access rights
if (!isset($user_id))
$user = new User_Model($device_model->user_id);
if (!$this->acl_check_new(get_class($this), 'devices', $user->member_id))
Controller::error(ACCESS);
if (empty($form_data["name"]))
$device_model->name = $user->login.'_'.$types[htmlspecialchars($form_data["type"])];
else
$device_model->name = htmlspecialchars($form_data["name"]);
$device_model->trade_name = htmlspecialchars($form_data["trade_name"]);
$device_model->type = htmlspecialchars($form_data["type"]);
$device_model->PPPoE_logging_in = htmlspecialchars($form_data["PPPoE_logging_in"]);
if ($this->acl_check_new(get_class($this),'login'))
$device_model->login = htmlspecialchars($form_data["login"]);
if ($this->acl_check_new(get_class($this),'password'))
$device_model->password = htmlspecialchars($form_data["login_password"]);
$device_model->price = htmlspecialchars($form_data["price"]);
$device_model->payment_rate = htmlspecialchars($form_data["payment_rate"]);
$device_model->buy_date = date('Y-m-d', htmlspecialchars($form_data["buy_date"]));
$device_model->comment = htmlspecialchars($form_data["comment"]);

$address_point_model = new Address_point_Model();
$address_point = $address_point_model->get_address_point(
$form_data["country_id"], $form_data["town_id"],
$form_data["street_id"], $form_data["street_number"],
$gpsx, $gpsy
);

// add address point if there is no such
if (!$address_point->id)
{
$address_point->save();
}
// add GPS
if (!empty($gpsx) && !empty($gpsy))
{ // save
$address_point->update_gps_coordinates($address_point->id, $gpsx, $gpsy);
}
else
{ // delete gps
$address_point->gps = '';
$address_point->save();
}

$device_model->address_point_id = $address_point->id;

$device_saved = $device_model->save();
// device engineer model
$device_engineer = new Device_engineer_Model();
$device_engineer->device_id = $device_model->id;
$device_engineer->user_id = $form_data["first_engineer_id"];
$device_engineer_saved = $device_engineer->save();

// interface model
$iface_model = new Iface_Model();
$iface_model->device_id = $device_model->id;
$iface_model->segment_id = $form_data['segment_id'];
$iface_model->mac = htmlspecialchars($form_data["mac"]);
$iface_model->name = $device_model->name;
$iface_model_saved = $iface_model->save();
// ip address model
$ip_address_model = new ip_address_Model();
$ip_address_model->iface_id = $iface_model->id;
$ip_address_model->vlan_iface_id = NULL;
$ip_address_model->subnet_id = $form_data['subnet_id'];
$ip_address_model->ip_address = $form_data['ip_address'];
$ip_address_model->dhcp = NULL;
$ip_address_saved = $ip_address_model->save();

Allowed_subnets_Controller::update_enabled(
$user->member_id, array($ip_address_model->subnet_id)
);
unset($form_data);

// has been everything saved successfully?
if ($device_saved && $device_engineer_saved && $iface_model_saved &&
$ip_address_saved)
{
status::success('Device has been successfully saved.');
}
url::redirect(url_lang::base().'devices/show/'.$device_model->id);
}
if (isset($user_id))
{
// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link('members/show_all', 'Members',
$this->acl_check_view('Members_Controller', 'members'))
->disable_translation()
->link('members/show/' . $user->member->id,
'ID ' . $user->member->id . ' - ' . $user->member->name,
$this->acl_check_view('Members_Controller', 'members', $user->member->id))
->enable_translation()
->link('users/show_by_member/' . $user->member_id, 'Users',
$this->acl_check_view('Users_Controller', 'users', $user->member_id))
->disable_translation()
->link('users/show/' . $user->id,
$user->name . ' ' . $user->surname . ' (' . $user->login . ')',
$this->acl_check_view('Users_Controller', 'users', $user->member_id))
->enable_translation()
->link('devices/show_by_user/' . $user->id, 'Devices',
$this->acl_check_view('Devices_Controller', 'devices', $user->member_id))
->text('Add new whole device');
}
else
{
// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link('devices/show_all', 'Devices',
$this->acl_check_view('Devices_Controller','devices'))
->text('Add new whole device');
}
$view = new View('main');
$view->title = __('Add new whole device');
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('form');
$view->content->form = $form->html();
$view->content->headline = __('Add new whole device');
$view->render(TRUE);
} // end of function add_whole

/**
* Function edits device.
*
* @param integer $device_id
*/
public function edit($device_id = null)
{
if (!isset($device_id) || !is_numeric($device_id))
Controller::warning(PARAMETER);
$device = new Device_Model($device_id);
if ($device->id == 0)
Controller::error(RECORD);
if (!$this->acl_check_edit(get_class($this),'devices',$device->user->member_id))
Controller::error(ACCESS);

// gps
$gpsx = "";
$gpsy = "";
if (!empty($device->address_point->gps))
{
$gps_result = $device->address_point->get_gps_coordinates($device->address_point->id);

if (!empty($gps_result))
{
$gpsx = gps::real2degrees($gps_result->gpsx, false);
$gpsy = gps::real2degrees($gps_result->gpsy, false);
}
}
// users
$user_model = new User_Model;
$arr_users = $user_model->select_list_grouped();
// types
$enum_type_model = new Enum_type_Model();
$types = $enum_type_model->get_values(Enum_type_Model::$device_type_id);

// country
$arr_countries = ORM::factory('country')->select_list('id', 'country_name');
// streets
$street_model = new Street_Model();
$streets_first = array
(
NULL => '----- '.__('select street').' -----'
);
$arr_streets = $streets_first + $street_model->select_list('id', 'street');
// towns
$town_model = new Town_Model();
$towns_first = array
(
NULL => '----- '.__('select town').' -----'
);
$arr_towns = $towns_first + $town_model->select_list_with_quater();
$form = new Forge(
url_lang::base()."devices/edit/".$device_id, '',
'POST', array('id' => 'article_form')
);
$form->set_attr('class', 'form_class')
->set_attr('method', 'post');
$form->group('')
->label(__('Basic data'));
$form->dropdown('user_id')
->label(__('user'))
->options($arr_users)
->rules('required')
->selected($device->user_id);
$form->input('name')
->label(__('Device name').':')
->rules('length[1,200]')
->value($device->name)
->style('width: 500px');
$form->input('trade_name')
->label(__('Trade name').':')
->rules('length[1,50]')
->value($device->trade_name)
->autocomplete('json/device_trade_name');
$form->dropdown('type')
->label(__('Type'))
->options($types)
->rules('required')
->selected($device->type);
$form->checkbox('PPPoE_logging_in')
->label(__('PPPoE'))
->value('1')
->checked($device->PPPoE_logging_in);
if ($this->acl_check_edit(get_class($this),'login'))
{
$form->input('login')
->label(__('username').':')
->rules('length[0,30]')
->value($device->login)
->autocomplete('json/device_login');
}
if ($this->acl_check_edit(get_class($this),'password'))
{
$form->input('login_password')
->label(__('password').':')
->rules('length[0,30]')
->value($device->password)
->autocomplete('json/device_password');
}
$form->textarea('comment')
->label(__('Comment').':')
->rules('length[0,254]')
->value($device->comment);
$form->group('')
->label(__('Device repayments'));
$form->input('price')
->label(__('Price').':')
->rules('valid_numeric')
->value($device->price);
$form->input('payment_rate')
->label(__('Monthly payment rate').':')
->rules('valid_numeric')
->value($device->payment_rate);
$form->date('buy_date')
->label(__('Buy date').':')
->years(date('Y')-100, date('Y'))
->value(strtotime($device->buy_date));
$form->group('')
->label(__('Address'));
$form->dropdown('street_id')
->label(__('Street').':')
->options($arr_streets)
->selected($device->address_point->street_id)
->add_button('streets');
$form->input('street_number')
->label(__('Street number').':')
->rules('length[1,50]|valid_numeric')
->value($device->address_point->street_number);
$form->dropdown('town_id')
->label(__('Town').':')
->rules('required')
->options($arr_towns)
->selected($device->address_point->town_id)
->add_button('towns');
$form->dropdown('country_id')
->label(__('country').':')
->rules('required')
->options($arr_countries)
->selected($device->address_point->country_id);
$form->input('gpsx')
->label(__('GPS').'&nbsp;X:&nbsp;'.help::hint('gps_coordinates'))
->value($gpsx)
->rules('gps');
$form->input('gpsy')
->label(__('GPS').'&nbsp;Y:&nbsp;'.help::hint('gps_coordinates'))
->value($gpsy)
->rules('gps');
$form->submit('submit')
->value(__('Edit'));
special::required_forge_style($form, ' *', 'required');
// validation
if($form->validate())
{
$form_data = $form->as_array();
// saves device changes
$device = new Device_Model($device_id);
foreach($form_data as $key => $value)
{
$form_data[$key] = htmlspecialchars($value);
}
// gps
$gpsx = NULL;
$gpsy = NULL;
if (!empty($form_data['gpsx']) && !empty($form_data['gpsy']))
{
$gpsx = doubleval($form_data["gpsx"]);
$gpsy = doubleval($form_data["gpsy"]);

if (gps::is_valid_degrees_coordinate($form->gpsx->value))
{
$gpsx = gps::degrees2real($form->gpsx->value);
}

if (gps::is_valid_degrees_coordinate($form->gpsy->value))
{
$gpsy = gps::degrees2real($form->gpsy->value);
}
}

$device->user_id = $form_data["user_id"];
$device->name = $form_data["name"];
$device->type = $form_data["type"];
$device->trade_name = $form_data["trade_name"];
$device->PPPoE_logging_in = $form_data["PPPoE_logging_in"];
if ($this->acl_check_new(get_class($this),'login'))
$device->login = $form_data["login"];
if ($this->acl_check_new(get_class($this),'password'))
$device->password = $form_data["login_password"];
$device->comment = $form_data["comment"];
$device->price = $form_data["price"];
$device->payment_rate = $form_data["payment_rate"];
$device->buy_date = date('Y-m-d', $form_data["buy_date"]);
$address_point_model = new Address_point_Model();
$address_point = $address_point_model->get_address_point(
$form_data["country_id"], $form_data["town_id"],
$form_data["street_id"], $form_data["street_number"],
$gpsx, $gpsy
);
$device_saved = $device->save();

// add address point if there is no such
if (!$address_point->id)
{
// save
$address_point->save();
}
// new addresspoint
if ($address_point->id != $device->address_point_id)
{
// delete old?
$addr_id = $device->address_point->id;
// add to device
$device->address_point_id = $address_point->id;
$device_saved = $device->save();
// change just for this device?
if ($address_point->count_all_items_by_address_point_id($addr_id) < 1)
{
$addr = new Address_point_Model($addr_id);
$addr->delete();
}
}
// add GPS
if (!empty($gpsx) && !empty($gpsy))
{ // save
$address_point->update_gps_coordinates($address_point->id, $gpsx, $gpsy);
}
else
{ // delete gps
$address_point->gps = '';
$address_point->save();
}
unset($form_data);
if ($device_saved)
{
status::success('Device has been successfully updated.');
url::redirect(url_lang::base().'devices/show/'.$device->id);
}
} // end of validation
// breadcrumbs navigation
$breadcrumbs = breadcrumbs::add()
->link('members/show_all', 'Members',
$this->acl_check_view('Members_Controller', 'members'))
->disable_translation()
->link('members/show/' . $device->user->member->id,
'ID ' . $device->user->member->id . ' - ' . $device->user->member->name,
$this->acl_check_view('Members_Controller', 'members', $device->user->member->id))
->enable_translation()
->link('users/show_by_member/' . $device->user->member_id, 'Users',
$this->acl_check_view('Users_Controller', 'users', $device->user->member_id))
->disable_translation()
->link('users/show/' . $device->user->id,
$device->user->name . ' ' . $device->user->surname . ' (' . $device->user->login . ')',
$this->acl_check_view('Users_Controller', 'users', $device->user->member_id))
->enable_translation()
->link('devices/show_by_user/' . $device->user->id, 'Devices',
$this->acl_check_view('Devices_Controller', 'devices', $device->user->member_id))
->disable_translation()
->link('devices/show/' . $device->id . '#device_' . $device_id . '_link',
$device->name,
$this->acl_check_edit('Devices_Controller', 'devices', $device->user->member_id))
->enable_translation()
->text('Edit device');
// view
$view = new View('main');
$view->title = __('Edit device');
$view->breadcrumbs = $breadcrumbs->html();
$view->content = new View('form');
$view->content->form = $form->html();
$view->content->headline = __('Edit device').': '.$device->name;
$view->render(TRUE);
} // end of function edit
/**
* Deletes device. Devices with dependent interfaces and ports.
*
* @author Jiri Svitak
* @param integer $device_id
*/
public function delete($device_id = null)
{
if (!isset($device_id))
Controller::warning(PARAMETER);
$device = new Device_Model($device_id);
if ($device->id == 0)
Controller::error(RECORD);
if ($this->session->get('ssUser_id'))
$linkback = url_lang::base()."devices/show_by_user/".$device->user_id;
else
$linkback = url_lang::base()."devices/show_all";
if (!$this->acl_check_delete('Devices_Controller', 'devices', $device->user->member_id))
Controller::error(ACCESS);
$ifaces = $device->ifaces;
$ports = $device->ports;
if (count($ifaces) == 0 && count($ports) == 0)
{
// it deletes relations from pivot tables
$device->delete_from_pivot_table($device->id);
if ($device->delete())
{
status::success('Device has been successfully deleted.');
}
else
{
status::error('Error - cant delete device.');
}
}
else
{
if (count($ifaces) != 0)
{
status::warning('Device still has at least one interface.');
}
else
{
status::warning('Device still has at least one port.');
}
}
// redirect
url::redirect($linkback);
}
/**
* Deletes device including all its interfaces and ip adresses.
* Devices with ports are not deleted.
*
* @author Jiri Svitak
* @param integer $device_id
*/
public function delete_whole($device_id = null)
{
if (!isset($device_id))
Controller::warning(PARAMETER);
$device = new Device_Model($device_id);
if ($device->id == 0)
Controller::error(RECORD);
if (!$this->acl_check_delete('Devices_Controller', 'devices', $device->user->member_id))
Controller::error(ACCESS);

$linkback = Path::instance()->previous()->current();

if (url::slice(url_lang::uri($linkback),1,1) == 'show')
$linkback = Path::instance()->previous()->current();
// count of deleted interfaces
$deleted_ifaces = 0;
// count of deleted ip adresses
$deleted_ips = 0;
// dependent interfaces and ports
$ifaces = $device->ifaces;
$ports = $device->ports;
// deleting of ports is not implemented yet
if (count($ports) > 0)
{
status::success(
__('Device still has at least one port.')." ".
__('Deleted').": $deleted_ifaces ".
__("Interfaces").", $deleted_ips IP",
FALSE
);
url::redirect($linkback);
}
// going through interfaces
foreach($ifaces as $iface)
{
// ORM doesn't work in this case (1:0 relation), so number of wireless settings has to be detected manually
// only solution seems to be upgrading to higher version of kohana
$ws_count = $iface->count_ws_of_iface($iface->id);
// find vlan_ifaces
$vlan_ifaces = $iface->vlan_ifaces;
if ($ws_count > 0)
{
status::success(
__('Interface still has at least one VLAN interface.')." ".
__('Deleted').": $deleted_ifaces ".
__("Interfaces").", $deleted_ips IP",
FALSE
);
url::redirect($linkback);
}
if (count($vlan_ifaces) > 0)
{
status::success(
__('Interface still has at least one wireless setting.')." ".
__('Deleted').": $deleted_ifaces ".
__("Interfaces").", $deleted_ips IP",
FALSE
);
url::redirect($linkback);
}
$ips = $iface->ip_addresses;
foreach($ips as $ip)
{
// before deleting IP address it is necessary to delete redirection from junction table first
$db = new Database();
$db->delete('messages_ip_addresses', array('ip_address_id' => $ip->id));
// delete IP address
$ip->delete();
$deleted_ips++;
}
$iface->delete();
$deleted_ifaces++;
}
// it deletes relations from pivot tables
$device->delete_from_pivot_table($device->id);
if ($device->delete())
{
status::success(
__('Device has been successfully deleted.')." ".
__('Deleted').": $deleted_ifaces ".
__("Interfaces").", $deleted_ips IP",
FALSE
);
}
else
{
status::error('Error - cant delete device.');
}
// redirect
url::redirect($linkback);
}

/**
* Shows port
*
* @param integer $port_id
*/
public function show_port($port_id = NULL)
{
Ports_Controller::show($port_id);
}
/**
* Shows iface
*
* @param integer $iface_id
*/
public function show_iface($iface_id = NULL)
{
Ifaces_Controller::show($iface_id);
}
public function show_vlan_iface($vlan_iface_id = NULL)
{
Vlan_ifaces_Controller::show($vlan_iface_id);
}
/**
*
* Shows IP address
*
* @author Michal Kliment
* @param type $ip_address_id
*/
public function show_ip_address ($ip_address_id = NULL)
{
Ip_addresses_Controller::show($ip_address_id);
}

/**
* Function checks validity of ip address.
*
* @param $input ip address to validate
*/
public function valid_ip($input)
{
//$method=$form->ip_address->method; // <FORM> method = POST, GET, ...
// ip2long??? why? half of all ipv4 addresses cannot be added (jsvitak)
$ip = ip2long($this->input->post('ip_address')); // Submitted values
$subnet_id = $this->input->post('subnet_id');
// if subnet was not selected
if ($subnet_id == 0)
{
// try to find a subnet matching to the ip
if ($ip > 0)
{
$input->add_error('required', __('Subnet has not been selected.'));
}
else
{
$input->add_error('required', __('Invalid IP address'));
}
return false;
}
$subnet = $this->arr_net_by_id[$subnet_id];
$mask = $this->arr_mask_by_id[$subnet_id];
// checks if exists this ip in database
$ip_model = new Ip_address_Model();
if ($ip_model->where('ip_address', $this->input->post('ip_address'))->count_all() > 0)
{
$input->add_error('required', __('IP address already exists.'));
}
}
/**
* Function checks ip address if matches subnet and mask.
*
* @param string $ip
* @param string $net
* @param string $mask
* @param object $input
*/
public function check_ip($ip, $net, $mask, $input)
{
$mask = 0xffffffff << (32 - $mask) & 0xffffffff;
//printf(" ip=%lx, net=%lx, mask=%lx, AND=%lx", $ip, $net, $mask, $ip & $mask);
if (($ip & $mask) != $net)
{
$input->add_error('required', __('IP address does not match the subnet/mask.'));
}
else if ($ip == $net || $ip == ($net | ~$mask))
{
$input->add_error('required', __('Invalid IP address'));
}
}
/**
* Functions validate wireless settings when "add_wireless" checkbox is checked.
*
* @param object $input
*/
public function valid_mode($input)
{
if (!$input->value && $this->input->post('add_wireless'))
{
$input->add_error('required', __('Value has not been entered.'));
}
}
/**
* Functions validate norm
*
* @param object $input
*/
public function valid_norm($input)
{
if (!$input->value && $this->input->post('add_wireless'))
{
$input->add_error('required', __('Value has not been entered.'));
}
}
/**
* Functions validate antenna
*
* @param object $input
*/
public function valid_antenna($input)
{
if (!$input->value && $this->input->post('add_wireless'))
{
$input->add_error('required', __('Value has not been entered.'));
}
}
/**
* Functions validate polarization
*
* @param object $input
*/
public function valid_polarization($input)
{
if (!$input->value && $this->input->post('add_wireless'))
{
$input->add_error('required', __('Value has not been entered.'));
}
}

}
(18-18/76)