Feature/warehouse

This commit is contained in:
Luca Haid
2024-07-16 06:55:46 +00:00
parent 8b4589523e
commit e75f13f8d8
64 changed files with 3207 additions and 265 deletions

View File

@@ -4,10 +4,9 @@ class Helper {
/**
* Generate SQL Filter condition (space separated) for a given column.
*
* @param string|null $filterValue The filter value to match against.
* @param string|null|array $filterValue The filter value to match against.
* @param string $columnName The name of the column in the database table.
* @return string The SQL condition generated based on the filter value and column name.
* @noinspection PhpMissingParamTypeInspection
*/
public static function generateFilterCondition($filterValue, string $columnName, bool $exactMatch = false): string {
$sql = "";
@@ -19,6 +18,8 @@ class Helper {
$sql = " AND `$columnName` >= " . $filterValue['from'];
} elseif (isset($filterValue['to'])) {
$sql = " AND `$columnName` <= " . $filterValue['to'];
} else if (isset($filterValue['exact'])) {
$sql = " AND `$columnName` = " . "'{$filterValue['exact']}'";
}
} else if ($filterValue === "0" || $filterValue === "1") {
$sql .= " AND `$columnName` = " . $filterValue;
@@ -37,4 +38,95 @@ class Helper {
return $sql;
}
/**
* Validates an array of data based on a set of predefined rules.
*
* @param array $data The data to validate. Keys represent field names, and values are the corresponding data.
* @param array $checkArray An associative array defining validation rules for each field:
* - key: The field name to validate.
* - value: An associative array of validation rules for that field:
* - required (bool, optional): Whether the field is required. Default: false.
* - title (string, optional): The human-readable name of the field to use in error messages.
* - required_length (int, optional): The minimum required length of the value. Default: 1.
* - regex (string, optional): A regular expression pattern the value must match.
*
* @return array|true Returns `true` if validation passes for all fields. Otherwise, returns an associative array of errors
* where keys are field names, and values are error messages.
*/
public static function validateArray(array $data, array $checkArray, bool $printErrors = true) {
$errors = [];
foreach ($checkArray as $key => $rules) {
$value = $data[$key] ?? null;
$title = $rules['title'] ?? $key;
// Apply default values for missing rules
$rules = array_merge([
'required' => false,
'required_length' => 1,
'regex' => false,
], $rules);
// Required Check
if ($rules['required'] && (is_null($value) || $value === '')) {
$errors[$key] = "$title wird benötigt.";
}
// Length Check (only if value exists)
if (!is_null($value) && strlen($value) < $rules['required_length']) {
$errors[$key] = "$title muss mindestens $rules[required_length] Zeichen lang sein.";
}
// Regex Check (only if value exists and regex is provided)
if (!is_null($value) && $rules['regex'] && !preg_match($rules['regex'], $value)) {
$errors[$key] = "$title hat ein ungültiges Format.";
}
}
if ($printErrors) {
if (!empty($errors)) {
header('Content-Type: application/json');
die(json_encode(
[
'success' => false,
'errors' => $errors
]
));
}
}
return empty($errors) ? true : $errors;
}
/**
* Displays Vue component with the given header title.
*
* @param mfBaseController $controller The controller instance to generate $JSGlobals for.
* @param string $pageName The name of the Vue component to render.
* @param string $headerTitle The title to display in the header.
* @param array $additionalGlobals Additional global variables to pass to the Vue component.
*/
public static function renderVue(mfBaseController $controller, string $pageName, string $headerTitle, array $additionalGlobals = []) {
$JSGlobals = ["BASE_URL" => $controller::getUrl($pageName),
"MF_URL" => $controller::getUrl(""),
"DASHBOARD_URL" => $controller::getUrl("Dashboard"),
"MF_APP_NAME" => MFAPPNAME_SLUG,
"BASE_PATH" => $controller::getUrl(""),
"PAGE_TITLE" => $headerTitle,
"PATH" => [
["text" => MFAPPNAME_SLUG, "href" => $controller::getUrl("Dashboard")],
["text" => $headerTitle, "href" => $controller::getUrl($pageName)]
],
];
$JSGlobals = array_merge($JSGlobals, $additionalGlobals);
$controller->layout()->set("vueViewName", $pageName);
$controller->layout()->set("JSGlobals", $JSGlobals);
$controller->layout()->setTemplate("VueViews/Vue");
}
}