<?php

/**
 *	Print all available VRFs and configurations
 ************************************************/
/* required functions */
require_once '../../functions/functions.php';
/* verify that user is admin */
checkAdmin();
/* get post */
$vlanPost = $_POST;
/* get all available VRFs */
$vlan = subnetGetVLANdetailsById($_POST['vlanId']);
/* get custom fields */
$custom = getCustomVLANFields();
if ($_POST['action'] == "delete") {
    $readonly = "readonly";
} else {
    $readonly = "";
}
/* set form name! */
if (isset($_POST['fromSubnet'])) {
    $formId = "vlanManagementEditFromSubnet";
} else {
    $formId = "vlanManagementEdit";
}
?>

<!-- header -->
<div class="pHeader"><?php 
print ucwords(_("{$_POST['action']}"));
/**
 * Search VLANS
 */
function searchVLANs($searchterm)
{
    global $db;
    # get variables from config file
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    # get custom VLAN fields
    $myFields = getCustomVLANFields();
    $custom = '';
    if (sizeof($myFields) > 0) {
        /* set inserts for custom */
        foreach ($myFields as $myField) {
            $custom .= ' or `' . $myField['name'] . '` like "%' . $searchterm . '%" ';
        }
    }
    /* set query */
    $query = 'select * from `vlans` where `name` like "%' . $searchterm . '%" or `description` like "%' . $searchterm . '%" or `number` like "%' . $searchterm . '%" ' . $custom . ';';
    /* execute */
    try {
        $search = $database->getArray($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
        print "<div class='alert alert-error'>" . _('Error') . ": {$error}</div>";
        return false;
    }
    /* return result */
    return $search;
}
/**
 * Get all VLANS
 */
function getAllVlans($tools = false)
{
    global $db;
    # get variables from config file
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    # custom fields
    $myFields = getCustomVLANFields();
    $myFieldsInsert['id'] = '';
    if (sizeof($myFields) > 0) {
        /* set inserts for custom */
        foreach ($myFields as $myField) {
            $myFieldsInsert['id'] .= ',`vlans`.`' . $myField['name'] . '`';
        }
    }
    /* check if it came from tools and use different query! */
    if ($tools) {
        $query = 'SELECT vlans.number,vlans.name,vlans.description,subnets.subnet,subnets.mask,subnets.id AS subnetId,subnets.sectionId' . $myFieldsInsert['id'] . ' FROM vlans LEFT JOIN subnets ON subnets.vlanId = vlans.vlanId ORDER BY vlans.number ASC;';
    } else {
        $query = 'select * from `vlans` order by `number` asc;';
    }
    /* execute */
    try {
        $vlan = $database->getArray($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
        print "<div class='alert alert-error'>" . _('Error') . ":{$error}</div>";
        return false;
    }
    /* return vlan details */
    return $vlan;
}
/**
 * Update VLAN details
 */
function updateVLANDetails($vlan)
{
    global $db;
    # get variables from config file
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    /* set querry based on action */
    if ($vlan['action'] == "add") {
        # custom fields
        $myFields = getCustomVLANFields();
        $myFieldsInsert['query'] = '';
        $myFieldsInsert['values'] = '';
        if (sizeof($myFields) > 0) {
            /* set inserts for custom */
            foreach ($myFields as $myField) {
                $myFieldsInsert['query'] .= ', `' . $myField['name'] . '`';
                $myFieldsInsert['values'] .= ", '" . $vlan[$myField['name']] . "'";
            }
        }
        $query = 'insert into `vlans` ' . "\n";
        $query .= '(`name`,`number`,`description` ' . $myFieldsInsert['query'] . ') values ' . "\n";
        $query .= '("' . $vlan['name'] . '", "' . $vlan['number'] . '", "' . $vlan['description'] . '" ' . $myFieldsInsert['values'] . ' ); ' . "\n";
    } else {
        if ($vlan['action'] == "edit") {
            # custom fields
            $myFields = getCustomVLANFields();
            $myFieldsInsert['query'] = '';
            if (sizeof($myFields) > 0) {
                /* set inserts for custom */
                foreach ($myFields as $myField) {
                    $myFieldsInsert['query'] .= ', `' . $myField['name'] . '` = "' . $vlan[$myField['name']] . '" ';
                }
            }
            $query = 'update `vlans` set ' . "\n";
            $query .= '`name` = "' . $vlan['name'] . '", `number` = "' . $vlan['number'] . '", `description` = "' . $vlan['description'] . '" ' . "\n";
            $query .= $myFieldsInsert['query'];
            $query .= 'where `vlanId` = "' . $vlan['vlanId'] . '";' . "\n";
        } else {
            if ($vlan['action'] == "delete") {
                $query = 'delete from `vlans` where `vlanId` = "' . $vlan['vlanId'] . '";' . "\n";
            }
        }
    }
    /* execute */
    try {
        $res = $database->executeQuery($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
        print "<div class='alert alert-error'>" . _('Error') . ": {$error}</div>";
        updateLogTable('VLAN ' . $vlan['action'] . ' failed (' . $vlan['name'] . ')' . $error, $log, 2);
        return false;
    }
    # if delete also NULL all subnets!
    if ($vlan['action'] == 'delete') {
        $query = "update `subnets` set `vlanId` = NULL where `vlanId` = '{$vlan['vlanId']}';";
        /* execute */
        try {
            $database->executeQuery($query);
        } catch (Exception $e) {
            $error = $e->getMessage();
            print '<div class="alert alert-error alert-absolute">' . $error . '</div>';
        }
    }
    /* prepare log */
    $log = prepareLogFromArray($vlan);
    /* return success */
    updateLogTable('VLAN ' . $vlan['action'] . ' success (' . $vlan['name'] . ')', $log, 0);
    return true;
}
/**
 * Script tomanage custom IP fields
 ****************************************/
/* verify that user is admin */
checkAdmin();
/* get all custom fields */
$myFields = getCustomIPaddrFields();
/* Custom fields by number */
$myFieldsNum = getCustomIPaddrFieldsNumArr();
/* get all custom subnet fields */
$myFieldsSubnets = getCustomSubnetFields();
/* Custom fields by number */
$myFieldsNumSubnets = getCustomSubnetsFieldsNumArr();
/* get all custom VLAN fields */
$myFieldsVLAN = getCustomVLANFields();
/* Custom VLAN fields by number */
$myFieldsNumVLAN = getCustomVLANFieldsNumArr();
/* get all custom User fields */
$myFieldsUser = getCustomUserFields();
/* Custom VLAN fields by number */
$myFieldsNumUser = getCustomUserFieldsNumArr();
?>


<h4><?php 
print _('Custom fields');
?>
</h4>
<hr>
<div class="alert alert-info alert-absolute"><?php