134 lines
4.7 KiB
PHP
134 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\ProductEntity;
|
|
|
|
class ProductModel extends BaseModel
|
|
{
|
|
private $_category_options = null;
|
|
protected $table = "tw_product";
|
|
protected $useAutoIncrement = false;
|
|
protected $returnType = ProductEntity::class;
|
|
protected $useSoftDeletes = false;
|
|
public function __construct()
|
|
{
|
|
parent::__construct('Product');
|
|
$this->allowedFields = ["uid", "user_uid", ...$this->allowedFields, ...$this->getFields(),];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
public function getTitleField(): string
|
|
{
|
|
return 'name';
|
|
}
|
|
public function getFields(string $action = ""): array
|
|
{
|
|
$fields = ["category_uid", $this->getTitleField(), "photo", "cost", "price", "sale", "stock", "view_cnt", "status", "content",];
|
|
switch ($action) {
|
|
case "index":
|
|
case "excel":
|
|
return ["category_uid", "user_uid", $this->getTitleField(), "photo", "cost", "price", "sale", "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", "status"];
|
|
}
|
|
public function getFieldBatchFilters(): array
|
|
{
|
|
return parent::getFieldBatchFilters();
|
|
}
|
|
protected function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case "category_uid":
|
|
$rules[$field] = "required|numeric";
|
|
break;
|
|
case "user_uid":
|
|
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
|
|
break;
|
|
case $this->getTitleField():
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case 'photo':
|
|
$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 'cost':
|
|
case 'price':
|
|
$rules[$field] = "required|numeric";
|
|
break;
|
|
case 'sale':
|
|
case 'stock':
|
|
$rules[$field] = "if_exist|numeric";
|
|
break;
|
|
case "content":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
|
|
//Field별 Form Option용
|
|
public function getFieldFormOption(string $field): array
|
|
{
|
|
switch ($field) {
|
|
case 'category_uid':
|
|
if (is_null($this->_category_options)) {
|
|
$categoryModel = new CategoryModel();
|
|
$this->_category_options = $categoryModel->getFormOptions(['status' => 'use']);
|
|
}
|
|
$options = $this->_category_options;
|
|
break;
|
|
default:
|
|
return parent::getFieldFormOption($field);
|
|
break;
|
|
}
|
|
if (!is_array($options)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$this->getClassName()}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true));
|
|
}
|
|
return $options;
|
|
}
|
|
|
|
public function getEntity($conditions): ProductEntity
|
|
{
|
|
return $this->where($conditions)->first() ?: throw new \Exception("해당 데이터가 없습니다.\n" . var_export($conditions, true));
|
|
}
|
|
public function create(array $formDatas): ProductEntity
|
|
{
|
|
return $this->create_process(new ProductEntity(), $formDatas);
|
|
}
|
|
public function modify(ProductEntity $entity, array $formDatas): ProductEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
|
|
//Index관련
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike($this->getTitleField(), $word, "both");
|
|
$this->orLike("content", $word, "both"); //befor , after , both
|
|
}
|
|
|
|
//Ecommerce Action
|
|
//장바구니에 넣기
|
|
final public function addCart(ProductEntity $entity, int $quantity): ProductEntity
|
|
{
|
|
if ($entity->getStock() == $quantity) {
|
|
$entity->status = "outofstock";
|
|
}
|
|
$entity->stock -= $quantity;
|
|
return $this->save_process($entity);
|
|
}
|
|
}
|