trafficmonitor init...2

This commit is contained in:
choi.jh 2025-11-11 14:51:31 +09:00
parent 46955cf337
commit e29f36f5a5
15 changed files with 228 additions and 133 deletions

View File

@ -36,18 +36,24 @@ class UserController extends AdminController
break;
case 'index':
$fields = ['id', 'name', 'email', 'mobile', 'role', 'status', 'created_at'];
$this->addViewDatas('index_batchjobFields', $this->service->getFormService()->getBatchjobFields($filters));
$this->addViewDatas('index_batchjobButtions', $this->service->getFormService()->getBatchjobButtons());
break;
default:
throw new \Exception("지원하지 않는 action입니다.({$action})");
// break;
}
$this->service->getFormService()->setFormFields($fields);
$this->service->getFormService()->setFormRules($action, $fields);
$this->service->getFormService()->setFormFilters($filters);
$this->service->getFormService()->getFormOptions($filters);
$this->service->getFormService()->setBatchjobFilters($filters);
$this->addViewDatas('helper', $this->service->getHelper());
$this->addViewDatas('formFields', $this->service->getFormService()->getFormFields($action, $fields));
$this->addViewDatas('formFilters', $this->service->getFormService()->getFormFilters($action, $filters));
$this->addViewDatas('formRules', $this->service->getFormService()->getFormRules($action, $fields));
$this->addViewDatas('formOptions', $this->service->getFormService()->getFormOptions($action, $filters));
$this->addViewDatas('formFields', $this->service->getFormService()->getFormFields());
$this->addViewDatas('formRules', $this->service->getFormService()->getFormRules());
$this->addViewDatas('formFilters', $this->service->getFormService()->getFormFilters());
$this->addViewDatas('formOptions', $this->service->getFormService()->getFormOptions());
$this->addViewDatas('index_batchjobFields', $this->service->getFormService()->getBatchjobFields());
$this->addViewDatas('index_batchjobButtions', $this->service->getFormService()->getBatchjobButtons());
}
protected function create_form_process(): void
{

View File

@ -36,9 +36,7 @@ abstract class AuthController extends CommonController
abstract protected function login_process(): UserEntity;
final public function login(): RedirectResponse
{
$action = __FUNCTION__;
try {
$this->action_init_process($action);
$this->login_process();
$redirect_url = $this->getAuthContext()->popPreviousUrl() ?? implode(DIRECTORY_SEPARATOR, $this->getActionPaths());
return redirect()->to($redirect_url)->with('message', MESSAGES['LOGIN']);

View File

@ -32,11 +32,17 @@ class GoogleController extends AuthController
throw new \Exception("지원하지 않는 action입니다.({$action})");
// break;
}
$this->service->getFormService()->setFormFields($fields);
$this->service->getFormService()->setFormRules($action, $fields);
$this->service->getFormService()->setFormFilters($filters);
$this->service->getFormService()->getFormOptions($filters);
$this->service->getFormService()->setBatchjobFilters($filters);
$this->addViewDatas('helper', $this->service->getHelper());
$this->addViewDatas('formFields', $this->service->getFormService()->getFormFields($action, $fields));
$this->addViewDatas('formFilters', $this->service->getFormService()->getFormFilters($action, $filters));
$this->addViewDatas('formRules', $this->service->getFormService()->getFormRules($action, $fields));
$this->addViewDatas('formOptions', $this->service->getFormService()->getFormOptions($action, $filters));
$this->addViewDatas('formFields', $this->service->getFormService()->getFormFields());
$this->addViewDatas('formRules', $this->service->getFormService()->getFormRules());
$this->addViewDatas('formFilters', $this->service->getFormService()->getFormFilters());
$this->addViewDatas('formOptions', $this->service->getFormService()->getFormOptions());
}
public function login_form_process(): void
{

View File

@ -37,15 +37,22 @@ class LocalController extends AuthController
throw new \Exception("지원하지 않는 action입니다.({$action})");
// break;
}
$this->service->getFormService()->setFormFields($fields);
$this->service->getFormService()->setFormRules($action, $fields);
$this->service->getFormService()->setFormFilters($filters);
$this->service->getFormService()->getFormOptions($filters);
$this->service->getFormService()->setBatchjobFilters($filters);
$this->addViewDatas('helper', $this->service->getHelper());
$this->addViewDatas('formFields', $this->service->getFormService()->getFormFields($action, $fields));
$this->addViewDatas('formFilters', $this->service->getFormService()->getFormFilters($action, $filters));
$this->addViewDatas('formRules', $this->service->getFormService()->getFormRules($action, $fields));
$this->addViewDatas('formOptions', $this->service->getFormService()->getFormOptions($action, $filters));
$this->addViewDatas('formFields', $this->service->getFormService()->getFormFields());
$this->addViewDatas('formRules', $this->service->getFormService()->getFormRules());
$this->addViewDatas('formFilters', $this->service->getFormService()->getFormFilters());
$this->addViewDatas('formOptions', $this->service->getFormService()->getFormOptions());
}
//로그인처리
protected function login_process(): UserEntity
{
$this->action_init_process('login');
$dto = new LocalDTO($this->request->getPost());
return $this->service->login($dto);
}

