36782-vm/includes/helpers.php
2025-12-12 14:13:03 +00:00

78 lines
2.6 KiB
PHP

<?php
// includes/helpers.php
/**
* Translates a status or payment method key into a human-readable string using the t() function.
*
* @param string $key The key to translate (e.g., 'pending_payment', 'bank_transfer').
* @return string The translated string.
*/
function get_status_translation(string $key): string {
// A set of known payment methods to prefix correctly.
$payment_methods = ['bank_transfer', 'online', 'credit'];
if (in_array($key, $payment_methods)) {
// It's a payment method, use 'payment_' prefix.
$translation_key = 'payment_' . $key;
} else {
// Assume it's an order status, use 'status_' prefix.
$translation_key = 'status_' . $key;
}
$translated = t($translation_key);
// If the translation key is returned, it means no translation was found.
// In that case, we provide a clean fallback.
if ($translated === $translation_key) {
return ucfirst(str_replace('_', ' ', $key));
}
return $translated;
}
function sanitize_filename($filename) {
// Keep the original extension
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$filename_without_ext = pathinfo($filename, PATHINFO_FILENAME);
// Replace spaces and special characters with underscores in the filename part
$sanitized_filename = preg_replace('/[\s\\\/:*?"<>|.]+/', '_', $filename_without_ext);
// Remove any leading/trailing underscores
$sanitized_filename = trim($sanitized_filename, '_');
// Ensure the filename is not empty
if (empty($sanitized_filename)) {
$sanitized_filename = 'unnamed_file';
}
// Re-append the extension if it exists
if (!empty($extension)) {
return $sanitized_filename . '.' . $extension;
}
return $sanitized_filename;
}
function upload_error_message($error_code) {
switch ($error_code) {
case UPLOAD_ERR_INI_SIZE:
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
case UPLOAD_ERR_FORM_SIZE:
return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
case UPLOAD_ERR_PARTIAL:
return 'The uploaded file was only partially uploaded';
case UPLOAD_ERR_NO_FILE:
return 'No file was uploaded';
case UPLOAD_ERR_NO_TMP_DIR:
return 'Missing a temporary folder';
case UPLOAD_ERR_CANT_WRITE:
return 'Failed to write file to disk.';
case UPLOAD_ERR_EXTENSION:
return 'A PHP extension stopped the file upload.';
default:
return 'Unknown upload error';
}
}