From e0f7632966d765518a4f99c56c4a05c032ec5946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Fri, 21 Jul 2023 21:03:13 +0900 Subject: [PATCH] servermgrv2 init... --- app/Config/Constants.php | 6 +- app/Controllers/Admin/BoardController.php | 95 ++++++++++++++++----- app/Controllers/Admin/UserSNSController.php | 30 +++---- app/Controllers/BaseController.php | 95 ++++++++++++++++----- app/Database/board.sql | 15 +++- app/Helpers/Admin/Board_helper.php | 3 + app/Language/en/Admin/Board.php | 1 + app/Models/CommonModel.php | 30 +++++-- app/Views/admin/board/insert.php | 2 +- app/Views/admin/board/reply.php | 2 +- app/Views/admin/board/update.php | 2 +- 11 files changed, 210 insertions(+), 71 deletions(-) diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 8396840..f2d0842 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -158,9 +158,9 @@ define('AUTH_ADAPTERS', [ ]); //등급 관련 define('ROLES', [ - 'CUSTOMERS' => ['user' => '일반회원', 'vip' => 'VIP회원'], - 'SELLERS' => ['bronze' => '일반판매자', 'silver' => '고급판매자', 'gold' => '파워리셀러'], - 'OPERATORS' => ['manager' => '관리자', 'cloudflare' => "Cloudflare관리자", 'director' => '감독자', 'master' => "마스터"], + 'user' => '일반회원', 'vip' => 'VIP회원', + 'bronze' => '일반판매자', 'silver' => '고급판매자', 'gold' => '파워리셀러', + 'manager' => '관리자', 'cloudflare' => "Cloudflare관리자", 'director' => '감독자', 'master' => "마스터", ]); define('STATUS', ["use" => "사용", "unuse" => "사용않함",]); diff --git a/app/Controllers/Admin/BoardController.php b/app/Controllers/Admin/BoardController.php index ecba48a..63d48b7 100644 --- a/app/Controllers/Admin/BoardController.php +++ b/app/Controllers/Admin/BoardController.php @@ -7,6 +7,7 @@ use App\Models\BoardModel; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; +use App\Libraries\Log\Log; class BoardController extends \App\Controllers\Admin\AdminController { @@ -19,30 +20,32 @@ class BoardController extends \App\Controllers\Admin\AdminController $this->_model = new BoardModel(); $this->_defines = [ 'insert' => [ - 'fields' => ['board_category', 'title', 'content', 'passwd', 'confirmpassword', 'status'], + 'fields' => ['board_category', 'title', 'content', 'passwd', 'confirmpassword', 'upload_file', 'status'], 'fieldFilters' => ['board_category', 'user_uid', 'status'], 'fieldRules' => [ - 'board_category' => 'required|string', - 'title' => 'required|string', - 'content' => 'required|string', - 'passwd' => 'if_exist|trim|string', - 'confirmpassword' => 'if_exist|trim|matches[passwd]', - 'view_cnt' => 'if_exist|numeric', - 'status' => 'if_exist|string', + 'board_category' => 'required|string', + 'title' => 'required|string', + 'content' => 'required|string', + 'passwd' => 'if_exist|trim|string', + 'confirmpassword' => 'if_exist|trim|matches[passwd]', + 'view_cnt' => 'if_exist|numeric', + 'status' => 'if_exist|string', + 'upload_file' => 'if_exist|uploaded[upload_file]|is_image[upload_file]|mime_in[upload_file,image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[upload_file,100]|max_dims[upload_file,1024,768]' ] ], 'update' => [ - 'fields' => ['board_category', 'title', 'content', 'passwd', 'confirmpassword', 'status'], + 'fields' => ['board_category', 'title', 'content', 'passwd', 'confirmpassword', 'upload_file', 'status'], 'fieldFilters' => ['board_category', 'user_uid', 'status'], 'fieldRules' => [ - 'board_category' => 'required|string', - 'title' => 'required|string', - 'content' => 'required|string', - 'passwd' => 'if_exist|trim|string', - 'confirmpassword' => 'if_exist|trim|matches[passwd]', - 'view_cnt' => 'if_exist|numeric', - 'status' => 'if_exist|string', - ] + 'board_category' => 'required|string', + 'title' => 'required|string', + 'content' => 'required|string', + 'passwd' => 'if_exist|trim|string', + 'confirmpassword' => 'if_exist|trim|matches[passwd]', + 'view_cnt' => 'if_exist|numeric', + 'status' => 'if_exist|string', + 'upload_file' => 'if_exist|uploaded[upload_file]|is_image[upload_file]|mime_in[upload_file,image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[upload_file,100]|max_dims[upload_file,1024,768]', + ], ], 'view' => [ 'fields' => ['board_category', 'user_uid', 'title', 'view_cnt', 'status', 'updated_at', 'created_at', 'content'], @@ -63,6 +66,29 @@ class BoardController extends \App\Controllers\Admin\AdminController $this->_viewPath = strtolower($this->_className); $this->_viewDatas['title'] = lang($this->_className . '.title'); $this->_viewDatas['className'] = $this->_className; + //게시판 초기화 + $this->initConfig(); + } + + private function initConfig() + { + foreach ((array)lang($this->_className . '.BOARD_CATEGORY') as $key => $label) { + $this->_viewDatas['board_configs'][$key] = [ + 'isHierarchy' => getenv("board.{$key}.hierarchy") ?: false, + 'isAccess' => getenv("board.{$key}.access") ?: true, + 'isAccessRoles' => getenv("board.{$key}.access.roles") ?: array_values(ROLES), + 'isRead' => getenv("board.{$key}.read") ?: true, + 'isReadRoles' => getenv("board.{$key}.read.roles") ?: array_values(ROLES), + 'isWrite' => getenv("board.{$key}.write") ?: false, + 'isWriteRoles' => getenv("board.{$key}.write.roles") ?: array_values(ROLES), + 'isReply' => getenv("board.{$key}.reply") ?: false, + 'isReplyRoles' => getenv("board.{$key}.reply.roles") ?: array_values(ROLES), + 'isUpload' => getenv("board.{$key}.upload") ?: false, + 'isUploadRoles' => getenv("board.{$key}.upload.roles") ?: array_values(ROLES), + ]; + } + // echo var_export($this->_viewDatas['board_configs'], true); + // exit; } private function getUserModel(): UserModel @@ -89,8 +115,26 @@ class BoardController extends \App\Controllers\Admin\AdminController } } + //Field별 Form Datas 처리용 + protected function getFieldFormData(string $field, $entity = null) + { + switch ($field) { + case 'upload_file': + $this->_viewDatas['fieldDatas'][$field] = $this->single_upload_procedure($field, $entity); + break; + default: + return parent::getFieldFormData($field, $entity); + break; + } + } + ////Action 모음 //Insert관련 + protected function insert_process() + { + // upload처리 + return parent::insert_process(); + } final public function insert() { return $this->insert_procedure(); @@ -100,6 +144,11 @@ class BoardController extends \App\Controllers\Admin\AdminController { return $this->update_procedure($uid); } + //Reply 관련 + final public function reply(int $uid) + { + return $this->reply_procedure($uid); + } //Toggle관련 final public function toggle($uid, string $field) { @@ -116,6 +165,12 @@ class BoardController extends \App\Controllers\Admin\AdminController return $this->delete_procedure($uid); } //View 관련 + protected function view_process($entity) + { + // view_cnt에 추가하기위함 + $this->_model->increaseViewCount($entity->getPrimaryKey()); + return parent::view_process($entity); + } final public function view($uid) { return $this->view_procedure($uid); @@ -130,10 +185,4 @@ class BoardController extends \App\Controllers\Admin\AdminController { return $this->excel_procedure(); } - //추가기능 - //Reply 관련 - final public function reply(int $uid) - { - return $this->reply_procedure($uid); - } } diff --git a/app/Controllers/Admin/UserSNSController.php b/app/Controllers/Admin/UserSNSController.php index a1305b4..d94ab51 100644 --- a/app/Controllers/Admin/UserSNSController.php +++ b/app/Controllers/Admin/UserSNSController.php @@ -65,26 +65,26 @@ class UserSNSController extends \App\Controllers\Admin\AdminController } ////Action 모음 - //Insert관련 - // final public function insert() - // { - // return $this->insert_procedure(); - // } - // //Update관련 - // final public function update($uid) - // { - // return $this->update_procedure($uid); - // } + // Insert관련 + final public function insert() + { + return $this->insert_procedure(); + } + //Update관련 + final public function update($uid) + { + return $this->update_procedure($uid); + } //Toggle관련 final public function toggle($uid, string $field) { return $this->toggle_procedure($uid, $field); } - // //Batchjob 관련 - // final public function batchjob() - // { - // return $this->batchjob_procedure(); - // } + //Batchjob 관련 + final public function batchjob() + { + return $this->batchjob_procedure(); + } //Delete 관련 final public function delete($uid) { diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index f7af559..dc05846 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -79,14 +79,14 @@ abstract class BaseController extends Controller { switch ($field) { default: - $temps = lang($this->_className . '.' . strtoupper($field)); - if (!is_array($temps)) { - throw new \Exception(__FUNCTION__ . "에서 {$field}의 데이터가 array가 아닙니다.\n" . var_export($temps, true)); + $fieldFormOptionDatas = (array)lang($this->_className . '.' . strtoupper($field)); + if (!is_array($fieldFormOptionDatas)) { + throw new \Exception(__FUNCTION__ . "에서 {$field}의 데이터가 array가 아닙니다.\n" . var_export($fieldFormOptionDatas, true)); } - return array_merge( - [DEFAULTS['EMPTY'] => lang($this->_className . '.label.' . $field) . ' 선택'], - lang($this->_className . '.' . strtoupper($field)) - ); + return [ + DEFAULTS['EMPTY'] => lang($this->_className . '.label.' . $field) . ' 선택', + ...$fieldFormOptionDatas + ]; break; } } @@ -117,6 +117,73 @@ abstract class BaseController extends Controller return $tempRules; } + //Field별 Form Datas 처리용 + protected function getFieldFormData(string $field, $entity = null) + { + switch ($field) { + case 'passwd': + //암호는 보안상 log에 남지 않기 + $this->_viewDatas['fieldDatas'][$field] = $this->request->getVar($field); + break; + default: + $this->_viewDatas['fieldDatas'][$field] = $this->request->getVar($field); + if (is_null($entity)) { + Log::add("info", "{$field} : {$this->_viewDatas['fieldDatas'][$field]}"); + } else { + Log::add( + "info", + "{$field} : {$entity->$field} => {$this->_viewDatas['fieldDatas'][$field]}" + ); + } + break; + } + } + + //Upload FIle관련 + protected function upload_file_process($upfile) + { + $fileName = ""; + if ($upfile->isValid() && !$upfile->hasMoved()) { + $fileName = $upfile->getRandomName(); + $upfile->move(PATHS['UPLOAD'], $fileName); + //move시 중복된파일명이 있다면 파일명이 바뀌므로 여기서 한번더 파일명 확인 필요 + $fileName = $upfile->getRandomName(); + } + return $fileName; + } + protected function single_upload_procedure(string $field, $entity = null) + { + $upfile = $this->request->getFile($field); + $fileName = $this->upload_file_process($upfile); + // $fileDatas=array(); + // if ($upfile->isValid() && !$upfile->hasMoved()) { + // $filepath = PATHS['UPLOAD'] . $upfile->store(); + // $fileDatas = [ + // 'uploaded_fileinfo' => new \CodeIgniter\Files\File($filepath) + // ]; + // return $fileDatas; + // } + return $fileName; + } + protected function multiple_upload_procedure(string $field, $entity = null): array + { + //Multiple파일의경우 html에서는 필드명[]를 넣어야하며 + //rule에서 "uploaded[필드명.0]|is_image[필드명]~~" 이런식으로 넣어야함 + $fileNames = array(); + if ($upfiles = $this->request->getFiles()) { + foreach ($upfiles[$field] as $upfile) { + if ($upfile->isValid() && !$upfile->hasMoved()) { + $fileName = $this->upload_file_process($upfile); + array_push( + $this->_viewDatas['fieldDatas'][$field], + $fileName + ); + } + } + } + return $fileNames; + } + //Insert관련 protected function insert_init() { @@ -150,8 +217,7 @@ abstract class BaseController extends Controller //변경된 값 적용 $this->_viewDatas['fieldDatas'] = array(); foreach ($this->_viewDatas['fields'] as $field) { - $this->_viewDatas['fieldDatas'][$field] = rtrim($this->request->getVar($field)); - Log::add("info", "{$field} : {$this->_viewDatas['fieldDatas'][$field]}"); + $this->getFieldFormData($field); } //변경할 값 확인 if (!$this->validate($this->_viewDatas['fieldRules'])) { @@ -217,16 +283,7 @@ abstract class BaseController extends Controller //변경된 값 적용 $this->_viewDatas['fieldDatas'] = array(); foreach ($this->_viewDatas['fields'] as $field) { - $this->_viewDatas['fieldDatas'][$field] = rtrim($this->request->getVar($field)); - if ($entity->$field != $this->_viewDatas['fieldDatas'][$field]) { - // 기존값을 DB에서 수정전까지 유지하기위해서 - // $entity->$field = $this->_viewDatas['fieldDatas'][$field]; - //암호는 보안상 log에 남지 않게하기 위함 - Log::add( - $field == 'passwd' ? "debug" : "info", - "{$field} : {$entity->$field} => {$this->_viewDatas['fieldDatas'][$field]}" - ); - } + $this->getFieldFormData($field, $entity); } //변경할 값 확인 if (!$this->validate($this->_viewDatas['fieldRules'])) { diff --git a/app/Database/board.sql b/app/Database/board.sql index d35f13e..08247ae 100644 --- a/app/Database/board.sql +++ b/app/Database/board.sql @@ -21,4 +21,17 @@ CREATE TABLE tw_board ( created_at timestamp NOT NULL DEFAULT current_timestamp(), PRIMARY KEY (uid), CONSTRAINT FOREIGN KEY (user_uid) REFERENCES tw_user (uid) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT ='게시판 정보'; \ No newline at end of file +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT ='게시판 정보'; + +CREATE TABLE tw_board_file ( + uid int(10) unsigned NOT NULL AUTO_INCREMENT, + tw_board_uid int(10) unsigned NOT NULL COMMENT '게시판 정보', + mime_type varchar(50) NOT NULL COMMENT 'Mime_Type', + name varchar(255) NOT NULL COMMENT '파일명', + real_name varchar(255) NOT NULL COMMENT '실제파일명', + status varchar(10) NOT NULL DEFAULT 'use' COMMENT 'use: 사용, unuse: 사용않함 등등', + updated_at timestamp NULL DEFAULT NULL, + created_at timestamp NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (uid), + CONSTRAINT FOREIGN KEY (tw_board_uid) REFERENCES tw_board (uid) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT ='게시판 File정보'; \ No newline at end of file diff --git a/app/Helpers/Admin/Board_helper.php b/app/Helpers/Admin/Board_helper.php index 0d1ff6d..8ac46c6 100644 --- a/app/Helpers/Admin/Board_helper.php +++ b/app/Helpers/Admin/Board_helper.php @@ -32,6 +32,9 @@ function getFieldForm_BoardHelper($field, $value, array $formOptions, array $att case 'content': return form_textarea($field, html_entity_decode($value), [...$attributes, 'class' => 'editor', 'rows' => '20', 'cols' => '100']); break; + case 'upload_file': + return form_upload($field); + break; default: return form_input($field, $value, [...$attributes, 'size' => '80']); break; diff --git a/app/Language/en/Admin/Board.php b/app/Language/en/Admin/Board.php index 531f6b7..c86f4ae 100644 --- a/app/Language/en/Admin/Board.php +++ b/app/Language/en/Admin/Board.php @@ -13,6 +13,7 @@ return [ 'passwd' => "암호", 'confirmpassword' => "암호확인", 'view_cnt' => "조회수", + 'upload_file' => "UploadFile", 'status' => "상태", 'updated_at' => "수정일", 'created_at' => "작성일" diff --git a/app/Models/CommonModel.php b/app/Models/CommonModel.php index 7f162db..7a010de 100644 --- a/app/Models/CommonModel.php +++ b/app/Models/CommonModel.php @@ -77,13 +77,21 @@ abstract class CommonModel extends Model //계층형구조구현 final protected function setHierarchyCreate($entity) { + //자기자신이 최상위가 되게 만들기위함 $entity->grpno = $entity->getPrimaryKey(); + // echo var_export($entity, true); + // exit; + //값변경후 다시 저장 + if (!$this->save($entity)) { + Log::add("error", __FUNCTION__ . "에서 호출:" . $this->getLastQuery()); + Log::add("error", implode("\n", $this->errors())); + throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->errors(), true)); + } return $entity; } - //부모의 그룹과 grpno가 같고, 부모의 grporder보다 1 큰것을 grporder+1을 해서 update - private function setHierarchyUpdate($entity) + final protected function setHierarchyReply($entity, $replyEntity) { - // return false; + //부모의 그룹과 grpno가 같고, 부모의 grporder보다 1 큰것을 grporder+1을 해서 update //escape -> false옵션 반드시 있어야함 $this->builder()->set('grporder', 'grporder+1', false); $this->builder()->where([ @@ -93,10 +101,7 @@ abstract class CommonModel extends Model $this->builder()->update(); // echo $this->getLastQuery(); // exit; - } - final protected function setHierarchyReply($entity, $replyEntity) - { - $this->setHierarchyUpdate($entity); + //reply용 설정 $replyEntity->grpno = $entity->grpno; $replyEntity->grporder = $entity->grporder + 1; @@ -161,6 +166,17 @@ abstract class CommonModel extends Model return $entity; } + //View관련 (게시판등의 조회수 증가함수) + final public function increaseViewCount($uid, $field = 'view_cnt', int $cnt = 1) + { + //escape -> false옵션 반드시 있어야함 + $this->builder()->set($field, "{$field}+{$cnt}", false); + $this->builder()->where($this->primaryKey, $uid); + $this->builder()->update(); + // echo $this->getLastQuery(); + // exit; + } + //Index관련 public function setIndexWordFilter(string $word) { diff --git a/app/Views/admin/board/insert.php b/app/Views/admin/board/insert.php index 537bb58..13660d6 100644 --- a/app/Views/admin/board/insert.php +++ b/app/Views/admin/board/insert.php @@ -1,7 +1,7 @@ extend('layouts/admin') ?> section('content') ?> include('templates/admin/header'); ?> - + diff --git a/app/Views/admin/board/reply.php b/app/Views/admin/board/reply.php index f187aee..1b1937a 100644 --- a/app/Views/admin/board/reply.php +++ b/app/Views/admin/board/reply.php @@ -1,7 +1,7 @@ extend('layouts/admin') ?> section('content') ?> include('templates/admin/header'); ?> - +
diff --git a/app/Views/admin/board/update.php b/app/Views/admin/board/update.php index f187aee..1b1937a 100644 --- a/app/Views/admin/board/update.php +++ b/app/Views/admin/board/update.php @@ -1,7 +1,7 @@ extend('layouts/admin') ?> section('content') ?> include('templates/admin/header'); ?> - +