View File

@ -11,16 +11,15 @@ class GoogleForm extends CommonForm
parent::__construct();
}
public function getFormRule(string $action, string $field, array $rules = []): array
public function getFormRule(string $action, string $field): string
{
$rules = parent::getFormRule($action, $field, $rules);
switch ($field) {
case "access_code":
$rules[$field] = "required|trim|string";
$rule = "required|trim|string";
default:
$rules = parent::getFormRule($action, $field, $rules);
$rule = parent::getFormRule($action, $field);
break;
}
return $rules;
return $rule;
}
}

View File

@ -11,22 +11,20 @@ class LocalForm extends CommonForm
parent::__construct();
}
public function getFormRule(string $action, string $field, array $rules = []): array
public function getFormRule(string $action, string $field): string
{
$rules = parent::getFormRule($action, $field, $rules);
switch ($field) {
case "id":
$rule = "required|trim|min_length[4]|max_length[20]";
$rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "";
$rules[$field] = $rule;
break;
case "passwd":
$rules[$field] = in_array($action, ["create", "create_form"]) ? "required|trim|string" : "permit_empty|trim|string";
$rule = in_array($action, ["create", "create_form"]) ? "required|trim|string" : "permit_empty|trim|string";
break;
default:
$rules = parent::getFormRule($action, $field, $rules);
$rule = parent::getFormRule($action, $field);
break;
}
return $rules;
return $rule;
}
}

View File

