cfmgrv4/app/Controllers/Admin/Cloudflare/RecordController.php
2024-09-25 19:45:42 +09:00

105 lines
3.8 KiB
PHP

<?php
namespace App\Controllers\Admin\Cloudflare;
use Psr\Log\LoggerInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\RedirectResponse;
use App\Libraries\MyCloudflare\Record;
class RecordController extends CloudflareController
{
private $_myLibrary = null;
private $_account_entity = null;
private $_zone_entity = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->class_name = "Record";
$this->class_path .= $this->class_name;
$this->title = lang("{$this->class_path}.title");
helper($this->class_name);
}
final protected function getMyLibrary(): Record
{
if ($this->_myLibrary === null) {
$this->_myLibrary = new Record($this->_account_entity, $this->_zone_entity);
}
return $this->_myLibrary;
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
case $this->getMyLibrary()->getMyStorage()::PARENT:
$this->getZoneModel()->where('status', DEFAULTS['STATUS']);
$options[$field] = [
DEFAULTS['EMPTY'] => lang($this->_className . '.label.' . $field) . ' 선택',
...$this->getZoneModel()->getFormFieldOption($field, $options),
];
break;
default:
$options = parent::getFormFieldOption($field, $options);
break;
}
return $options;
}
//전송된 데이터
protected function getFormData(string $field): void
{
switch ($field) {
case 'hosts':
$this->formDatas[$field] = explode("\n", $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 'hosts':
break;
default:
parent::validateFormData($field);
break;
}
}
protected function create_init(): void
{
$this->action = 'create';
$this->fields = [$this->getMyLibrary()->getMyStorage()::PARENT, 'type', 'content', 'proxied', 'hosts'];
$this->filter_fields = [$this->getMyLibrary()->getMyStorage()::PARENT, 'type', 'proxied'];
$this->field_rules = $this->getFormFieldRules();
$this->field_options = $this->getFormFieldOptions();
$this->getMyLibrary()->getMyStorage()->setAction($this->action);
}
// public function create_form(): RedirectResponse|string
// {
// return $this->create_form_process();
// }
protected function create_process_submit(): void
{
//Record생성
foreach ($this->formDatas['hosts'] as $host) {
$this->getMyLibrary()->create($host, $this->formDatas);
}
}
public function create(string $zone_uid): RedirectResponse
{
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($zone_uid);
$this->_account_entity = $this->getAccountModel()->getEntityByPK($this->_zone_entity->getParent());
$this->formDatas = [];
$this->getFormDatas();
$this->convertFormDatas();
$this->validateFormDatas();
return parent::create_process();
}
}