Automation/app/Models/Cloudflare/RecordModel.php
2024-09-22 01:04:33 +09:00

114 lines
3.7 KiB
PHP

<?php
namespace App\Models\Cloudflare\API;
use CodeIgniter\Model;
use App\Entities\Cloudflare\ZoneEntity;
use App\Entities\Cloudflare\RecordEntity;
class RecordModel extends Model
{
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, 'type', self::TITLE, '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 getFieldRule(string $field, array $rules): array
{
switch ($field) {
case self::PARENT:
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";;
break;
case self::TITLE:
case "content":
case "type":
$rules[$field] = "required|trim|string";
break;
case "ttl":
$rules[$field] = "if_exist|numeric";
break;
case "proxied":
case "proxiable":
case "fixed":
case "locked":
$rules[$field] = "if_exist|in_list[on,off]";
break;
default:
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;
}
public function getEntityByPK(int $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();
}
//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);
}
public function getEntitysByParent(ZoneEntity $zone_entity)
{
$this->where(self::PARENT, $zone_entity->getPK());
return $this->getEntitys();
}
//도메인이 이미 존재하는지 체크
public function isUniqueHost($zone_uid, string $host, string $content): bool
{
$this->where(self::PARENT, $zone_uid);
$this->where('host', $host);
$this->where('content', $content);
return is_null($this->first()) ? true : false;
}
//CDN값 수정 못하는 고정 Record 처리
public function setFixedCDNRecord(array $hosts)
{
if (count($hosts)) {
$this->whereIn('host', $hosts)->set(['fixed' => 'on'])->update();
log_message("notice", "-----set fixed Records " . implode(",", $hosts) . "처리 완료-----");
}
}
//Index 검색어용
public function setIndexWordFilter(string $word)
{
$this->like('host', $word, 'before'); //befor , after , both
$this->orWhere('content', $word);
}
public function setIndexDateFilter($start, $end)
{
$this->where('created_at >=', $start);
$this->where('created_at <=', $end);
}
public function setIndexOrderBy($field, $order = 'ASC')
{
$this->orderBy(self::PARENT . " ASC, host ASC, {$field} {$order}");
}
}