trafficmonitor/app/Helpers/CommonHelper.php

300 lines
11 KiB
PHP

<?php
namespace App\Helpers;
use App\Entities\CommonEntity;
use App\Traits\UtilTrait;
use RuntimeException;
class CommonHelper
{
use UtilTrait;
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 form_dropdown_common(string $field, mixed $value, array $viewDatas, array $extras = [], array $attributes = [], bool $isAll = false): string
// {
// // 필터 옵션이 없으면 빈 배열로 초기화
// if (!array_key_exists($field, $viewDatas['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("{$this->getAttribute('class_path')}.label.{$field}") . " 선택" . "</option>";
// $html .= $this->form_dropdown_common_process($field, $value, $viewDatas, $extras, $attributes, $isAll);
// $html .= '</select>';
// return $html;
// }
// //필수함수
// 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['formOptions'][$field]['options'] 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() != DEFAULTS['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 getFieldLabel(string $field, string $label, array $viewDatas, array $extras = []): string
{
switch ($field) {
default:
$extras = (strpos($viewDatas['formRules'][$field], 'required') !== false) ? ["class" => "text-danger", "required" => "", ...$extras] : $extras;
$label = form_label($label, $field, ['class' => 'form-label-sm', ...$extras]);
break;
}
return $label;
}
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 '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['formFilters'])) {
$form = form_dropdown($field, $viewDatas['formOptions'][$field]['options'], $value, $viewDatas['formOptions'][$field]['extras']);
// $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;
}
public function getFieldView(string $field, mixed $value, array $viewDatas, array $extras = []): string|null
{
switch ($field) {
case 'clientinfo_uid':
if (array_key_exists($value, $viewDatas['formOptions'][$field]['options'])) {
$value = "<a href=\"/admin/customer/client/detail/{$value}\">{$viewDatas['formOptions'][$field]['options'][$value]}</a>";
}
break;
case 'role':
$roles = [];
foreach ($value as $key) {
$roles[] = $viewDatas['formOptions'][$field]['options'][$key] ?? "";
}
$value = implode(" , ", $roles);
break;
case 'description':
case 'content':
case 'detail':
case 'history':
if (in_array($viewDatas['action'], ['view', 'index'])) {
$value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
}
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':
$value = $value ? date("Y-m-d", strtotime($value)) : "";
break;
default:
if (in_array($field, $viewDatas['formFilters'])) {
$value = $viewDatas['formOptions'][$field]['options'][$value] ?? "";
}
break;
}
if (is_array($value)) {
throw new \Exception(__METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다");
}
return $value;
}
public function getListFilter(string $field, mixed $value, array $viewDatas, $extras = []): string
{
switch ($field) {
default:
$filter = "";
if (in_array($field, $viewDatas['formFilters'])) {
$filter = form_dropdown(
$field,
$viewDatas['formOptions'][$field]['options'],
$value,
[...$extras, ...$viewDatas['formOptions'][$field]['extras']]
);
}
break;
}
return $filter;
}
public function getListLabel(string $field, string $label, array $viewDatas, array $extras = []): string
{
switch ($field) {
default:
if (isset($viewDatas['order_field']) && $viewDatas['order_field'] == $field) {
$label .= $viewDatas['order_value'] == 'ASC' ? ICONS["UP"] : ICONS["DOWN"];
}
$query = $viewDatas['uri']->getQuery(['except' => ['order_field', 'order_value']]);
$query .= empty($query) ? "" : "&";
$query .= "order_field={$field}&order_value=";
$query .= isset($viewDatas['order_value']) && $viewDatas['order_value'] == 'DESC' ? "ASC" : "DESC";
$label = anchor(current_url() . "?" . $query, $label);
form_dropdown('perpage', $viewDatas['index_pagination_options'], $viewDatas['perpage'], ['onChange' => 'this.form.submit()']);
break;
}
return $label;
}
public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
{
switch ($action) {
case 'create':
$action = form_label(
$label ? $label : '입력',
$action,
[
"data-src" => current_url() . '/' . $action . '?' . $viewDatas['uri']->getQuery(),
"data-bs-toggle" => "modal",
"data-bs-target" => "#modal_action_form",
"class" => "btn btn-sm btn-primary form-label-sm",
...$extras
]
);
break;
case 'modify':
$oldBatchJobUids = old("batchjob_uids", null);
$oldBatchJobUids = is_array($oldBatchJobUids) ? $oldBatchJobUids : [$oldBatchJobUids];
$checkbox = form_checkbox([
"id" => "checkbox_uid_{$viewDatas['entity']->getPK()}",
"name" => "batchjob_uids[]",
"value" => $viewDatas['entity']->getPK(),
"class" => "batchjobuids_checkboxs",
"checked" => in_array($viewDatas['entity']->getPK(), $oldBatchJobUids)
]);
$action = $checkbox . form_label(
$label ? $label : '수정',
$action,
[
"data-src" => current_url() . '/' . $action . '/' . $viewDatas['entity']->getPK() . '?' . $viewDatas['uri']->getQuery(),
"data-bs-toggle" => "modal",
"data-bs-target" => "#modal_action_form",
'class' => 'form-label-sm',
...$extras
]
);
break;
case 'view':
$action = form_label(
$label ? $label : ICONS['SEARCH'],
$action,
[
"data-src" => current_url() . '/' . $action . '/' . $viewDatas['entity']->getPK() . '?' . $viewDatas['uri']->getQuery(),
"data-bs-toggle" => "modal",
"data-bs-target" => "#modal_action_form",
"class" => "btn btn-sm btn-primary form-label-sm",
...$extras
]
);
break;
case 'delete':
$action = anchor(
current_url() . '/' . $action . '/' . $viewDatas['entity']->getPK(),
$label ? $label : ICONS['DELETE'],
[
"class" => "btn btn-sm btn-danger form-label-sm",
...$extras
]
);
break;
case 'batchjob':
$action = form_submit("batchjob_submit", $label ? $label : '일괄처리', [
"formaction" => current_url() . '/batchjob',
"class" => "btn btn-sm btn-warning form-label-sm",
...$extras,
// "onclick" => "return submitBatchJob()"
]);
break;
case 'batchjob_delete':
$action = form_submit("batchjob_submit", $label ? $label : '일괄삭제', [
"formaction" => current_url() . '/batchjob_delete',
"class" => "btn btn-sm btn-danger form-label-sm",
...$extras,
// "onclick" => "return submitBatchJobDelete()"
]);
break;
default:
$action = "";
break;
}
return $action;
}
}