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 clouds actions.
*
* @package Controller
* @author Ondřej Fibich
*/
class Clouds_Controller extends Controller
{
/**
* Index redirects to show all
*/
public function index()
{
url::redirect(url_lang::base() . 'clouds/show_all');
}

/**
* Shows all clouds
*/
public function show_all()
{
// access check
if (!$this->acl_check_view('Clouds_Controller', 'clouds'))
{
Controller::error(ACCESS);
}

// model
$cloud_model = new Cloud_Model();

// gets data
$query = $cloud_model->get_all_clouds();

// grid
$grid = new Grid(url_lang::base() . 'clouds', null, array
(
'use_paginator' => false,
'use_selector' => false
));

if ($this->acl_check_new('Clouds_Controller', 'clouds'))
{
$grid->add_new_button(url_lang::base() . 'clouds/add', url_lang::lang('texts.Add new cloud'));
}

$grid->field('id')
->label('ID')
->class('center');
$grid->field('name')
->label(url_lang::lang('texts.Name'));
$grid->field('subnet_count')
->label(url_lang::lang('texts.Subnet count'));
$grid->field('admin_count')
->label(url_lang::lang('texts.Admin count'));
$grid->action_field('id')
->label(url_lang::lang('texts.Show'))
->url(url_lang::base() . 'clouds/show')
->action(url_lang::lang('texts.Show'))
->class('center');
if ($this->acl_check_edit('Clouds_Controller', 'clouds'))
{
$grid->action_field('id')
->label(url_lang::lang('texts.Edit'))
->url(url_lang::base() . 'clouds/edit')
->action(url_lang::lang('texts.Edit'))
->class('center');
}
if ($this->acl_check_delete('Clouds_Controller', 'clouds'))
{
$grid->action_field('id')
->label(url_lang::lang('texts.Delete'))
->url(url_lang::base() . 'clouds/delete')
->action(url_lang::lang('texts.Delete'))
->class('delete_link');
}
// load datasource
$grid->datasource($query);
// bread crumbs
$breadcrumbs = breadcrumbs::add()
->text('Clouds')
->html();

// main view
$view = new View('main');
$view->title = url_lang::lang('texts.List of all clouds');
$view->content = new View('show_all');
$view->breadcrumbs = $breadcrumbs;
$view->content->headline = url_lang::lang('texts.List of all clouds');
$view->content->table = $grid;
$view->render(TRUE);
}

/**
* Show subnet with given ID
*
* @param integer $cloud_id
*/
public function show($cloud_id = NULL)
{
// param check
if (!$cloud_id || !is_numeric($cloud_id))
{
Controller::warning(PARAMETER);
}
// check acess
if (!$this->acl_check_view('Clouds_Controller', 'clouds'))
{
Controller::error(ACCESS);
}

$cloud_model = new Cloud_Model($cloud_id);
// exist record
if (!$cloud_model->id)
{
Controller::error(RECORD);
}
/* ADMINS */

// gets admins of cloud
$admins = $cloud_model->get_cloud_admins($cloud_id);

// grid
$grid_admins = new Grid(url_lang::base() . 'clouds', null, array
(
'use_paginator' => false,
'use_selector' => false
));
if ($this->acl_check_new('Clouds_Controller', 'clouds'))
{
$grid_admins->add_new_button(
url_lang::base() . 'clouds/add_admin/' . $cloud_id,
url_lang::lang('texts.Add admin of cloud')
);
}
$grid_admins->field('id')
->label('ID')
->class('center');
$grid_admins->field('user')
->label(url_lang::lang('texts.User'));
if ($this->acl_check_view('Users_Controller', 'users'))
{
$grid_admins->action_field('id')
->label(url_lang::lang('texts.Show'))
->url(url_lang::base() . 'users/show')
->action(url_lang::lang('texts.Show'));
}
if ($this->acl_check_delete('Clouds_Controller', 'clouds'))
{
$grid_admins->action_field('id')
->label(url_lang::lang('texts.Remove'))
->url(url_lang::base() . 'clouds/remove_admin/' . $cloud_id)
->action(url_lang::lang('texts.Remove'));
}
// load data
$grid_admins->datasource($admins);
/* SUBNETS */

// get cloud subnets
$subnets = $cloud_model->get_cloud_subnets($cloud_id);

// grid
$grid_subnets = new Grid(url_lang::base() . 'clouds', null, array
(
'use_paginator' => false,
'use_selector' => false
));

if ($this->acl_check_new('Devices_Controller', 'subnet'))
{
$grid_subnets->add_new_button(
url_lang::base() . 'subnets/add/' . $cloud_id,
url_lang::lang('texts.Add new subnet')
);
}
if ($this->acl_check_new('Clouds_Controller', 'clouds'))
{
$grid_subnets->add_new_button(
url_lang::base() . 'clouds/add_subnet/' . $cloud_id,
url_lang::lang('texts.Add subnet to cloud')
);
}
$grid_subnets->field('id')
->label('ID')
->class('center');
$grid_subnets->field('name')
->label(url_lang::lang('texts.Name'));
$grid_subnets->field('network_address')
->label(url_lang::lang('texts.Address network'));
$grid_subnets->field('netmask')
->label(url_lang::lang('texts.Netmask'));
if ($this->acl_check_view('Devices_Controller', 'subnet'))
{
$grid_subnets->action_field('id')
->label(url_lang::lang('texts.Show'))
->url(url_lang::base() . 'subnets/show')
->action(url_lang::lang('texts.Show'));
}
if ($this->acl_check_delete('Clouds_Controller', 'clouds'))
{
$grid_subnets->action_field('id')
->label(url_lang::lang('texts.Remove'))
->url(url_lang::base() . 'clouds/remove_subnet/' . $cloud_id)
->action(url_lang::lang('texts.Remove'));
}

// load data
$grid_subnets->datasource($subnets);
$headline = $cloud_model->name . ' (' . $cloud_id . ')';
// bread crumbs
$breadcrumbs = breadcrumbs::add()
->link('clouds/show_all', 'Clouds',
$this->acl_check_view('Clouds_Controller', 'clouds'))
->disable_translation()
->text($headline)
->html();

// view
$view = new View('main');
$view->title = $headline;
$view->content = new View('show_clouds');
$view->breadcrumbs = $breadcrumbs;
$view->content->cloud = $cloud_model;
$view->content->admins = $grid_admins;
$view->content->subnets = $grid_subnets;
$view->render(TRUE);
}

/**
* Adds cloud
*/
public function add()
{
// check access
if (!$this->acl_check_new('Clouds_Controller', 'clouds'))
{
Controller::error(ACCESS);
}
// gets all subnets
$subnet_model = new Subnet_Model();
$subnets = $subnet_model->select_list('id', 'name');
// gets all users
$user_model = new User_Model();
$users = $user_model->select_list_grouped();

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

$form->group('')
->label(url_lang::lang('texts.Basic information'));
$form->input('name')
->label(url_lang::lang('texts.Name') . ':')
->rules('required');
$form->dropdown('subnets[]')
->label(url_lang::lang('texts.Subnets') . ': ' . help::hint('multiple_dropdown'))
->size(20)
->multiple('multiple')
->options($subnets)
->class('max');
$form->dropdown('admins[]')
->label(url_lang::lang('texts.Admins') . ': ' . help::hint('multiple_dropdown'))
->size(20)
->multiple('multiple')
->options($users)
->class('max');
$form->submit('submit')
->value(url_lang::lang('texts.Add'));
special::required_forge_style($form, ' *', 'required');

// validate form and save data
if ($form->validate())
{
try
{
// cloud model
$cloud_model = new Cloud_Model();
// start transaction
$cloud_model->transaction_start();
// save cloud
$cloud_model->name = htmlspecialchars($form->name->value);
$issave = $cloud_model->save_throwable();
// add admins
if (isset($_POST['admins']) && is_array($_POST['admins']))
{
foreach ($_POST['admins'] as $user_id)
{
$user_model->find($user_id);
if ($user_model->id)
{
$cloud_model->add($user_model);
$cloud_model->save_throwable();
}
}
}
// add subnets
if (isset($_POST['subnets']) && is_array($_POST['subnets']))
{
foreach ($_POST['subnets'] as $subnet_id)
{
$subnet_model->find($subnet_id);
if ($subnet_model->id)
{
$cloud_model->add($subnet_model);
$cloud_model->save_throwable();
}
}
}
// commit transaction
$cloud_model->transaction_commit();

// message
$this->session->set_flash(
'message', url_lang::lang('texts.Cloud has been successfully added.')
);
// redirection
url::redirect(url_lang::base() . 'clouds/show_all');
}
catch (Exception $e)
{
// roolback transaction
$cloud_model->transaction_rollback();
// message
$this->session->set_flash(
'message', url_lang::lang('texts.Error - cannot add cloud') .
'.<br />' . url_lang::lang('Error') . ': ' . $e->getMessage()
);
}
}
$headline = url_lang::lang('texts.Add new cloud');
// bread crumbs
$breadcrumbs = breadcrumbs::add()
->link('clouds/show_all', 'Clouds',
$this->acl_check_view('Clouds_Controller', 'clouds'))
->disable_translation()
->text($headline)
->html();

// view
$view = new View('main');
$view->title = url_lang::lang('texts.Add new cloud');
$view->content = new View('form');
$view->breadcrumbs = $breadcrumbs;
$view->content->headline = $headline;
$view->content->form = $form->html();
$view->render(TRUE);
}

/**
* Assign admin as admin of cloud
*
* @param integer $cloud_id
*/
public function add_admin($cloud_id = null)
{
// check param
if (!$cloud_id || !is_numeric($cloud_id))
{
Controller::warning(PARAMETER);
}
// check access
if (!$this->acl_check_delete('Clouds_Controller', 'clouds'))
{
Controller::error(ACCESS);
}

// load model
$cloud_model = new Cloud_Model($cloud_id);
// check exists
if (!$cloud_model->id)
{
Controller::error(RECORD);
}

// list of free
$arr_admin_first = array('0' => '----- ' . url_lang::lang('texts.Select admin') . ' -----');
$arr_admin = $arr_admin_first + $cloud_model->select_list_of_admins_not_in($cloud_id);

//are there any unassigned amins?
if (count($arr_admin) == 1)
{
$this->session->set_flash(
'message', url_lang::lang('texts.There are not any unassigned admins.')
);
url::redirect(url_lang::base() . 'clouds/show/' . $cloud_id);
}

// form
$form = new Forge(
url_lang::base() . "clouds/add_admin/" . $cloud_id, '',
'POST', array('id' => 'article_form')
);
$form->set_attr('class', 'form_class')
->set_attr('method', 'post');
$form->dropdown('admin')
->label(url_lang::lang('texts.Admin') . ':')
->options($arr_admin);
$form->submit('submit')
->value(url_lang::lang('texts.Assign'));
special::required_forge_style($form, ' *', 'required');

// validate form and save data
if ($form->validate())
{
// load subnet
$user_model = new User_Model($form->admin->value);
// correct data?
if ($user_model->id)
{
// assign
$cloud_model->add($user_model);
// confirm
if ($cloud_model->save())
{
$this->session->set_flash(
'message', url_lang::lang('texts.Admin has been successfully assigned to cloud')
);
}
}
// redirect
url::redirect(url_lang::base() . 'clouds/show/' . $cloud_id);
}
$headline = url_lang::lang('texts.Assign admin to cloud');
// bread crumbs
$breadcrumbs = breadcrumbs::add()
->link('clouds/show_all', 'Clouds',
$this->acl_check_view('Clouds_Controller', 'clouds'))
->disable_translation()
->link('clouds/show/' . $cloud_id,
$cloud_model->name . ' (' . $cloud_id . ')',
$this->acl_check_view('Clouds_Controller', 'clouds'))
->text($headline)
->html();

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

/**
* Adds subnet to cloud
*
* @param integer $cloud_id
*/
public function add_subnet($cloud_id = null)
{
// check param
if (!$cloud_id || !is_numeric($cloud_id))
{
Controller::warning(PARAMETER);
}
// check access
if (!$this->acl_check_delete('Clouds_Controller', 'clouds'))
{
Controller::error(ACCESS);
}

// load model
$cloud_model = new Cloud_Model($cloud_id);
// check exists
if (!$cloud_model->id)
{
Controller::error(RECORD);
}

// list of free
$arr_subnet_first = array('0' => '----- ' . url_lang::lang('texts.Select free subnet') . ' -----');
$arr_subnet = $arr_subnet_first + $cloud_model->select_list_of_subnets_not_in($cloud_id);

//are there any unassigned subnets?
if (count($arr_subnet) == 1)
{
$this->session->set_flash(
'message', url_lang::lang('texts.There are not any unassigned subnets.')
);
url::redirect(url_lang::base() . 'clouds/show/' . $cloud_id);
}

// form
$form = new Forge(
url_lang::base() . "clouds/add_subnet/" . $cloud_id, '',
'POST', array('id' => 'article_form')
);
$form->set_attr('class', 'form_class')
->set_attr('method', 'post');
$form->dropdown('subnet')
->label(url_lang::lang('texts.Subnet') . ':')
->options($arr_subnet);
$form->submit('submit')
->value(url_lang::lang('texts.Insert'));
special::required_forge_style($form, ' *', 'required');

// validate form and save data
if ($form->validate())
{
// load subnet
$subnet_model = new Subnet_Model($form->subnet->value);
// correct data?
if ($subnet_model->id)
{
// assign
$cloud_model->add($subnet_model);
// confirm
if ($cloud_model->save())
{
$this->session->set_flash(
'message', url_lang::lang('texts.Subnet has been successfully assigned to cloud')
);
}
}
// redirect
url::redirect(url_lang::base() . 'clouds/show/' . $cloud_id);
}
// bread crumbs
$headline = url_lang::lang('texts.Assign subnet to cloud');
// bread crumbs
$breadcrumbs = breadcrumbs::add()
->link('clouds/show_all', 'Clouds',
$this->acl_check_view('Clouds_Controller', 'clouds'))
->disable_translation()
->link('clouds/show/' . $cloud_id,
$cloud_model->name . ' (' . $cloud_id . ')',
$this->acl_check_view('Clouds_Controller', 'clouds'))
->text($headline)
->html();

// view
$view = new View('main');
$view->title = $headline;
$view->breadcrumbs = $breadcrumbs;
$view->content = new View('form');
$view->content->headline = $headline . ' - ' . $cloud_model->name;
$view->content->form = $form->html();
$view->render(TRUE);
}

/**
* Edits cloud
*
* @param integer $cloud_id
*/
public function edit($cloud_id = NULL)
{
// check param
if (!$cloud_id || !is_numeric($cloud_id))
{
Controller::warning(PARAMETER);
}
// check access
if (!$this->acl_check_delete('Clouds_Controller', 'clouds'))
{
Controller::error(ACCESS);
}

// load model
$cloud_model = new Cloud_Model($cloud_id);
// check exists
if (!$cloud_model->id)
{
Controller::error(RECORD);
}
// form
$form = new Forge(
url_lang::base() . 'clouds/edit/' . $cloud_id, '',
'POST', array('id' => 'article_form')
);
$form->set_attr('class', 'form_class')
->set_attr('method', 'post');
$form->input('name')
->label(url_lang::lang('texts.Name') . ':')
->rules('required')
->value($cloud_model->name);
$form->submit('submit')
->value(url_lang::lang('texts.Edit'));

special::required_forge_style($form, ' *', 'required');

//validate form and save data
if ($form->validate())
{
$cloud_model->name = $form->name->value;
if ($cloud_model->save())
{
// message
$this->session->set_flash(
'message', url_lang::lang('texts.Cloud has been successfully updated.')
);
// redirect
url::redirect(url_lang::base() . 'clouds/show_all');
}
else
{
$this->session->set_flash(
'message', url_lang::lang('texts.Error - cloud cannot be updated.')
);
}
}
$headline = url_lang::lang('texts.Edit cloud');
// bread crumbs
$breadcrumbs = breadcrumbs::add()
->link('clouds/show_all', 'Clouds',
$this->acl_check_view('Clouds_Controller', 'clouds'))
->disable_translation()
->link('clouds/show/' . $cloud_id,
$cloud_model->name . ' (' . $cloud_id . ')',
$this->acl_check_view('Clouds_Controller', 'clouds'))
->text($headline)
->html();

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

/**
* Deletes cloud and its records in pivot tables
*
* @param integer $cloud_id
*/
public function delete($cloud_id = NULL)
{
// check param
if (!$cloud_id || !is_numeric($cloud_id))
{
Controller::warning(PARAMETER);
}
// check access
if (!$this->acl_check_delete('Clouds_Controller', 'clouds'))
{
Controller::error(ACCESS);
}

// load model
$cloud_model = new Cloud_Model($cloud_id);
// check exists
if (!$cloud_model->id)
{
Controller::error(RECORD);
}
// delete (pivot tables are deleted by forein keys)
if ($cloud_model->delete())
{
$this->session->set_flash(
'message', url_lang::lang('texts.cloud has been successfully deleted.')
);
}
else
{
$this->session->set_flash(
'message', url_lang::lang('texts.Error - cant delete this cloud.')
);
}

// redirect to show all
url::redirect(url_lang::base() . 'clouds/show_all/');
}
/**
* Removes admin of cloud
*
* @param integer $cloud_id
* @param integer $user_id
*/
public function remove_admin($cloud_id = NULL, $user_id = NULL)
{
// check param
if (!$cloud_id || !is_numeric($cloud_id) ||
!$user_id || !is_numeric($user_id))
{
Controller::warning(PARAMETER);
}
// check access
if (!$this->acl_check_delete('Clouds_Controller', 'clouds'))
{
Controller::error(ACCESS);
}

// load models
$cloud_model = new Cloud_Model($cloud_id);
$user_model = new User_Model($user_id);
// check exists
if (!$cloud_model->id || !$user_model->id)
{
Controller::error(RECORD);
}
// remove record from pivot table
$cloud_model->remove($user_model);
// confirm removine
if (!$cloud_model->save())
{
$this->session->set_flash(
'message', url_lang::lang('texts.Error - cannot remove admin of cloud')
);
}

// redirect to show
url::redirect(url_lang::base() . 'clouds/show/' . $cloud_id);
}
/**
* Removes subnet of cloud
*
* @param integer $cloud_id
* @param integer $subnet_id
*/
public function remove_subnet($cloud_id = NULL, $subnet_id = NULL)
{
// check param
if (!$cloud_id || !is_numeric($cloud_id) ||
!$subnet_id || !is_numeric($subnet_id))
{
Controller::warning(PARAMETER);
}
// check access
if (!$this->acl_check_delete('Clouds_Controller', 'clouds'))
{
Controller::error(ACCESS);
}

// load models
$cloud_model = new Cloud_Model($cloud_id);
$subnet_model = new Subnet_Model($subnet_id);
// check exists
if (!$cloud_model->id || !$subnet_model->id)
{
Controller::error(RECORD);
}
// remove record from pivot table
$cloud_model->remove($subnet_model);
// confirm removine
if (!$cloud_model->save())
{
$this->session->set_flash(
'message', url_lang::lang('texts.Error - cannot remove subnet of cloud')
);
}

// redirect to show
url::redirect(url_lang::base() . 'clouds/show/' . $cloud_id);
}

}
(13-13/74)