164 lines
6.9 KiB
PHP
164 lines
6.9 KiB
PHP
<?php
|
|
|
|
class Helper {
|
|
/**
|
|
* Generate SQL Filter condition (space separated) for a given column.
|
|
*
|
|
* @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.
|
|
*/
|
|
public static function generateFilterCondition($filterValue, string $columnName, bool $exactMatch = false): string {
|
|
$sql = "";
|
|
|
|
if (is_array($filterValue)) {
|
|
if (isset($filterValue['from']) && isset($filterValue['to'])) {
|
|
$sql = " AND `$columnName` >= " . $filterValue['from'] . " AND `$columnName` <= " . $filterValue['to'];
|
|
} elseif (isset($filterValue['from'])) {
|
|
$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;
|
|
} else if ($filterValue === null) {
|
|
$sql .= " AND `$columnName` IS NULL";
|
|
} else if (!empty($filterValue)) {
|
|
if ($exactMatch) {
|
|
$sql .= " AND `$columnName` = '" . $filterValue . "'";
|
|
} else {
|
|
$filterItems = explode(" ", $filterValue);
|
|
foreach ($filterItems as $item) {
|
|
$sql .= " AND `$columnName` LIKE '%" . $item . "%'";
|
|
}
|
|
}
|
|
} else if ($filterValue === 0) {
|
|
$sql .= " AND `$columnName` = 0";
|
|
}
|
|
|
|
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;
|
|
|
|
//TODO: fix this, skip arrays for now
|
|
if (is_array($value)) {
|
|
continue;
|
|
}
|
|
|
|
// 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");
|
|
}
|
|
|
|
/**
|
|
* Converts an array of objects to a CSV file.
|
|
* @param array $rows The array of objects to convert to CSV.
|
|
* @return string The CSV file content.
|
|
*/
|
|
public static function arrayToCsv(array $rows): string {
|
|
$output = fopen('php://temp', 'w');
|
|
|
|
// Add headers
|
|
fputcsv($output, array_keys((array) $rows[0]));
|
|
|
|
// Add rows
|
|
foreach ($rows as $row) {
|
|
fputcsv($output, (array) $row);
|
|
}
|
|
|
|
rewind($output);
|
|
$csv = stream_get_contents($output);
|
|
fclose($output);
|
|
|
|
return $csv;
|
|
}
|
|
|
|
/**
|
|
* Formats a number with the given number of decimals, decimal point, and thousands separator.
|
|
* @param $number
|
|
* @param int $decimals
|
|
* @param string $decPoint
|
|
* @param string $thousandsSep
|
|
* @return float
|
|
*/
|
|
public static function formatNumber($number, int $decimals = 2, string $decPoint = ",", string $thousandsSep = "."): string {
|
|
return number_format(intval($number), $decimals, $decPoint, $thousandsSep);
|
|
}
|
|
|
|
} |