83 lines
2.3 KiB
PHP
Executable File
83 lines
2.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\ChatGPT\Http\Controllers;
|
|
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\ChatGPT\Http\Requests\StoreChatGptApiSettingRequest;
|
|
use Modules\ChatGPT\Http\Requests\UpdateChatGptApiSettingRequest;
|
|
use Modules\ChatGPT\Models\ChatGptApiSetting;
|
|
|
|
class ChatGPTController extends Controller
|
|
{
|
|
/**
|
|
* Display the ChatGPT module settings page.
|
|
*/
|
|
public function index(): Renderable
|
|
{
|
|
return view('chatgpt::index', [
|
|
'settings' => ChatGptApiSetting::query()->latest('id')->get(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a new encrypted OpenAI API key setting.
|
|
*/
|
|
public function store(StoreChatGptApiSettingRequest $request): RedirectResponse
|
|
{
|
|
$data = $request->validated();
|
|
|
|
ChatGptApiSetting::query()->update(['is_active' => false]);
|
|
|
|
ChatGptApiSetting::query()->create([
|
|
'name' => $data['name'] ?: 'OpenAI API Key',
|
|
'api_key' => $data['api_key'],
|
|
'is_active' => (bool) ($data['is_active'] ?? true),
|
|
]);
|
|
|
|
return redirect()
|
|
->route('chatgpt.index')
|
|
->with('status', 'OpenAI API key saved.');
|
|
}
|
|
|
|
/**
|
|
* Update an encrypted OpenAI API key setting.
|
|
*/
|
|
public function update(UpdateChatGptApiSettingRequest $request, ChatGptApiSetting $setting): RedirectResponse
|
|
{
|
|
$data = $request->validated();
|
|
|
|
if ((bool) ($data['is_active'] ?? false)) {
|
|
ChatGptApiSetting::query()
|
|
->whereKeyNot($setting->getKey())
|
|
->update(['is_active' => false]);
|
|
}
|
|
|
|
$setting->name = $data['name'] ?: $setting->name;
|
|
$setting->is_active = (bool) ($data['is_active'] ?? false);
|
|
|
|
if (! empty($data['api_key'])) {
|
|
$setting->api_key = $data['api_key'];
|
|
}
|
|
|
|
$setting->save();
|
|
|
|
return redirect()
|
|
->route('chatgpt.index')
|
|
->with('status', 'OpenAI API key updated.');
|
|
}
|
|
|
|
/**
|
|
* Delete an encrypted OpenAI API key setting.
|
|
*/
|
|
public function destroy(ChatGptApiSetting $setting): RedirectResponse
|
|
{
|
|
$setting->delete();
|
|
|
|
return redirect()
|
|
->route('chatgpt.index')
|
|
->with('status', 'OpenAI API key deleted.');
|
|
}
|
|
}
|