cfmgrv4/app/Controllers/Admin/Cloudflare/AccountController.php
2024-10-22 11:37:32 +09:00

96 lines
3.4 KiB
PHP

<?php
namespace App\Controllers\Admin\Cloudflare;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Helpers\Cloudflare\AccountHelper;
use App\Models\Cloudflare\AccountModel;
use App\Services\Cloudflare\Account;
class AccountController extends CloudflareController
{
private $_auth_entity = null;
private $_myLibrays = [];
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->class_name = "Account";
$this->class_path .= $this->class_name;
$this->title = lang("{$this->class_path}.title");
$this->helper = new AccountHelper();
}
final protected function getModel(): AccountModel
{
if ($this->model === null) {
$this->model = new AccountModel();
}
return $this->model;
}
final protected function getMyLibrary(): Account
{
if (!isset($this->_myLibrays[$this->_auth_entity->getPK()])) {
$this->_myLibrays[$this->_auth_entity->getPK()] = new Account($this->_auth_entity);
}
return $this->_myLibrays[$this->_auth_entity->getPK()];
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
case $this->getModel()::PARENT:
// $this->getAuthModel()->where('status', DEFAULTS['STATUS']);
$options[$field] = $this->getAuthModel()->getFormFieldOption($field);
// echo $this->getAuthModel()->getLastQuery();
// dd($options);
break;
default:
$options = parent::getFormFieldOption($field, $options);
break;
}
return $options;
}
private function init(string $action, array $fields = []): void
{
$this->action = $action;
$this->fields = count($fields) ? $fields : [$this->getModel()::PARENT, $this->getModel()::TITLE, 'type', 'status', 'updated_at', 'created_at'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = [$this->getModel()::PARENT, 'type', 'status'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->batchjob_fields = [];
}
//View
public function view(string $uid): RedirectResponse|string
{
$this->init(__FUNCTION__);
return $this->view_procedure($uid);
}
// 리스트
public function index(): string
{
$this->init(__FUNCTION__);
return $this->list_procedure();
}
// Download
public function download(string $output_type, mixed $uid = false): DownloadResponse|string
{
$this->init(__FUNCTION__);
return $this->download_procedure($output_type, $uid);
}
//reload Account By Auth
protected function reload_process(mixed $uid): void
{
$this->_auth_entity = $this->getAuthModel()->getEntityByPK($uid);
if ($this->_auth_entity === null) {
throw new \Exception("Auth: {$uid} 정보를 찾을수 없습니다.");
}
$this->getMyLibrary()->reload();
}
public function reload(int $uid): RedirectResponse
{
return $this->reload_procedure($uid);
}
}