Fix consistentie benamingen.

This commit is contained in:
2026-09-11 22:14:50 +02:00
parent deba8463b8
commit 38701bf06a
12 changed files with 1746 additions and 162 deletions
+65 -16
View File
@@ -106,26 +106,75 @@ function get_workday_settings(): array {
}
/**
* Haalt AI configuratie-instellingen op (Release v1.5)
* Haalt AI configuratie-instellingen op (Release v2.0 - Google Gemini Pro & OpenAI)
* Ondersteunt organisatiebrede en optioneel persoonlijke sleutels.
*/
function get_ai_settings(): array {
$envKey = fociz_env('OPENAI_API_KEY', '');
$savedKeyRaw = get_system_setting('ai_api_key', '');
$savedKey = '';
if (!empty($savedKeyRaw)) {
if (!function_exists('fociz_decrypt')) {
require_once __DIR__ . '/auth.php';
}
$savedKey = fociz_decrypt($savedKeyRaw);
function get_ai_settings(?int $userId = null): array {
if (!function_exists('fociz_decrypt')) {
require_once __DIR__ . '/auth.php';
}
$apiKey = !empty($savedKey) ? $savedKey : $envKey;
$apiKey = '';
$isPersonal = false;
// 1. Controleer persoonlijke sleutel van de ingelogde gebruiker
if ($userId !== null && $userId > 0) {
try {
$db = get_db_connection();
$stmt = $db->prepare("SELECT access_token, settings, is_enabled FROM integrations WHERE user_id = ? AND provider IN ('GEMINI', 'AI') AND is_enabled = 1 ORDER BY id DESC LIMIT 1");
$stmt->execute([$userId]);
$row = $stmt->fetch();
if ($row) {
// Sleutel kan in access_token staan of in settings['api_key']
if (!empty($row['access_token'])) {
$apiKey = fociz_decrypt($row['access_token']);
$isPersonal = true;
} elseif (!empty($row['settings'])) {
$userSettings = json_decode($row['settings'], true) ?: [];
if (!empty($userSettings['api_key'])) {
$apiKey = fociz_decrypt($userSettings['api_key']);
$isPersonal = true;
}
}
if (!empty($row['settings'])) {
$userSettings = json_decode($row['settings'], true) ?: [];
if (!empty($userSettings['provider'])) {
$personalProvider = $userSettings['provider'];
}
if (!empty($userSettings['model'])) {
$personalModel = $userSettings['model'];
}
}
}
} catch (Throwable $e) {
error_log("Fout bij ophalen persoonlijke AI sleutel: " . $e->getMessage());
}
}
// 2. Fallback naar organisatiebrede instellingen
if (empty($apiKey)) {
$savedKeyRaw = get_system_setting('ai_api_key', '');
if (!empty($savedKeyRaw)) {
$apiKey = fociz_decrypt($savedKeyRaw);
}
}
// 3. Fallback naar .env variabelen (GEMINI_API_KEY of OPENAI_API_KEY)
if (empty($apiKey)) {
$apiKey = fociz_env('GEMINI_API_KEY', '') ?: fociz_env('OPENAI_API_KEY', '');
}
$provider = !empty($personalProvider) ? $personalProvider : get_system_setting('ai_provider', 'gemini');
$model = !empty($personalModel) ? $personalModel : get_system_setting('ai_model', 'gemini-1.5-pro');
return [
'ai_enabled' => (bool)get_system_setting('ai_enabled', !empty($apiKey)),
'ai_provider' => get_system_setting('ai_provider', 'openai'),
'ai_api_key' => $apiKey,
'ai_model' => get_system_setting('ai_model', 'gpt-4o-mini'),
'has_key' => !empty($apiKey),
'ai_enabled' => (bool)get_system_setting('ai_enabled', !empty($apiKey)),
'ai_provider' => $provider,
'ai_api_key' => $apiKey,
'ai_model' => $model,
'has_key' => !empty($apiKey),
'is_personal_key'=> $isPersonal,
];
}