Automation init...1

This commit is contained in:
최준흠 2024-09-06 01:10:56 +09:00
parent 3b912875c3
commit 305c5fd460
5 changed files with 189 additions and 243 deletions

View File

@ -23,6 +23,7 @@ class FreeboardLibrary extends MangboardLibrary
public function create(FreeboardEntity $entity, array $formDatas = []): FreeboardEntity
{
$this->getModel()->setCreateField(); //Create용 Field,Rule지정
$entity = $this->getModel()->create($entity, $formDatas);
log_message("debug", __FUNCTION__ . "=>등록이 완료되었습니다.");
return $entity;

View File

@ -5,24 +5,6 @@ namespace App\Libraries\Mangboard;
use App\Entities\Mangboard\UserEntity;
use App\Models\Mangboard\UserModel;
//MANGBOARD_USER_USER 정의
// define('MANGBOARD_USER', [
// 'point' => ['unit' => 1000],
// 'admin' => ['level' => 10],
// 'manager' => [
// 'level' => [
// 'min' => 6,
// 'max' => 9,
// ]
// ],
// 'user' => [
// 'level' => [
// 'min' => 1,
// 'max' => 5,
// ]
// ],
// ]);
class UserLibrary extends MangboardLibrary
{
@ -64,7 +46,14 @@ class UserLibrary extends MangboardLibrary
}
// echo "point:" . $entity->getPoint() . ",level:" . $level . "\n";
return $this->getModel()->setLevel($entity, $level);
//기존정보와 Level값이 다르면 저장
if ($entity->getLevel() != $level) {
$old_level = $entity->getLevel();
$entity->setLevel($level);
$entity = $this->getModel()->modify($entity);
log_message("notice", __FUNCTION__ . "=>{$entity}의 Level이 {$old_level}에서 {$entity->getLevel()}로 변경되었습니다.");
}
return $entity;
}
public function setPoint($id, int $point, $sign = '+'): UserEntity
@ -88,37 +77,16 @@ class UserLibrary extends MangboardLibrary
throw new \Exception(__FUNCTION__ . "에서는 {$sign}은 사용할수 없습니다.\n");
// break;
}
$entity = $this->getModel()->setPoint($entity, $point);
//기존정보와 Point값이 다르면 저장
if ($entity->getPoint() != $point) {
$old_point = $entity->getPoint();
$entity->setPoint($point);
$entity = $this->getModel()->modify($entity);
log_message("notice", __FUNCTION__ . "=>{$entity}의 Point가 {$old_point}에서 {$entity->getPoint()}로 변경되었습니다.");
}
return $this->checkLevel($entity);
}
// private function checkLevel(UserEntity $entity): UserEntity
// {
// //Admin용 Level로는 변경불가
// if ($entity->getLevel() == MANGBOARD_USER['admin']['level']) {
// // throw new \Exception("Admin용 Level을 변경하실수 없습니다.\n");
// return $entity;
// }
// //사용자 Point별 Level 계산
// $level = intval($entity->getPoint() / MANGBOARD_USER['point']['unit'] * MANGBOARD_USER['point']['unit'] / MANGBOARD_USER['point']['unit']);
// //운영자면 7~9
// if (MANGBOARD_USER['manager']['level']['min'] <= $level && $level <= MANGBOARD_USER['manager']['level']['max']) {
// $level = $level < MANGBOARD_USER['manager']['level']['min'] ? MANGBOARD_USER['manager']['level']['min'] : $level;
// $level = MANGBOARD_USER['manager']['level']['max'] < $level ? MANGBOARD_USER['manager']['level']['max'] : $level;
// }
// // echo "point:" . $entity->getPoint() . ",level:" . $level . "\n";
// //사용자 Level 1~5;
// if (MANGBOARD_USER['user']['level']['min'] <= $level && $level <= MANGBOARD_USER['user']['level']['max']) {
// $level = $level < MANGBOARD_USER['user']['level']['min'] ? MANGBOARD_USER['user']['level']['min'] : $level;
// $level = MANGBOARD_USER['user']['level']['max'] < $level ? MANGBOARD_USER['user']['level']['max'] : $level;
// }
// // echo "point:" . $entity->getPoint() . ",level:" . $level . "\n";
// return $this->getModel()->setLevel($entity, $level);
// }
public function setLevel()
{
foreach ($this->getModel()->getEntitys() as $entity) {

View File

@ -45,30 +45,97 @@ abstract class CommonModel extends Model
protected $beforeDelete = [];
protected $afterDelete = [];
protected function __construct() {}
protected function __construct()
{
parent::__construct();
}
abstract public function getPK(): string;
abstract public function getTitleField(): string;
abstract public function setCreateField();
abstract public function setModifyField();
final public function getFields(): array
{
return $this->allowedFields;
}
final public function setFields(array $fields): void
{
$this->allowedFields = $fields;
}
final public function getRules(array $options): array
{
return $this->getValidationRules($options); //options=>except or only
}
final public function setRules(array $fields, $rules = []): void
{
foreach ($fields as $field) {
$rules = $this->getFieldRule($field, $rules);
}
$this->setValidationRules($rules);
}
final public function getUUID(): string
{
$randomBytes = bin2hex(random_bytes(32));
return sprintf(
'%08s-%04s-%04x-%04x-%12s',
substr($randomBytes, 0, 8),
substr($randomBytes, 8, 4),
substr($randomBytes, 12, 4),
substr($randomBytes, 16, 4),
substr($randomBytes, 20, 12)
);
}
protected function getUUIDFieldRule($condition = 'required'): string
{
return "{$condition}|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
}
protected function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case $this->getPK():
//수동입력인경우
$rules[$field] = !$this->useAutoIncrement ? $this->getUUIDFieldRule() . "|is_unique[{$this->table}.{$field}]" : "required|numeric";
break;
case $this->getTitleField():
$rules[$field] = "required|trim|string";
break;
case "passwd":
$rules[$field] = "required|trim|string";
break;
case "confirmpassword":
$rules["confirmpassword"] = "required|trim|string|matches[passwd]";
break;
case "email":
$rules[$field] = "if_exist|trim|valid_email";
break;
case 'image':
$rules[$field] = "is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},300]|max_dims[{$field},2048,768]";
break;
case "updated_at":
case "created_at":
case "deleted_at":
$rules[$field] = "if_exist|valid_date";
break;
default:
$rules[$field] = "if_exist|string";
break;
}
return $rules;
}
final public function getEntity()
{
return $this->asObject($this->returnType)->first();
}
final protected function setEntity($entity)
{
if ($entity->hasChanged()) {
if (!$this->save($entity)) {
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->errors(), true));
}
}
return $entity;;
}
final public function getEntitys(): array
{
return $this->asObject($this->returnType)->findAll();
}
final protected function save_process($entity)
private function save_process($entity)
{
// echo var_export($entity, true);
// exit;
@ -85,19 +152,9 @@ abstract class CommonModel extends Model
}
//create , modify 직전 작업용 작업
protected function changeFormData(string $action, string $field, array $formDatas, $entity)
protected function changeFormData(string $field, array $formDatas, $entity)
{
switch ($field) {
// case "user_uid": //입력데이터로 있을시 관리툴에서 (사용자,등)추가, 없을시는 입력의 경우에만 자동(장바구니,등)으로 추가
// if (array_key_exists($field, $formDatas) && !is_null($formDatas[$field])) {
// //관리툴 USERSNS에서 사용자 연동 시 추가기능등에 사용
// $entity->$field = $formDatas[$field];
// } elseif ($action == 'create' && $this->_session->get(SESSION_NAMES["ISLOGIN"])) {
// //Front에서 장바구니,게시판등에 추가시 로그온한경우 자동 추가기능등에 사용
// $auth = $this->_session->get(SESSION_NAMES["AUTH"]);
// $entity->$field = $auth[AUTH_FIELDS["ID"]];
// }
// break;
case "passwd":
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
$entity->$field = password_hash($formDatas[$field], PASSWORD_DEFAULT);
@ -117,24 +174,23 @@ abstract class CommonModel extends Model
return $entity;
}
protected function create_process($entity, array $formDatas)
protected function create_process($entity, array $formDatas = [])
{
foreach ($this->allowedFields as $field) {
$entity = $this->changeFormData('create', $field, $formDatas, $entity);
foreach ($this->getFields() as $field) {
$entity = $this->changeFormData($field, $formDatas, $entity);
}
$entity = $this->save_process($entity);
//primaryKey가 자동입력이면
if ($this->useAutoIncrement) {
$pk = $this->getPK();
$entity->$pk = $this->insertID();
$entity->$pk = $this->getInsertID();
}
return $entity;
}
protected function modify_process($entity, array $formDatas)
protected function modify_process($entity, array $formDatas = [])
{
$entity->updated_at = time(); //수정한 시간정의
foreach ($this->allowedFields as $field) {
$entity = $this->changeFormData('modify', $field, $formDatas, $entity);
foreach ($this->getFields() as $field) {
$entity = $this->changeFormData($field, $formDatas, $entity);
}
return $this->save_process($entity);
}

View File

@ -9,132 +9,8 @@ class FreeboardModel extends CommonModel
{
protected $table = 'mb_board_free';
protected $primaryKey = 'pid';
protected $useAutoIncrement = true;
protected $allowedFields = [
// // 'pid',
// // 'gid',
// 'reply',
// 'depth',
// 'user_id',
// 'user_name',
'title',
// 'passwd',
// 'homepage',
// 'email',
// 'address',
// 'phone',
// 'reg_date',
// 'modify_date',
// 'calendar_date',
// 'hit',
// 'user_pid',
// 'parent_pid',
// 'parent_user_pid',
// 'level',
// 'file_count',
// 'comment_count',
// 'vote_good_count',
// 'vote_bad_count',
// 'vote_type',
// 'ip',
// 'agent',
// 'is_notice',
// 'is_secret',
// 'status',
// 'is_show',
// 'reply_email',
'text',
'content',
// 'content_type',
// 'data_type',
// 'editor_type',
// 'tag',
// 'category1',
// 'category2',
// 'category3',
// 'image_path',
// 'site_link1',
// 'site_link2',
// 'gps_latitude',
// 'gps_longitude',
// 'ext1',
// 'ext2',
// 'ext3',
// 'ext4',
// 'ext5',
// 'ext6',
// 'ext7',
// 'ext8',
// 'ext9',
// 'ext10',
];
protected $returnType = FreeboardEntity::class;
// Validation
// protected $validationRules = [];
protected $validationRules = [
'pid' => 'if_exist|trim|numeric',
'gid' => 'if_exist|trim|numeric',
'reply' => 'if_exist|trim|numeric',
'depth' => 'if_exist|trim|numeric',
'user_id' => 'if_exist|trim|string',
'user_name' => 'if_exist|trim|string',
'title' => 'if_exist|trim|string',
'passwd' => 'if_exist|trim|string',
'homepage' => 'if_exist|trim|string',
'email' => 'if_exist|trim|valid_email',
'address' => 'if_exist|trim|string',
'phone' => 'if_exist|trim|string',
'reg_date' => 'if_exist|valid_date[Y-m-d H:i:s]',
'modify_date' => 'if_exist|valid_date[Y-m-d h:i:s]',
'calendar_date' => 'if_exist|valid_date[Y-m-d H:i:s]',
// 'reg_date' => 'if_exist|string',
// 'modify_date' => 'if_exist|string',
// 'calendar_date' => 'if_exist|string',
'hit' => 'if_exist|trim|numeric',
'user_pid' => 'if_exist|trim|numeric',
'parent_pid' => 'if_exist|trim|numeric',
'parent_user_pid' => 'if_exist|trim|numeric',
'level' => 'if_exist|trim|numeric',
'file_count' => 'if_exist|trim|numeric',
'comment_count' => 'if_exist|trim|numeric',
'vote_good_count' => 'if_exist|trim|numeric',
'vote_bad_count' => 'if_exist|trim|numeric',
'vote_type' => 'if_exist|trim|numeric',
'ip' => 'if_exist|trim|string',
'agent' => 'if_exist|trim|string',
'is_notice' => 'if_exist|trim|numeric',
'is_secret' => 'if_exist|trim|numeric',
'status' => 'if_exist|trim|string',
'is_show' => 'if_exist|trim|numeric',
'reply_email' => 'if_exist|trim|numeric',
'text' => 'if_exist|string',
'content' => 'if_exist|string',
'content_type' => 'if_exist|trim|string',
'data_type' => 'if_exist|trim|string',
'editor_type' => 'if_exist|trim|string',
'tag' => 'if_exist|trim|string',
'category1' => 'if_exist|trim|string',
'category2' => 'if_exist|trim|string',
'category3' => 'if_exist|trim|string',
'image_path' => 'if_exist|trim|string',
'site_link1' => 'if_exist|trim|string',
'site_link2' => 'if_exist|trim|string',
'gps_latitude' => 'if_exist|trim|string',
'gps_longitude' => 'if_exist|trim|string',
'ext1' => 'if_exist|string',
'ext2' => 'if_exist|string',
'ext3' => 'if_exist|string',
'ext4' => 'if_exist|string',
'ext5' => 'if_exist|string',
'ext6' => 'if_exist|string',
'ext7' => 'if_exist|string',
'ext8' => 'if_exist|string',
'ext9' => 'if_exist|string',
'ext10' => 'if_exist|string',
// 'updated_at' => 'if_exist|valid_date',
// 'created_at' => 'if_exist|valid_date',
// 'deleted_at' => 'if_exist|valid_date',
];
public function __construct()
{
parent::__construct();
@ -144,6 +20,26 @@ class FreeboardModel extends CommonModel
{
return $this->primaryKey;
}
public function getTitleField(): string
{
return 'title';
}
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case 'gid':
$rules[$field] = "required|numeric";
break;
case "text":
case "content":
$rules[$field] = "required|trim|string";
break;
default:
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;
}
public function getEntityByPK(int $uid): null|FreeboardEntity
{
@ -156,13 +52,27 @@ class FreeboardModel extends CommonModel
return $this->getEntity();
}
//create용
public function setCreateField()
{
$this->setFields(["title", "text", 'content']);
$this->setRules($this->getFields());
}
public function create(FreeboardEntity $entity, array $formDatas = []): FreeboardEntity
{
$entity = $this->create_process($entity, $formDatas);
//GID값이 PK랑 같게하기위해
//GID값이 PK랑 같은 값 전달 후 Entity 수정
$this->setFields(["gid"]);
$this->setRules($this->getFields());
$entity->setGID($entity->getPK());
$entity = $this->modify_process($entity, $formDatas);
return $entity;
return $this->modify_process($entity, []);
}
//modify용
public function setModifyField()
{
$this->setFields(["title", "text", 'content']);
$this->setRules($this->getFields());
}
public function modify(FreeboardEntity $entity, array $formDatas = []): FreeboardEntity
{

View File

@ -9,37 +9,45 @@ class UserModel extends CommonModel
{
protected $table = 'mb_users';
protected $primaryKey = 'pid';
protected $useAutoIncrement = true;
protected $returnType = UserEntity::class;
protected $allowedFields = ['pid', 'user_id', 'passwd', 'user_name', 'user_email', 'user_state', 'user_level', 'user_point'];
// Validation
// protected $validationRules = [];
protected $validationRules = [
'pid' => 'if_exist|numeric',
'user_id' => 'if_exist|trim|string',
'passwd' => 'if_exist|trim|string',
// 'confirmpassword' => 'if_exist|trim|matches[passwd]',
'user_name' => 'if_exist|trim|string',
'user_state' => 'if_exist|trim|string',
'user_email' => 'if_exist|trim|valid_email',
'user_level' => 'if_exist|numeric',
'user_point' => 'if_exist|numeric',
// 'proxied' => 'if_exist|in_list[on,off]',
// 'fixed' => 'if_exist|in_list[on,off]',
// 'locked' => 'if_exist|in_list[on,off]',
// 'updated_at' => 'if_exist|valid_date',
// 'created_at' => 'if_exist|valid_date',
];
public function __construct()
{
parent::__construct();
}
public function getPK(): string
{
return $this->primaryKey;
}
public function getTitleField(): string
{
return 'user_name';
}
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case "user_id":
case "passwd":
$rules[$field] = "required|trim|string";
break;
case "user_state":
$rules[$field] = "if_exist|trim|string";
break;
case "user_email":
$rules[$field] = "if_exist|trim|valid_email";
break;
case "user_level":
case "user_point":
$rules[$field] = "if_exist|numeric";
break;
default:
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;
}
public function getEntityByPK(int $uid): null|UserEntity
{
@ -52,25 +60,28 @@ class UserModel extends CommonModel
return $this->getEntity();
}
public function setPoint(UserEntity $entity, int $point): UserEntity
//create용
public function setCreateField()
{
if ($entity->getPoint() != $point) {
$old_point = $entity->getPoint();
$entity->setPoint($point);
$entity = $this->setEntity($entity);
log_message("notice", __FUNCTION__ . "=>{$entity}의 Point가 {$old_point}에서 {$entity->getPoint()}로 변경되었습니다.");
}
return $entity;
$this->setFields(["user_id", "passwd", "user_name", "user_email", "user_state", "user_level", "user_point"]);
$this->setRules($this->getFields());
}
public function create(UserEntity $entity, array $formDatas = []): UserEntity
{
$entity = $this->create_process($entity, $formDatas);
//GID값이 PK랑 같은 값 전달 후 Entity 수정
$entity->setGID($entity->getPK());
return $this->modify_process($entity);
}
final public function setLevel(UserEntity $entity, int $level): UserEntity
//modify용
public function setModifyField()
{
if ($entity->getLevel() != $level) {
$old_level = $entity->getLevel();
$entity->setLevel($level);
$entity = $this->setEntity($entity);
log_message("notice", __FUNCTION__ . "=>{$entity}의 Level이 {$old_level}에서 {$entity->getLevel()}로 변경되었습니다.");
}
return $entity;
$this->setFields(["user_id", "passwd", "user_name", "user_email", "user_state", "user_level", "user_point"]);
$this->setRules($this->getFields());
}
public function modify(UserEntity $entity, array $formDatas = []): UserEntity
{
return $this->modify_process($entity, $formDatas);
}
}