100 lines
2.9 KiB
PHP
100 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\LocalGovernmentModel;
|
|
use Config\Roles;
|
|
|
|
class LocalGovernment extends BaseController
|
|
{
|
|
private LocalGovernmentModel $lgModel;
|
|
private Roles $roles;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->lgModel = model(LocalGovernmentModel::class);
|
|
$this->roles = config('Roles');
|
|
}
|
|
|
|
private function isSuperAdmin(): bool
|
|
{
|
|
return (int) session()->get('mb_level') === Roles::LEVEL_SUPER_ADMIN;
|
|
}
|
|
|
|
/**
|
|
* 지자체 목록
|
|
*/
|
|
public function index()
|
|
{
|
|
if (! $this->isSuperAdmin()) {
|
|
return redirect()->to(site_url('admin'))
|
|
->with('error', '지자체 관리는 super admin만 접근할 수 있습니다.');
|
|
}
|
|
|
|
$list = $this->lgModel->orderBy('lg_idx', 'DESC')->findAll();
|
|
|
|
return view('admin/layout', [
|
|
'title' => '지자체 관리',
|
|
'content' => view('admin/local_government/index', ['list' => $list]),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 지자체 등록 폼
|
|
*/
|
|
public function create()
|
|
{
|
|
if (! $this->isSuperAdmin()) {
|
|
return redirect()->to(site_url('admin/local-governments'))
|
|
->with('error', '지자체 등록은 super admin만 가능합니다.');
|
|
}
|
|
|
|
return view('admin/layout', [
|
|
'title' => '지자체 등록',
|
|
'content' => view('admin/local_government/create'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 지자체 등록 처리
|
|
*/
|
|
public function store()
|
|
{
|
|
if (! $this->isSuperAdmin()) {
|
|
return redirect()->to(site_url('admin/local-governments'))
|
|
->with('error', '지자체 등록은 super admin만 가능합니다.');
|
|
}
|
|
|
|
$rules = [
|
|
'lg_name' => 'required|max_length[100]',
|
|
'lg_code' => 'required|max_length[20]|is_unique[local_government.lg_code]',
|
|
'lg_sido' => 'required|max_length[50]',
|
|
'lg_gugun' => 'required|max_length[50]',
|
|
'lg_addr' => 'permit_empty|max_length[255]',
|
|
];
|
|
|
|
if (! $this->validate($rules)) {
|
|
return redirect()->back()
|
|
->withInput()
|
|
->with('errors', $this->validator->getErrors());
|
|
}
|
|
|
|
$data = [
|
|
'lg_name' => (string) $this->request->getPost('lg_name'),
|
|
'lg_code' => (string) $this->request->getPost('lg_code'),
|
|
'lg_sido' => (string) $this->request->getPost('lg_sido'),
|
|
'lg_gugun' => (string) $this->request->getPost('lg_gugun'),
|
|
'lg_addr' => (string) $this->request->getPost('lg_addr'),
|
|
'lg_state' => 1,
|
|
'lg_regdate' => date('Y-m-d H:i:s'),
|
|
];
|
|
|
|
$this->lgModel->insert($data);
|
|
|
|
return redirect()->to(site_url('admin/local-governments'))
|
|
->with('success', '지자체가 등록되었습니다.');
|
|
}
|
|
}
|
|
|