147 lines
5.1 KiB
PHP
147 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Cloudflare;
|
|
|
|
use App\Entities\Cloudflare\AccountEntity;
|
|
use App\Entities\Cloudflare\ZoneEntity;
|
|
use App\Models\CommonModel;
|
|
|
|
class ZoneModel extends CommonModel
|
|
{
|
|
const TABLE = "cloudflarezone";
|
|
const PK = "uid";
|
|
const TITLE = "domain";
|
|
const PARENT = "account_uid";
|
|
protected $table = self::TABLE;
|
|
protected $primaryKey = self::PK;
|
|
protected $useAutoIncrement = false;
|
|
protected $returnType = ZoneEntity::class; //object,array,entity명::class
|
|
protected $allowedFields = [self::PK, self::PARENT, self::TITLE, 'name_servers', 'original_name_servers', 'plan', 'development_mode', 'ipv6', 'security_level', 'ssl_mode', 'status', 'updated_at', 'crated_at '];
|
|
protected $useTimestamps = true;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
public function getTitleField(): string
|
|
{
|
|
return self::TITLE;
|
|
}
|
|
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 "plan":
|
|
$rule = "required|trim|string";
|
|
break;
|
|
case "name_servers":
|
|
case "security_level":
|
|
case "ssl_mode":
|
|
$rule = "if_exist|trim|string";
|
|
break;
|
|
case "development_mode":
|
|
case "ipv6":
|
|
$rule = "if_exist|in_list[on,off]";
|
|
break;
|
|
case "status":
|
|
$rule = "if_exist|in_list[active,pending,moved]";
|
|
break;
|
|
case "domains": //create form에서 사용
|
|
$rule = "required|string";
|
|
break;
|
|
case "hosts": //create form에서 사용
|
|
$rule = "required|string";
|
|
break;
|
|
case "type": //create form에서 사용
|
|
$rule = "required|in_list[A,CNAME,NS,MX,PTR,SPF,TXT,SRV,INFO]";
|
|
break;
|
|
case "content": //create form에서 사용
|
|
$rule = "required|trim|string";
|
|
break;
|
|
case "proxied": //create form에서 사
|
|
$rule = "required|in_list[on,off]";
|
|
break;
|
|
default:
|
|
$rule = parent::getFieldRule($action, $field);
|
|
break;
|
|
}
|
|
return $rule;
|
|
}
|
|
public function getFormFieldOption(string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
$this->orderBy(self::TITLE, 'asc');
|
|
$options = parent::getFormFieldOption($field, $options);
|
|
break;
|
|
}
|
|
return $options;
|
|
}
|
|
public function getEntityByPK(string $uid): null|ZoneEntity
|
|
{
|
|
$this->where(self::PK, $uid);
|
|
return $this->getEntity();
|
|
}
|
|
public function getEntityByID(string $id): null|ZoneEntity
|
|
{
|
|
$this->where(self::TITLE, $id);
|
|
return $this->getEntity();
|
|
}
|
|
public function getEntitysByParent(AccountEntity $account_entity)
|
|
{
|
|
$this->where(self::PARENT, $account_entity->getPK());
|
|
return $this->getEntitys();
|
|
}
|
|
//create용
|
|
public function create(array $formDatas = []): ZoneEntity
|
|
{
|
|
return $this->create_process(new ZoneEntity(), $formDatas);
|
|
}
|
|
//modify용
|
|
public function modify(ZoneEntity $entity, array $formDatas): ZoneEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
|
|
//도메인이 이미 존재하는지 체크
|
|
public function isUniqueDomain(AccountEntity $account_entity, string $domain): bool
|
|
{
|
|
$this->where(self::PARENT, $account_entity->getPK());
|
|
$this->where(self::TITLE, $domain);
|
|
return is_null($this->first()) ? true : false;
|
|
}
|
|
//List 검색용
|
|
public function setList_WordFilter(string $word, $field = null): void
|
|
{
|
|
//Record의 content(IP검색)을 하기위함
|
|
//Join 방식사용
|
|
// $this->join(RecordModel::TABLE, sprintf(
|
|
// "%s.%s=%s.%s",
|
|
// self::TABLE,
|
|
// self::PK,
|
|
// RecordModel::TABLE,
|
|
// RecordModel::PARENT
|
|
// ));
|
|
// parent::setList_WordFilter($word, $field);
|
|
// $this->orLike(RecordModel::TABLE . '.content', $word, 'both');
|
|
//Subquery 방식사용
|
|
$recordModel = new RecordModel();
|
|
$recordModel->like(RecordModel::TABLE . '.content', $word, 'both');
|
|
$recordModel->orlike(RecordModel::TABLE . '.' . RecordModel::TITLE, $word, 'both');
|
|
$recorde_entitys = $recordModel->select(RecordModel::PARENT)->findAll();
|
|
$zone_uids = array_column($recorde_entitys, RecordModel::PARENT);
|
|
parent::setList_WordFilter($word, $field);
|
|
if (count($zone_uids)) {
|
|
$this->orWhereIn(self::TABLE . '.' . self::PK, array_values($zone_uids));
|
|
}
|
|
// var_dump($zone_uids);
|
|
// exit;
|
|
}
|
|
}
|