cfmgrv4/app/Controllers/Admin/Cloudflare/FirewallController.php
2025-03-13 11:25:45 +09:00

111 lines
4.5 KiB
PHP

<?php
namespace App\Controllers\Admin\Cloudflare;
use App\Entities\Cloudflare\ZoneEntity;
use App\Helpers\Cloudflare\FirewallHelper;
use App\Models\Cloudflare\ZoneModel;
use App\Services\Cloudflare\FirewallService;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class FirewallController extends CloudflareController
{
private $_zone_entity = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->title = lang("{$this->getService()->getClassPath()}.title");
$this->helper = new FirewallHelper();
}
protected function getService(): FirewallService
{
if ($this->service === null) {
$this->service = new FirewallService();
$this->class_name = $this->service->getClassName();
$this->class_path = $this->service->getClassPath();
}
return $this->service;
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
case $this->getService()->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;
}
//부모데이터 정의
private function getParentEntity($uid): ZoneEntity
{
//부모데이터정의
$zone_entity = $this->getZoneModel()->getEntityByPK($uid);
if ($zone_entity === null) {
throw new \Exception("Zone: [{$this->entity->getParent()}] 정보를 찾을수 없습니다.");
}
return $zone_entity;
}
//수정 (modify,toggle,batchjob사용)
protected function modify_process(mixed $uid): void
{
//DB작업도 Socket에서 다 처리하므로 parent::modify_process($uid)하면 않됨
$this->formDatas = $this->modify_validate($this->action, $this->fields);
//자신정보정의
$this->entity = $this->getService()->getModel()->getEntityByPK($uid);
if ($this->entity === null) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
}
//부모데이터정의
$this->_zone_entity = $this->getParentEntity($this->entity->getParent());
//Socket처리
$this->entity = $this->getService()->modify($this->_zone_entity, $this->entity, $this->formDatas);
}
// 리스트
protected function index_entitys_process(): array
{
$this->index_condition_process();
//기본Soring처리
$this->getService()->getModel()->orderBy(ZoneModel::TABLE . "." . ZoneModel::TITLE . " ASC ," . $this->getService()->getModel()::TITLE . " ASC");
//Sorting 처리
$this->order_field = $this->request->getVar('order_field') ?: DEFAULTS['EMPTY'];
$this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY'];
if ($this->order_field !== DEFAULTS['EMPTY'] && $this->order_value !== DEFAULTS['EMPTY']) {
$this->getService()->getModel()->orderBy(sprintf(
"%s.%s %s",
$this->getService()->getModel()::TABLE,
$this->order_field,
$this->order_value
));
}
$this->getService()->getModel()->limit($this->per_page, $this->page * $this->per_page - $this->per_page);
//Join을 해서 도메인부터 Sorting하기위함
$this->getService()->getModel()->join(ZoneModel::TABLE, sprintf(
"%s.%s=%s.%s",
$this->getService()->getModel()::TABLE,
$this->getService()->getModel()::PARENT,
ZoneModel::TABLE,
ZoneModel::PK
));
$entitys = $this->getService()->getModel()->select($this->getService()->getModel()::TABLE . '.*')->findAll();
log_message("debug", $this->getService()->getModel()->getLastQuery());
return $entitys;
}
//reload Firewall By Zone
protected function reload_process(mixed $uid): void
{
//부모데이터정의
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($uid);
$this->getService()->reload($this->_zone_entity);
}
}