104 lines
3.9 KiB
PHP
104 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace Komponent\Component\Testquestions\Administrator\Controller;
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
use Joomla\CMS\Factory;
|
|
use Joomla\CMS\Language\Text;
|
|
use Joomla\CMS\MVC\Controller\BaseController;
|
|
use Joomla\CMS\Router\Route;
|
|
use Joomla\CMS\Session\Session;
|
|
use Komponent\Component\Testquestions\Administrator\Service\AiService;
|
|
|
|
class AiController extends BaseController
|
|
{
|
|
public function save(): void
|
|
{
|
|
$this->saveSettings();
|
|
|
|
$this->setRedirect(
|
|
Route::_('index.php?option=com_testquestions&view=ai', false),
|
|
Text::_('COM_TESTQUESTIONS_AI_SETTINGS_SAVED')
|
|
);
|
|
}
|
|
|
|
private function saveSettings(): array
|
|
{
|
|
if (!Session::checkToken()) {
|
|
throw new \RuntimeException(Text::_('JINVALID_TOKEN'), 403);
|
|
}
|
|
|
|
$user = Factory::getApplication()->getIdentity();
|
|
if (!$user->authorise('core.admin', 'com_testquestions') && !$user->authorise('core.options', 'com_testquestions')) {
|
|
throw new \RuntimeException(Text::_('JERROR_ALERTNOAUTHOR'), 403);
|
|
}
|
|
|
|
$input = Factory::getApplication()->getInput();
|
|
$provider = strtolower($input->post->getCmd('ai_provider', ''));
|
|
if (!in_array($provider, ['openai', 'grok', 'groq', 'gemini'], true)) {
|
|
throw new \RuntimeException(Text::_('COM_TESTQUESTIONS_AI_PROVIDER_INVALID'));
|
|
}
|
|
|
|
$models = AiService::availableModels();
|
|
$selectedModels = [];
|
|
foreach (['openai', 'grok', 'groq', 'gemini'] as $modelProvider) {
|
|
$model = trim($input->post->getString('ai_' . $modelProvider . '_model', ''));
|
|
if (!isset($models[$modelProvider][$model])) {
|
|
throw new \RuntimeException(Text::_('COM_TESTQUESTIONS_AI_MODEL_INVALID'));
|
|
}
|
|
$selectedModels[$modelProvider] = $model;
|
|
}
|
|
|
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
|
$row = $db->setQuery(
|
|
$db->getQuery(true)
|
|
->select([$db->quoteName('extension_id'), $db->quoteName('params')])
|
|
->from($db->quoteName('#__extensions'))
|
|
->where($db->quoteName('type') . ' = ' . $db->quote('component'))
|
|
->where($db->quoteName('element') . ' = ' . $db->quote('com_testquestions'))
|
|
)->loadObject();
|
|
|
|
if (!$row) {
|
|
throw new \RuntimeException(Text::_('COM_TESTQUESTIONS_AI_SAVE_FAILED'));
|
|
}
|
|
|
|
$settings = json_decode((string) $row->params, true);
|
|
$settings = is_array($settings) ? $settings : [];
|
|
$legacyKey = trim((string) ($settings['ai_api_key'] ?? ''));
|
|
$settings['ai_provider'] = $provider;
|
|
foreach ($selectedModels as $modelProvider => $model) {
|
|
$settings['ai_' . $modelProvider . '_model'] = $model;
|
|
}
|
|
|
|
foreach (['openai', 'grok', 'groq', 'gemini'] as $keyProvider) {
|
|
$fieldName = 'ai_' . $keyProvider . '_api_key';
|
|
$apiKey = trim((string) $input->post->get($fieldName, '', 'RAW'));
|
|
if ($apiKey !== '') {
|
|
$settings[$fieldName] = $apiKey;
|
|
}
|
|
if ($keyProvider === $provider && empty($settings[$fieldName]) && $legacyKey !== '') {
|
|
$settings[$fieldName] = $legacyKey;
|
|
}
|
|
}
|
|
|
|
unset($settings['ai_api_key']);
|
|
unset($settings['ai_grok_team_id'], $settings['ai_grok_management_key']);
|
|
|
|
$extension = (object) [
|
|
'extension_id' => (int) $row->extension_id,
|
|
'params' => json_encode($settings, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
|
];
|
|
if (!$db->updateObject('#__extensions', $extension, 'extension_id')) {
|
|
throw new \RuntimeException(Text::_('COM_TESTQUESTIONS_AI_SAVE_FAILED'));
|
|
}
|
|
|
|
return $settings;
|
|
}
|
|
|
|
public function cancel(): void
|
|
{
|
|
$this->setRedirect(Route::_('index.php?option=com_testquestions&view=questions', false));
|
|
}
|
|
}
|