140 lines
4.6 KiB
PHP
140 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Cloudflare;
|
|
|
|
use App\Entities\Cloudflare\ZoneEntity;
|
|
use App\Entities\Cloudflare\RecordEntity;
|
|
use App\Models\CommonModel;
|
|
|
|
class RecordModel extends CommonModel
|
|
{
|
|
const TABLE = "cloudflarerecord";
|
|
const PK = "uid";
|
|
const TITLE = "host";
|
|
const PARENT = "zone_uid";
|
|
protected $table = self::TABLE;
|
|
protected $primaryKey = self::PK;
|
|
protected $useAutoIncrement = false;
|
|
protected $returnType = RecordEntity::class; //object,array,entity명::class
|
|
protected $allowedFields = [self::PK, self::PARENT, self::TITLE, 'type', 'content', 'ttl', 'proxiable', 'proxied', 'fixed', 'locked', 'updated_at', 'crated_at'];
|
|
protected $useTimestamps = true;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
public function getTitleField(): string
|
|
{
|
|
return self::TITLE;
|
|
}
|
|
public function getFields(): array
|
|
{
|
|
return [self::PARENT, 'host', 'type', 'content', 'proxied', 'updated_at', 'created_at'];
|
|
}
|
|
public function getFilterFields(): array
|
|
{
|
|
return [self::PARENT, 'type', 'proxied', 'fixed'];
|
|
}
|
|
public function getBatchJobFields(): array
|
|
{
|
|
return ['type', 'proxied', 'fixed', 'host'];
|
|
}
|
|
public function getFieldRule(string $action, string $field): string
|
|
{
|
|
switch ($field) {
|
|
case self::PK:
|
|
$rule = "required|trim|alpha_numeric";
|
|
$rule .= $action == "create" ? "|is_unique[{$this->table}.{$field}]" : "";
|
|
break;
|
|
case self::PARENT:
|
|
$rule = "required|trim|alpha_numeric";
|
|
break;
|
|
case self::TITLE:
|
|
case "content":
|
|
$rule = "required|trim|string";
|
|
break;
|
|
case "type":
|
|
$rule = "required|in_list[A,CNAME,NS,MX,PTR,SPF,TXT,SRV,INFO]";
|
|
break;
|
|
case "ttl":
|
|
$rule = "if_exist|numeric";
|
|
break;
|
|
case "proxied":
|
|
$rule = "required|in_list[on,off]";
|
|
break;
|
|
case "proxiable":
|
|
case "fixed":
|
|
case "locked":
|
|
$rule = "if_exist|in_list[on,off]";
|
|
break;
|
|
case "hosts": //create form에서 사용
|
|
$rule = "required|string";
|
|
break;
|
|
default:
|
|
$rule = parent::getFieldRule($action, $field);
|
|
break;
|
|
}
|
|
return $rule;
|
|
}
|
|
public function getFormFieldInputOption(string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
$this->orderBy(self::TITLE, 'asc');
|
|
$options = parent::getFormFieldInputOption($field, $options);
|
|
break;
|
|
}
|
|
return $options;
|
|
}
|
|
public function getEntityByPK(string $uid): null|RecordEntity
|
|
{
|
|
$this->where(self::PK, $uid);
|
|
return $this->getEntity();
|
|
}
|
|
public function getEntityByID(string $id): null|RecordEntity
|
|
{
|
|
$this->where(self::TITLE, $id);
|
|
return $this->getEntity();
|
|
}
|
|
public function getEntitysByParent(ZoneEntity $zone_entity)
|
|
{
|
|
$this->where(self::PARENT, $zone_entity->getPK());
|
|
return $this->getEntitys();
|
|
}
|
|
//create용
|
|
public function create(array $formDatas = []): RecordEntity
|
|
{
|
|
return $this->create_process(new RecordEntity(), $formDatas);
|
|
}
|
|
//modify용
|
|
public function modify(RecordEntity $entity, array $formDatas): RecordEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
//host가 이미 존재하는지 체크
|
|
public function isUniqueHost($zone_uid, string $host, string $content): bool
|
|
{
|
|
$this->where(self::PARENT, $zone_uid);
|
|
$this->where(self::TABLE . '.host', $host);
|
|
return is_null($this->first()) ? true : false;
|
|
}
|
|
//CDN값 수정 못하는 고정 Record 처리
|
|
public function setImmobilized(array $hosts)
|
|
{
|
|
if (count($hosts)) {
|
|
$this->whereIn(self::TABLE . '.host', $hosts)->set(['fixed' => 'on'])->update();
|
|
log_message("notice", "-----set fixed Records " . implode(",", $hosts) . "처리 완료-----");
|
|
}
|
|
}
|
|
//List 검색용
|
|
public function setList_WordFilter(string $word): void
|
|
{
|
|
if ($this->isIPAddress($word, 'ipv4')) {
|
|
$this->where(self::TABLE . '.content', $word);
|
|
} else {
|
|
$this->orLike(self::TABLE . "." . self::TITLE, $word, 'both');
|
|
$this->orLike(self::TABLE . '.content', $word, 'both');
|
|
}
|
|
}
|
|
}
|