shoppingmallv2/app/Controllers/Admin/ProductController.php
2023-08-23 18:58:51 +09:00

136 lines
5.1 KiB
PHP

<?php
namespace App\Controllers\Admin;
use App\Controllers\Trait\UpDownloadTrait;
use App\Models\ProductModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class ProductController extends AdminController
{
use UpDownloadTrait;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->_model = new ProductModel();
$this->_viewDatas['className'] = 'Product';
$this->_viewPath .= strtolower($this->_viewDatas['className']);
$this->_viewDatas['title'] = lang($this->_viewDatas['className'] . '.title');
$this->_viewDatas['class_icon'] = CLASS_ICONS[strtoupper($this->_viewDatas['className'])];
helper($this->_viewDatas['className']);
}
public function getFields(string $action = ""): array
{
$fields = ["category_uid", 'type', 'name', "photo", "cost", "sale", "stock", "view_cnt", "status", "content",];
switch ($action) {
case "index":
case "excel":
return ["category_uid", "user_uid", 'type', 'name', "cost", "sale", "price", "stock", "view_cnt", "status", "created_at"];
break;
case "view":
return [...$fields, "created_at"];
break;
default:
return $fields;
break;
}
}
public function getFieldFilters(): array
{
return ["category_uid", "user_uid", 'type', "status"];
}
public function getFieldBatchFilters(): array
{
return parent::getFieldBatchFilters();
}
//Field별 Form Datas 처리용
protected function getFieldFormData(string $field, $entity = null): array
{
switch ($field) {
case 'photo':
$file = $this->upload_image_procedure($field);
if (!is_null($file)) {
$this->_viewDatas['fieldDatas'][$field] = $file;
}
break;
default:
return parent::getFieldFormData($field, $entity);
break;
}
return $this->_viewDatas['fieldDatas'];
}
private function calculate_price(): int
{
if ($this->_viewDatas['fieldDatas']['cost'] < $this->_viewDatas['fieldDatas']['sale']) {
throw new \Exception(sprintf(
"%s가[%s] %s[%s]보다 작습니다.",
lang($this->_viewDatas['className'] . '.label.cost'),
number_format($this->_viewDatas['fieldDatas']['cost']),
lang($this->_viewDatas['className'] . '.label.sale'),
number_format($this->_viewDatas['fieldDatas']['sale']),
));
}
return $this->_viewDatas['fieldDatas']['cost'] - $this->_viewDatas['fieldDatas']['sale'];
}
//Insert관련
protected function insert_validate()
{
//Upload된 파일 검증(photo)때문에 먼처 체크
$this->_validation->setRules($this->_viewDatas['fieldRules']);
if (!$this->_validation->withRequest($this->request)->run()) {
throw new \Exception("{$this->_viewDatas['title']}의 검증 오류발생\n" . implode("\n", $this->_validation->getErrors()));
}
//fieldData 적용
$this->_viewDatas['fieldDatas'] = array();
foreach ($this->_viewDatas['fields'] as $field) {
$this->_viewDatas['fieldDatas'] = $this->getFieldFormData($field);
}
}
protected function insert_process()
{
$this->_viewDatas['fieldDatas']['price'] = $this->calculate_price();
return parent::insert_process();
}
//Update관련
protected function update_validate($entity)
{
//Upload된 파일 검증(photo)때문에 먼처 체크
$this->_validation->setRules($this->_viewDatas['fieldRules']);
if (!$this->_validation->withRequest($this->request)->run()) {
throw new \Exception("{$this->_viewDatas['title']}의 검증 오류발생\n" . implode("\n", $this->_validation->getErrors()));
}
//fieldData 적용
$this->_viewDatas['fieldDatas'] = array();
foreach ($this->_viewDatas['fields'] as $field) {
$this->_viewDatas['fieldDatas'] = $this->getFieldFormData($field, $entity);
}
//변견된 데이터 Log로 남기기
foreach ($this->_viewDatas['fieldDatas'] as $field => $value) {
if ($field != "passwd") { //보안위험성이 있으므로 passwd는 Log에 남기지 않는다.
log_message(
"info",
sprintf(
"{$field} 변경: ---원본--\n%s\n---변경---\n%s",
$entity->$field,
var_export($value, true)
)
);
}
}
}
protected function update_process($entity)
{
$this->_viewDatas['fieldDatas']['price'] = $this->calculate_price();
return parent::update_process($entity);
}
}