cfmgrv4/app/Controllers/Admin/Cloudflare/RecordController.php
2024-10-03 19:29:11 +09:00

175 lines
7.6 KiB
PHP

<?php
namespace App\Controllers\Admin\Cloudflare;
use App\Libraries\MySocket\Cloudflare\RecordSocket;
use App\Models\Cloudflare\RecordModel;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class RecordController extends CloudflareController
{
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->root . $this->class_name;
$this->title = lang("{$this->class_path}.title");
$this->action_form = getenv("mvc.default." . strtolower($this->class_name) . ".form") ?: "modal_form";
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->_zone_entity);
}
return $this->_mySocket;
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
case $this->getModel()::PARENT:
// $this->getZoneModel()->where('status', 'active');
$options[$field] = $this->getZoneModel()->getFormFieldOption($field);
// echo $this->getAccountModel()->getLastQuery();
// dd($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;
}
private function init(string $action): void
{
$this->action = $action;
$this->fields = [$this->getModel()::PARENT, 'type', 'content', 'proxied', 'hosts'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
}
//생성
public function create_form(): RedirectResponse|string
{
$this->init('create');
return $this->create_form_procedure();
}
protected function create_validate(string $action, array $fields): void
{
//hosts를 제외한 fields Valid처리
parent::create_validate($action, array_diff($fields, ['hosts']));
}
protected function create_process(): void
{
//DB작업도 Socket에서 다 처리하므로 parent::create_process()하면 않됨
//부모데이터정의
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]);
foreach ($this->formDatas['hosts'] as $host) {
//Cloudflare 생성
$entity = $this->getMySocket()->create($host, $this->formDatas['type'], $this->formDatas['content'], $this->formDatas['proxied']);
log_message("debug", "Record:{$entity->getTitle()} 생성 작업을 완료하였습니다.");
}
}
public function create(): RedirectResponse|string
{
$this->init(__FUNCTION__);
$this->create_validate($this->action, $this->fields);
$this->formDatas = $this->getFormDatas();
$cnt = 1;
foreach ($this->formDatas['hosts'] as $host) {
//호스트명 형식확인
if (!isHost_CommonHelper($host)) {
throw new \Exception("{$this->_account_entity->getTitle()}{$cnt}번째 {$host} 호스트명 형식 오류");
}
$cnt++;
}
return $this->create_procedure($this->action_form);
}
//수정 (modify,toggle,batchjob사용)
protected function modify_process(string $uid): void
{
//DB작업도 Socket에서 다 처리하므로 parent::modify_process($uid)하면 않됨
$this->entity = $this->getModel()->getEntityByPK(intval($uid));
if ($this->entity === null) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
}
//부모데이터정의
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]);
//Cloudflare 수정
$entity = $this->getMySocket()->modify($this->entity, $this->formDatas);
log_message("debug", "Record:{$entity->getTitle()} 수정 작업을 완료하였습니다.");
}
//일괄처리작업
public function batcjob(): RedirectResponse
{
$this->action = __FUNCTION__;
$this->fields = ['proxied'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->modify_validate($this->action, $this->fields);
$this->formDatas = $this->getFormDatas();
return $this->batcjob_procedure();
}
//삭제
protected function delete_process(string $uid): void
{
//DB작업도 Socket에서 다 처리하므로 parent::delete_process($uid)하면 않됨
$this->entity = $this->getModel()->getEntityByPK(intval($uid));
if ($this->entity === null) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
}
//부모데이터정의
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]);
//Cloudflare 삭제
$this->getMySocket()->delete($this->entity);
}
// 리스트
public function index(): string
{
$this->action = __FUNCTION__;
$this->fields = [$this->getModel()::PARENT, $this->getModel()::TITLE, 'type', 'content', 'ttl', 'proxied', 'locked', 'updated_at', 'created_at'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied', 'fixed'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->batchjob_fields = ['proxied'];
return $this->list_procedure();
}
// Download
public function download(string $output_type, $uid = false): DownloadResponse|string
{
$this->action = __FUNCTION__;
$this->fields = [$this->getModel()::PARENT, $this->getModel()::TITLE, 'type', 'content', 'ttl', 'proxied', 'locked', 'updated_at', 'created_at'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->batchjob_fields = ['proxied'];
return $this->download_procedure($output_type, $uid);
}
}