91 lines
3.0 KiB
PHP
91 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Cloudflare;
|
|
|
|
use App\Entities\Cloudflare\ZoneEntity;
|
|
use CodeIgniter\Model;
|
|
use stdClass;
|
|
|
|
class ZoneModel extends Model
|
|
{
|
|
protected $table = 'cloudflarezone';
|
|
protected $primaryKey = 'uid';
|
|
protected $useAutoIncrement = false;
|
|
protected $returnType = ZoneEntity::class; //object,array,entity명::class
|
|
protected $allowedFields = ['uid', 'account_uid', 'uid', 'domain', 'name_servers', 'original_name_servers', 'plan', 'development_mode', 'ipv6', 'security_level', 'status', 'updated_at', 'crated_at '];
|
|
protected $useTimestamps = true;
|
|
const TITLE_FIELD = "id";
|
|
const PARENT_FIELD = "account_uid";
|
|
public function getFieldRule(string $field, array $rules): array
|
|
{
|
|
switch ($field) {
|
|
case "account_uid":
|
|
$rules[$field] = $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 "domain":
|
|
case "plan":
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case "name_servers":
|
|
case "security_level":
|
|
$rules[$field] = "if_exist|trim|string";
|
|
break;
|
|
case "development_mode":
|
|
case "ipv6":
|
|
$rules[$field] = "if_exist|in_list[on,off]";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
public function getEntityByPK(int $uid): null | ZoneEntity
|
|
{
|
|
$this->where($this->getPKField(), $uid);
|
|
return $this->getEntity();
|
|
}
|
|
public function getEntityByID(string $id): null | ZoneEntity
|
|
{
|
|
$this->where($this->getTitleField(), $id);
|
|
return $this->getEntity();
|
|
}
|
|
|
|
//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(string $account_uid, string $domain): bool
|
|
{
|
|
$this->where('account_uid', $account_uid);
|
|
$this->where('domain', $domain);
|
|
return is_null($this->first()) ? true : false;
|
|
}
|
|
|
|
//Index 검색용
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
$subquery = $this->db->table('cloudflarerecord')->select('zone_uid')->like('content', $word, 'both');
|
|
$this->like('domain', $word, 'both'); //befor , after , both
|
|
$this->orWhereIn('uid', $subquery);
|
|
}
|
|
|
|
public function setIndexDateFilter($start, $end)
|
|
{
|
|
$this->where('created_at >=', $start);
|
|
$this->where('created_at <=', $end);
|
|
}
|
|
public function setIndexOrderBy($field, $order = 'ASC')
|
|
{
|
|
$this->orderBy("domain ASC, {$field} {$order}");
|
|
}
|
|
}
|