dbmsv2/app/Helpers/Equipment/ServerPartHelper.php
2025-09-16 16:47:46 +09:00

143 lines
6.5 KiB
PHP

<?php
namespace App\Helpers\Equipment;
use App\Entities\CommonEntity;
use App\Models\Equipment\ServerPartModel;
class ServerPartHelper extends EquipmentHelper
{
public function __construct()
{
parent::__construct();
$this->setTitleField(field: ServerPartModel::TITLE);
}
protected function form_dropdown_common_process(string $field, mixed $value, array $viewDatas, array $extras = [], array $attributes = []): string
{
$html = "";
switch ($field) {
case 'part_uid':
if (!array_key_exists('type', $viewDatas['control']['form_datas']) || !$viewDatas['control']['form_datas']['type']) {
throw new \Exception(__METHOD__ . "에서 오류발생: Type가 정의되지 않았습니다.");
}
foreach ($viewDatas['control']['field_optons'][$field][$viewDatas['control']['form_datas']['type']] as $option_key => $option_value) {
$isSelected = $option_key == $value ? ' selected' : '';
$isDisabled = "";
$attribute = "";
$label = "";
if ($option_value instanceof CommonEntity) {
// if (in_array($viewDatas['control']['action'], ['create_form'])) {
// if ($option_value->getStatus() != $option_value::DEFAULT_STATUS) {
// $isDisabled = " disabled";
// }
// }
foreach ($attributes as $attribute_name => $attribute_value) {
$attribute = sprintf(" %s=\"%s\"", $attribute_name, $option_value->$attribute_value());
}
$label = $option_value->getStatus() != $option_value::DEFAULT_STATUS ? STATUS_ICONS['NOT_AVAILABLE'] : STATUS_ICONS['AVAILABLE'];
$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;
default:
$html = parent::form_dropdown_common_process($field, $value, $viewDatas, $extras, $attributes);
break;
}
return $html;
}
public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
{
switch ($field) {
case 'part_uid':
$extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
$extras['onChange'] = "document.querySelector('input[name=\'amount\']').value = this.options[this.selectedIndex].getAttribute('data-price')";
$attributes = ['data-price' => 'getPrice'];
$form = $this->form_dropdown_common($field, $value, $viewDatas, $extras, $attributes);
break;
default:
$form = parent::getFieldForm($field, $value, $viewDatas, $extras);
break;
}
return $form;
}
public function getFieldView(string $field, mixed $value, array $viewDatas, array $extras = []): string|null
{
switch ($field) {
case 'CPU':
case 'RAM':
case 'DISK':
case 'OS':
case 'DB':
case 'SOFTWARE':
case 'SWITCH':
case 'IP':
case 'CS':
//파트 Entity
$title = $viewDatas['entity']->getPartEntity()->getTitle();
$title .= $viewDatas['entity']->getCnt() > 1 ? "*" . $viewDatas['entity']->getCnt() . "" : "";
$title .= $viewDatas['entity']->getExtra() !== "" ? "[" . $viewDatas['entity']->getExtra() . "]" : "";
if (array_key_exists('return', $extras) && $extras['return'] == 'onlyText') {
$value = $title;
} else {
$billing = $viewDatas['entity']->getBilling() === "" ? "" : ($viewDatas['entity']->getBilling() === PAYMENT['BILLING']["ONETIME"] ? ICONS['ONETIME'] : ($viewDatas['entity']->getBilling() === PAYMENT['BILLING']["MONTH"] ? ICONS['MONTH'] : ""));
$value = form_label(
$billing . $title,
$field,
[
"data-src" => "/admin/equipment/serverpart/modify/{$viewDatas['entity']->getPK()}?type={$viewDatas['entity']->getType()}&ActionTemplate=popup",
"data-bs-toggle" => "modal",
"data-bs-target" => "#index_action_form",
"class" => "btn btn-sm btn-outline btn-circle",
"target" => "_self",
]
);
}
break;
default:
$value = parent::getFieldView($field, $value, $viewDatas, $extras);
break;
}
if (is_array($value)) {
throw new \Exception(__METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다");
}
return $value;
}
public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
{
switch ($action) {
case 'SWITCH':
case 'IP':
case 'CS':
case 'CPU':
case 'RAM':
case 'DISK':
case 'OS':
case 'DB':
case 'SOFTWARE':
$extras = [
"class" => "btn btn-sm btn-outline btn-circle",
"target" => "_self",
...$extras,
];
$action = form_label(
$label ? $label : ICONS["SERVER_ITEM_{$action}"],
$action,
[
"data-src" => "/admin/equipment/serverpart?serverinfo_uid={$viewDatas['serverinfo_uid']}&type={$action}&ActionTemplate=popup",
"data-bs-toggle" => "modal",
"data-bs-target" => "#index_action_form",
...$extras,
]
);
break;
default:
$action = parent::getListButton($action, $label, $viewDatas, $extras);
break;
}
return $action;
}
}