diff --git a/app/Controllers/Admin/MyLogController.php b/app/Controllers/Admin/MyLogController.php index 99f49d1..7d01acb 100644 --- a/app/Controllers/Admin/MyLogController.php +++ b/app/Controllers/Admin/MyLogController.php @@ -24,7 +24,7 @@ class MyLogController extends AdminController protected function getServiceClass(): Service { if ($this->service === null) { - $this->service = new Service($this->request); + $this->service = new Service(); } return $this->service; } @@ -67,11 +67,11 @@ class MyLogController extends AdminController //View관련 - protected function view_init(string $action, $fields = []): void + protected function view_process($uid): mixed { $fields = [ 'fields' => ['user_uid', 'class_name', 'method_name', $this->getService()->getModel()::TITLE, 'created_at', 'status', 'content'], ]; - parent::view_init($action, $fields); + return parent::view_process($uid); } } diff --git a/app/Controllers/CommonController.php b/app/Controllers/CommonController.php index 7bd1d7f..e5ae436 100644 --- a/app/Controllers/CommonController.php +++ b/app/Controllers/CommonController.php @@ -3,13 +3,15 @@ namespace App\Controllers; use App\Controllers\BaseController; +use App\Services\MyLogService; use CodeIgniter\HTTP\DownloadResponse; use CodeIgniter\HTTP\IncomingRequest; -use CodeIgniter\HTTP\RedirectResponse; +use CodeIgniter\HTTP\RedirectResponse; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\Validation\Validation; +use PDOException; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Html; use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf; @@ -265,24 +267,28 @@ abstract class CommonController extends BaseController } } //일괄처리작업 - protected function batchjob_process(mixed $uid): mixed + protected function batchjob_process(string $uids): array { - //데이터 검증 - $this->formDatas = $this->doValidate($this->action, $this->fields); - //자신정보정의 - $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); - $entity = $this->getService()->getEntity(); - if (!$entity) { - throw new \Exception(__FUNCTION__ . " => {$uid} 정보를 찾을수 없습니다."); + $temps = []; + foreach (explode(",", $uids) as $uid) { + $entity = $this->getService()->getEntity(); + if (!$entity) { + throw new \Exception("{$uid} 정보를 찾을수 없습니다."); + } + $temps[] = $entity; } - return $this->getService()->modify($entity, $this->formDatas); + $entities = []; + foreach ($temps as $entity) { + $entities[] = $this->getService()->modify($entity, $this->formDatas); + } + return $entities; } final public function batchjob(): RedirectResponse { - $this->init(__FUNCTION__); //Transaction Start $this->getService()->getModel()->transStart(); try { + $this->init(__FUNCTION__); //변경할 UIDS $uids = $this->request->getVar('batchjob_uids'); if (!$uids) { @@ -295,13 +301,9 @@ abstract class CommonController extends BaseController $fields[$field] = $field; } } - $this->fields = $fields; - $entities = []; - foreach (explode(",", $uids) as $uid) { - $entities[$uid] = $this->batchjob_process($uid); - } - $this->entities = $entities; - $this->getService()->getModel()->transCommit(); + //데이터 검증 + $this->formDatas = $this->doValidate($this->action, $fields); + $this->entities = $this->batchjob_process($uids); return $this->getResultPageByActon($this->action); } catch (\Exception $e) { //Transaction Rollback @@ -311,21 +313,25 @@ abstract class CommonController extends BaseController } //삭제,일괄삭제 공통사용 - protected function delete_process(mixed $uid): mixed + protected function delete_process(mixed $entity): bool { - //자신정보정의 - $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); - $entity = $this->getService()->getModel()->getEntity(); - if ($entity === null) { - throw new \Exception("{$uid} 정보를 찾을수 없습니다."); + $result = $this->getService()->delete($entity); + if (!$result) { + throw new \Exception("[$entity->getTitle()] 삭제를 실패하였습니다."); } - return $this->getService()->delete($entity); + return $result; } public function delete(mixed $uid): RedirectResponse|string { //Transaction Start $this->getService()->getModel()->transStart(); try { + $this->init(__FUNCTION__); + $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); + $entity = $this->getService()->getEntity(); + if ($entity === null) { + throw new \Exception("{$uid} 정보를 찾을수 없습니다."); + } $this->entity = $this->delete_process($uid); $this->getService()->getModel()->transCommit(); return $this->getResultPageByActon($this->action); @@ -336,26 +342,46 @@ abstract class CommonController extends BaseController } } //일괄삭제 + protected function batchjob_delete_process(string $uids): void + { + $entities = []; + foreach (explode(",", $uids) as $uid) { + $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); + $entity = $this->getService()->getEntity(); + if (!$entity) { + throw new \Exception("{$uid} 정보를 찾을수 없습니다."); + } + $entities[] = $entity; + } + foreach ($entities as $entity) { + try { + if ($this->getService()->delete($entity)) { + MyLogService::debug("[$entity->getTitle()]" . MESSAGES["DELETED"]); + } + } catch (PDOException $e) { + MyLogService::debug("[$entity->getTitle()]" . MESSAGES["FAILED"] . ":" . $e->getMessage()); + } + } + } final public function batchjob_delete(): RedirectResponse|string { //Transaction Start $this->getService()->getModel()->transStart(); try { + $this->init(__FUNCTION__); //변경할 UIDS $uids = $this->request->getVar('batchjob_uids'); if (!$uids) { throw new \Exception("적용할 리스트를 선택하셔야합니다."); } - $entities = []; - foreach (explode(",", $uids) as $uid) { - $entities[$uid] = $this->delete_process($uid); - } - $this->entities = $entities; + $this->batchjob_delete_process($uids); + MyLogService::save($this->getService(), __FUNCTION__, $this->myauth, MESSAGES["SUCCESS"]); $this->getService()->getModel()->transCommit(); return $this->getResultPageByActon($this->action); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); + MyLogService::save($this->getService(), __FUNCTION__, $this->myauth, MESSAGES["FAILED"]); return redirect()->back()->withInput()->with('error', $e->getMessage()); } } @@ -523,7 +549,7 @@ abstract class CommonController extends BaseController throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다."); } $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); - $this->entity = $this->getService()->getModel()->getEntity(); + $this->entity = $this->getService()->getEntity(); if ($this->entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); } diff --git a/app/Entities/CommonEntity.php b/app/Entities/CommonEntity.php index 083d61a..1e79924 100644 --- a/app/Entities/CommonEntity.php +++ b/app/Entities/CommonEntity.php @@ -25,7 +25,7 @@ abstract class CommonEntity extends Entity } final public function getTitle(): string { - $field = constant("static::Title"); + $field = constant("static::TITLE"); return $this->$field; } final public function getUpdatedAt(): string diff --git a/app/Entities/MyLogEntity.php b/app/Entities/MyLogEntity.php index 0135b28..57dc7ab 100644 --- a/app/Entities/MyLogEntity.php +++ b/app/Entities/MyLogEntity.php @@ -8,7 +8,7 @@ use App\Models\MyLogModel as Model; class MyLogEntity extends Entity { const PK = Model::PK; - const Title = Model::TITLE; + const TITLE = Model::TITLE; public function __toString(): string { return "{$this->getPK()}:{$this->getTitle()}}"; diff --git a/app/Entities/UserEntity.php b/app/Entities/UserEntity.php index ef0b9a1..dbf8d2d 100644 --- a/app/Entities/UserEntity.php +++ b/app/Entities/UserEntity.php @@ -8,7 +8,7 @@ use App\Models\UserModel as Model; class UserEntity extends CommonEntity { const PK = Model::PK; - const Title = Model::TITLE; + const TITLE = Model::TITLE; public function __toString(): string { return "{$this->getPK()}:{$this->getID()}:{$this->getTitle()},{$this->getRole()}}"; diff --git a/app/Entities/UserSNSEntity.php b/app/Entities/UserSNSEntity.php index 3ecec96..38a81dc 100644 --- a/app/Entities/UserSNSEntity.php +++ b/app/Entities/UserSNSEntity.php @@ -8,15 +8,11 @@ use App\Models\UserSNSModel as Model; class UserSNSEntity extends CommonEntity { const PK = Model::PK; - const Title = Model::TITLE; + const TITLE = Model::TITLE; public function __toString(): string { return "{$this->getPK()}|{$this->getID()}|{$this->getTitle()}"; } - public function setTitle(string $title): void - { - $this->attributes[Model::TITLE] = $title; - } //Common Function public function getParent(): int|null { diff --git a/app/Models/CommonModel.php b/app/Models/CommonModel.php index 9c8c91f..c0859b9 100644 --- a/app/Models/CommonModel.php +++ b/app/Models/CommonModel.php @@ -151,16 +151,15 @@ abstract class CommonModel extends Model // log_message("debug", $this->getLastQuery()); return $entity; } catch (\Exception $e) { - $message = sprintf( + throw new \Exception(sprintf( "\n------%s SQL오류-----\n%s\n%s\n------------------------------\n", __FUNCTION__, $this->getLastQuery(), $e->getMessage() - ); - throw new \Exception($message); + )); } } - public function create(array $formDatas, $entity): mixed + public function create(array $formDatas, mixed $entity): mixed { // Field에 맞는 Validation Rule 재정의 $this->setValidationRules($this->getFieldRules('create', $this->allowedFields)); @@ -178,13 +177,11 @@ abstract class CommonModel extends Model // log_message("debug", $this->getTable() . " => " . __FUNCTION__ . " DB 작업 완료"); return $entity; } - public function modify($entity, array $formDatas): mixed + public function modify(mixed $entity, array $formDatas): mixed { // Field에 맞는 Validation Rule 재정의 $this->setValidationRules($this->getFieldRules('modify', $this->allowedFields)); // 저장하기 전에 데이터 값 변경이 필요한 Field - echo var_dump($formDatas); - exit; foreach (array_keys($formDatas) as $field) { $entity->$field = $this->convertEntityData($field, $formDatas); } diff --git a/app/Models/MyLogModel.php b/app/Models/MyLogModel.php index 84440af..9e28f7c 100644 --- a/app/Models/MyLogModel.php +++ b/app/Models/MyLogModel.php @@ -19,7 +19,7 @@ class MyLogModel extends CommonModel "method_name", "title", "content", - "created_at", + "status" ]; public function __construct() { diff --git a/app/Services/Auth/GoogleService.php b/app/Services/Auth/GoogleService.php index befe13b..0ebd757 100644 --- a/app/Services/Auth/GoogleService.php +++ b/app/Services/Auth/GoogleService.php @@ -50,7 +50,7 @@ class GoogleService extends AuthService $userSNS_entity = $this->getMySocket($access_code)->getUserSNSEntity(); // local db 사용와의 연결 확인 $this->getModel()->where($this->getModel()::PK, $userSNS_entity->getParent()); - $entity = $this->getModel()->getEntity(); + $entity = $this->getEntity(); if ($entity === null) { throw new PageNotFoundException("회원[{$userSNS_entity->getTitle()}]님은 아직 로컬사용자 연결이 이루어지지 않았습니다."); } diff --git a/app/Services/MyLogService.php b/app/Services/MyLogService.php index c00fa55..a99b6ce 100644 --- a/app/Services/MyLogService.php +++ b/app/Services/MyLogService.php @@ -5,53 +5,45 @@ namespace App\Services; use App\Entities\MyLogEntity as Entity; use App\Models\MyLogModel as Model; use App\Services\Auth\AuthService; -use CodeIgniter\HTTP\IncomingRequest; -class MyLogService extends CommonService +class MyLogService { + private static $_model = null; private static $_logBuffers = []; - public function __construct(IncomingRequest $request) + public function __construct() {} + static public function getModel(): Model { - parent::__construct($request); - } - final public function getClassName(): string - { - return "MyLog"; - } - final public function getClassPath(): string - { - return $this->getClassName(); - } - public function getModelClass(): string - { - return Model::class; - } - public function getEntityClass(): string - { - return Entity::class; + if (self::$_model === null) { + self::$_model = new Model(); + } + return self::$_model; } static public function log(string $message, string $level = "info"): void { - self::$_logBuffers[$level] = sprintf("%s[%s]: %s", date("H:i:s"), $level, $message); + self::$_logBuffers[] = sprintf("%s[%s]: %s", date("H:i:s"), $level, $message); log_message($level, $message); } - public function info(string $message): void + static public function info(string $message): void { self::log($message, 'info'); } - public function error(string $message): void + static public function error(string $message): void { self::log($message, 'error'); } - public function warning(string $message): void + static public function warning(string $message): void { self::log($message, 'warning'); } - public function debug(string $message): void + static public function debug(string $message): void { self::log($message, 'debug'); } - public function save($service, string $method, AuthService $myauth, string $title): Entity + static public function dump() + { + return implode("\n", self::$_logBuffers); + } + static public function save($service, string $method, AuthService $myauth, string $title): Entity { $formDatas = [ 'user_uid' => $myauth->getUIDByAuthInfo(), @@ -61,6 +53,6 @@ class MyLogService extends CommonService 'content' => implode("\n", self::$_logBuffers), ]; self::$_logBuffers = []; - return $this->getModel()->create($formDatas, new Entity()); + return self::getModel()->create($formDatas, new Entity()); } } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index aaabb9d..7b0c154 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -59,8 +59,8 @@ class UserService extends CommonService // die(var_export($formDatas, true)); return $this->getModel()->modify($entity, $formDatas); } - public function delete(Entity $entity): void + public function delete(Entity $entity): bool { - $this->getModel()->delete($entity); + return $this->getModel()->delete($entity->getPK()); } } diff --git a/app/Traits/MylogTrait.php b/app/Traits/MylogTrait.php index 5e1c34b..7bbe881 100644 --- a/app/Traits/MylogTrait.php +++ b/app/Traits/MylogTrait.php @@ -13,21 +13,21 @@ trait MylogTrait switch ($action) { case 'create': foreach ($formDatas as $field => $value) { - MyLogService::add("info", "{$field}:{$entity->$field}"); + MyLogService::info("{$field}:{$entity->$field}"); } - MyLogService::add("info", "{$entity->getTitle()}를 생성하였습니다."); + MyLogService::log("{$entity->getTitle()}를 생성하였습니다."); break; case 'modify': foreach ($formDatas as $field => $value) { - MyLogService::add("info", "{$field}:{$entity->$field}=>{$value}"); + MyLogService::log("{$field}:{$entity->$field}=>{$value}"); } - MyLogService::add("info", "{$entity->getTitle()}를 수정하였습니다."); + MyLogService::log("{$entity->getTitle()}를 수정하였습니다."); break; case 'delete': foreach ($this->getModel()->getFields() as $field) { - MyLogService::add("info", "{$field}:{$entity->$field}"); + MyLogService::log("{$field}:{$entity->$field}"); } - MyLogService::add("info", "{$entity->getTitle()}를 삭제하였습니다."); + MyLogService::log("{$entity->getTitle()}를 삭제하였습니다."); break; } }