diff options
Diffstat (limited to 'includes/sys_page.php')
-rw-r--r-- | includes/sys_page.php | 85 |
1 files changed, 82 insertions, 3 deletions
diff --git a/includes/sys_page.php b/includes/sys_page.php index e336261d..3c548bab 100644 --- a/includes/sys_page.php +++ b/includes/sys_page.php @@ -1,6 +1,53 @@ <?php /** + * Provide page/request helper functions + */ + +/** + * Parse a date from da day and a time textfield. + * + * @param string $date_name + * Name of the textfield containing the day (format Y-m-d) + * @param string $time_name + * Name of the textfield containing the time (format H:i) + * @param string[] $allowed_days + * List of allowed days in format Y-m-d + * @param int $default_value + * Default value unix timestamp + */ +function check_request_datetime($date_name, $time_name, $allowed_days, $default_value) { + $time = date("H:i", $default_value); + $day = date("Y-m-d", $default_value); + + if (isset($_REQUEST[$time_name]) && preg_match('#^\d{1,2}:\d\d$#', trim($_REQUEST[$time_name]))) { + $time = trim($_REQUEST[$time_name]); + } + if (isset($_REQUEST[$date_name]) && in_array($_REQUEST[$date_name], $allowed_days)) { + $day = $_REQUEST[$date_name]; + } + + return parse_date("Y-m-d H:i", $day . " " . $time); +} + +/** + * Parse a date into unix timestamp + * + * @param string $pattern + * The date pattern (i.e. Y-m-d H:i) + * @param string $value + * The string to parse + * @return The parsed unix timestamp + */ +function parse_date($pattern, $value) { + $datetime = DateTime::createFromFormat($pattern, trim($value)); + if ($datetime == null) { + return null; + } + return $datetime->getTimestamp(); +} + +/** * Leitet den Browser an die übergebene URL weiter und hält das Script an. */ function redirect($url) { @@ -11,7 +58,8 @@ function redirect($url) { /** * Echoes given output and dies. * - * @param String $output + * @param String $output + * String to display */ function raw_output($output) { echo $output; @@ -19,6 +67,37 @@ function raw_output($output) { } /** + * Helper function for transforming list of entities into array for select boxes. + * + * @param array $data + * The data array + * @param string $key_name + * name of the column to use as id/key + * @param string $value_name + * name of the column to use as displayed value + */ +function select_array($data, $key_name, $value_name) { + $ret = []; + foreach ($data as $value) { + $ret[$value[$key_name]] = $value[$value_name]; + } + return $ret; +} + +/** + * Returns an int[] from given request param name. + * + * @param String $name + * Name of the request param + */ +function check_request_int_array($name) { + if (isset($_REQUEST[$name]) && is_array($_REQUEST[$name])) { + return array_filter($_REQUEST[$name], 'is_numeric'); + } + return []; +} + +/** * Checks if given request item (name) can be parsed to a date. * If not parsable, given error message is put into msg() and null is returned. * @@ -50,8 +129,8 @@ function check_request_date($name, $error_message = null, $null_allowed = false) * @return ValidationResult containing the parsed date */ function check_date($input, $error_message = null, $null_allowed = false) { - if (DateTime::createFromFormat("Y-m-d", trim($input))) { - return new ValidationResult(true, DateTime::createFromFormat("Y-m-d", trim($input))->getTimestamp()); + if ($tmp = parse_date("Y-m-d", trim($input))) { + return new ValidationResult(true, $tmp); } if ($null_allowed) { return new ValidationResult(true, null); |