diff --git a/app/Controllers/Admin/BoardController.php b/app/Controllers/Admin/BoardController.php index a3f1b89..4e5f359 100644 --- a/app/Controllers/Admin/BoardController.php +++ b/app/Controllers/Admin/BoardController.php @@ -6,9 +6,11 @@ use App\Models\BoardModel; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; +use App\Controllers\Trait\FileTrait; class BoardController extends AdminController { + use FileTrait; public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { $this->_model = new BoardModel(); diff --git a/app/Controllers/Admin/ProductController.php b/app/Controllers/Admin/ProductController.php index 0527b85..09e914c 100644 --- a/app/Controllers/Admin/ProductController.php +++ b/app/Controllers/Admin/ProductController.php @@ -6,9 +6,11 @@ use App\Models\ProductModel; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; +use App\Controllers\Trait\FileTrait; class ProductController extends AdminController { + use FileTrait; public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { $this->_model = new ProductModel(); @@ -45,7 +47,7 @@ class ProductController extends AdminController { switch ($field) { case 'photo': - $this->_viewDatas['fieldDatas'][$field] = $this->upload_photo_procedure($field); + $this->_viewDatas['fieldDatas'][$field] = $this->upload_image_procedure($field); break; default: return parent::getFieldFormData($field, $entity); diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index 0695a87..d8f95f0 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -5,7 +5,6 @@ namespace App\Controllers; use CodeIgniter\Controller; use CodeIgniter\HTTP\CLIRequest; -use CodeIgniter\HTTP\Files\UploadedFile; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; @@ -37,7 +36,7 @@ abstract class BaseController extends Controller * * @var array */ - protected $helpers = ['Common', 'Base']; + protected $helpers = ['Common']; /** * Be sure to declare properties for any property fetch you initialized. @@ -94,58 +93,6 @@ abstract class BaseController extends Controller return $this->_viewDatas['fieldDatas']; } - //Upload FIle관련 - private function upload_file_process(UploadedFile $upfile) - { - $fileName = null; - if ($upfile->isValid() && !$upfile->hasMoved()) { - $originName = $upfile->getName(); - $upfile->move(PATHS['UPLOAD'], $upfile->getRandomName()); - //move시 중복된파일명이 있다면 파일명이 바뀌므로 여기서 한번더 파일명 확인 필요 - $fileName = $originName . DEFAULTS['DELIMITER_FILE'] . $upfile->getName(); - } - return $fileName; - } - private function upload_image_process(UploadedFile $upfile, $sizeX = 100, $sizeY = 100) - { - //참고:https://www.positronx.io/codeigniter-resize-image-with-image-manipulation-tutorial/ - $fileName = null; - if ($upfile->isValid() && !$upfile->hasMoved()) { - $originName = $upfile->getName(); - $fileName = $upfile->getRandomName(); - $image = \Config\Services::image(); - $image->withFile($upfile) - ->resize($sizeX, $sizeY, true, 'height') - ->save(PATHS['UPLOAD_PHOTO'] . $fileName); - $upfile->move(PATHS['UPLOAD_PHOTO'], "original_" . $fileName); - $fileName = $originName . DEFAULTS['DELIMITER_FILE'] . $fileName; - } - return $fileName; - } - protected function upload_file_procedure(string $field) - { - return $this->upload_file_process($this->request->getFile($field)); - } - protected function upload_photo_procedure(string $field, $sizeX = 100, $sizeY = 100) - { - return $this->upload_image_process($this->request->getFile($field), $sizeX, $sizeY); - } - protected function upload_multiple_file_process(string $field): 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; - } - //초기화 final public function init(string $action, $fields = null) { diff --git a/app/Controllers/Front/BoardController.php b/app/Controllers/Front/BoardController.php index ed503a6..8b0b7f2 100644 --- a/app/Controllers/Front/BoardController.php +++ b/app/Controllers/Front/BoardController.php @@ -17,7 +17,7 @@ class BoardController extends FrontController } public function getFields(string $action = ""): array { - $fields = ['title', "board_file", "passwd", "content"]; + $fields = ['title', "passwd", "content"]; switch ($action) { case "index": case "excel": @@ -47,9 +47,6 @@ class BoardController extends FrontController $this->_viewDatas['fieldDatas'][$field] = $this->request->getVar($field); $this->_viewDatas['fieldDatas']['confirmpassword'] = $this->request->getVar('confirmpassword'); break; - case 'board_file': - $this->_viewDatas['fieldDatas'][$field] = $this->upload_file_procedure($field); - break; default: return parent::getFieldFormData($field, $entity); break; diff --git a/app/Controllers/Trait/FileTrait.php b/app/Controllers/Trait/FileTrait.php new file mode 100644 index 0000000..c85fea7 --- /dev/null +++ b/app/Controllers/Trait/FileTrait.php @@ -0,0 +1,85 @@ +isValid() && !$upfile->hasMoved()) { + $name = $upfile->getName(); + $filename = $upfile->getRandomName(); + $upfile->move(PATHS['UPLOAD'], $filename); + //move시 중복된파일명이 있다면 파일명이 바뀌므로 여기서 한번더 파일명 확인 필요 + $filename = $upfile->getName(); + } + return $name . DEFAULTS['DELIMITER_FILE'] . $filename; + } + public function upload_file_procedure(string $field): string + { + return $this->upload_file_process($this->request->getFile($field)); + } + public function upload_multiple_file_procedure(string $field): array + { + //Multiple파일의경우 html에서는 필드명[]를 넣어야하며 + //rule에서 "uploaded[필드명.0]|is_image[필드명]~~" 이런식으로 넣어야함 + $files = array(); + if ($upfiles = $this->request->getFiles()) { + foreach ($upfiles[$field] as $upfile) { + if ($upfile->isValid() && !$upfile->hasMoved()) { + array_push($files, $this->upload_file_process($upfile)); + } + } + } + return $files; + } + + private function image_resize_process(UploadedFile $upfile, $filename, $x = 25, $y = 25) + { + $image = \Config\Services::image(); + $image->withFile($upfile) + ->resize($x, $y, true, 'height') + ->save(PATHS['UPLOAD_PHOTO'] . $filename); + } + private function upload_image_process(UploadedFile $upfile): string + { + //참고:https://www.positronx.io/codeigniter-resize-image-with-image-manipulation-tutorial/ + $name = null; + $filename = null; + if ($upfile->isValid() && !$upfile->hasMoved()) { + $name = $upfile->getName(); + $filename = $upfile->getRandomName(); + $this->image_resize_process($upfile, "small_" . $filename); + $this->image_resize_process($upfile, "middle_" . $filename, 50, 50); + $this->image_resize_process($upfile, "large_" . $filename, 100, 100); + $upfile->move(PATHS['UPLOAD_PHOTO'], $filename); + //move시 중복된파일명이 있다면 파일명이 바뀌므로 여기서 한번더 파일명 확인 필요 + $filename = $upfile->getName(); + } + return $name . DEFAULTS['DELIMITER_FILE'] . $filename; + } + + public function upload_image_procedure(string $field): string + { + return $this->upload_image_process($this->request->getFile($field)); + } + public function upload_multiple_image_procedure(string $field): array + { + //Multiple파일의경우 html에서는 필드명[]를 넣어야하며 + //rule에서 "uploaded[필드명.0]|is_image[필드명]~~" 이런식으로 넣어야함 + $names = array(); + if ($upfiles = $this->request->getFiles()) { + foreach ($upfiles[$field] as $upfile) { + if ($upfile->isValid() && !$upfile->hasMoved()) { + array_push($files, $this->upload_image_process($upfile)); + } + } + } + return $names; + } +} diff --git a/app/Database/shoppingmall.sql b/app/Database/shoppingmall.sql index 5af66c1..3aaf112 100644 --- a/app/Database/shoppingmall.sql +++ b/app/Database/shoppingmall.sql @@ -99,7 +99,7 @@ CREATE TABLE shoppingmall.tw_product ( category_uid int(10) UNSIGNED NOT NULL COMMENT '상품분류', user_uid varchar(36) NULL COMMENT '생산자 정보', name varchar(255) NOT NULL COMMENT '상품명', - photo varchar(50) NULL COMMENT '이미지', + photo varchar(255) NULL COMMENT '이미지', cost int(10) UNSIGNED NOT NULL COMMENT '원가', price int(10) UNSIGNED NOT NULL COMMENT '판매가', sale int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '할인가', diff --git a/app/Entities/BaseEntity.php b/app/Entities/BaseEntity.php index deab2a9..f3f4b05 100644 --- a/app/Entities/BaseEntity.php +++ b/app/Entities/BaseEntity.php @@ -8,5 +8,4 @@ abstract class BaseEntity extends Entity { abstract public function getPrimaryKey(); abstract public function getTitle(): string; - abstract public function getStatus(): string; } diff --git a/app/Entities/ProductEntity.php b/app/Entities/ProductEntity.php index dea4e51..b667f59 100644 --- a/app/Entities/ProductEntity.php +++ b/app/Entities/ProductEntity.php @@ -47,9 +47,9 @@ class ProductEntity extends BaseEntity { return $this->attributes['photo']; } - public function getPhotoBlock() + public function getPhotoFileName($size = 'small') { - return explode(DEFAULTS['DELIMITER_FILE'], $this->attributes['photo'])[1]; + return $size . '_' . explode(DEFAULTS['DELIMITER_FILE'], $this->attributes['photo'])[1]; } public function getContent() diff --git a/app/Helpers/Order_helper.php b/app/Helpers/Order_helper.php index d078ef0..4aed9a3 100644 --- a/app/Helpers/Order_helper.php +++ b/app/Helpers/Order_helper.php @@ -130,7 +130,7 @@ function getFieldIndex_Row_OrderHelper_Admin($field, $entity, array $viewDatas): $field, $field ); - return getFieldForm_OrderHelper($field, $entity->field, $viewDatas, $attributes); + return getFieldForm_OrderHelper($field, $entity->$field, $viewDatas, $attributes); } return getFieldView_OrderHelper($field, $entity, $viewDatas); break; diff --git a/app/Helpers/Product_helper.php b/app/Helpers/Product_helper.php index 1122c50..b6923c5 100644 --- a/app/Helpers/Product_helper.php +++ b/app/Helpers/Product_helper.php @@ -82,7 +82,8 @@ function getFieldView_ProductHelper($field, $entity, array $viewDatas) return $value == DEFAULTS['EMPTY'] ? DEFAULTS['EMPTY'] : anchor(current_url() . "/download/{$field}/{$entity->getPrimaryKey()}", ICONS['IMAGE_FILE'] . explode(DEFAULTS['DELIMITER_FILE'], $value)[0], ["target" => "_self"]); break; case 'photo': - return "getPhotoBlock() . "\">"; + // return $entity->getPhotoFileName(); + return "getPhotoFileName() . "\">"; break; case 'cost': case 'price': @@ -138,7 +139,7 @@ function getFieldIndex_Row_ProductHelper_Admin($field, $entity, array $viewDatas $field, $field ); - return getFieldForm_ProductHelper($field, $entity->field, $viewDatas, $attributes); + return getFieldForm_ProductHelper($field, $entity->$field, $viewDatas, $attributes); } return getFieldView_ProductHelper($field, $entity, $viewDatas); break; diff --git a/app/Helpers/UserSNS_helper.php b/app/Helpers/UserSNS_helper.php index 5053ccb..9a45ba5 100644 --- a/app/Helpers/UserSNS_helper.php +++ b/app/Helpers/UserSNS_helper.php @@ -133,7 +133,7 @@ function getFieldIndex_Row_UserSNSHelper_Admin($field, $entity, array $viewDatas $field, $field ); - return getFieldForm_UserSNSHelper($field, $entity->field, $viewDatas, $attributes); + return getFieldForm_UserSNSHelper($field, $entity->$field, $viewDatas, $attributes); } return getFieldView_UserSNSHelper($field, $entity, $viewDatas); break; diff --git a/app/Helpers/User_helper.php b/app/Helpers/User_helper.php index 2771da9..303adab 100644 --- a/app/Helpers/User_helper.php +++ b/app/Helpers/User_helper.php @@ -139,7 +139,7 @@ function getFieldIndex_Row_UserHelper_Admin($field, $entity, array $viewDatas): $field, $field ); - return getFieldForm_UserHelper($field, $entity->field, $viewDatas, $attributes); + return getFieldForm_UserHelper($field, $entity->$field, $viewDatas, $attributes); } return getFieldView_UserHelper($field, $entity, $viewDatas); break; diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index 4983985..586489d 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -94,9 +94,6 @@ abstract class BaseModel extends Model case "view_cnt": $rules[$field] = "if_exist|numeric"; break; - case "upload_file": //uploaded[{$field}] == requried와 같은의미 - $rules[$field] = !$action ? "if_exist|string" : "is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},100]|max_dims[{$field},1024,768]"; - break; case "updated_at": case "created_at": case "deleted_at":