@ -11,7 +11,7 @@ class CollectorForm extends CommonForm
parent::__construct();
}
public function getFormRule(string $action, string $field, array $rules = []): array
public function getFormRule(string $action, string $field): string
{
switch ($field) {
case "trafficinfo_uid":
@ -20,27 +20,26 @@ class CollectorForm extends CommonForm
case "raw_in":
case "raw_out":
$rule = "required|trim|neumeric";
$rules[$field] = $rule;
break;
default:
$rules = parent::getFormRule($action, $field, $rules);
$rule = parent::getFormRule($action, $field);
break;
}
return $rules;
return $rule;
}
public function getFormOption(string $action, string $field, array $options = []): array
public function getFormOption(string $field, array $options = ['options' => [], 'extras' => [], 'atttributes' => []]): array
{
$tempOptions = ['' => lang("{$this->getAttribute('class_path')}.label.{$field}") . " 선택"];
switch ($field) {
case 'trafficinfo_uid':
$tempOptions = ['' => lang("{$this->getAttribute('class_path')}.label.{$field}") . " 선택"];
foreach (service('trafficservice')->getEntities() as $entity) {
$tempOptions[$entity->getPK()] = $entity->getTitle();
}
$options[$field]['options'] = $tempOptions;
$options[$field]['extras'] = ['class' => 'select-field'];
$options[$field]['extras'] = ['class' => 'form-control select-field'];
break;
default:
$options = parent::getFormOption($action, $field, $options);
$options = parent::getFormOption($field, $options);
break;
}
return $options;

View File

@ -8,6 +8,11 @@ use RuntimeException;
abstract class CommonForm
{
private array $_attributes = [];
private array $_formFields = [];
private array $_formRules = [];
private array $_formFilters = [];
private array $_batchjobFilters = [];
private array $_formOptions = [];
protected function __construct() {}
final public function setAttributes(array $attributes): void
{
@ -20,34 +25,73 @@ abstract class CommonForm
}
return $this->_attributes[$key];
}
final public function getFormFields(string $action, array $fields, $tempFormFields = []): array
final public function setFormFields(array $fields): void
{
foreach ($fields as $field) {
$tempFormFields = $this->getFormField($action, $field, $tempFormFields);
$this->_formFields[$field] = $this->getFormFieldLabel($field);
}
return $tempFormFields;
}
final public function getFormFilters(string $action, array $fields = []): array
//$fields 매치된것만 반환, []->전체
final public function getFormFields(array $fields = []): array
{
return $fields;
// 1. $fields 배열이 비어있는지 확인합니다.
// getFormFields('create') 와 같이 호출될 경우 $fields는 빈 배열[]이 됩니다.
if (empty($fields)) {
// 비어있다면, _formFields 전체를 반환합니다.
return $this->_formFields;
}
// $fields의 값(원하는 필드명)을 키로 변환합니다.
// 예: ['test1'] -> ['test1' => 0]
$fieldsAsKeys = array_flip($fields);
// _formFields와 $fieldsAsKeys 간의 키를 비교하여 교집합을 반환합니다.
// 즉, $fields에 지정된 필드 정의만 추출됩니다.
return array_intersect_key($this->_formFields, $fieldsAsKeys);
}
final public function getFormRules(string $action, array $fields, array $rules = []): array
final public function setFormRules(string $action, array $fields): void
{
foreach ($fields as $field) {
$rules = $this->getFormRule($action, $field, $rules);
$this->_formRules[$field] = $this->getFormRule($action, $field);
}
return $rules;
}
final public function getFormOptions(string $action, array $fields, array $options = []): array
//$fields 매치된것만 반환, []->전체
final public function getFormRules(array $fields = []): array
{
if (empty($fields)) {
return $this->_formRules;
}
$fieldsAsKeys = array_flip($fields);
return array_intersect_key($this->_formRules, $fieldsAsKeys);
}
final public function setFormOptions(array $fields): void
{
foreach ($fields as $field) {
$options = $this->getFormOption($action, $field, $options);
$this->_formOptions[$field] = $this->getFormOption($field);
}
return $options;
}
final public function getBatchjobFields(array $fields = []): array
//$fields 매치된것만 반환, []->전체
final public function getFormOptions(array $fields = []): array
{
return $fields;
if (empty($fields)) {
return $this->_formOptions;
}
$fieldsAsKeys = array_flip($fields);
return array_intersect_key($this->_formOptions, $fieldsAsKeys);
}
final public function setFormFilters(array $fields): void
{
$this->_formFilters = $fields;
}
final public function getFormFilters(): array
{
return $this->_formFilters;
}
final public function setBatchjobFilters(array $fields): void
{
$this->_batchjobFilters = $fields;;
}
final public function getBatchjobFilters(): array
{
return $this->_batchjobFilters;
}
final public function getBatchjobButtons(array $buttions = [
'batchjob' => '일괄 처리',
@ -56,18 +100,36 @@ abstract class CommonForm
{
return $buttions;
}
//Validation용
public function validate(array $formDatas): bool
{
$validation = service('validation');
$dynamicRules = [];
foreach ($this->getFormRules() as $field => $rule) {
//field별 추가 커스텀 룰 적용
list($field, $rule) = $this->getValidationRule($field, $rule);
$dynamicRules[$field] = $rule;
}
$validation->setRules($dynamicRules);
return $validation->run($formDatas);
}
//필수함수
//사용자정의 함수
public function getFormField(string $action, string $field, array $tempFormFields = []): array
protected function getValidationRule(string $field, string $rule): array
{
return array($field, $rule);
}
public function getFormFieldLabel(string $field, ?string $label = null): string
{
switch ($field) {
default:
$tempFormFields[$field] = lang("{$this->getAttribute('class_path')}.label.{$field}");
$label = $label ?? lang("{$this->getAttribute('class_path')}.label.{$field}");
break;
}
return $tempFormFields;
return $label;
}
public function getFormRule(string $action, string $field, array $rules = []): array
public function getFormRule(string $action, string $field): string
{
switch ($field) {
case $this->getAttribute('pk_field'):
@ -77,57 +139,55 @@ abstract class CommonForm
} else {
$rule = "required|numeric";
}
$rules[$field] = $rule;
break;
case $this->getAttribute('title_field'):
$rules[$field] = "required|trim|string";
$rule = "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]";
$rule = "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";
$rule = "permit_empty|valid_date";
break;
default:
$rules[$field] = "permit_empty|trim|string";
$rule = "permit_empty|trim|string";
break;
}
return $rules;
return $rule;
}
public function getFormOption(string $action, string $field, array $options = []): array
public function getFormOption(string $field, array $options = ['options' => [], 'extras' => [], 'atttributes' => []]): array
{
$tempOptions = ['' => lang("{$this->getAttribute('class_path')}.label.{$field}") . " 선택"];
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['attributes'][$entity->getPK()] = ['data-role' => implode(DEFAULTS['DELIMITER_ROLE'], $entity->getRole())];
}
$options[$field]['options'] = $tempOptions;
$options[$field]['extras'] = ['class' => 'select-field'];
$options['options'] = $tempOptions;
$options['extras'] = ['class' => 'form-control 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['attributes'][$entity->getPK()] = ['data-role' => implode(DEFAULTS['DELIMITER_ROLE'], $entity->getRole())];
}
$options[$field]['options'] = $tempOptions;
$options[$field]['extras'] = ['class' => 'select-field'];
$options['options'] = $tempOptions;
$options['extras'] = ['class' => 'form-control 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'] = [];
foreach (lang($this->getAttribute('class_path') . "." . strtoupper($field)) as $key => $label) {
$tempOptions[$key] = $label;
}
$options['options'] = $tempOptions;
$options['extras'] = ['class' => 'form-control'];
break;
}
return $options;

View File

@ -11,20 +11,19 @@ class MylogForm extends CommonForm
parent::__construct();
}
public function getFormRule(string $action, string $field, array $rules = []): array
public function getFormRule(string $action, string $field): string
{
switch ($field) {
case "title":
$rule = "required|trime|string";
$rules[$field] = $rule;
break;
case "content":
$rules[$field] = "permit_empty|trim|string";
$rule = "permit_empty|trim|string";
break;
default:
$rules = parent::getFormRule($action, $field, $rules);
$rule = parent::getFormRule($action, $field);
break;
}
return $rules;
return $rule;
}
}

