Initial commit - CMS Gov Bapenda Garut dengan EditorJS
This commit is contained in:
83
app/Controllers/Admin/Settings.php
Normal file
83
app/Controllers/Admin/Settings.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Admin;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Models\SettingsModel;
|
||||
|
||||
class Settings extends BaseController
|
||||
{
|
||||
protected $settingsModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->settingsModel = new SettingsModel();
|
||||
|
||||
// Check if user is admin
|
||||
if (session()->get('role') !== 'admin') {
|
||||
throw new \CodeIgniter\Exceptions\PageNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display settings form
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// Get all settings
|
||||
$settings = $this->settingsModel->findAll();
|
||||
|
||||
// Convert to key-value array for easier access
|
||||
$settingsArray = [];
|
||||
foreach ($settings as $setting) {
|
||||
$settingsArray[$setting['key']] = $setting;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'title' => 'Pengaturan',
|
||||
'settings' => $settingsArray,
|
||||
];
|
||||
|
||||
return view('admin/settings/index', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update settings
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
$validation = \Config\Services::validation();
|
||||
|
||||
$rules = [
|
||||
'site_name' => 'required|min_length[3]|max_length[100]',
|
||||
'site_description' => 'permit_empty|max_length[255]',
|
||||
];
|
||||
|
||||
if (!$this->validate($rules)) {
|
||||
return redirect()->back()
|
||||
->withInput()
|
||||
->with('errors', $validation->getErrors());
|
||||
}
|
||||
|
||||
$siteName = $this->request->getPost('site_name');
|
||||
$siteDescription = $this->request->getPost('site_description') ?: '';
|
||||
|
||||
// Update or create site_name
|
||||
$this->settingsModel->setSetting(
|
||||
'site_name',
|
||||
$siteName,
|
||||
'Nama situs yang ditampilkan di sidebar dan judul halaman'
|
||||
);
|
||||
|
||||
// Update or create site_description
|
||||
$this->settingsModel->setSetting(
|
||||
'site_description',
|
||||
$siteDescription,
|
||||
'Deskripsi singkat tentang situs'
|
||||
);
|
||||
|
||||
return redirect()->to('/admin/settings')
|
||||
->with('success', 'Pengaturan berhasil diperbarui.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user