diff --git a/.env.ini b/.env.ini new file mode 100644 index 0000000..b59efb5 --- /dev/null +++ b/.env.ini @@ -0,0 +1,4 @@ +[db] +dsn="mysql:host=localhost;dbname=wordpress;charset=UTF8" +id="wordpress" +passwd="12clqkidc@!" \ No newline at end of file diff --git a/app.php b/app.php new file mode 100644 index 0000000..ccffd09 --- /dev/null +++ b/app.php @@ -0,0 +1,33 @@ + $argc) { + throw new \Exception("사용법 : php app.php 클래스명 [함수명(default:execute) parameters]\n"); + } + //php 파일명 + $phpName = array_shift($argv); + + //클래스 검사(Autoload) + $className = CONTROLLER . array_shift($argv); + $class = new $className(); + + //함수 검사 + $functionName = array_shift($argv) ?: "execute"; + if (!method_exists($class, $functionName)) { + throw new \Exception("클래스[{$className}]에 함수[{$functionName}]는 존재하지 않습니다.\n"); + } + echo $class->$functionName($argv); +} catch (\Exception $e) { + echo $e->getMessage(); +} // \ No newline at end of file diff --git a/lib/Controller/Mangboard/BaseController.php b/lib/Controller/Mangboard/BaseController.php new file mode 100644 index 0000000..ff16cc5 --- /dev/null +++ b/lib/Controller/Mangboard/BaseController.php @@ -0,0 +1,13 @@ +_model = $this->getModel("Mangboard\\UserModel"); + } // + + final public function setPoint($params) + { + if (count($params) != 3) { + return "사용법: php app.php Mangboard\UserController setPoint 아이디 포인트 +|-\n" . var_export($params); + } + $id = $params[0]; + + $point = intval($params[1]); + if (!is_int($point)) { + return "{$params[1]}은 숫자형식이 아닙니다."; + } + $point = intval($params[1]); + switch ($params[2]) { + case '+': + case '-': + $sign = $params[2]; + break; + default: + return "형식오류 +/- 가 아닙니다"; + // break; + } + $this->_model->where("user_id", $id); + $entity = $this->_model->getEntity(); + if (!$entity) { + return "[{$id}]에 해당하는 회원은 없습니다"; + } + $old_point = $entity->getPoint(); + if (is_int($point)) { + $entity = $this->_model->setPoint($entity, $point, $sign); + } + return "[{$entity}] 회원님의 포인트는 {$old_point}->{$entity->getPoint()} 입니다."; + } // + + final public function getPoint($params) + { + if (count($params) == 1) { + $id = $params[0]; + if ($id) { + $this->_model->where("user_id", $id); + $entity = $this->_model->getEntity(); + if (!$entity) { + return "[{$id}]에 해당하는 회원은 없습니다"; + } + } + } + $msgs = []; + foreach ($this->_model->getEntitys() as $entity) { + $msgs[] = "[{$entity}] 회원님의 포인트는 {$entity->getPoint()} 입니다."; + } + return implode("\n", $msgs); + } // + + private function setLevel(UserEntity $entity, int $level) + { + $old_level = $entity->getLevel(); + $entity = $this->_model->setLevel($entity, $level); + return "[{$entity}] 회원님의 레벨 {$old_level} -> {$entity->getLevel()}로 변경되었습니다."; + } + final public function checkLevel($id = false) + { + $entitys = []; + if ($id) { + $this->_model->where("user_id", $id); + $entity = $this->_model->getEntity(); + if (!$entity) { + return "[{$id}]에 해당하는 회원은 없습니다"; + } + $entitys[] = $entity; + } else { + $entitys = $this->_model->getEntitys(); + } + $msgs = []; + foreach ($entitys as $entity) { + $level = intval($entity->getPoint() / MANGBOARD_POINT_UNIT * MANGBOARD_POINT_UNIT / MANGBOARD_POINT_UNIT); + if ($entity->getLevel() != $level) { + $msgs[] = $this->setLevel($entity, $level); + } + } + return PHP_EOL . "------" . date('Y/m/d-H:m') . "---------" . PHP_EOL . implode(PHP_EOL, $msgs) . PHP_EOL . "------------------------------" . PHP_EOL; + } // +} diff --git a/lib/Core/Controller.php b/lib/Core/Controller.php new file mode 100644 index 0000000..1b73839 --- /dev/null +++ b/lib/Core/Controller.php @@ -0,0 +1,38 @@ +_view = new View(); + } // + + final public function __get($name) + { + return array_key_exists($name, $this->_datas) ? $this->_datas[$name] : null; + } + final public function __set($name, $value) + { + $this->_datas[$name] = $value; + } + + //Model + final public function getModel($name) + { + if (!array_key_exists($name, $this->_models)) { + $className = MODEL . $name; + $this->_models[$name] = new $className(); + } + return $this->_models[$name]; + } + + final public function view($file, array $datas = []) + { + return $this->_view->render($file, $datas); + } +} //Class diff --git a/lib/Core/Entity.php b/lib/Core/Entity.php new file mode 100644 index 0000000..7c35d16 --- /dev/null +++ b/lib/Core/Entity.php @@ -0,0 +1,53 @@ +_values[$key]; + } + final public function __set($key, $value) + { + if (array_key_exists($key, $this->_values)) { + // echo "{$this->_values[$key]} !== {$value}"; + if ($this->_values[$key] !== $value) { + $this->_isChanged = true; + $this->_changedFields[] = $key; + $this->_origins[$key] = $this->_values[$key]; + $this->_values[$key] = $value; + } + } else { + $this->_isChanged = true; + $this->_changedFields[] = $key; + $this->_origins = $this->_values; + $this->_values[$key] = $value; + } + } + + final public function isChanged() + { + return $this->_isChanged; + } + final public function clearChangedFields() + { + $this->_isChanged = false; + $this->_changedFields = $this->_values; + } + final public function getChangedFields(): array + { + return $this->_changedFields; + } + final public function getOrigins(): array + { + return $this->_origins; + } +} //Class diff --git a/lib/Core/Model.php b/lib/Core/Model.php new file mode 100644 index 0000000..ad7c927 --- /dev/null +++ b/lib/Core/Model.php @@ -0,0 +1,145 @@ +_primaryKey = $primaryKey; + } // + + abstract public function getTable(); + final public function getPrimaryKey(): string + { + if (!$this->_primaryKey) { + throw new \Exception("PrimayKey가 지정되지 않았습니다."); + } + return $this->_primaryKey; + } + final public function getDB() + { + if ($this->_db === null) { + $envs = parse_ini_file(APP . DIRECTORY_SEPARATOR . ".env.ini", true); + if (!$envs) { + throw new Exception(var_export($envs, true)); + } + $this->_db = new PDO($envs['db']['dsn'], $envs['db']['id'], $envs['db']['passwd']); + // $this->_db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + // echo "Connected successfully: " . $this->_db->getAttribute(PDO::ATTR_CONNECTION_STATUS) . "\n"; + } + return $this->_db; + } + + final public function where($key, $value, $condition = "AND") + { + if (is_array($value)) { + throw new \Exception("Value is Array:" . var_export($value, true)); + } + $this->_wheres[] = sprintf( + " %s {$key}=%s", + count($this->_wheres) ? $condition : "WHERE", + is_int($value) ? $value : "'{$value}'" + ); + } + + private function chanegValueToSQLTypeValue($value) + { + return is_int($value) ? $value : "'{$value}'"; + } + final public function execute($sql): bool|PDOStatement + { + $sql .= implode(" ", $this->_wheres); + //echo $sql . "\n"; + $stmt = $this->getDB()->prepare($sql); + if (!$stmt->execute()) { + throw new Exception("SQL 오류: {$stmt->errorInfo()} \n{$sql}\n"); + } + $this->_wheres = []; + return $stmt; + } + + //SQL문 + final public function select(array $columns = ["*"]): bool|PDOStatement + { + $sql = sprintf("SELECT %s FROM %s", implode(",", $columns), $this->getTable()); + return $this->execute($sql); + } + + final public function insert(array $datas) + { + $sqlDatas = []; + foreach ($datas as $key => $value) { + $sqlDatas[$key] = $this->chanegValueToSQLTypeValue($value); + } + $sql = sprintf("INSERT INTO {$this->getTable()} SET (%s) VALUES (%s}", array_keys($sqlDatas), array_values($sqlDatas)); + return $this->execute($sql); + } + final public function update(array $datas) + { + $sqlDatas = []; + foreach ($datas as $key => $value) { + if ($key === $this->getPrimaryKey()) { //PrimaryKey변경불가 + continue; + } + $value = $this->chanegValueToSQLTypeValue($value); + $sqlDatas[] = "{$key}={$value}"; + } + $sql = sprintf("UPDATE {$this->getTable()} SET %s", implode(",", $sqlDatas)); + return $this->execute($sql); + } + public function save($entity) + { + if ($entity->isChanged()) { + $pk = $this->getPrimaryKey(); + if ($entity->$pk) { + $this->where($pk, $entity->$pk); + $datas = []; + foreach ($entity->getChangedFields() as $key) { + $datas[$key] = $entity->$key; + } + $this->update($datas); + $entity->clearChangedFields(); + } else { + $datas = []; + foreach ($entity->getChangedFields() as $key) { + $datas[$key] = $entity->$key; + } + $this->insert($datas); + $entity->$pk = $this->getDB()->lastInsertId(); + $entity->clearChangedFields(); + } + } + return $entity; + } + + final public function getEntity() + { + $stmt = $this->select(); + if (class_exists($this->resultMode)) { + $stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, $this->resultMode); + return $stmt->fetch(); + } + return $stmt->fetch(PDO::FETCH_DEFAULT); + } + + final public function getEntitys() + { + $stmt = $this->select(); + if (class_exists($this->resultMode)) { + $stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, $this->resultMode); + return $stmt->fetchAll(); + } + return $stmt->fetchAll(PDO::FETCH_DEFAULT); + } +} //Class diff --git a/lib/Core/View.php b/lib/Core/View.php new file mode 100644 index 0000000..76e3820 --- /dev/null +++ b/lib/Core/View.php @@ -0,0 +1,42 @@ +_values = $datas; + } // + + final public function __get($name) + { + return $this->_values[$name]; + } + final public function __set($name, $value) + { + $this->_values[$name] = $value; + } + final public function setDatas(array $datas) + { + foreach ($datas as $key => $value) { + $this->_values[$key] = $value; + } + } + + final public function render($name, array $datas = []) + { + $fullPath = $this->_viewPath . DIRECTORY_SEPARATOR . $name . ".php"; + if (!file_exists($fullPath)) { + throw new Exception(sprintf("%s 파일이 존재하지 않습니다.", $fullPath)); + } + $this->setDatas($datas); + ob_start(); + include $fullPath; + return ob_end_flush(); + } +} //Class diff --git a/lib/Entity/Mangboard/BaseEntity.php b/lib/Entity/Mangboard/BaseEntity.php new file mode 100644 index 0000000..783b782 --- /dev/null +++ b/lib/Entity/Mangboard/BaseEntity.php @@ -0,0 +1,12 @@ +getName()}"; + } + + public function getUserID() + { + return $this->user_id; + } + public function getName() + { + return $this->user_name; + } + public function getPoint() + { + return $this->user_point; + } + public function setPoint(int $point) + { + return $this->user_point = $point; + } + + public function getLevel() + { + return $this->user_level; + } + public function setLevel(int $level) + { + return $this->user_level = $level; + } +} //Class diff --git a/lib/Model/Mangboard/BaseModel.php b/lib/Model/Mangboard/BaseModel.php new file mode 100644 index 0000000..078ac62 --- /dev/null +++ b/lib/Model/Mangboard/BaseModel.php @@ -0,0 +1,13 @@ +getPoint() < $point) { + throw new \Exception("기존포인트:{$entity->getPoint()}가 감소 포인트:-{$point} 작습니다.\n"); + } + $entity->setPoint($entity->getPoint() - $point); + break; + case '+': + $entity->setPoint($entity->getPoint() + $point); + break; + default: + throw new \Exception("{$sign}에 해당하는 작업은 수행할수 없습니다.\n"); + // break; + } + return $this->save($entity); + } + + final public function setLevel(UserEntity $entity, int $level): UserEntity + { + //관리자면 변경불가 + if ($entity->getUserID() == MANGBOARD_ADMIN_ID) { + return $entity; + } + //사용자 Level 1~5; + $level = $level < MANGBOARD_LEVEL_USER_MIN ? MANGBOARD_LEVEL_USER_MIN : $level; + $level = MANGBOARD_LEVEL_USER_MAX < $level ? MANGBOARD_LEVEL_USER_MAX : $level; + $entity->setLevel($level); + return $this->save($entity); + } +} //Class diff --git a/lib/View/total_counting.php b/lib/View/total_counting.php new file mode 100644 index 0000000..31a16b0 --- /dev/null +++ b/lib/View/total_counting.php @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + results as $company => $service) {?> + + + $location) {?> + + + + + + + + + + + + + + _values['types'] as $type) {?> + + + + + + + + + +
고객명일반방어전용대체테스트합계
도쿄치바도쿄치바도쿄치바도쿄치바도쿄치바합계도쿄치바합계
summary[$company]['Tokyo'] - $service['test']['Tokyo']; ?>summary[$company]['Chiba'] - $service['test']['Chiba']; ?>summary[$company]['Tokyo'] - $service['test']['Tokyo'] + $this->summary[$company]['Chiba'] - $service['test']['Chiba']; ?>
총합계summary[$type]['Tokyo']; ?>summary[$type]['Chiba']; ?>summary['test']['Tokyo'] + $this->summary['test']['Chiba']; ?>total['Tokyo'] - $this->summary['test']['Tokyo']; ?>total['Chiba'] - $this->summary['test']['Chiba']; ?>total['Tokyo'] - $this->summary['test']['Tokyo'] + $this->total['Chiba'] - $this->summary['test']['Chiba']; ?>
\ No newline at end of file diff --git a/lib/autoload.php b/lib/autoload.php new file mode 100644 index 0000000..023bb87 --- /dev/null +++ b/lib/autoload.php @@ -0,0 +1,8 @@ +