Automation/app/Controllers/Cloudflare/RecordController.php
2024-09-23 21:11:50 +09:00

94 lines
3.0 KiB
PHP

<?php
namespace App\Controllers\Cloudflare;
use App\Controllers\MVController;
use Psr\Log\LoggerInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\RedirectResponse;
use App\Traits\AuthTrait;
use App\Libraries\MySocket\CloudflareSocket;
use App\Libraries\MyCloudflare\Record;
use App\Models\Cloudflare\ZoneModel;
use App\Models\Cloudflare\AccountModel;
use App\Models\Cloudflare\API\RecordModel;
use App\Entities\Cloudflare\RecordEntity;
class RecordController extends MVController
{
use AuthTrait;
private $_model = null;
private $_accountModel = null;
private $_zoneModel = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->class_name .= "Record";
$this->layout = LAYOUTS['admin'];
$this->title = lang("{$this->class_name}.title");
$this->session = $this->session_AuthTrait();
helper($this->class_name);
}
final protected function getModel(): RecordModel
{
if ($this->_model === null) {
$this->_model = new RecordModel();
}
return $this->_model;
}
final protected function getAccountModel(): AccountModel
{
if ($this->_accountModel === null) {
$this->_accountModel = new AccountModel();
}
return $this->_accountModel;
}
final protected function getZoneModel(): ZoneModel
{
if ($this->_zoneModel === null) {
$this->_zoneModel = new ZoneModel();
}
return $this->_zoneModel;
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
case RecordModel::PARENT:
$options = [
DEFAULTS['EMPTY'] => lang($this->_className . '.label.' . $field) . ' 선택',
...$this->getZoneModel()->getFilterFieldOption($field, $options)
];
break;
default:
$options = parent::getFormFieldOption($field, $options);
break;
}
return $options;
}
protected function create_init(): void
{
$this->fields = ['id', 'apikey'];
$this->filter_fields = ['status'];
$this->action = 'create';
$this->getModel()->setAction($this->action);
}
public function create_form(): RedirectResponse|string
{
return $this->create_form_process();
}
protected function create_process_submit(): RecordEntity
{
$zone_entity = $this->getZoneModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]);
$account_entity = $this->getAccountModel()->getEntityByPK($zone_entity->getParent());
$Record = new Record($account_entity, $zone_entity);
return $Record->create($this->formDatas);
}
public function create(): RedirectResponse
{
return parent::create_process();
}
}