View File

@ -10,31 +10,27 @@ class TrafficForm extends CommonForm
{
parent::__construct();
}
public function getFormRule(string $action, string $field, array $rules = []): array
public function getFormRule(string $action, string $field): string
{
switch ($field) {
case "client":
case "switch":
$rule = "required|trim|string";
$rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "";
$rules[$field] = $rule;
break;
case "ip":
$rule = "required|trim|valid_ip[both]"; //ipv4 , ipv6 , both(ipv4,ipv6)
$rules[$field] = $rule;
break;
case "interface":
$rule = "required|trim|string";
$rules[$field] = $rule;
break;
case "status":
$rule = "required|trim|string";
$rules[$field] = $rule;
break;
default:
$rules = parent::getFormRule($action, $field, $rules);
$rule = parent::getFormRule($action, $field);
break;
}
return $rules;
return $rule;
}
}

View File

@ -10,42 +10,51 @@ class UserForm extends CommonForm
{
parent::__construct();
}
public function getFormField(string $action, string $field, array $tempFormFields = []): array
protected function getValidationRule(string $field, string $rule): array
{
switch ($field) {
default:
$tempFormFields = parent::getFormField($action, $field, $tempFormFields);
case 'role':
$field = "{$field}.*";
break;
default:
return parent::getValidationRule($field, $rule);
// break;
}
return $tempFormFields;
return array($field, $rule);
}
public function getFormRule(string $action, string $field, array $rules = []): array
// public function getFormFieldLabel(string $field, ?string $label = null): string
// {
// switch ($field) {
// default:
// $label = parent::getFormFieldLabel($field, $label);
// break;
// }
// return $label;
// }
public function getFormRule(string $action, string $field): string
{
switch ($field) {
case "id":
$rule = "required|trim|min_length[4]|max_length[20]";
$rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "";
$rules[$field] = $rule;
break;
case "passwd":
$rules[$field] = in_array($action, ["create", "create_form"]) ? "required" : "permit_empty" . "|trim|string";
$rule = in_array($action, ["create", "create_form"]) ? "required" : "permit_empty" . "|trim|string";
break;
case "confirmpassword":
$rules[$field] = in_array($action, ["create", "create_form"]) ? "required" : "permit_empty" . "|trim|string|matches[passwd]";
$rule = in_array($action, ["create", "create_form"]) ? "required" : "permit_empty" . "|trim|string|matches[passwd]";
break;
case "email":
$rule = "required|trim|valid_email";
$rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "" . "|trim|valid_email";
$rules[$field] = $rule;
break;
case "role":
$rules[$field] = "required|trim|string";
$rule = "required|trim|string";
break;
default:
$rules = parent::getFormRule($action, $field, $rules);
$rule = parent::getFormRule($action, $field);
break;
}
return $rules;
return $rule;
}
}

View File

