Files
citrus-cms/app/Functions/inspection-history-config.php
T
2026-04-28 21:14:25 +03:00

76 lines
2.4 KiB
PHP

<?php
/**
* Get inspection history configuration for a module from settings
*
* @param string $moduleSlug Module slug to get configuration for
* @param string|null $moduleTitle Optional module title for path generation
* @return array|null Returns array with 'path', 'pattern', and 'active' keys, or null if not found
*/
function getInspectionHistoryConfig($moduleSlug, $moduleTitle = null)
{
if (empty($moduleSlug)) {
return null;
}
// Get the module settings value
$settingsJson = setting('inspection_history_management', false, '{}');
$settings = json_decode($settingsJson, true);
$moduleConfig = null;
if (isset($settings[$moduleSlug])) {
$moduleConfig = $settings[$moduleSlug];
} else {
// Try kebab-case version if underscore version not found
$kebabSlug = str_replace('_', '-', $moduleSlug);
if (isset($settings[$kebabSlug])) {
$moduleConfig = $settings[$kebabSlug];
}
}
if (!$moduleConfig) {
if ($moduleSlug === 'Other') {
$moduleConfig = [
'active' => true,
'path_pattern' => '',
'file_pattern' => '{timestamp}_{file_name}'
];
} else {
return null;
}
}
// Get the main path setting
$mainPath = setting('inspection_history_main_path', false, '');
// If no main path is set, return null (feature not configured)
if (empty($mainPath)) {
return null;
}
// Get module title if not provided
if (empty($moduleTitle)) {
$module = \App\Types::where('slug', $moduleSlug)->first(['title']);
$moduleTitle = $module ? $module->title : $moduleSlug;
}
// Clean module title for use as directory name
$cleanTitle = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $moduleTitle);
$cleanTitle = preg_replace('/_+/', '_', $cleanTitle);
$cleanTitle = trim($cleanTitle, '_');
// Build the full path: main_path/module_title
$fullPath = $mainPath . '/' . $cleanTitle;
return [
'path' => $fullPath,
'path' => $fullPath,
'path_pattern' => $moduleConfig['path_pattern'] ?? null,
'file_pattern' => $moduleConfig['file_pattern'] ?? $moduleConfig['pattern'] ?? null,
'pattern' => $moduleConfig['pattern'] ?? null, // Keep for backward compatibility elsewhere if needed
'active' => $moduleConfig['active'] ?? false
];
}