125 lines
3.6 KiB
PHP
125 lines
3.6 KiB
PHP
<?php
|
|
/******************************************************************************
|
|
* YMS - Yacht Management Software
|
|
* Copyright (C) 2024-2026 Thomas Hooge
|
|
*
|
|
* SPDX-License-Identifier: WTFPL
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
// ========== GPC FUNCTIONS ===================================================
|
|
|
|
function gpc_get_string(&$GPC, $varname, $maxlen = NULL, $default = NULL) {
|
|
// TODO Parameter nullval: single value or array of values which considered as null
|
|
// Default value for nullval is "-1"
|
|
if (!isset($GPC[$varname]) or (strlen(trim($GPC[$varname])) == 0)) {
|
|
return $default;
|
|
}
|
|
$s = trim($GPC[$varname]);
|
|
if (isset($maxlen)) {
|
|
return substr($s, 0, $maxlen);
|
|
}
|
|
return $s;
|
|
}
|
|
|
|
function gpc_get_int(&$GPC, $varname, $default = NULL) {
|
|
if (!isset($GPC[$varname])) {
|
|
return $default;
|
|
}
|
|
return (int)$GPC[$varname];
|
|
}
|
|
|
|
function gpc_get_float(&$GPC, $varname, $default = NULL) {
|
|
global $g_lconv;
|
|
if (!isset($GPC[$varname]) or (strlen(trim($GPC[$varname])) == 0)) {
|
|
return $default;
|
|
}
|
|
$var = str_replace($g_lconv['thousands_sep'], '', $GPC[$varname]);
|
|
if ($g_lconv['decimal_point'] != '.') {
|
|
$var = str_replace($g_lconv['decimal_point'], '.', $var);
|
|
}
|
|
return (double)$var;
|
|
}
|
|
|
|
function gpc_get_currency(&$GPC, $varname, $default = NULL) {
|
|
global $g_lconv;
|
|
if (!isset($GPC[$varname]) or (strlen(trim($GPC[$varname])) == 0)) {
|
|
return $default;
|
|
}
|
|
$var = str_replace($g_lconv['thousands_sep'], '', $GPC[$varname]);
|
|
if ($g_lconv['decimal_point'] != '.') {
|
|
$var = str_replace($g_lconv['decimal_point'], '.', $var);
|
|
}
|
|
return (double)$var;
|
|
}
|
|
|
|
function gpc_get_bool(&$GPC, $varname, $default = FALSE) {
|
|
if (!isset($GPC[$varname])) {
|
|
return $default;
|
|
}
|
|
$gpcvar = trim($GPC[$varname]);
|
|
if (strcasecmp('off', $gpcvar) == 0 ||
|
|
strcasecmp('no', $gpcvar) == 0 ||
|
|
strcasecmp('false', $gpcvar) == 0 ||
|
|
strcasecmp('no', $gpcvar) == 0 ||
|
|
strcasecmp('0', $gpcvar) == 0 ||
|
|
strcasecmp('nein', $gpcvar) == 0)
|
|
{
|
|
return FALSE;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
function gpc_get_date(&$GPC, $varname, $default = NULL) {
|
|
// TODO improve
|
|
if (!isset($GPC[$varname]) || $GPC[$varname] == '') {
|
|
return $default;
|
|
}
|
|
return $GPC[$varname];
|
|
}
|
|
|
|
function gpc_get_datetime(&$GPC, $varname, $default = NULL) {
|
|
// TODO improve
|
|
if (!isset($GPC[$varname]) || $GPC[$varname] == '') {
|
|
return $default;
|
|
}
|
|
return $GPC[$varname];
|
|
}
|
|
|
|
function gpc_get_color(&$GPC, $varname, $default = NULL) {
|
|
if (!isset($GPC[$varname]) or (strlen(trim($GPC[$varname])) == 0)) {
|
|
return $default;
|
|
}
|
|
$color = $GPC[$varname];
|
|
if ((strlen($color) == 7) and (substr($color, 0, 1) == '#')) {
|
|
$color = substr($color, 1);
|
|
}
|
|
if ((strlen($color) != 6) or (! ctype_xdigit($color))) {
|
|
return $default;
|
|
}
|
|
return strtoupper($color);
|
|
}
|
|
|
|
function gpc_get_enum(&$GPC, $varname, $values, $default = NULL) {
|
|
if (!isset($GPC[$varname]) or (strlen(trim($GPC[$varname])) == 0)) {
|
|
return $default;
|
|
}
|
|
if (! in_array($GPC[$varname], $values)) {
|
|
return $default;
|
|
}
|
|
return $GPC[$varname];
|
|
}
|
|
|
|
function gpc_get_set(&$GPC, $varname, $allowed, $as_string = false, $default = []) {
|
|
if (!isset($GPC[$varname])) {
|
|
return $default;
|
|
}
|
|
$selected = array_intersect($allowed, array_keys($GPC[$varname]));
|
|
if ($as_string) {
|
|
return implode(',', $selected);
|
|
} else {
|
|
return array_values($selected);
|
|
}
|
|
}
|