@ -32,6 +32,54 @@ class CommonHelper
}
return $label;
}
/**
* CI4의 form_dropdown() 확장하여 <option> 태그에 data-* 속성을 추가할 있도록 합니다.
*
* @param string $name 필드 이름
* @param array $options 드롭다운 옵션 배열.
* 값은 문자열/정수, 또는 추가 속성을 포함하는 배열일 있습니다.
* : ['value' => 'test1', 'text' => 'Test 1', 'data-price' => 1000]
* @param string|array|null $selected 현재 선택된
* @param string $extra <select> 태그에 추가할 속성
* @return string HTML <select> 태그
*/
public function form_dropdown_custom(string $name = '', array $options = [], $selected = null, string $extra = ''): string
{
$select = '<select name="' . esc($name) . '"' . $extra . ">\n";
// 선택된 값 처리
$selected = is_array($selected) ? $selected : [$selected];
foreach ($options as $key => $val) {
// 💡 핵심 로직: $val이 배열인지 확인합니다.
if (is_array($val)) {
// $val이 배열인 경우: 복합 옵션 (value, text, data-* 속성 포함)
$optionValue = $val['value'] ?? $key; // value가 없으면 키 사용
$optionText = $val['text'] ?? $optionValue;
$optionAttributes = '';
// data-* 속성 추출 및 조합
foreach ($val as $attrKey => $attrValue) {
// 'value'와 'text'는 <option> 속성이 아니므로 제외
if ($attrKey !== 'value' && $attrKey !== 'text') {
$optionAttributes .= " " . esc($attrKey) . '="' . esc($attrValue) . '"';
}
}
$optionSelected = in_array($optionValue, $selected) ? ' selected="selected"' : '';
$select .= '<option value="' . esc($optionValue) . '"' . $optionAttributes . $optionSelected . '>'
. esc($optionText)
. "</option>\n";
} else {
// $val이 배열이 아닌 경우: 일반 옵션 (기존 form_dropdown() 방식)
$optionSelected = in_array($key, $selected) ? ' selected="selected"' : '';
$select .= '<option value="' . esc($key) . '"' . $optionSelected . '>'
. esc($val)
. "</option>\n";
}
}
$select .= "</select>\n";
return $select;
}
public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
{
switch ($field) {

View File

@ -105,9 +105,8 @@ abstract class AuthService
$formDatas = (array)$dto;
// dd($formDatas);
//입력값 검증
$validation = service('validation')->setRules($this->getValidationRules(__FUNCTION__));
if (!$validation->run($formDatas)) {
throw new ValidationException(implode("\n", $validation->getErrors()));
if (!$this->getFormService()->validate($formDatas)) {
throw new ValidationException(implode("\n", service('validation')->getErrors()));
}
//인증처리
$entity = $this->login_process($formDatas);

View File

@ -86,21 +86,6 @@ abstract class CommonService
}
return $entities;
}
//Validation용
final protected function getValidationRules(string $action, array $rules): array
{
$dynamicRules = [];
foreach ($rules as $field => $rule) {
//field별 추가 커스텀 룰 적용
list($field, $rule) = $this->getValidationRule($field, $rule);
$dynamicRules[$field] = $rule;
}
return $dynamicRules;
}
protected function getValidationRule(string $field, string $rule): array
{
return array($field, $rule);
}
//CURD 결과처리용
//DB 결과 처리 로직 통합 및 개선
protected function handle_save_result(mixed $result, CommonEntity $entity): CommonEntity
@ -129,9 +114,8 @@ abstract class CommonService
{
$formDatas = (array)$dto;
//입력값 검증
$validation = service('validation')->setRules($this->getValidationRules(__FUNCTION__,));
if (!$validation->run($formDatas)) {
throw new ValidationException(implode("\n", $validation->getErrors()));
if (!$this->getFormService()->validate($formDatas)) {
throw new ValidationException(implode("\n", service('validation')->getErrors()));
}
$entity = $this->create_process($formDatas);
$result = $this->model->insert($entity, $this->model->useAutoIncrement());
@ -143,9 +127,8 @@ abstract class CommonService
{
$formDatas = (array)$dto;
//입력값 검증
$validation = service('validation')->setRules($this->getValidationRules(__FUNCTION__));
if (!$validation->run($formDatas)) {
throw new ValidationException(implode("\n", $validation->getErrors()));
if (!$this->getFormService()->validate($formDatas)) {
throw new ValidationException(implode("\n", service('validation')->getErrors()));
}
$updatedEntity = $this->modify_process($uid, $formDatas);
$result = $this->model->save($updatedEntity);

View File

@ -45,18 +45,6 @@ class UserService extends CommonService
return $this->helperInstance;
}
//기본 기능부분
protected function getValidationRule(string $field, string $rule): array
{
switch ($field) {
case 'role':
$field = "{$field}.*";
break;
default:
return parent::getValidationRule($field, $rule);
// break;
}
return array($field, $rule);
}
protected function create_process(array $formDatas): UserEntity
{
if (isset($formDatas['confirmpassword'])) {