Revize c1bdc1c4
Přidáno uživatelem Michal Kliment před více než 9 roky(ů)
application/controllers/device_templates.php | ||
---|---|---|
*/
|
||
class Device_templates_Controller extends Controller
|
||
{
|
||
/**
|
||
* Constructor, only test if networks is enabled
|
||
*/
|
||
public function __construct()
|
||
{
|
||
parent::__construct();
|
||
|
||
// access control
|
||
if (!Settings::get('networks_enabled'))
|
||
Controller::error (ACCESS);
|
||
}
|
||
|
||
/**
|
||
* Index redirects to show all
|
||
*/
|
||
... | ... | |
public function show_all()
|
||
{
|
||
// access check
|
||
if (!$this->acl_check_view('Devices_Controller', 'devices'))
|
||
if (!$this->acl_check_view('Device_templates_Controller', 'device_template'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
... | ... | |
'use_selector' => false
|
||
));
|
||
|
||
if ($this->acl_check_new('Devices_Controller', 'devices'))
|
||
if ($this->acl_check_new('Device_templates_Controller', 'device_template'))
|
||
{
|
||
$grid->add_new_button('device_templates/add', 'Add new template');
|
||
$grid->add_new_button('device_templates/import_from_file', 'Import device templates');
|
||
$grid->add_new_button('device_templates/export_to_json', 'Export device templates');
|
||
}
|
||
|
||
$grid->add_new_button('device_templates/export_to_json', 'Export device templates');
|
||
|
||
$grid->field('id')
|
||
->label('ID');
|
||
... | ... | |
->icon_action('show')
|
||
->url('device_templates/show');
|
||
|
||
if ($this->acl_check_edit('Devices_Controller', 'devices'))
|
||
if ($this->acl_check_edit('Device_templates_Controller', 'device_template'))
|
||
{
|
||
$actions->add_action()
|
||
->icon_action('edit')
|
||
->url('device_templates/edit');
|
||
}
|
||
|
||
if ($this->acl_check_delete('Devices_Controller', 'devices'))
|
||
if ($this->acl_check_delete('Device_templates_Controller', 'device_template'))
|
||
{
|
||
$actions->add_action()
|
||
->icon_action('delete')
|
||
... | ... | |
}
|
||
|
||
// check acess
|
||
if (!$this->acl_check_view('Devices_Controller', 'devices'))
|
||
if (!$this->acl_check_view('Device_templates_Controller', 'device_template'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
... | ... | |
Controller::error(RECORD);
|
||
}
|
||
|
||
$device_active_link = new Device_active_link_Model();
|
||
$active_links = $device_active_link->get_device_active_links(
|
||
$device_template_id,
|
||
Device_active_link_Model::TYPE_TEMPLATE
|
||
);
|
||
|
||
$active_links_grid = new Grid('devices', null, array
|
||
(
|
||
'use_paginator' => false,
|
||
'use_selector' => false,
|
||
'total_items' => $active_links->count()
|
||
));
|
||
|
||
$active_links_grid->Field('id')
|
||
->label('ID');
|
||
|
||
$active_links_grid->Field('url_pattern')
|
||
->label('URL pattern');
|
||
|
||
$active_links_grid->Field('name');
|
||
|
||
$active_links_grid->Field('title');
|
||
|
||
if ($this->acl_check_view('Device_active_links_Controller', 'active_links'))
|
||
{
|
||
$actions = $active_links_grid->grouped_action_field();
|
||
|
||
$actions->add_action('id')
|
||
->icon_action('show')
|
||
->url('device_active_links/show');
|
||
}
|
||
|
||
$active_links_grid->datasource($active_links);
|
||
|
||
$headline = $device_templates_model->name . ' (' . $device_template_id . ')';
|
||
|
||
// bread crumbs
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('device_templates/show_all', 'Devices templates',
|
||
$this->acl_check_view('Devices_Controller', 'devices'))
|
||
$this->acl_check_view('Device_templates_Controller', 'device_template'))
|
||
->disable_translation()
|
||
->text($headline)
|
||
->html();
|
||
... | ... | |
$view->content->device_template = $device_templates_model;
|
||
$view->content->iface_model = new Iface_Model();
|
||
$view->content->ivals = $device_templates_model->get_value();
|
||
$view->content->active_links_grid = $active_links_grid;
|
||
$view->render(TRUE);
|
||
}
|
||
|
||
... | ... | |
public function add($enum_type_id = NULL)
|
||
{
|
||
// check access
|
||
if (!$this->acl_check_new('Devices_Controller', 'devices'))
|
||
if (!$this->acl_check_new('Device_templates_Controller', 'device_template'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
... | ... | |
NULL => '---- ' . __('Select type') . ' ----'
|
||
) + $et_model->get_values(Enum_type_Model::DEVICE_TYPE_ID);
|
||
|
||
// Device active links
|
||
$device_active_links_model = new Device_active_link_Model();
|
||
|
||
$all_active_links = $device_active_links_model->get_all_active_links();
|
||
|
||
$active_links = array();
|
||
|
||
foreach($all_active_links AS $active_link)
|
||
{
|
||
if (!$active_link->name)
|
||
$active_links[$active_link->id] = $active_link->title;
|
||
else
|
||
$active_links[$active_link->id] = $active_link->name.' ('.$active_link->title.')';
|
||
}
|
||
|
||
// form
|
||
$form = new Forge();
|
||
|
||
... | ... | |
$form->checkbox('default')
|
||
->label('Default for this device type?');
|
||
|
||
$form->dropdown('active_links_select[]')
|
||
->label('Device active links')
|
||
->options($active_links)
|
||
->multiple('multiple')
|
||
->size(10);
|
||
|
||
// eth
|
||
|
||
$eth_form = $form->group('Ethernet interfaces');
|
||
... | ... | |
// data
|
||
$form_data = $form->as_array(FALSE);
|
||
$vals = $_POST['values'];
|
||
$vals['default_iface'] = @$_POST['default_iface'];
|
||
|
||
// parse values
|
||
$this->validate_form_value($vals);
|
||
... | ... | |
$tdefault->save();
|
||
}
|
||
}
|
||
|
||
|
||
// map to device active links
|
||
$device_active_links_model->map_device_to_active_links(
|
||
$device_template_model,
|
||
$form_data['active_links_select'],
|
||
Device_active_link_Model::TYPE_TEMPLATE
|
||
);
|
||
|
||
// clean
|
||
unset($vals);
|
||
unset($form_data);
|
||
... | ... | |
// bread crumbs
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('device_templates/show_all', 'Device templates',
|
||
$this->acl_check_view('Devices_Controller', 'devices'))
|
||
$this->acl_check_view('Device_templates_Controller', 'device_template'))
|
||
->disable_translation()
|
||
->text($headline)
|
||
->html();
|
||
... | ... | |
}
|
||
|
||
// check access
|
||
if (!$this->acl_check_delete('Devices_Controller', 'devices'))
|
||
if (!$this->acl_check_edit('Device_templates_Controller', 'device_template'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
... | ... | |
// values
|
||
$ivals = $device_template_model->get_value();
|
||
|
||
// Device active links
|
||
$device_active_links_model = new Device_active_link_Model();
|
||
|
||
$all_active_links = $device_active_links_model->get_all_active_links();
|
||
$all_selected_active_link = $device_active_links_model->get_device_active_links(
|
||
$device_templates_id,
|
||
Device_active_link_Model::TYPE_TEMPLATE
|
||
);
|
||
|
||
$active_links = array();
|
||
|
||
foreach($all_active_links AS $active_link)
|
||
{
|
||
if (!$active_link->name)
|
||
$active_links[$active_link->id] = $active_link->title;
|
||
else
|
||
$active_links[$active_link->id] = $active_link->name.' ('.$active_link->title.')';
|
||
}
|
||
|
||
$selected_active_links = array();
|
||
|
||
foreach ($all_selected_active_link AS $active_link)
|
||
{
|
||
$selected_active_links[] = $active_link->id;
|
||
}
|
||
|
||
// form
|
||
$form = new Forge('device_templates/edit/' . $device_templates_id);
|
||
|
||
... | ... | |
->label('Default for this device type?')
|
||
->checked($device_template_model->default);
|
||
|
||
$form->dropdown('active_links[]')
|
||
->label('Device active links')
|
||
->options($active_links)
|
||
->selected($selected_active_links)
|
||
->multiple('multiple')
|
||
->size(10);
|
||
|
||
|
||
// eth
|
||
|
||
$eth_form = $form->group('Ethernet interfaces');
|
||
... | ... | |
// data
|
||
$form_data = $form->as_array(FALSE);
|
||
$vals = $_POST['values'];
|
||
$vals['default_iface'] = @$_POST['default_iface'];
|
||
|
||
// parse values
|
||
$this->validate_form_value($vals);
|
||
... | ... | |
$tdefault->save();
|
||
}
|
||
}
|
||
|
||
$device_active_links_model->unmap_device_from_active_links(
|
||
$device_templates_id,
|
||
Device_active_link_Model::TYPE_TEMPLATE
|
||
);
|
||
|
||
$device_active_links_model->map_device_to_active_links(
|
||
$device_templates_id,
|
||
$form_data['active_links'],
|
||
Device_active_link_Model::TYPE_TEMPLATE
|
||
);
|
||
|
||
// clean
|
||
unset($vals);
|
||
... | ... | |
// bread crumbs
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('device_templates/show_all', 'Device templates',
|
||
$this->acl_check_view('Devices_Controller', 'devices'))
|
||
$this->acl_check_view('Device_templates_Controller', 'device_template'))
|
||
->disable_translation()
|
||
->link('device_templates/show/' . $device_templates_id,
|
||
$device_template_model->name . ' (' . $device_templates_id . ')',
|
||
$this->acl_check_view('Devices_Controller', 'devices'))
|
||
$this->acl_check_view('Device_templates_Controller', 'device_template'))
|
||
->text($headline)
|
||
->html();
|
||
|
||
... | ... | |
}
|
||
|
||
// check access
|
||
if (!$this->acl_check_delete('Devices_Controller', 'devices'))
|
||
if (!$this->acl_check_delete('Device_templates_Controller', 'device_template'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
... | ... | |
public function import_from_file()
|
||
{
|
||
// check acess
|
||
if (!$this->acl_check_view('Devices_Controller', 'devices'))
|
||
if (!$this->acl_check_new('Device_templates_Controller', 'device_template'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
... | ... | |
// bread crumbs
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('device_templates/show_all', 'Device templates',
|
||
$this->acl_check_view('Devices_Controller', 'devices'))
|
||
$this->acl_check_view('Device_templates_Controller', 'device_template'))
|
||
->disable_translation()
|
||
->text($headline)
|
||
->html();
|
||
... | ... | |
$device_template_model->values = json_encode($template->values);
|
||
$device_template_model->default = $template->default;
|
||
|
||
$device_template_model->save();
|
||
$device_template_model->save_throwable();
|
||
|
||
// remove old defauts
|
||
if ($template->default)
|
||
... | ... | |
foreach ($tdefaults as $tdefault)
|
||
{
|
||
$tdefault->default = 0;
|
||
$tdefault->save();
|
||
$tdefault->save_throwable();
|
||
}
|
||
}
|
||
|
||
... | ... | |
// bread crumbs
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('device_templates/show_all', 'Device templates',
|
||
$this->acl_check_view('Devices_Controller', 'devices'))
|
||
$this->acl_check_view('Device_templates_Controller', 'device_template'))
|
||
->link('device_templates/import_from_file', 'Upload device templates',
|
||
$this->acl_check_view('Devices_Controller', 'devices'))
|
||
$this->acl_check_view('Device_templates_Controller', 'device_template'))
|
||
->text('Import results')
|
||
->html();
|
||
}
|
||
... | ... | |
public function export_to_json()
|
||
{
|
||
// check acess
|
||
if (!$this->acl_check_view('Devices_Controller', 'devices'))
|
||
if (!$this->acl_check_view('Device_templates_Controller', 'device_template'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
Také k dispozici: Unified diff
Release 1.1.0