dbms/app/Controllers/Admin/Equipment/ServerController.php
2025-05-15 19:03:38 +09:00

156 lines
5.9 KiB
PHP

<?php
namespace App\Controllers\Admin\Equipment;
use App\Entities\Equipment\ServerPartEntity;
use App\Helpers\Equipment\ServerHelper;
use App\Services\Equipment\ServerPartService;
use App\Services\Equipment\PartService;
use App\Services\Equipment\ServerService;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Validation;
use Psr\Log\LoggerInterface;
class ServerController extends EquipmentController
{
private ?PartService $_partService = null;
private ?ServerPartService $_serverPartService = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->uri_path .= strtolower($this->getService()->getClassName()) . '/';
$this->view_path .= strtolower($this->getService()->getClassName()) . DIRECTORY_SEPARATOR;
// echo $this->view_path;
// exit;
$this->class_path = $this->getService()->getClassPath();
$this->title = lang("{$this->getService()->getClassPath()}.title");
$this->helper = $this->getHelper();
$this->individualStylesheets = ['server_partinfo.css'];;
$this->individualScripts = ['server_partinfo.js'];
}
public function getService(): ServerService
{
if (!$this->_service) {
$this->_service = new ServerService($this->request);
}
return $this->_service;
}
public function getHelper(): mixed
{
if (!$this->_helper) {
$this->_helper = new ServerHelper($this->request);
}
return $this->_helper;
}
final public function getPartService(): PartService
{
if (!$this->_partService) {
$this->_partService = new PartService($this->request);
}
return $this->_partService;
}
final public function getServerPartService(): ServerPartService
{
if (!$this->_serverPartService) {
$this->_serverPartService = new ServerPartService();
}
return $this->_serverPartService;
}
//Index,FieldForm관련
protected function getFieldRule(string $action, string $field): string
{
if (is_array($field)) {
throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
}
switch ($field) {
case 'cpu_partinfo_uid':
case 'ram_partinfo_uid':
case 'disk_partinfo_uid':
$rule = "if_exist|permit_empty|numeric";
break;
default:
$rule = parent::getFieldRule($action, $field);
break;
}
return $rule;
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
case 'cpu_partinfo_uid':
case 'ram_partinfo_uid':
case 'disk_partinfo_uid':
$options[$field] = $this->getPartService()->getFormFieldOption($field);
break;
default:
$options = parent::getFormFieldOption($field, $options);
break;
}
return $options;
}
protected function setValidation(Validation $validation, string $action, string $field, ?string $rule = null): Validation
{
switch ($field) {
case 'cpu_partinfo_uid':
case 'ram_partinfo_uid':
case 'disk_partinfo_uid':
//아래 Rule Array는 필드명.* checkbox를 사용
$validation->setRule("{$field}.*", $field, $rule);
break;
default:
$validation = parent::setValidation($validation, $action, $field, $rule);
break;
}
return $validation;
}
//Index,FieldForm관련
protected function create_process(): mixed
{
$entity = parent::create_process();
//변경할 UIDS
$cpu_uids = $this->request->getVar('cpu_partinfo_uid[]');
if (!is_array($cpu_uids)) {
throw new \Exception("CPU가 정의되지 않았습니다.");
}
foreach ($cpu_uids as $uid) {
$temps = ['serverinfo_uid' => $entity->getPK(), 'partinfo_uid' => $uid];
$this->getServerPartService()->create($temps, new ServerPartEntity());
}
$ram_uids = $this->request->getVar('ram_partinfo_uid[]');
if (!is_array($ram_uids)) {
throw new \Exception("RAM가 정의되지 않았습니다.");
}
foreach ($ram_uids as $uid) {
$temps = ['serverinfo_uid' => $entity->getPK(), 'partinfo_uid' => $uid];
$this->getServerPartService()->create($temps, new ServerPartEntity());
}
$disk_uids = $this->request->getVar('disk_partinfo_uid[]');
if (!is_array($disk_uids)) {
throw new \Exception("DISK가 정의되지 않았습니다.");
}
foreach ($disk_uids as $uid) {
$temps = ['serverinfo_uid' => $entity->getPK(), 'partinfo_uid' => $uid];
$this->getServerPartService()->create($temps, new ServerPartEntity());
}
return $entity;
}
protected function index_process(): array
{
$fields = [
'fields' => ['code', 'clientinfo_uid', 'type', $this->getService()->getModel()->getTitleField(), 'price', 'status'],
];
$this->init('index', $fields);
$this->modal_type = 'modal_iframe';
$entities = parent::index_process();
foreach ($entities as $key => $entity) {
$entities[$key]['partinfo_uid'] = $this->getServerPartService()->getEntities(['serverinfo_uid' => $entity['id']);
$entities[$key]['ram_partinfo_uid'] = $this->getServerPartService()->getEntities(['serverinfo_uid' => $entity['id']);
$entities[$key]['disk_partinfo_uid'] = $this->getServerPartService()->getEntities(['serverinfo_uid' => $entity['id']);
}
}
}