getHookManager()->addAction($hook, $listener, $priority); } } if (!function_exists('do_action')) { function do_action(string $hook, ...$args): void { Application::getInstance()->getHookManager()->doAction($hook, ...$args); } } if (!function_exists('add_filter')) { function add_filter(string $hook, callable $listener, int $priority = 10): void { Application::getInstance()->getHookManager()->addFilter($hook, $listener, $priority); } } if (!function_exists('apply_filters')) { function apply_filters(string $hook, $value, ...$args) { return Application::getInstance()->getHookManager()->applyFilters($hook, $value, ...$args); } } if (!function_exists('get_active_theme')) { /** * Get the currently active theme name */ function get_active_theme(): string { static $activeTheme = null; if ($activeTheme === null) { try { $themeConfig = \Illuminate\Database\Capsule\Manager::table('system_config') ->where('system_config_key', 'active_theme') ->first(); $activeTheme = $themeConfig ? $themeConfig->system_config_value : 'default'; } catch (\Exception $e) { $activeTheme = 'default'; } } return $activeTheme; } } if (!function_exists('asset_url')) { /** * Generate asset URL with theme support * * @param string $path The asset path * @return string The complete asset URL */ function asset_url(string $path): string { $activeTheme = get_active_theme(); $themeAssetPath = 'themes/' . $activeTheme . '/assets/' . ltrim($path, '/'); // Check if the asset exists in the theme folder if (file_exists(dirname(__DIR__) . '/' . $themeAssetPath)) { return '/' . $themeAssetPath; } return '/assets/' . ltrim($path, '/'); } } if (!function_exists('url')) { /** * Generate application URL * * @param string $path The path to append to the base URL * @return string The complete URL */ function url(string $path = ''): string { return '/' . ltrim($path, '/'); } } if (!function_exists('route')) { /** * Generate route URL (placeholder for future implementation) * * @param string $name The route name * @param array $params Route parameters * @return string The route URL */ function route(string $name, array $params = []): string { // This will be implemented when we have a named route system return url($name); } } if (!function_exists('csrf_field')) { /** * Generate CSRF token field * * @return string HTML input with CSRF token */ function csrf_field(): string { if (!isset($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } $token = $_SESSION['csrf_token']; return ''; } } if (!function_exists('csrf_token')) { /** * Get CSRF token value * * @return string The CSRF token */ function csrf_token(): string { if (!isset($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } return $_SESSION['csrf_token']; } } if (!function_exists('verify_csrf_token')) { /** * Verify CSRF token * * @param string $token The token to verify * @return bool True if valid, false otherwise */ function verify_csrf_token(string $token): bool { return isset($_SESSION['csrf_token']) && hash_equals($_SESSION['csrf_token'], $token); } } if (!function_exists('logger')) { /** * Log a message to the application log * * @param string $message The message to log * @param array $context Contextual information * @param string $level Log level (info, error, warning, debug) */ function logger(string $message, array $context = [], string $level = 'info'): void { switch ($level) { case 'error': Log::error($message, $context); break; case 'warning': Log::warning($message, $context); break; case 'debug': Log::debug($message, $context); break; default: Log::info($message, $context); break; } } } if (!function_exists('is_multi_lang_enabled')) { /** * Check if multi-language is enabled * * @return bool True if multi-language is enabled, false otherwise */ function is_multi_lang_enabled(): bool { return ($_ENV['APP_MULTI_LANG'] ?? 'false') === 'true'; } } if (!function_exists('bus_module_enabled')) { /** * Check if a module is enabled for the current authenticated user's company. * * @param string $moduleName The module name to check (e.g., 'erppoe') * @return bool */ function bus_module_enabled(string $moduleName): bool { if (session_status() === PHP_SESSION_NONE) { return false; } if (!isset($_SESSION['user_id'])) { return false; } try { $user = \Modules\Auth\Models\User::find($_SESSION['user_id']); if (!$user) { return false; } if ($user->users_role === 'super_admin') { return \App\Models\Module::where('modules_name', $moduleName) ->where('modules_ative', true) ->exists(); } return \App\Models\Module::join('bus_modules', 'modules.modules_id', '=', 'bus_modules.bus_modules_module_id') ->where('bus_modules.bus_modules_bus_id', $user->users_bus_id) ->where('bus_modules.bus_modules_is_enabled', true) ->where('modules.modules_name', $moduleName) ->where('modules.modules_ative', true) ->exists(); } catch (\Exception $e) { return false; } } } if (!function_exists('get_config')) { /** * Get a system configuration value from the database. * * @param string $key The configuration key. * @param mixed $default Default value if the key is not found. * @return mixed */ function get_config(string $key, $default = null) { try { $config = \Illuminate\Database\Capsule\Manager::table('system_config') ->where('system_config_key', $key) ->first(); return $config ? $config->system_config_value : $default; } catch (\Exception $e) { return $default; } } }