152 lines
4.6 KiB
PHP
152 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Backend;
|
|
|
|
use App\Entities\BaseEntity;
|
|
use App\Models\UserModel;
|
|
|
|
abstract class BaseBackend
|
|
{
|
|
private $_userModel = null;
|
|
private $_className = null;
|
|
private $_user_uids = array();
|
|
protected $_model = null;
|
|
protected $_session = null;
|
|
protected function __construct(string $className)
|
|
{
|
|
$this->_className = $className;
|
|
$this->_session = \Config\Services::session();
|
|
}
|
|
|
|
final public function getClassName()
|
|
{
|
|
return $this->_className;
|
|
}
|
|
|
|
//User모델
|
|
final public function getUserModel(): UserModel
|
|
{
|
|
return is_null($this->_userModel) ? new UserModel() : $this->_userModel;
|
|
}
|
|
//Entity값 가져오기
|
|
final public function getEntity($uid)
|
|
{
|
|
return $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
}
|
|
|
|
//transaction관련
|
|
final public function transBegin()
|
|
{
|
|
$this->_model->transBegin();
|
|
}
|
|
final public function transCommit()
|
|
{
|
|
$this->_model->transBegin();
|
|
}
|
|
final public function transRollback()
|
|
{
|
|
$this->_model->transRollback();
|
|
}
|
|
|
|
//초기화
|
|
final public function getFields(string $action)
|
|
{
|
|
return $this->_model->getFields($action);
|
|
}
|
|
//TitleField
|
|
final public function getTitleField()
|
|
{
|
|
return $this->_model->getTitleField();
|
|
}
|
|
//Field별 Form Rule용
|
|
final public function getFieldRules(array $fields, string $action)
|
|
{
|
|
return $this->_model->getFieldRules($fields, $action);
|
|
}
|
|
//Field별 Form Filter용
|
|
final public function getFieldFilters()
|
|
{
|
|
return $this->_model->getFieldFilters();
|
|
}
|
|
//Field별 Form BatchFilter용
|
|
final public function getFieldBatchFilters()
|
|
{
|
|
return $this->_model->getFieldBatchFilters();
|
|
}
|
|
//Field별 Form Option용
|
|
public function getFieldFormOption(string $field): array
|
|
{
|
|
switch ($field) {
|
|
case 'user_uid':
|
|
$options = $this->_user_uids = $this->_user_uids ?: $this->getUserModel()->getFieldFormOptions(['status' => 'use']);
|
|
break;
|
|
default:
|
|
$options = lang($this->_className . '.' . strtoupper($field));
|
|
break;
|
|
}
|
|
if (!is_array($options)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$this->_className}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true));
|
|
}
|
|
return $options;
|
|
}
|
|
//Field별 Form Option용
|
|
final public function getFieldFormOptions(array $fields): array
|
|
{
|
|
$fieldFormOptions = array();
|
|
foreach ($fields as $field) {
|
|
if (!is_string($field)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$this->_className}의 Field:{$field}가 string 아닙니다.\n" . var_export($fields, true));
|
|
}
|
|
$fieldFormOptions[$field] = $this->getFieldFormOption($field);
|
|
}
|
|
return $fieldFormOptions;
|
|
}
|
|
|
|
//Insert관련
|
|
public function insert(array $fieldDatas): BaseEntity
|
|
{
|
|
return $this->_model->create($fieldDatas);
|
|
}
|
|
//Update관련
|
|
public function update($entity, array $fieldDatas)
|
|
{
|
|
return $this->_model->modify($entity, $fieldDatas);
|
|
}
|
|
//Delete 관련
|
|
public function delete($entity)
|
|
{
|
|
if (!$this->_model->delete($entity->getPrimaryKey())) {
|
|
log_message("error", __FUNCTION__ . "에서 호출:" . $this->_model->getLastQuery());
|
|
log_message("error", implode("\n", $this->_model->errors()));
|
|
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->_model->errors(), true));
|
|
}
|
|
}
|
|
//View 관련
|
|
public function view($entity)
|
|
{
|
|
return $entity;
|
|
}
|
|
|
|
public function setCondition(array $filterFields, $word, $start, $end, $order_field, $order_value)
|
|
{
|
|
foreach ($filterFields as $field => $value) {
|
|
$this->_model->where($field, $value);
|
|
}
|
|
if (!is_null($word)) {
|
|
$this->_model->setIndexWordFilter($word);
|
|
}
|
|
if (!is_null($start) && !is_null($end)) {
|
|
$this->_model->setIndexDateFilter($start, $end);
|
|
}
|
|
$this->_model->setIndexOrderBy($order_field, $order_value);
|
|
}
|
|
final public function getTotalCount()
|
|
{
|
|
return $this->_model->countAllResults();
|
|
}
|
|
final public function getFindEntitys(int $page = 0, int $per_page = 0)
|
|
{
|
|
return $per_page ? $this->_model->findAll($per_page, $page * $per_page - $per_page) : $this->_model->findAll();
|
|
}
|
|
}
|