<?
switch ($_GET["action"]) {
    case 'edit':
        edit_permission();
        break;
    case 'delete':
        delete_permission($permission['id']);
        break;
    case 'new':
        require_once("edit_permission_cardform.inc.php");
        break;
    case 'save':
        save_permission();
        break;
    default:
        require_once("edit_permission_listform.inc.php");
        break;
}

function edit_permission( $_id = "", $messages = array() ) {
    if ((int)$_id > 0) {
        $input_id = $_id;
    } else {
        $input_id = $_POST["input_id"];
    }

    if ($input_id <> '') {

        $pdoHost = getenv('MAIN_MYSQL_DB_HOST');
        $pdoPort = getenv('MAIN_MYSQL_DB_PORT');
        $pdoUser = getenv('MAIN_MYSQL_DB_USER');
        $pdoPass = getenv('MAIN_MYSQL_DB_PASS');
        $pdoSchema = getenv('MAIN_MYSQL_DB_SCHEMA');

        $pdo = new \DynCom\mysyde\common\classes\PDOQueryWrapper($pdoHost, $pdoPort, $pdoSchema, $pdoUser, $pdoPass);

        $prepStatement = "  SELECT * FROM main_permission WHERE id = :id LIMIT 1 ";
        $params = [
            [':id', $input_id, PDO::PARAM_STR],
        ];
        $pdo->setQuery($prepStatement);
        $pdo->prepareQuery();
        $pdo->bindParameters($params);
        $pdo->executePreparedStatement();
        $result = $pdo->getResultArray();

        if (count($result) == 1) {
            $input_permission             = $result[0];
            $input_permission["password"] = "nochange";
            require_once("edit_permission_cardform.inc.php");
        }
    } else {
        $input_permission = array();
        require_once("edit_permission_cardform.inc.php");
    }
}

function delete_permission($curr_permission_id) {
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    if ($_POST["input_id"] <> '') {
        if ($_POST["input_id"] <> $curr_permission_id) {
            $query      = "DELETE FROM main_permission WHERE id = '" . $_POST["input_id"] . "' LIMIT 1";
            @mysqli_query($GLOBALS['mysql_con'], $query);
            $deletePermission = @mysqli_query($GLOBALS["mysql_con"], "DELETE FROM main_contact_permission WHERE main_permission_id = ".$_POST["input_id"]);  
        } else {
            header('EDIT_ERROR: 1');
            $messages[] = "<div class=\"errorbox\">" . $translation->get("permission_error5") . "</div>";
            edit_permission($_POST["input_id"], $messages);
        }
    }
    require_once("edit_permission_listform.inc.php");
}

function save_permission() {
    $messages    = array();
    $error       = FALSE;
    $input_id    = "";
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");

    $only_main_mandant = ($_POST["input_only_main_mandant"] == "on") ? 1 : 0;
    $default_active = ($_POST["input_default_active"] == "on") ? 1 : 0;

    if ($_POST["input_id"] <> '') {
        $inserted = FALSE;
        $input_id = $_POST["input_id"];
        $query = "UPDATE main_permission
			SET description = '" . $_POST['input_description'] . "',
			code = '" . $_POST['input_code'] . "',
			short_text = '" . $_POST['input_short_text'] . "',
			only_main_mandant = '" . $only_main_mandant . "',
			default_active = '" . $default_active . "'
            WHERE id = '" . $input_id . "' LIMIT 1";
    } else {
        $query    = "INSERT INTO main_permission (id, description, code, short_text, only_main_mandant, default_active) VALUES (
			NULL,
			'" . $_POST['input_description'] . "',
			'" . $_POST['input_code'] . "',
			'" . $_POST['input_short_text'] . "'
			'" . $only_main_mandant . "',
			'" . $default_active . "'
			)";
        $inserted = TRUE;
    }

    if (!$error) {

        @mysqli_query($GLOBALS['mysql_con'], $query);
        if ($inserted == TRUE) {
            $input_id   = mysqli_insert_id($GLOBALS['mysql_con']);
            $messages[] = '<div class="successbox">' . $translation->get("permission_msg_success1") . '</div>';
        } else {
            $messages[] = '<div class="successbox">' . $translation->get("permission_msg_success2") . '</div>';
        } 
        edit_permission($input_id, $messages);
    } else {
        header('EDIT_ERROR: 1');
        require_once("edit_permission_cardform.inc.php");
    }
}
