379 lines
7.5 KiB
PHP
379 lines
7.5 KiB
PHP
<?php
|
|
/**
|
|
* Helper Functions
|
|
*/
|
|
|
|
/**
|
|
* Escape HTML output
|
|
*/
|
|
function e(string $value): string
|
|
{
|
|
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
/**
|
|
* Generate CSRF token
|
|
*/
|
|
function csrf_token(): string
|
|
{
|
|
$session = new \App\Core\Session();
|
|
return $session->getCsrfToken();
|
|
}
|
|
|
|
/**
|
|
* Generate CSRF field
|
|
*/
|
|
function csrf_field(): string
|
|
{
|
|
return '<input type="hidden" name="csrf_token" value="' . csrf_token() . '">';
|
|
}
|
|
|
|
/**
|
|
* Format date
|
|
*/
|
|
function format_date(string $date, string $format = 'd.m.Y'): string
|
|
{
|
|
return date($format, strtotime($date));
|
|
}
|
|
|
|
/**
|
|
* Format datetime
|
|
*/
|
|
function format_datetime(string $date, string $format = 'd.m.Y H:i'): string
|
|
{
|
|
return date($format, strtotime($date));
|
|
}
|
|
|
|
/**
|
|
* Format currency
|
|
*/
|
|
function format_currency(float $amount): string
|
|
{
|
|
return number_format($amount, 2, ',', '.') . ' €';
|
|
}
|
|
|
|
/**
|
|
* Generate asset number
|
|
*/
|
|
function generate_asset_number(string $prefix = 'ASSET'): string
|
|
{
|
|
return $prefix . '-' . date('Y') . '-' . str_pad(rand(1, 9999), 4, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
/**
|
|
* Validate file upload
|
|
*/
|
|
function validate_file_upload(array $file, array $allowedTypes = [], int $maxSize = 52428800): array
|
|
{
|
|
$errors = [];
|
|
|
|
if ($file['error'] !== UPLOAD_ERR_OK) {
|
|
$errors[] = 'Upload error: ' . $file['error'];
|
|
return $errors;
|
|
}
|
|
|
|
if ($file['size'] > $maxSize) {
|
|
$errors[] = 'File too large. Maximum size: ' . format_bytes($maxSize);
|
|
}
|
|
|
|
if (!empty($allowedTypes)) {
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
$mimeType = finfo_file($finfo, $file['tmp_name']);
|
|
finfo_close($finfo);
|
|
|
|
if (!in_array($mimeType, $allowedTypes)) {
|
|
$errors[] = 'File type not allowed. Allowed types: ' . implode(', ', $allowedTypes);
|
|
}
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
|
|
/**
|
|
* Format bytes to human readable
|
|
*/
|
|
function format_bytes(int $bytes): string
|
|
{
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
|
|
for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
|
|
$bytes /= 1024;
|
|
}
|
|
|
|
return round($bytes, 2) . ' ' . $units[$i];
|
|
}
|
|
|
|
/**
|
|
* Generate random string
|
|
*/
|
|
function random_string(int $length = 32): string
|
|
{
|
|
return bin2hex(random_bytes($length / 2));
|
|
}
|
|
|
|
/**
|
|
* Check if user has permission
|
|
*/
|
|
function has_permission(string $permission): bool
|
|
{
|
|
$session = new \App\Core\Session();
|
|
$role = $session->getUserRole();
|
|
|
|
switch ($permission) {
|
|
case 'admin':
|
|
return $role === 'admin';
|
|
case 'auditor':
|
|
return in_array($role, ['admin', 'auditor']);
|
|
case 'employee':
|
|
return in_array($role, ['admin', 'auditor', 'employee']);
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get user role name
|
|
*/
|
|
function get_role_name(string $role): string
|
|
{
|
|
$roles = [
|
|
'admin' => 'Administrator',
|
|
'auditor' => 'Auditor',
|
|
'employee' => 'Mitarbeiter'
|
|
];
|
|
|
|
return $roles[$role] ?? $role;
|
|
}
|
|
|
|
/**
|
|
* Get asset status name
|
|
*/
|
|
function get_asset_status_name(string $status): string
|
|
{
|
|
$statuses = [
|
|
'aktiv' => 'Aktiv',
|
|
'inaktiv' => 'Inaktiv',
|
|
'ausgemustert' => 'Ausgemustert'
|
|
];
|
|
|
|
return $statuses[$status] ?? $status;
|
|
}
|
|
|
|
/**
|
|
* Get asset condition name
|
|
*/
|
|
function get_asset_condition_name(string $condition): string
|
|
{
|
|
$conditions = [
|
|
'neu' => 'Neu',
|
|
'gut' => 'Gut',
|
|
'befriedigend' => 'Befriedigend',
|
|
'schlecht' => 'Schlecht',
|
|
'defekt' => 'Defekt'
|
|
];
|
|
|
|
return $conditions[$condition] ?? $condition;
|
|
}
|
|
|
|
/**
|
|
* Get inventory status name
|
|
*/
|
|
function get_inventory_status_name(string $status): string
|
|
{
|
|
$statuses = [
|
|
'offen' => 'Offen',
|
|
'abgeschlossen' => 'Abgeschlossen'
|
|
];
|
|
|
|
return $statuses[$status] ?? $status;
|
|
}
|
|
|
|
/**
|
|
* Get inventory item status name
|
|
*/
|
|
function get_inventory_item_status_name(string $status): string
|
|
{
|
|
$statuses = [
|
|
'gefunden' => 'Gefunden',
|
|
'nicht_gefunden' => 'Nicht gefunden',
|
|
'defekt' => 'Defekt',
|
|
'verschoben' => 'Verschoben'
|
|
];
|
|
|
|
return $statuses[$status] ?? $status;
|
|
}
|
|
|
|
/**
|
|
* Generate QR code data for asset
|
|
*/
|
|
function generate_qr_data(array $asset): string
|
|
{
|
|
return json_encode([
|
|
'id' => $asset['id'],
|
|
'inventarnummer' => $asset['inventarnummer'],
|
|
'bezeichnung' => $asset['bezeichnung']
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Check if warranty is expiring soon
|
|
*/
|
|
function is_warranty_expiring_soon(string $warrantyDate, int $days = 30): bool
|
|
{
|
|
if (empty($warrantyDate)) {
|
|
return false;
|
|
}
|
|
|
|
$warranty = strtotime($warrantyDate);
|
|
$now = time();
|
|
$expiring = strtotime("+{$days} days", $now);
|
|
|
|
return $warranty <= $expiring && $warranty > $now;
|
|
}
|
|
|
|
/**
|
|
* Calculate asset age in years
|
|
*/
|
|
function calculate_asset_age(string $purchaseDate): int
|
|
{
|
|
if (empty($purchaseDate)) {
|
|
return 0;
|
|
}
|
|
|
|
$purchase = new DateTime($purchaseDate);
|
|
$now = new DateTime();
|
|
$diff = $now->diff($purchase);
|
|
|
|
return $diff->y;
|
|
}
|
|
|
|
/**
|
|
* Get asset value depreciation
|
|
*/
|
|
function calculate_depreciation(float $purchasePrice, string $purchaseDate, float $depreciationRate = 0.1): float
|
|
{
|
|
$age = calculate_asset_age($purchaseDate);
|
|
$depreciation = $purchasePrice * $depreciationRate * $age;
|
|
|
|
return max(0, $purchasePrice - $depreciation);
|
|
}
|
|
|
|
/**
|
|
* Sanitize filename
|
|
*/
|
|
function sanitize_filename(string $filename): string
|
|
{
|
|
// Remove special characters
|
|
$filename = preg_replace('/[^a-zA-Z0-9._-]/', '_', $filename);
|
|
|
|
// Remove multiple underscores
|
|
$filename = preg_replace('/_+/', '_', $filename);
|
|
|
|
// Remove leading/trailing underscores
|
|
$filename = trim($filename, '_');
|
|
|
|
return $filename;
|
|
}
|
|
|
|
/**
|
|
* Get file extension from mime type
|
|
*/
|
|
function get_extension_from_mime(string $mimeType): string
|
|
{
|
|
$extensions = [
|
|
'application/pdf' => 'pdf',
|
|
'image/jpeg' => 'jpg',
|
|
'image/png' => 'png',
|
|
'image/gif' => 'gif',
|
|
'application/msword' => 'doc',
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
|
|
'application/vnd.ms-excel' => 'xls',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
|
|
'text/plain' => 'txt',
|
|
'text/csv' => 'csv'
|
|
];
|
|
|
|
return $extensions[$mimeType] ?? 'bin';
|
|
}
|
|
|
|
/**
|
|
* Check if string is valid JSON
|
|
*/
|
|
function is_valid_json(string $string): bool
|
|
{
|
|
json_decode($string);
|
|
return json_last_error() === JSON_ERROR_NONE;
|
|
}
|
|
|
|
/**
|
|
* Get current user
|
|
*/
|
|
function current_user(): ?array
|
|
{
|
|
$session = new \App\Core\Session();
|
|
return $session->getUser();
|
|
}
|
|
|
|
/**
|
|
* Get current user ID
|
|
*/
|
|
function current_user_id(): ?int
|
|
{
|
|
$session = new \App\Core\Session();
|
|
return $session->getUserId();
|
|
}
|
|
|
|
/**
|
|
* Check if user is logged in
|
|
*/
|
|
function is_logged_in(): bool
|
|
{
|
|
$session = new \App\Core\Session();
|
|
return $session->isLoggedIn();
|
|
}
|
|
|
|
/**
|
|
* Check if user is admin
|
|
*/
|
|
function is_admin(): bool
|
|
{
|
|
$session = new \App\Core\Session();
|
|
return $session->isAdmin();
|
|
}
|
|
|
|
/**
|
|
* Redirect to URL
|
|
*/
|
|
function redirect(string $url): void
|
|
{
|
|
header("Location: {$url}");
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Get base URL
|
|
*/
|
|
function base_url(string $path = ''): string
|
|
{
|
|
$baseUrl = rtrim(APP_URL, '/');
|
|
$path = ltrim($path, '/');
|
|
|
|
return $baseUrl . '/' . $path;
|
|
}
|
|
|
|
/**
|
|
* Asset URL
|
|
*/
|
|
function asset_url(string $path): string
|
|
{
|
|
return base_url('assets/' . ltrim($path, '/'));
|
|
}
|
|
|
|
/**
|
|
* Storage URL
|
|
*/
|
|
function storage_url(string $path): string
|
|
{
|
|
return base_url('storage/' . ltrim($path, '/'));
|
|
}
|