cfmgrv4/app/Controllers/Admin/Cloudflare/RecordController.php
2024-09-27 17:51:58 +09:00

136 lines
5.7 KiB
PHP

<?php
namespace App\Controllers\Admin\Cloudflare;
use App\Libraries\MySocket\Cloudflare\RecordSocket;
use App\Models\Cloudflare\RecordModel;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
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_path);
}
final protected function getModel(): RecordModel
{
if ($this->_model === null) {
$this->_model = new RecordModel();
}
return $this->_model;
}
final protected function getMySocket(): RecordSocket
{
if ($this->_mySocket === null) {
$this->_mySocket = new RecordSocket($this->_account_entity, $this->_zone_entity);
}
return $this->_mySocket;
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
case $this->getModel()::PARENT:
$this->getZoneModel()->where('status', DEFAULTS['STATUS']);
$options[$field] = [
DEFAULTS['EMPTY'] => lang($this->class_path . '.label.' . $field) . ' 선택',
$this->getZoneModel()->getFormFieldOption($field, $options),
];
break;
default:
$options = parent::getFormFieldOption($field, $options);
break;
}
return $options;
}
//전송된 데이터
protected function getFormData(string $field, array $formDatas): array
{
switch ($field) {
case 'hosts':
$formDatas[$field] = explode("\n", $this->request->getVar($field));
if (!is_array($this->formDatas[$field]) || !count($this->formDatas[$field])) {
throw new \Exception("호스트명이 정의되지 않았습니다.");
}
break;
default:
$formDatas = parent::getFormData($field, $formDatas);
break;
}
return $formDatas;
}
//생성
public function create_form(): RedirectResponse|string
{
$this->fields = [$this->getModel()::PARENT, 'type', 'content', 'proxied', 'hosts'];
$this->field_rules = $this->getModel()->getFieldRules(__FUNCTION__, $this->fields);
$this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
return $this->create_form_procedure();
}
protected function create_validate(): void
{
//hosts를 제외한 fields Valid처리
$this->validateFormDatas(array_diff($this->fields, ['hosts']));
}
protected function create_process(): void
{
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]);
$this->_account_entity = $this->getAccountModel()->getEntityByPK($this->_zone_entity->getParent());
if ($this->_account_entity === null) {
throw new \Exception("해당 계정정보를 찾을수 없습니다.");
}
//Record생성
foreach ($this->formDatas['hosts'] as $host) {
$result = $this->getMySocket()->create($host, [
'type' => $this->formDatas['type'],
'content' => $this->formDatas['content'],
'proxied' => $this->formDatas['proxied'],
]);
$formDatas = $this->getMySocket()->getArrayByResult($result);
$entity = $this->getRecordModel()->create($formDatas);
log_message("notice", "Record:" . __FUNCTION__ . "=> 작업을 완료하였습니다.");
}
}
public function create(): RedirectResponse
{
$this->fields = [$this->getModel()::PARENT, 'type', 'content', 'proxied', 'hosts'];
$this->field_rules = $this->getModel()->getFieldRules(__FUNCTION__, $this->fields);
// $this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied'];
// $this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->create_validate();
$this->formDatas = $this->getFormDatas();
//부모데이터 정의
$this->_account_entity = $this->getAccountModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]);
return $this->create_procedure();
}
//수정
// 리스트
public function index(): string
{
$this->fields = [$this->getModel()::PARENT, $this->getModel()::TITLE, 'type', 'content', 'ttl', 'proxied', 'locked', 'updated_at', 'created_at'];
$this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied'];
$this->field_rules = $this->getModel()->getFieldRules(__FUNCTION__, $this->fields);
$this->batchjob_fields = ['proxied'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
return $this->list_procedure();
}
//reload
public function reload(string $zone_uid): RedirectResponse
{
//부모데이터 정의
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($zone_uid);
$this->_account_entity = $this->getAccountModel()->getEntityByPK($this->_zone_entity->getParent());
return $this->reload_procedure();
}
}