Revize 31ca0a32
Přidáno uživatelem Michal Kliment před více než 9 roky(ů)
application/controllers/bank_accounts_auto_down_settings.php | ||
---|---|---|
<?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 manages settings of automatical downloading of bank statements.
|
||
*
|
||
* @package Controller
|
||
* @author Ondřej Fibich
|
||
*/
|
||
class Bank_accounts_auto_down_settings_Controller extends Controller
|
||
{
|
||
|
||
/**
|
||
* Shows all settings of the given bank account.
|
||
*/
|
||
public function show($bank_account_id = NULL)
|
||
{
|
||
// check param
|
||
if (!intval($bank_account_id))
|
||
{
|
||
self::warning(PARAMETER);
|
||
}
|
||
|
||
$ba = new Bank_account_Model($bank_account_id);
|
||
$ba_ad = new Bank_accounts_automatical_download_Model();
|
||
|
||
$ba_settings = Bank_Account_Settings::factory($ba->type);
|
||
$ba_settings->load_column_data($ba->settings);
|
||
|
||
// check if exists
|
||
if (!$ba || !$ba->id ||
|
||
!$ba_settings->can_download_statements_automatically())
|
||
{
|
||
self::error(RECORD);
|
||
}
|
||
|
||
// access check
|
||
if (!$this->acl_check_view('Accounts_Controller', 'bank_account_auto_down_config'))
|
||
{
|
||
self::error(ACCESS);
|
||
}
|
||
|
||
// gets data
|
||
$query = $ba_ad->get_bank_account_settings($ba->id);
|
||
|
||
// grid
|
||
$grid = new Grid('bank_accounts_auto_down_settings', null, array
|
||
(
|
||
'use_paginator' => false,
|
||
'use_selector' => false
|
||
));
|
||
|
||
if ($this->acl_check_new('Accounts_Controller', 'bank_account_auto_down_config'))
|
||
{
|
||
$grid->add_new_button(
|
||
'bank_accounts_auto_down_settings/add/' . $ba->id,
|
||
__('Add new rule'), array('class' => 'popup_link')
|
||
);
|
||
}
|
||
|
||
$grid->field('id')
|
||
->label('ID');
|
||
|
||
$grid->callback_field('type')
|
||
->callback('callback::message_auto_setting_type');
|
||
|
||
$grid->callback_field('attribute')
|
||
->callback('callback::message_auto_setting_attribute');
|
||
|
||
if (module::e('notification'))
|
||
{
|
||
if (Settings::get('email_enabled'))
|
||
{
|
||
$grid->callback_field('email_enabled')
|
||
->callback('callback::boolean')
|
||
->label('E-mail');
|
||
}
|
||
|
||
if (Settings::get('sms_enabled'))
|
||
{
|
||
$grid->callback_field('sms_enabled')
|
||
->callback('callback::boolean')
|
||
->label('SMS');
|
||
}
|
||
}
|
||
|
||
$actions = $grid->grouped_action_field();
|
||
|
||
if ($this->acl_check_delete('Accounts_Controller', 'bank_account_auto_down_config'))
|
||
{
|
||
$actions->add_action()
|
||
->icon_action('delete')
|
||
->url('bank_accounts_auto_down_settings/delete')
|
||
->class('delete_link');
|
||
}
|
||
|
||
// load datasource
|
||
$grid->datasource($query);
|
||
|
||
// bread crumbs
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('bank_accounts/show_all', 'Bank accounts',
|
||
$this->acl_check_view('Accounts_Controller', 'bank_accounts'))
|
||
->text($ba->account_nr . '/' . $ba->bank_nr)
|
||
->text('Setup automatical downloading of statements')
|
||
->html();
|
||
|
||
// main view
|
||
$view = new View('main');
|
||
$view->title = __('Automatical settings for downloading of statements');
|
||
$view->content = new View('show_all');
|
||
$view->breadcrumbs = $breadcrumbs;
|
||
$view->content->headline = $view->title;
|
||
$view->content->table = $grid;
|
||
$view->render(TRUE);
|
||
}
|
||
|
||
/**
|
||
* Adds a new rule
|
||
*
|
||
* @param integer $bank_account_id
|
||
*/
|
||
public function add($bank_account_id = NULL)
|
||
{
|
||
// check param
|
||
if (!$bank_account_id || !is_numeric($bank_account_id))
|
||
{
|
||
self::warning(PARAMETER);
|
||
}
|
||
|
||
// check access
|
||
if (!$this->acl_check_new('Accounts_Controller', 'bank_account_auto_down_config'))
|
||
{
|
||
self::error(ACCESS);
|
||
}
|
||
|
||
// load model
|
||
$ba = new Bank_account_Model($bank_account_id);
|
||
$types = Bank_accounts_automatical_download_Model::get_type_messages();
|
||
|
||
$ba_settings = Bank_Account_Settings::factory($ba->type);
|
||
$ba_settings->load_column_data($ba->settings);
|
||
|
||
// check if exists
|
||
if (!$ba || !$ba->id ||
|
||
!$ba_settings->can_download_statements_automatically())
|
||
{
|
||
self::error(RECORD);
|
||
}
|
||
|
||
// form
|
||
$form = new Forge('bank_accounts_auto_down_settings/add/' . $bank_account_id);
|
||
|
||
$form->group('Basic information');
|
||
|
||
$form->dropdown('type')
|
||
->rules('required')
|
||
->options(array_map('strtolower', $types))
|
||
->style('width:200px');
|
||
|
||
for ($i = 0; $i < Time_Activity_Rule::get_attribute_types_max_count(); $i++)
|
||
{
|
||
$form->input('attribute[' . $i . ']')
|
||
->callback(array($this, 'valid_attribute'))
|
||
->label(__('Attribute') . ' ' . ($i + 1));
|
||
}
|
||
|
||
if (module::e('notification'))
|
||
{
|
||
if (Settings::get('email_enabled'))
|
||
{
|
||
$form->checkbox('email_enabled')
|
||
->label('Sending of e-mail messages enabled')
|
||
->value('1')
|
||
->checked(TRUE);
|
||
}
|
||
|
||
if (Settings::get('sms_enabled'))
|
||
{
|
||
$form->checkbox('sms_enabled')
|
||
->label('Sending of SMS messages enabled')
|
||
->value('1');
|
||
}
|
||
}
|
||
|
||
$form->submit('Add');
|
||
|
||
// validate form and save data
|
||
if ($form->validate())
|
||
{
|
||
// model
|
||
$baad = new Bank_accounts_automatical_download_Model();
|
||
|
||
try
|
||
{
|
||
// start transaction
|
||
$baad->transaction_start();
|
||
|
||
// load data
|
||
$form_data = $form->as_array();
|
||
|
||
// prepare attribute
|
||
$attrs = @$_POST['attribute'];
|
||
$attrs_finished = array();
|
||
$count = Time_Activity_Rule::get_type_attributes_count($form_data['type']);
|
||
|
||
for ($i = 0; $i < $count; $i++)
|
||
{
|
||
if (is_array($attrs) && count($attrs))
|
||
{
|
||
$attrs_finished[] = array_shift($attrs);
|
||
}
|
||
else
|
||
{
|
||
$attrs_finished[] = NULL;
|
||
}
|
||
}
|
||
|
||
// save
|
||
$baad->bank_account_id = $ba->id;
|
||
$baad->type = $form_data['type'];
|
||
$baad->attribute = implode('/', $attrs_finished);
|
||
|
||
$baad->email_enabled =
|
||
Settings::get('email_enabled') &&
|
||
$form_data['email_enabled'];
|
||
|
||
$baad->sms_enabled =
|
||
Settings::get('sms_enabled') &&
|
||
$form_data['sms_enabled'];
|
||
|
||
$baad->save_throwable();
|
||
|
||
// commit transaction
|
||
$baad->transaction_commit();
|
||
|
||
// message
|
||
status::success('Rule for automatical download of statement has been succesfully added');
|
||
|
||
// redirection
|
||
$this->redirect('bank_accounts_auto_down_settings/show', $ba->id);
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
// roolback transaction
|
||
$baad->transaction_rollback();
|
||
Log::add_exception($e);
|
||
// message
|
||
status::error('Error - cant add rule for automatical download of statement', $e);
|
||
}
|
||
}
|
||
|
||
// headline
|
||
$headline = __('Add a new rule for automatical download of statement');
|
||
|
||
// bread crumbs
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('bank_accounts/show_all', 'Bank accounts',
|
||
$this->acl_check_view('Accounts_Controller', 'bank_accounts'))
|
||
->disable_translation()
|
||
->text($ba->account_nr . '/' . $ba->bank_nr)
|
||
->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 settings rule
|
||
*
|
||
* @param integer $baad_setting_id
|
||
*/
|
||
public function delete($baad_setting_id = NULL)
|
||
{
|
||
// check param
|
||
if (!$baad_setting_id || !is_numeric($baad_setting_id))
|
||
{
|
||
self::warning(PARAMETER);
|
||
}
|
||
|
||
// check access
|
||
if (!$this->acl_check_delete('Accounts_Controller', 'bank_account_auto_down_config'))
|
||
{
|
||
self::error(ACCESS);
|
||
}
|
||
|
||
// load model
|
||
$baad = new Bank_accounts_automatical_download_Model($baad_setting_id);
|
||
|
||
$bank_account_id = $baad->bank_account_id;
|
||
|
||
// check exists
|
||
if (!$baad->id)
|
||
{
|
||
Controller::error(RECORD);
|
||
}
|
||
|
||
// delete
|
||
if ($baad->delete())
|
||
{
|
||
status::success('Rule for automatical download of statement has been succesfully deleted.');
|
||
}
|
||
else
|
||
{
|
||
status::error('Error - cant delete rule for automatical download of statement.');
|
||
}
|
||
|
||
// redirect to show all
|
||
url::redirect('bank_accounts_auto_down_settings/show/' . $bank_account_id);
|
||
}
|
||
|
||
/**
|
||
* Checks if attribute form element has valid value
|
||
*
|
||
* @param object $input
|
||
*/
|
||
public function valid_attribute($input = NULL)
|
||
{
|
||
if (empty($input) || !is_object($input))
|
||
{
|
||
Controller::error(PAGE);
|
||
}
|
||
|
||
$type = $this->input->post('type');
|
||
$value = trim($input->value);
|
||
|
||
$at = Bank_accounts_automatical_download_Model::get_type_attributes($type);
|
||
$index = intval(substr($input->name, strlen('attribute[')));
|
||
|
||
if (!$at)
|
||
{
|
||
$input->add_error('required', __('Wrong input.'));
|
||
}
|
||
else if (isset($at[$index]['type']) && ($at[$index]['type'] !== FALSE))
|
||
{
|
||
if ($at[$index]['type'] == 'integer')
|
||
{
|
||
if (!preg_match("/^[0-9]+$/", $value))
|
||
{
|
||
$input->add_error('required', __('Numeric value required'));
|
||
}
|
||
else
|
||
{
|
||
if (isset($at[$index]['range_from']) &&
|
||
$at[$index]['range_from'] > intval($value))
|
||
{
|
||
$input->add_error('min_value', array($at[$index]['range_from']));
|
||
}
|
||
else if (isset($at[$index]['range_to']) &&
|
||
$at[$index]['range_to'] < intval($value))
|
||
{
|
||
$input->add_error('max_value', array($at[$index]['range_to']));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
application/controllers/connection_requests.php | ||
---|---|---|
<?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/
|
||
*
|
||
*/
|
||
|
||
/**
|
||
* Manages connection requests of users.
|
||
*
|
||
* @author Ondřej Fibich
|
||
* @package Controller
|
||
*/
|
||
class Connection_Requests_Controller extends Controller
|
||
{
|
||
|
||
/**
|
||
* Index redirect to show all
|
||
*/
|
||
public function index()
|
||
{
|
||
url::redirect('connection_requests/show_all');
|
||
}
|
||
|
||
/**
|
||
* Shows all requests.
|
||
*
|
||
* @param integer $limit_results
|
||
* @param string $order_by
|
||
* @param string $order_by_direction
|
||
* @param string $page_word
|
||
* @param integer $page
|
||
*/
|
||
public function show_all(
|
||
$limit_results = 50, $order_by = 'created_at',
|
||
$order_by_direction = 'desc', $page_word = null, $page = 1)
|
||
{
|
||
if (!$this->acl_check_view('Connection_Requests_Controller', 'request'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
|
||
if (!Settings::get('connection_request_enable'))
|
||
{
|
||
status::warning('Connection requests are not enabled.');
|
||
url::redirect('settings/system');
|
||
}
|
||
|
||
if (is_numeric($this->input->post('record_per_page')))
|
||
{
|
||
$limit_results = (int) $this->input->post('record_per_page');
|
||
}
|
||
|
||
$allowed_order_type = array
|
||
(
|
||
'id', 'member_name', 'user_name', 'created_at', 'ip_address',
|
||
'mac_address', 'comment', 'state'
|
||
);
|
||
|
||
if (!in_array(strtolower($order_by), $allowed_order_type))
|
||
{
|
||
$order_by = 'id';
|
||
}
|
||
|
||
if (strtolower($order_by_direction) != 'desc')
|
||
{
|
||
$order_by_direction = 'asc';
|
||
}
|
||
|
||
// filter
|
||
$filter_form = new Filter_form();
|
||
|
||
$filter_form->add('state')
|
||
->type('select')
|
||
->values(Connection_request_Model::get_state_messages());
|
||
|
||
$filter_form->add('member_name')
|
||
->callback('json/member_name');
|
||
|
||
$filter_form->add('member_id')
|
||
->type('number');
|
||
|
||
$filter_form->add('created_at')
|
||
->type('date')
|
||
->label('Time');
|
||
|
||
$filter_form->add('ip_address');
|
||
|
||
$filter_form->add('mac_address');
|
||
|
||
// get data
|
||
$cr_model = new Connection_request_Model();
|
||
|
||
$total = $cr_model->count_all_connection_requests($filter_form->as_sql());
|
||
|
||
if (($sql_offset = ($page - 1) * $limit_results) > $total)
|
||
$sql_offset = 0;
|
||
|
||
$connection_requests = $cr_model->get_all_connection_requests(
|
||
$sql_offset, $limit_results, $order_by, $order_by_direction,
|
||
$filter_form->as_sql()
|
||
);
|
||
|
||
$headline = __('Connection requests');
|
||
|
||
$grid = new Grid('connection_request', null, array
|
||
(
|
||
'current' => $limit_results,
|
||
'selector_increace' => 500,
|
||
'selector_min' => 500,
|
||
'selector_max_multiplier' => 10,
|
||
'base_url' => Config::get('lang')
|
||
. '/connection_requests/show_all/' . $limit_results
|
||
. '/' . $order_by . '/' . $order_by_direction,
|
||
'uri_segment' => 'page',
|
||
'total_items' => $total,
|
||
'items_per_page' => $limit_results,
|
||
'style' => 'classic',
|
||
'order_by' => $order_by,
|
||
'order_by_direction' => $order_by_direction,
|
||
'limit_results' => $limit_results,
|
||
'filter' => $filter_form->html()
|
||
));
|
||
|
||
$grid->order_field('id')
|
||
->label('ID');
|
||
|
||
$grid->order_link_field('member_id', 'member_name')
|
||
->link('members/show', 'member_name')
|
||
->label('Member');
|
||
|
||
$grid->order_link_field('user_id')
|
||
->link('users/show', 'user_name')
|
||
->label('Added by');
|
||
|
||
$grid->order_callback_field('state')
|
||
->callback('callback::connection_request_state_field');
|
||
|
||
$grid->order_field('created_at')
|
||
->label('Time');
|
||
|
||
$grid->order_field('mac_address')
|
||
->label('MAC address');
|
||
|
||
$grid->order_field('ip_address')
|
||
->label('IP address');
|
||
|
||
if ($this->acl_check_view('Subnets_Controller', 'subnet'))
|
||
{
|
||
$grid->order_link_field('subnet_id')
|
||
->link('subnets/show', 'subnet_name')
|
||
->label('Subnet');
|
||
}
|
||
else
|
||
{
|
||
$grid->order_field('subnet_name')
|
||
->label('Subnet');
|
||
}
|
||
|
||
$actions = $grid->grouped_action_field();
|
||
|
||
if ($this->acl_check_edit('Connection_Requests_Controller', 'request') &&
|
||
$this->acl_check_new('Devices_Controller', 'devices'))
|
||
{
|
||
$actions->add_conditional_action()
|
||
->condition('is_connection_request_undecided')
|
||
->icon_action('approve')
|
||
->url('connection_requests/approve_request');
|
||
}
|
||
|
||
if ($this->acl_check_edit('Connection_Requests_Controller', 'request'))
|
||
{
|
||
$actions->add_conditional_action()
|
||
->condition('is_connection_request_undecided')
|
||
->icon_action('reject')
|
||
->url('connection_requests/reject_request')
|
||
->class('confirm_reject');
|
||
}
|
||
|
||
$actions->add_action()
|
||
->icon_action('show')
|
||
->url('connection_requests/show');
|
||
|
||
if ($this->acl_check_edit('Connection_Requests_Controller', 'request'))
|
||
{
|
||
$actions->add_conditional_action()
|
||
->condition('is_connection_request_undecided')
|
||
->icon_action('edit')
|
||
->url('connection_requests/edit')
|
||
->class('popup_link');
|
||
}
|
||
|
||
$grid->datasource($connection_requests);
|
||
|
||
$view = new View('main');
|
||
$view->title = $headline;
|
||
$view->breadcrumbs = $headline;
|
||
$view->content = new View('show_all');
|
||
$view->content->headline = $headline;
|
||
$view->content->table = $grid;
|
||
$view->render(TRUE);
|
||
}
|
||
|
||
/**
|
||
* Shows all requests od member.
|
||
*
|
||
* @param integer $member_id
|
||
*/
|
||
public function show_by_member($member_id = NULL)
|
||
{
|
||
if (!is_numeric($member_id))
|
||
{
|
||
Controller::warning(PARAMETER);
|
||
}
|
||
|
||
$member = new Member_Model($member_id);
|
||
|
||
if (!$member || !$member->id)
|
||
{
|
||
Controller::error(RECORD);
|
||
}
|
||
|
||
if (!$this->acl_check_view('Connection_Requests_Controller', 'request', $member->id))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
|
||
if (!Settings::get('connection_request_enable'))
|
||
{
|
||
status::warning('Connection requests are not enabled.');
|
||
url::redirect('settings/system');
|
||
}
|
||
|
||
// get data
|
||
$cr_model = new Connection_request_Model();
|
||
|
||
$total = $cr_model->count_all_connection_requests_of_member($member_id);
|
||
|
||
$connection_requests = $cr_model->get_all_connection_requests_of_member(
|
||
$member_id
|
||
);
|
||
|
||
$headline = __('List of connection requests');
|
||
|
||
$grid = new Grid('messages', null, array
|
||
(
|
||
'use_paginator' => false,
|
||
'use_selector' => false,
|
||
'total_items' => $total
|
||
));
|
||
|
||
$grid->field('id')
|
||
->label('ID');
|
||
|
||
$grid->link_field('member_id', 'member_name')
|
||
->link('members/show', 'member_name')
|
||
->label('Member');
|
||
|
||
$grid->link_field('user_id')
|
||
->link('users/show', 'user_name')
|
||
->label('Added by');
|
||
|
||
$grid->callback_field('state')
|
||
->callback('callback::connection_request_state_field');
|
||
|
||
$grid->field('created_at')
|
||
->label('Time');
|
||
|
||
$grid->field('mac_address')
|
||
->label('MAC address');
|
||
|
||
$grid->field('ip_address')
|
||
->label('IP address');
|
||
|
||
if ($this->acl_check_view('Subnets_Controller', 'subnet'))
|
||
{
|
||
$grid->link_field('subnet_id')
|
||
->link('subnets/show', 'subnet_name')
|
||
->label('Subnet');
|
||
}
|
||
else
|
||
{
|
||
$grid->field('subnet_name')
|
||
->label('Subnet');
|
||
}
|
||
|
||
$actions = $grid->grouped_action_field();
|
||
|
||
$actions->add_action()
|
||
->icon_action('show')
|
||
->url('connection_requests/show');
|
||
|
||
if ($this->acl_check_edit('Connection_Requests_Controller', 'request'))
|
||
{
|
||
$actions->add_conditional_action()
|
||
->condition('is_connection_request_undecided')
|
||
->icon_action('edit')
|
||
->url('connection_requests/edit')
|
||
->class('popup_link');
|
||
}
|
||
|
||
$grid->datasource($connection_requests);
|
||
|
||
// breadcrumbs
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('members/show_all', 'Members',
|
||
$this->acl_check_view('Members_Controller', 'members')
|
||
)->disable_translation()
|
||
->link('members/show/' . $member->id,
|
||
'ID ' . $member->id . ' - ' . $member->name,
|
||
$this->acl_check_view('Members_Controller', 'members', $member->id)
|
||
)->enable_translation()
|
||
->text('Connection requests');
|
||
|
||
// view
|
||
$view = new View('main');
|
||
$view->title = $headline;
|
||
$view->breadcrumbs = $breadcrumbs;
|
||
$view->content = new View('show_all');
|
||
$view->content->headline = $headline;
|
||
$view->content->table = $grid;
|
||
$view->render(TRUE);
|
||
}
|
||
|
||
/**
|
||
* Shows a request.
|
||
*
|
||
* @param integer $connection_request_id
|
||
*/
|
||
public function show($connection_request_id = NULL)
|
||
{
|
||
if (!is_numeric($connection_request_id))
|
||
{
|
||
Controller::warning(PARAMETER);
|
||
}
|
||
|
||
$cr_model = new Connection_request_Model($connection_request_id);
|
||
|
||
if (!$cr_model || !$cr_model->id)
|
||
{
|
||
Controller::error(RECORD);
|
||
}
|
||
|
||
if (!$this->acl_check_view('Connection_Requests_Controller', 'request', $cr_model->member_id))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
|
||
if (!Settings::get('connection_request_enable'))
|
||
{
|
||
status::warning('Connection requests are not enabled.');
|
||
url::redirect('settings/system');
|
||
}
|
||
|
||
// comments grid
|
||
$comment_model = new Comment_Model();
|
||
|
||
$comments = $comment_model->get_all_comments_by_comments_thread(
|
||
$cr_model->comments_thread_id
|
||
);
|
||
|
||
$comments_grid = new Grid('comments', NULL, array
|
||
(
|
||
'separator' => '<br /><br />',
|
||
'use_paginator' => FALSE,
|
||
'use_selector' => FALSE,
|
||
));
|
||
|
||
$url = ($cr_model->comments_thread_id) ?
|
||
'comments/add/'.$cr_model->comments_thread_id :
|
||
'comments_threads/add/connection_request/'.$cr_model->id;
|
||
|
||
if ($this->acl_check_edit('Connection_Requests_Controller', 'request'))
|
||
{
|
||
$comments_grid->add_new_button(
|
||
$url, 'Add comment to connection request',
|
||
array('class' => 'popup_link')
|
||
);
|
||
}
|
||
|
||
$comments_grid->field('text');
|
||
|
||
if ($this->acl_check_view('Users_Controller', 'users'))
|
||
{
|
||
$comments_grid->link_field('user_id')
|
||
->link('users/show', 'user_name')
|
||
->label('User');
|
||
}
|
||
else
|
||
{
|
||
$comments_grid->field('user_name')
|
||
->label('User');
|
||
}
|
||
|
||
$comments_grid->field('datetime')
|
||
->label('Time');
|
||
|
||
$actions = $comments_grid->grouped_action_field();
|
||
|
||
$actions->add_conditional_action()
|
||
->icon_action('edit')
|
||
->url('comments/edit')
|
||
->condition('is_own')
|
||
->class('popup_link');
|
||
|
||
$actions->add_conditional_action()
|
||
->icon_action('delete')
|
||
->url('comments/delete')
|
||
->condition('is_own')
|
||
->class('delete_link');
|
||
|
||
$comments_grid->datasource($comments);
|
||
|
||
$headline = __('Connection request');
|
||
|
||
// breadcrumbs navigation
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('connection_requests/show_all', 'Connection requests',
|
||
$this->acl_check_view('Connection_requests_Controller', 'request'))
|
||
->disable_translation()
|
||
->text($cr_model->ip_address . ' (' . $cr_model->mac_address . ')');
|
||
|
||
// view
|
||
$view = new View('main');
|
||
$view->title = $headline;
|
||
$view->breadcrumbs = $breadcrumbs->html();
|
||
$view->content = new View('connection_requests/show');
|
||
$view->content->headline = $headline;
|
||
$view->content->connection_request = $cr_model;
|
||
$view->content->comments_grid = $comments_grid;
|
||
|
||
$view->render(TRUE);
|
||
}
|
||
|
||
/**
|
||
* Add new request.
|
||
*
|
||
* @param integer $subnet_id Subnet in which the IP addres belongs
|
||
* @param string $ip_address Requested IP address
|
||
*/
|
||
public function add($subnet_id = NULL, $ip_address = NULL)
|
||
{
|
||
if (!$this->acl_check_new('Connection_Requests_Controller', 'request', $this->member_id))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
|
||
if (!Settings::get('connection_request_enable'))
|
||
{
|
||
status::warning('Connection requests are not enabled.');
|
||
url::redirect('settings/system');
|
||
}
|
||
|
||
$subnet_model = new Subnet_Model($subnet_id);
|
||
|
||
if (!$subnet_model || !$subnet_model->id)
|
||
{
|
||
Controller::warning(PARAMETER);
|
||
}
|
||
|
||
if (!valid::ip($ip_address))
|
||
{
|
||
Controller::warning(PARAMETER);
|
||
}
|
||
|
||
$subnet = $subnet_model->get_net_and_mask_of_subnet();
|
||
|
||
if (!valid::ip_check_subnet(ip2long($ip_address), $subnet->net + 0, ip2long($subnet->netmask)))
|
||
{
|
||
Controller::warning(PARAMETER);
|
||
}
|
||
|
||
// someone stole that IP, or someone tried to hack FN.
|
||
if ($subnet_model->get_subnet_for_connection_request($ip_address) == NULL)
|
||
{
|
||
status::warning('IP address is not available anymore.');
|
||
url::redirect('connection_requests/show_by_member/' . $this->member_id);
|
||
}
|
||
|
||
// check if someone does not requested for this IP already
|
||
$cr_model = new Connection_request_Model();
|
||
$prevs = $cr_model->get_undecided_connection_with_ip($ip_address);
|
||
if ($prevs->count() > 0)
|
||
{
|
||
// wait you have done this already
|
||
if ($prevs->current()->member_id == $this->member_id)
|
||
{
|
||
status::warning('You have already requested for the same connection.');
|
||
}
|
||
else
|
||
{
|
||
status::warning('Someone have already requested for the same connection.');
|
||
}
|
||
url::redirect('connection_requests/show_by_member/' . $this->member_id);
|
||
}
|
||
unset($prevs);
|
||
|
||
// all device templates
|
||
$arr_device_templates = array
|
||
(
|
||
NULL => ''
|
||
) + ORM::factory('device_template')->select_list();
|
||
|
||
// enum types for device
|
||
$enum_type_model = new Enum_type_Model();
|
||
$types = $enum_type_model->get_values(Enum_type_Model::DEVICE_TYPE_ID);
|
||
|
||
// throw away unallowed types
|
||
if (Settings::get('connection_request_device_types'))
|
||
{
|
||
$allowed_types = explode(':', Settings::get('connection_request_device_types'));
|
||
|
||
foreach ($types as $key => $val)
|
||
{
|
||
if (array_search($key, $allowed_types) === FALSE)
|
||
{
|
||
unset($types[$key]);
|
||
}
|
||
}
|
||
}
|
||
|
||
$types[NULL] = '--- '.__('Select type').' ---';
|
||
asort($types);
|
||
|
||
// get MAC address using SNMP to DHCP server
|
||
if (!$this->session->get('connection_request_mac'))
|
||
{
|
||
$ip_address_model = new Ip_address_Model();
|
||
$gateway = $ip_address_model->get_gateway_of_subnet($subnet_id);
|
||
|
||
if ($gateway && $gateway->id)
|
||
{
|
||
// first try SNMP
|
||
if (module::e('snmp'))
|
||
{
|
||
try
|
||
{
|
||
$snmp = Snmp_Factory::factoryForDevice($gateway->ip_address);
|
||
|
||
// try find MAC address in DHCP
|
||
$mac_address = $snmp->getDHCPMacAddressOf($ip_address);
|
||
$this->session->set('connection_request_mac', $mac_address);
|
||
}
|
||
catch (DHCPMacAddressException $e)
|
||
{
|
||
try
|
||
{
|
||
// try find MAC address in ARP table
|
||
$mac_address = $snmp->getARPMacAddressOf($ip_address);
|
||
$this->session->set('connection_request_mac', $mac_address);
|
||
}
|
||
catch(Exception $e)
|
||
{
|
||
Log::add_exception($e);
|
||
status::mwarning($e->getMessage());
|
||
}
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
Log::add_exception($e);
|
||
status::mwarning($e->getMessage());
|
||
}
|
||
}
|
||
// now try CGI scripts
|
||
else if (module::e('cgi'))
|
||
{
|
||
$vars = arr::to_object(array
|
||
(
|
||
'GATEWAY_IP_ADDRESS' => $gateway->ip_address,
|
||
'IP_ADDRESS' => $ip_address
|
||
));
|
||
|
||
$url = text::object_format($vars, Settings::get('cgi_arp_url'));
|
||
|
||
$mac_address = @file_get_contents($url);
|
||
|
||
if ($mac_address !== FALSE)
|
||
{
|
||
$this->session->set('connection_request_mac', $mac_address);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// form
|
||
$form = new Forge();
|
||
|
||
if ($this->acl_check_new('Devices_Controller', 'devices'))
|
||
{
|
||
// if an admin added a member before, select this member in dropdown
|
||
$selected_mid = $this->session->get('last_added_member_id', $this->member_id);
|
||
|
||
$form->dropdown('member_id')
|
||
->label('Connection owner')
|
||
->options(ORM::factory('member')->select_list_grouped())
|
||
->selected($selected_mid)
|
||
->rules('required')
|
||
->style('width:200px');
|
||
}
|
||
|
||
$form->dropdown('device_type_id')
|
||
->options($types)
|
||
->selected(Settings::get('connection_request_device_default_type'))
|
||
->label('Device type')
|
||
->rules('required')
|
||
->style('width:200px')
|
||
->help('connection_request_device_type');
|
||
|
||
if ($this->acl_check_new('Devices_Controller', 'devices'))
|
||
{
|
||
$form->dropdown('device_template_id')
|
||
->options($arr_device_templates)
|
||
->rules('required')
|
||
->label('Device template')
|
||
->style('width:200px');
|
||
}
|
||
|
||
if (!$this->session->get('connection_request_mac') ||
|
||
$this->acl_check_new('Devices_Controller', 'devices'))
|
||
{ // admin or not detected
|
||
$form->input('mac_address')
|
||
->label('MAC address of device')
|
||
->value($this->session->get('connection_request_mac'))
|
||
->rules('required|valid_mac_address')
|
||
->help('connection_request_mac_address');
|
||
}
|
||
|
||
// show comment only for non-engeneers
|
||
if (!$this->acl_check_new('Devices_Controller', 'devices'))
|
||
{
|
||
$form->textarea('note');
|
||
}
|
||
|
||
$form->submit('Send');
|
||
|
||
// post
|
||
if ($form->validate())
|
||
{
|
||
$form_data = $form->as_array();
|
||
$cr_model->clear();
|
||
|
||
if (!isset($form_data['member_id']))
|
||
{
|
||
$form_data['member_id'] = $this->member_id;
|
||
}
|
||
|
||
if (!isset($form_data['device_template_id']))
|
||
{
|
||
$form_data['device_template_id'] = NULL;
|
||
}
|
||
|
||
try
|
||
{
|
||
$cr_model->transaction_start();
|
||
|
||
$cr_model->member_id = $form_data['member_id'];
|
||
$cr_model->added_user_id = $this->user_id;
|
||
$cr_model->created_at = date('Y-m-d H:i:s');
|
||
$cr_model->ip_address = $ip_address;
|
||
|
||
if (!$this->session->get('connection_request_mac') ||
|
||
$this->acl_check_new('Devices_Controller', 'devices'))
|
||
{
|
||
$cr_model->mac_address = $form_data['mac_address'];
|
||
}
|
||
else
|
||
{
|
||
$cr_model->mac_address = $this->session->get('connection_request_mac');
|
||
}
|
||
|
||
$cr_model->subnet_id = $subnet_model->id;
|
||
$cr_model->device_type_id = $form_data['device_type_id'];
|
||
$cr_model->device_template_id = $form_data['device_template_id'];
|
||
|
||
if (!$this->acl_check_new('Devices_Controller', 'devices'))
|
||
{
|
||
$cr_model->comment = $form_data['note'];
|
||
}
|
||
|
||
$cr_model->save_throwable();
|
||
|
||
$cr_model->transaction_commit();
|
||
|
||
// make next action
|
||
if ($this->acl_check_edit('Connection_Requests_Controller', 'request') &&
|
||
$this->acl_check_new('Devices_Controller', 'devices'))
|
||
{
|
||
// redirect to add form
|
||
$this->session->del('connection_request_mac');
|
||
url::redirect('connection_requests/approve_request/' . $cr_model->id);
|
||
}
|
||
else if (module::e('notification'))
|
||
{
|
||
// create comment for user
|
||
$comment = '<table>'
|
||
. '<tr><th>' . __('IP address') . ':</th>'
|
||
. '<td>' . $cr_model->ip_address . '</td></tr>'
|
||
. '<tr><th>' . __('Device type') . ':</th>'
|
||
. '<td>' . $cr_model->device_type->get_value() . '</td></tr>'
|
||
. '<tr><th>' . __('Date') . ':</th>'
|
||
. '<td>' . $cr_model->created_at . '</td></tr>'
|
||
. '</table>';
|
||
|
||
// trigger notice for member
|
||
Message_Model::activate_special_notice(
|
||
Message_Model::CONNECTION_REQUEST_INFO,
|
||
$cr_model->member_id, $this->session->get('user_id'),
|
||
Notifications_Controller::ACTIVATE,
|
||
Notifications_Controller::KEEP, $comment
|
||
);
|
||
|
||
status::success(
|
||
__('Your request has been succesfully stored.') . ' ' .
|
||
__('You will be informed about it\'s result by email.'),
|
||
FALSE
|
||
);
|
||
|
||
$this->session->del('connection_request_mac');
|
||
$this->redirect('connection_requests/show/', $cr_model->id);
|
||
}
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
$cr_model->transaction_rollback();
|
||
Log::add_exception($e);
|
||
status::error('Cannot add connection request.', $e);
|
||
}
|
||
}
|
||
|
||
// breadcrumbs navigation
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('connection_requests/show_all', 'Connection requests',
|
||
$this->acl_check_view('Connection_requests_Controller', 'request'))
|
||
->text('Add new request for connection');
|
||
|
||
$headline = __('Add new request for connection');
|
||
|
||
// default info
|
||
if (trim(Settings::get('connection_request_info')) == '')
|
||
{
|
||
// info for engeneer
|
||
if ($this->acl_check_view('Connection_Requests_Controller', 'request'))
|
||
{
|
||
$info = url_lang::lang('help.connection_request_info_short');
|
||
}
|
||
// info for user
|
||
else
|
||
{
|
||
$info = url_lang::lang('help.connection_request_info');
|
||
}
|
||
}
|
||
// set uped info
|
||
else
|
||
{
|
||
$info = Settings::get('connection_request_info');
|
||
}
|
||
|
||
// view
|
||
$view = new View('main');
|
||
$view->title = $headline;
|
||
$view->breadcrumbs = $breadcrumbs->html();
|
||
$view->content = new View('form');
|
||
$view->content->form = $form->html();
|
||
$view->content->headline = $headline;
|
||
$view->content->link_back = $info;
|
||
$view->render(TRUE);
|
||
}
|
||
|
||
/**
|
||
* Edit request.
|
||
*
|
||
* @param integer $connection_request_id
|
||
*/
|
||
public function edit($connection_request_id = NULL)
|
||
{
|
||
if (!is_numeric($connection_request_id))
|
||
{
|
||
Controller::warning(PARAMETER);
|
||
}
|
||
|
||
$cr_model = new Connection_request_Model($connection_request_id);
|
||
|
||
if (!$cr_model || !$cr_model->id)
|
||
{
|
||
Controller::error(RECORD);
|
||
}
|
||
|
||
if (!$this->acl_check_edit('Connection_Requests_Controller', 'request'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
|
||
if (!Settings::get('connection_request_enable'))
|
||
{
|
||
status::warning('Connection requests are not enabled.');
|
||
url::redirect('settings/system');
|
||
}
|
||
|
||
if ($cr_model->state != Connection_request_Model::STATE_UNDECIDED)
|
||
{
|
||
Controller::error(RECORD);
|
||
}
|
||
|
||
if (!$this->acl_check_edit('Connection_Requests_Controller', 'request'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
|
||
// all device templates
|
||
$arr_device_templates = array
|
||
(
|
||
NULL => ''
|
||
) + ORM::factory('device_template')->select_list();
|
||
|
||
// enum types for device
|
||
$enum_type_model = new Enum_type_Model();
|
||
$types = $enum_type_model->get_values(Enum_type_Model::DEVICE_TYPE_ID);
|
||
$types[NULL] = '----- '.__('Select type').' -----';
|
||
asort($types);
|
||
|
||
// form
|
||
$form = new Forge();
|
||
|
||
$form->hidden('crform_is_edit');
|
||
|
||
$form->dropdown('member_id')
|
||
->label('Connection owner')
|
||
->options(ORM::factory('member')->select_list_grouped())
|
||
->selected($cr_model->member_id)
|
||
->rules('required')
|
||
->style('width:200px');
|
||
|
||
$form->dropdown('device_type_id')
|
||
->options($types)
|
||
->selected($cr_model->device_type_id)
|
||
->label('Device type')
|
||
->rules('required')
|
||
->style('width:200px');
|
||
|
||
$form->dropdown('device_template_id')
|
||
->options($arr_device_templates)
|
||
->rules('required')
|
||
->label('Device template')
|
||
->selected($cr_model->device_template_id)
|
||
->style('width:200px');
|
||
|
||
$form->input('mac_address')
|
||
->label('MAC address of device')
|
||
->rules('required|valid_mac_address')
|
||
->value($cr_model->mac_address);
|
||
|
||
$form->textarea('note')
|
||
->value($cr_model->comment);
|
||
|
||
$form->submit('Save');
|
||
|
||
// post
|
||
if ($form->validate())
|
||
{
|
||
$form_data = $form->as_array();
|
||
|
||
try
|
||
{
|
||
$cr_model->transaction_start();
|
||
|
||
$cr_model->member_id = $form_data['member_id'];
|
||
$cr_model->mac_address = $form_data['mac_address'];
|
||
$cr_model->device_type_id = $form_data['device_type_id'];
|
||
$cr_model->device_template_id = $form_data['device_template_id'];
|
||
$cr_model->comment = $form_data['note'];
|
||
$cr_model->save_throwable();
|
||
|
||
$cr_model->transaction_commit();
|
||
|
||
status::success('Connection request has been succesfully edited.');
|
||
$this->redirect('connection_requests/show/', $cr_model->id);
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
$cr_model->transaction_rollback();
|
||
Log::add_exception($e);
|
||
status::error('Cannot edit connection request.', $e);
|
||
}
|
||
}
|
||
|
||
$headline = __('Edit request for connection');
|
||
|
||
// breadcrumbs navigation
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('connection_requests/show_all', 'Connection requests',
|
||
$this->acl_check_view('Connection_requests_Controller', 'request'))
|
||
->disable_translation()
|
||
->link('connection_requests/show/' . $cr_model->id,
|
||
$cr_model->ip_address . ' (' . $cr_model->mac_address . ')',
|
||
$this->acl_check_view('Connection_requests_Controller', 'request', $cr_model->member_id))
|
||
->text($headline);
|
||
|
||
// view
|
||
$view = new View('main');
|
||
$view->title = $headline;
|
||
$view->breadcrumbs = $breadcrumbs->html();
|
||
$view->content = new View('form');
|
||
$view->content->form = $form->html();
|
||
$view->content->headline = $headline;
|
||
$view->render(TRUE);
|
||
}
|
||
|
||
/**
|
||
* Approve the connection request.
|
||
* If all informations are set, a redirection to add device is made.
|
||
* Otherwise, user first must fill in missing informations.
|
||
*
|
||
* @param integer $connection_request_id
|
||
*/
|
||
public function approve_request($connection_request_id = NULL)
|
||
{
|
||
if (!is_numeric($connection_request_id))
|
||
{
|
||
Controller::warning(PARAMETER);
|
||
}
|
||
|
||
$cr_model = new Connection_request_Model($connection_request_id);
|
||
|
||
if (!$cr_model || !$cr_model->id)
|
||
{
|
||
Controller::error(RECORD);
|
||
}
|
||
|
||
if (!$this->acl_check_edit('Connection_Requests_Controller', 'request'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
|
||
if (!Settings::get('connection_request_enable'))
|
||
{
|
||
status::warning('Connection requests are not enabled.');
|
||
url::redirect('settings/system');
|
||
}
|
||
|
||
if ($cr_model->state != Connection_request_Model::STATE_UNDECIDED)
|
||
{
|
||
Controller::error(RECORD);
|
||
}
|
||
|
||
// some info were not entered?
|
||
if (!$cr_model->device_template_id)
|
||
{
|
||
$templates = array
|
||
(
|
||
NULL => '--- ' . __('Select template') . ' ---'
|
||
);
|
||
|
||
$templates += ORM::factory('device_template')
|
||
->where('enum_type_id', $cr_model->device_type_id)
|
||
->select_list();
|
||
|
||
// form
|
||
$form = new Forge();
|
||
|
||
$form->dropdown('device_template_id')
|
||
->options($templates)
|
||
->rules('required')
|
||
->label('Device template')
|
||
->style('width:200px');
|
||
|
||
$form->submit('Continue');
|
||
|
||
// post
|
||
if ($form->validate())
|
||
{
|
||
$form_data = $form->as_array();
|
||
|
||
try
|
||
{
|
||
$cr_model->transaction_start();
|
||
|
||
$cr_model->device_template_id = $form_data['device_template_id'];
|
||
$cr_model->save_throwable();
|
||
|
||
$cr_model->transaction_commit();
|
||
|
||
status::info('Add device for acception of connection request.');
|
||
// hard redirect, no $this->redirect here!
|
||
url::redirect('devices/add_from_connection_request/' . $cr_model->id);
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
$cr_model->transaction_rollback();
|
||
Log::add_exception($e);
|
||
status::error('Cannot edit connection request.', $e);
|
||
}
|
||
}
|
||
|
||
// breadcrumbs navigation
|
||
$breadcrumbs = breadcrumbs::add()
|
||
->link('connection_requests/show_all', 'Connection requests',
|
||
$this->acl_check_view('Connection_requests_Controller', 'request'))
|
||
->disable_translation()
|
||
->link('connection_requests/show/' . $cr_model->id,
|
||
$cr_model->ip_address . ' (' . $cr_model->mac_address . ')',
|
||
$this->acl_check_view('Connection_requests_Controller', 'request', $cr_model->member_id))
|
||
->enable_translation()
|
||
->text('Approve connection request');
|
||
|
||
// view
|
||
$headline = __('Approve connection request') . ': ' . __('Step') . ' 1';
|
||
$view = new View('main');
|
||
$view->title = $headline;
|
||
$view->breadcrumbs = $breadcrumbs->html();
|
||
$view->content = new View('form');
|
||
$view->content->form = $form->html();
|
||
$view->content->headline = $headline;
|
||
$view->render(TRUE);
|
||
}
|
||
else
|
||
{
|
||
// hard redirect, no $this->redirect here!
|
||
url::redirect('devices/add_from_connection_request/' . $cr_model->id);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Reject the connection request.
|
||
*
|
||
* @param integer $connection_request_id
|
||
*/
|
||
public function reject_request($connection_request_id = NULL)
|
||
{
|
||
if (!is_numeric($connection_request_id))
|
||
{
|
||
Controller::warning(PARAMETER);
|
||
}
|
||
|
||
$cr_model = new Connection_request_Model($connection_request_id);
|
||
|
||
if (!$cr_model || !$cr_model->id)
|
||
{
|
||
Controller::error(RECORD);
|
||
}
|
||
|
||
if (!$this->acl_check_edit('Connection_Requests_Controller', 'request'))
|
||
{
|
||
Controller::error(ACCESS);
|
||
}
|
||
|
||
if (!Settings::get('connection_request_enable'))
|
||
{
|
||
status::warning('Connection requests are not enabled.');
|
||
url::redirect('settings/system');
|
||
}
|
||
|
||
if ($cr_model->state != Connection_request_Model::STATE_UNDECIDED)
|
||
{
|
||
Controller::error(RECORD);
|
||
}
|
||
|
||
try
|
||
{
|
||
$cr_model->transaction_start();
|
||
|
||
// set data
|
||
$cr_model->state = Connection_request_Model::STATE_REJECTED;
|
||
$cr_model->decided_at = date('Y-m-d H:i:s');
|
||
$cr_model->decided_user_id = $this->user_id;
|
||
$cr_model->save_throwable();
|
||
|
||
$cr_model->transaction_commit();
|
||
|
||
// only if user sended request by him self
|
||
if (module::e('notification') &&
|
||
$cr_model->member_id == $cr_model->added_user->member_id)
|
||
{
|
||
$link = html::anchor(
|
||
'/connection_requests/show/' . $cr_model->id,
|
||
FALSE, FALSE, FALSE, FALSE
|
||
);
|
||
// create comment for user
|
||
$comment = '<table>'
|
||
. '<tr><th>' . __('Rejected by') . ':</th>'
|
||
. '<td>' . $cr_model->decided_user->get_full_name() . '</td></tr>'
|
||
. '<tr><th>' . __('Date') . ':</th>'
|
||
. '<td>' . $cr_model->decided_at . '</td></tr>'
|
||
. '<tr><th>' . __('Details') . ':</th>'
|
||
. '<td>' . $link . '</td></tr>'
|
||
. '</table>';
|
||
|
||
// trigger notice for member (if request was not added by some other)
|
||
Message_Model::activate_special_notice(
|
||
Message_Model::CONNECTION_REQUEST_REFUSE,
|
||
$cr_model->member_id, $this->user_id,
|
||
Notifications_Controller::ACTIVATE,
|
||
Notifications_Controller::ACTIVATE, $comment
|
||
);
|
||
}
|
||
|
||
status::success('Connection request has been rejected.');
|
||
}
|
||
catch (Exception $e)
|
||
{
|
||
$cr_model->transaction_rollback();
|
||
Log::add_exception($e);
|
Také k dispozici: Unified diff
Release 1.1.1