136 lines
4.7 KiB
PHP
136 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Forms;
|
|
|
|
use App\Entities\CommonEntity;
|
|
use RuntimeException;
|
|
|
|
abstract class CommonForm
|
|
{
|
|
private array $_attributes = [];
|
|
protected function __construct() {}
|
|
final public function setAttributes(array $attributes): void
|
|
{
|
|
$this->_attributes = $attributes;
|
|
}
|
|
final public function getAttribute(string $key): string
|
|
{
|
|
if (!array_key_exists($key, $this->_attributes)) {
|
|
throw new RuntimeException(__METHOD__ . "에서 오류발생: {$key}에 해당하는 속성이 정의되지 않았습니다.");
|
|
}
|
|
return $this->_attributes[$key];
|
|
}
|
|
final public function getFormFields(string $action, array $fields, $tempFormFields = []): array
|
|
{
|
|
foreach ($fields as $field) {
|
|
$tempFormFields = $this->getFormField($action, $field, $tempFormFields);
|
|
}
|
|
return $tempFormFields;
|
|
}
|
|
final public function getFormFilters(string $action, array $fields = []): array
|
|
{
|
|
return $fields;
|
|
}
|
|
final public function getFormRules(string $action, array $fields, array $rules = []): array
|
|
{
|
|
foreach ($fields as $field) {
|
|
$rules = $this->getFormRule($action, $field, $rules);
|
|
}
|
|
return $rules;
|
|
}
|
|
final public function getFormOptions(string $action, array $fields, array $options = []): array
|
|
{
|
|
foreach ($fields as $field) {
|
|
$options = $this->getFormOption($action, $field, $options);
|
|
}
|
|
return $options;
|
|
}
|
|
final public function getBatchjobFields(array $fields = []): array
|
|
{
|
|
return $fields;
|
|
}
|
|
final public function getBatchjobButtons(array $buttions = [
|
|
'batchjob' => '일괄 처리',
|
|
'batchjob_delete' => '일괄 삭제',
|
|
]): array
|
|
{
|
|
return $buttions;
|
|
}
|
|
//필수함수
|
|
//사용자정의 함수
|
|
public function getFormField(string $action, string $field, array $tempFormFields = []): array
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
$tempFormFields[$field] = lang("{$this->getAttribute('class_path')}.label.{$field}");
|
|
break;
|
|
}
|
|
return $tempFormFields;
|
|
}
|
|
public function getFormRule(string $action, string $field, array $rules = []): array
|
|
{
|
|
switch ($field) {
|
|
case $this->getAttribute('pk_field'):
|
|
if (!$this->$this->getAttribute('useAutoIncrement')) {
|
|
$rule = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
|
|
$rule .= in_array($action, ["create"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "";
|
|
} else {
|
|
$rule = "required|numeric";
|
|
}
|
|
$rules[$field] = $rule;
|
|
break;
|
|
case $this->getAttribute('title_field'):
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case "code":
|
|
// a-zA-Z → 영문 대소문자,0-9 → 숫자,가-힣 → 한글 완성형,\- → 하이픈
|
|
$rule = "required|regex_match[/^[a-zA-Z0-9가-힣\-]+$/]|min_length[4]|max_length[20]";
|
|
$rule .= in_array($action, ["create"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "";
|
|
$rules[$field] = $rule;
|
|
break;
|
|
case 'picture':
|
|
$rules[$field] = "is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},300]|max_dims[{$field},2048,768]";
|
|
break;
|
|
case "updated_at":
|
|
case "created_at":
|
|
case "deleted_at":
|
|
$rules[$field] = "permit_empty|valid_date";
|
|
break;
|
|
default:
|
|
$rules[$field] = "permit_empty|trim|string";
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
public function getFormOption(string $action, string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
case 'user_uid':
|
|
$tempOptions = ['' => lang("{$this->getAttribute('class_path')}.label.{$field}") . " 선택"];
|
|
foreach (service('userservice')->getEntities() as $entity) {
|
|
$tempOptions[$entity->getPK()] = $entity->getTitle();
|
|
}
|
|
$options[$field]['options'] = $tempOptions;
|
|
$options[$field]['extras'] = ['class' => 'select-field'];
|
|
break;
|
|
case 'clientinfo_uid':
|
|
$tempOptions = ['' => lang("{$this->getAttribute('class_path')}.label.{$field}") . " 선택"];
|
|
foreach (service('clientervice')->getEntities() as $entity) {
|
|
$tempOptions[$entity->getPK()] = $entity->getTitle();
|
|
}
|
|
$options[$field]['options'] = $tempOptions;
|
|
$options[$field]['extras'] = ['class' => 'select-field'];
|
|
break;
|
|
default:
|
|
$tempOptions = [
|
|
'' => lang("{$this->getAttribute('class_path')}.label.{$field}") . " 선택",
|
|
...lang($this->getAttribute('class_path') . "." . strtoupper($field))
|
|
];
|
|
$options[$field]['options'] = $tempOptions;
|
|
$options[$field]['extras'] = [];
|
|
break;
|
|
}
|
|
return $options;
|
|
}
|
|
}
|