249 lines
9.0 KiB
PHP
249 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace App\Forms;
|
|
|
|
use App\Entities\CommonEntity;
|
|
use RuntimeException;
|
|
|
|
abstract class CommonForm
|
|
{
|
|
private array $_attributes = [];
|
|
protected function __construct() {}
|
|
abstract public function getFormFields(): array;
|
|
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 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 $class_path, string $action, array $fields, array $options = []): array
|
|
{
|
|
foreach ($fields as $field) {
|
|
$options = $this->getFormOption($class_path, $action, $field, $options);
|
|
}
|
|
return $options;
|
|
}
|
|
final public function form_dropdown_common(string $field, mixed $value, array $viewDatas, array $extras = [], array $attributes = [], bool $isAll = false): string
|
|
{
|
|
// 필터 옵션이 없으면 빈 배열로 초기화
|
|
if (!array_key_exists($field, $viewDatas['control']['formOptions'])) {
|
|
return ""; // 필터 옵션이 없으면 빈 문자열 반환
|
|
}
|
|
$extra = "";
|
|
foreach ($extras as $extra_tag => $extra_value) {
|
|
$extra .= sprintf(" %s=\"%s\"", $extra_tag, $extra_value);
|
|
}
|
|
// $formOptions는 필터 옵션 배열로, key는 필터 엔티티의 PK, value는 필터 엔티티 객체
|
|
$html = sprintf("<select name=\"%s\" %s>", $field, $extra);
|
|
$html .= "<option value=\"\">" . lang("{$viewDatas['class_path']}.label.{$field}") . " 선택" . "</option>";
|
|
$html .= $this->form_dropdown_common_process($field, $value, $viewDatas, $extras, $attributes, $isAll);
|
|
$html .= '</select>';
|
|
return $html;
|
|
}
|
|
//필수함수
|
|
public function getFormFilters(): array
|
|
{
|
|
return [];
|
|
}
|
|
public function getViewFields(): array
|
|
{
|
|
return $this->getFormFields();
|
|
}
|
|
public function getViewFilters(): array
|
|
{
|
|
return $this->getFormFilters();
|
|
}
|
|
public function getIndexFields(): array
|
|
{
|
|
return $this->getFormFields();
|
|
}
|
|
public function getIndexFilters(): array
|
|
{
|
|
return $this->getFormFilters();
|
|
}
|
|
public function getBatchjobFields(): array
|
|
{
|
|
return $this->getFormFields();
|
|
}
|
|
public function getBatchjobButtons(): array
|
|
{
|
|
return [
|
|
'batchjob' => '일괄 처리',
|
|
'batchjob_delete' => '일괄 삭제',
|
|
];
|
|
}
|
|
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 $class_path, string $action, string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
case 'user_uid':
|
|
$userService = service('UserService');
|
|
$option = $userService->getEntities();
|
|
break;
|
|
case 'clientinfo_uid':
|
|
$clientService = service('ClientService');
|
|
$option = $clientService->getEntities();
|
|
break;
|
|
default:
|
|
$option = lang($class_path . "." . strtoupper($field));
|
|
break;
|
|
}
|
|
if (!is_array($option)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$field}의 options 값들이 배열이 아닙니다.\n" . var_export($option, true));
|
|
}
|
|
$options[$field] = $option;
|
|
return $options;
|
|
}
|
|
protected function form_dropdown_common_process(string $field, mixed $value, array $viewDatas, array $extras = [], array $attributes = [], bool $isAll = false): string
|
|
{
|
|
$html = "";
|
|
switch ($field) {
|
|
default:
|
|
foreach ($viewDatas['control']['formOptions'][$field] as $option_key => $option_value) {
|
|
$isSelected = $option_key == $value ? ' selected' : '';
|
|
$isDisabled = "";
|
|
$attribute = "";
|
|
$label = "";
|
|
if ($option_value instanceof CommonEntity) {
|
|
if ($option_key != $value && $option_value->getStatus() != $option_value::DEFAULT_STATUS && !$isAll) {
|
|
continue;
|
|
}
|
|
foreach ($attributes as $attribute_name => $attribute_value) {
|
|
$attribute .= sprintf(" %s=\"%s\"", $attribute_name, $option_value->$attribute_value);
|
|
}
|
|
$label = $option_value->getCustomTitle();
|
|
} else {
|
|
$label = $option_value;
|
|
}
|
|
$html .= sprintf("<option value=\"%s\"%s%s%s>%s</option>", $option_key, $isSelected, $isDisabled, $attribute, $label);
|
|
}
|
|
break;
|
|
}
|
|
return $html;
|
|
}
|
|
public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
|
|
{
|
|
switch ($field) {
|
|
case 'email':
|
|
$form = form_input($field, $value ?? "", [
|
|
"class" => "form-control",
|
|
'style' => 'width:100%;',
|
|
"placeholder" => "예)test@example.com",
|
|
...$extras
|
|
]);
|
|
break;
|
|
case 'mobile':
|
|
case 'phone':
|
|
$form = form_input($field, $value ?? "", [
|
|
"class" => "form-control",
|
|
'style' => 'width:100%;',
|
|
"placeholder" => "예)010-0010-0010",
|
|
...$extras
|
|
]);
|
|
break;
|
|
case 'role':
|
|
if (!is_array($viewDatas['control']['formOptions'][$field])) {
|
|
throw new \Exception(__METHOD__ . "에서 {$field}의 field_options가 array형태가 아닙니다.");
|
|
}
|
|
// create, modify, create_form, modify_form일때 checkbox로 표시
|
|
if (in_array($viewDatas['control']['action'], ['create_form', 'modify_form'])) {
|
|
$forms = [];
|
|
foreach ($viewDatas['control']['formOptions'][$field] as $key => $label) {
|
|
if ($key !== '') { // 빈값은 제외
|
|
$values = is_array($value) ? $value : explode(DEFAULTS["DELIMITER_ROLE"], $value);
|
|
//form_check에는 "class" => "form-control" 쓰면 않되거나 form-check를 써야함
|
|
$forms[] = form_checkbox("{$field}[]", $key, in_array($key, $values), $extras) . $label;
|
|
}
|
|
}
|
|
$form = implode(" ", $forms);
|
|
} else {
|
|
$form = form_dropdown($field, $value, $viewDatas, $extras);
|
|
}
|
|
break;
|
|
case 'issue_at':
|
|
case 'expired_at':
|
|
case 'billing_at':
|
|
case 'start_at':
|
|
case 'end_at':
|
|
case 'updated_at':
|
|
case 'created_at':
|
|
case 'deleted_at':
|
|
$extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' calender' : 'calender';
|
|
$form = form_input($field, $value ?? "", $extras);
|
|
break;
|
|
case 'description':
|
|
case 'content':
|
|
case 'detail':
|
|
case 'history':
|
|
$extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' tinymce' : 'tinymce';
|
|
$form = form_textarea($field, html_entity_decode($value ?? "", ENT_QUOTES, 'UTF-8'), [
|
|
"class" => "form-control",
|
|
'style' => 'width:100%;',
|
|
...$extras
|
|
]);
|
|
break;
|
|
default:
|
|
if (in_array($field, $viewDatas['control']['formFilters'])) {
|
|
if (str_contains($field, "_uid")) {
|
|
$extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
|
|
}
|
|
$form = $this->form_dropdown_common($field, $value, $viewDatas, $extras);
|
|
} else {
|
|
$form = form_input($field, $value ?? "", [
|
|
"class" => "form-control",
|
|
'style' => 'width:100%;',
|
|
...$extras
|
|
]);
|
|
}
|
|
break;
|
|
}
|
|
return $form;
|
|
}
|
|
}
|