Automation/app/Controllers/Cloudflare/ZoneController.php
2024-09-24 17:09:29 +09:00

145 lines
5.1 KiB
PHP

<?php
namespace App\Controllers\Cloudflare;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Libraries\MyCloudflare\Zone;
use App\Libraries\MyCloudflare\Record;
class ZoneController extends CloudflareController
{
private $_myLibrary = null;
private $_account_entity = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->class_name .= "Zone";
$this->layout = LAYOUTS['admin'];
$this->title = lang("{$this->class_name}.title");
helper($this->class_name);
}
final protected function getMyLibrary(): Zone
{
if ($this->_myLibrary === null) {
$this->_myLibrary = new Zone($this->_account_entity);
}
return $this->_myLibrary;
}
protected function getFormFieldInputOption(string $field, array $options = []): array
{
switch ($field) {
case $this->getMyLibrary()->getMyStorage()::PARENT:
$options = [
DEFAULTS['EMPTY'] => lang($this->_className . '.label.' . $field) . ' 선택'
];
$this->getAccountModel()->where('status', DEFAULTS['STATUS']);
$options = $this->getAccountModel()->getFormFieldInputOption($field, $options);
break;
case 'type':
case 'proxied':
$options = [
DEFAULTS['EMPTY'] => lang("Cloudflare/Record.label." . $field) . ' 선택',
...lang("Cloudflare/Record.label." . strtoupper($field)),
];
break;
default:
$options = parent::getFormFieldInputOption($field, $options);
break;
}
return $options;
}
protected function getFormFieldRule(string $field, array $rules): array
{
if (is_array($field)) {
throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
}
switch ($field) {
default:
$rules[$field] = $this->getMyLibrary()->getMyStorage()->getFieldRule($field, $rules);
;
break;
}
return $rules;
}
//전송된 데이터
protected function getFormData(string $field): void
{
switch ($field) {
case 'domains':
$this->formDatas[$field] = explode("\n", $this->request->getVar($field));
if (!is_array($this->formDatas[$field]) || !count($this->formDatas[$field])) {
throw new \Exception("도메인명이 정의되지 않았습니다.");
}
break;
case 'hosts':
$this->formDatas[$field] = $this->request->getVar($field);
if (!is_array($this->formDatas[$field]) || !count($this->formDatas[$field])) {
throw new \Exception("호스트명이 정의되지 않았습니다");
}
break;
default:
$this->formDatas[$field] = $this->request->getVar($field);
break;
}
}
//전송된 데이터 검증
protected function validateFormData(string $field): void
{
switch ($field) {
case 'domains':
case 'hosts':
break;
default:
parent::validateFormData($field);
break;
}
}
protected function create_init(): void
{
$this->fields = [
$this->getMyLibrary()->getMyStorage()::PARENT,
'domains',
'hosts',
'type',
'content',
'proxied',
];
$this->filter_fields = [$this->getMyLibrary()->getMyStorage()::PARENT, 'type', 'proxied'];
$this->action = 'create';
$this->getMyLibrary()->getMyStorage()->setAction($this->action);
}
// public function create_form(): RedirectResponse|string
// {
// return $this->create_form_process();
// }
protected function create_process_submit(): void
{
foreach ($this->formDatas['domains'] as $domain) {
//Zone생성
$zone_entity = $this->getMyLibrary()->create($domain);
//Record생성
$record = new Record($this->_account_entity, $zone_entity);
foreach ($this->formDatas['hosts'] as $host) {
$record->create($host, [
'type' => $this->formDatas['type'],
'content' => $this->formDatas['content'],
'proxied' => $this->formDatas['proxied'],
]);
}
}
}
public function create(string $account_uid): RedirectResponse
{
$this->_account_entity = $this->getAccountModel()->getEntityByPK($account_uid);
$this->formDatas = [];
$this->getFormDatas();
$this->convertFormDatas();
$this->validateFormDatas();
return parent::create_process();
}
}