From 711c7a9f0862090f742cf7f02580c58a48473f3f Mon Sep 17 00:00:00 2001 From: John Vincent Bonza Date: Tue, 2 May 2023 07:27:09 +0800 Subject: [PATCH] Updates --- src/Core/Application.php | 21 +++- src/Core/Controller.php | 14 ++- src/Core/Database.php | 9 +- src/Core/FileStorage.php | 33 ++++++ src/Core/Pagination.php | 177 +++++++++++++++++++++++++++++++++ src/Core/Request.php | 13 ++- src/Core/Response.php | 2 +- src/Core/Session.php | 13 +++ src/Core/View.php | 4 + src/Helpers/StringHelper.php | 21 ++++ src/Services/MasterService.php | 2 +- 11 files changed, 301 insertions(+), 8 deletions(-) create mode 100644 src/Core/FileStorage.php create mode 100644 src/Core/Pagination.php create mode 100644 src/Helpers/StringHelper.php diff --git a/src/Core/Application.php b/src/Core/Application.php index f6605e9..9cafe06 100644 --- a/src/Core/Application.php +++ b/src/Core/Application.php @@ -12,24 +12,35 @@ class Application { public string $layout = 'app'; public ?Controller $controller = null; public $config = []; + public $files = []; public function __construct($config = []) { $this->LoadSettings($config); self::$app = $this; - $this->user = null; + $this->user = NULL; + $this->guest = NULL; $this->request = new Request(); $this->response = new Response(); + $this->storage = new FileStorage(); $this->router = new Router($this->request, $this->response); $this->database = new Database(); $this->session = new Session($this->config->auth->defaults->session); $this->view = new View(); $this->services = new ServiceProvider(); + $this->helpers = $this->ToObject([ + 'StringHelper' => '\InfinityBrackets\Helpers\StringHelper' + ]); $userId = Application::$app->session->GetAuth(); if ($userId) { $this->user = $this->userClass::FindUser($userId); + } else { + $guestUserId = Application::$app->session->GetAuthGuest(); + if($guestUserId) { + $this->guest = $this->guestClass::FindGuestUser($guestUserId); + } } } @@ -40,6 +51,7 @@ public function __construct($config = []) public function LoadSettings($config) { self::$ROOT_DIR = $config['root']; $this->userClass = $config['auth']['userClass']; + $this->guestClass = $config['auth']['guestClass']; $this->config = $config; // Transform config type (Array) to (Object) @@ -88,4 +100,11 @@ public function ToJSON($data) { } echo json_encode($data); } + + public function HasPermission($name) { + $count = $this->database->CountTable("user_permissions", "WHERE `permission_id` IN (SELECT `id` FROM `permissions` WHERE `name` = :in_name) AND `user_id` = :in_user_id", ['in_name' => $name, 'in_user_id' => $this->session->GetAuth()]); + + echo ($count > 0 ? '' : 'disabled'); + //echo 'disabled'; + } } \ No newline at end of file diff --git a/src/Core/Controller.php b/src/Core/Controller.php index 48a46b1..d22a1d2 100644 --- a/src/Core/Controller.php +++ b/src/Core/Controller.php @@ -8,7 +8,8 @@ class Controller { public string $layout = 'app'; public string $action = ''; - + public $model = NULL; + public $models = []; protected array $middlewares = []; public function SetLayout($layout): void @@ -30,4 +31,15 @@ public function GetMiddlewares(): array { return $this->middlewares; } + + public function RegisterModel($model) { + $this->model = new $model(); + } + + public function BindModel($classes = []) { + foreach($classes as $class) { + $temp = explode('\\', $class); + $this->models[end($temp)] = new $class(); + } + } } \ No newline at end of file diff --git a/src/Core/Database.php b/src/Core/Database.php index 9615ae7..31c7e42 100644 --- a/src/Core/Database.php +++ b/src/Core/Database.php @@ -33,6 +33,9 @@ public function __construct($config = NULL) } } } + if(is_array($config)) { + $default = $config; + } if(is_null($default)) { $default = Application::$app->config->env; } @@ -125,11 +128,11 @@ public function Rollback() { public function Query($statement = "", $parameters = []) { try { - $stmt = $this->pdo->query($statement); - $stmt->execute($parameters); + $stmt = $this->ExecuteStatement($statement, $parameters); + $this->results = $stmt->fetchAll(); return $this; } catch(Exception $e) { - throw new Exception($e->getMessage()); + throw new Exception($e->getMessage()); } } diff --git a/src/Core/FileStorage.php b/src/Core/FileStorage.php new file mode 100644 index 0000000..7974b1d --- /dev/null +++ b/src/Core/FileStorage.php @@ -0,0 +1,33 @@ + 0 ? TRUE : FALSE; + } + + public function Upload($storage) { + $fileName = self::$files['name']; + $tempLocation = self::$files['tmp_name']; + $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION); + $name = uniqid(true) . '.' . $fileExtension; + + $upload = move_uploaded_file($tempLocation, $storage . $name) ? TRUE : FALSE; + + return Application::$app->ToObject(['success' => $upload, 'name' => $name]); + } +} \ No newline at end of file diff --git a/src/Core/Pagination.php b/src/Core/Pagination.php new file mode 100644 index 0000000..5210500 --- /dev/null +++ b/src/Core/Pagination.php @@ -0,0 +1,177 @@ +current = $config['current']; + } + if(array_key_exists('last', $config)) { + $this->last = $config['last']; + } + if(array_key_exists('queryString', $config)) { + $this->queryString .= $config['queryString']; + } + if(array_key_exists('orderBy', $config)) { + $this->queryString .= '&orderBy=' . $config['orderBy']; + } + } + + public function GeneratePagination() { + $this->queryString .='&page='; + if($this->last != 1) { + if ($this->current > 1) { + // First Page + if($this->current >= $this->maxControlsPerPage - 1) { + $this->paginationControls .= '
  • '; + } + // Before Active Page + for($i = $this->current - ($this->maxControlsPerPage - 1); $i < $this->current; $i++) { + if($i > 0) { + if($this->current - 3 < $i) { + $this->paginationControls .= '
  • ' . $i . '
  • '; + } + } + } + } + // Active Page + $this->paginationControls .= '
  • ' . $this->current . '
  • '; + // After Active Page + for ($i = $this->current + 1; $i <= $this->last; $i++){ + $this->paginationControls .= '
  • ' . $i . '
  • '; + if($i >= $this->current + 2) { + break; + } + } + // this->last Page + if($this->last >= $this->current + 3) { + $this->paginationControls .= '
  • '; + } + } + return $this; + } + + public function Render() { + return $this->paginationControls; + } + + public function Paginate($total, $limit, $options = []) { + $last = ceil($total/$limit); + if($last < 1){ + $last = 1; + } + + // Establish the $pagenum variable + $page = 1; + $link = '?page='; + + // Configure options + if($options) { + // querystring + if(array_key_exists('querystring', $options)) { + $link = $options['querystring'] . '&page='; + } + //page + if(array_key_exists('page', $options)) { + $page = $options['page']; + } + } + + // Get page from URL vars if it is present, else it is = 1 + if(isset($request['page'])) { + $page = preg_replace('#[^0-9]#', '', $request['page']); + } + if ($page < 1) { + $page = 1; + } else if ($page > $last) { + $page = $last; + } + + $pages = []; + $current = $page; + + if($last != 1) { + // Previous button + if($page == 1) { + $pages[] = [ + 'page' => NULL, + 'link' => NULL, + 'type' => 'previous' + ]; + } else { + $pages[] = [ + 'page' => $page - 1, + 'link' => $link . ($page - 1), + 'type' => 'previous' + ]; + } + if ($current > 1) { + // Before Active Page + for($i = $current - ($this->maxControlsPerPage - 1); $i < $current; $i++) { + if($i > 0) { + if($current - 4 < $i) { + $pages[] = [ + 'page' => $i, + 'link' => $link . $i, + 'type' => 'default' + ]; + } + } + } + } + + // Current page + $pages[] = [ + 'page' => $page, + 'link' => $link . $page, + 'type' => 'active' + ]; + + // After Active Page + for ($i = $current + 1; $i <= $last; $i++){ + $pages[] = [ + 'page' => $i, + 'link' => $link . $i, + 'type' => 'default' + ]; + if($i >= $current + 3) { + break; + } + } + + // Next button + if($page == $last) { + $pages[] = [ + 'page' => NULL, + 'link' => NULL, + 'type' => 'next' + ]; + } else { + $pages[] = [ + 'page' => $page + 1, + 'link' => $link . ($page + 1), + 'type' => 'next' + ]; + } + } + + return Application::$app->ToObject($pages); + } +} \ No newline at end of file diff --git a/src/Core/Request.php b/src/Core/Request.php index a79b952..a6078cc 100644 --- a/src/Core/Request.php +++ b/src/Core/Request.php @@ -39,12 +39,17 @@ public function GetUrl() foreach($path as $temp) { $temprow = explode('=', $temp); + if(in_array('view', $temprow)) { $path = '/' . $temprow[1]; - break; + } else if(in_array('tab', $temprow)) { + $path .= '/' . $temprow[1]; + } else if(in_array('action', $temprow)) { + $path .= '/' . $temprow[1]; } } } + return $path; } @@ -91,6 +96,12 @@ public function GetBody() $data[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS); } } + if($_FILES) { + FileStorage::Push($value); + foreach ($_FILES as $file) { + FileStorage::Push($file); + } + } return $data; } diff --git a/src/Core/Response.php b/src/Core/Response.php index 386c41e..b438475 100644 --- a/src/Core/Response.php +++ b/src/Core/Response.php @@ -49,7 +49,7 @@ public function DownloadFile($file = NULL) { $this->Header($file); } - protected function Header($url) { + public function Header($url) { header('location: ' . $url); } } \ No newline at end of file diff --git a/src/Core/Session.php b/src/Core/Session.php index 521e20c..0c67a2e 100644 --- a/src/Core/Session.php +++ b/src/Core/Session.php @@ -6,6 +6,7 @@ class Session { protected const FLASH_KEY = 'flash_messages'; protected $USER_KEY = 'user'; + protected $GUEST_KEY = 'guest'; public function __construct($userKey) { @@ -63,6 +64,18 @@ public function DeAuth() { $this->Remove($this->USER_KEY); } + public function AuthGuest($value) { + $this->Set($this->GUEST_KEY, $value); + } + + public function GetAuthGuest() { + return $this->Get($this->GUEST_KEY); + } + + public function DeAuthGuest() { + $this->Remove($this->GUEST_KEY); + } + public function Set($key, $value) { $_SESSION[$key] = $value; diff --git a/src/Core/View.php b/src/Core/View.php index 4d4fea6..2016e83 100644 --- a/src/Core/View.php +++ b/src/Core/View.php @@ -42,6 +42,10 @@ public function Render($view) { include_once Application::$ROOT_DIR . '/' . $this->rootDirectory . "$view.php"; } + public function ForceRender($view) { + include Application::$ROOT_DIR . '/' . $this->rootDirectory . "$view.php"; + } + public static function Route($url, $params = []) { return Application::$app->router->PrintRoute($url, $params); } diff --git a/src/Helpers/StringHelper.php b/src/Helpers/StringHelper.php new file mode 100644 index 0000000..0a5f3f7 --- /dev/null +++ b/src/Helpers/StringHelper.php @@ -0,0 +1,21 @@ +config->env->APP_ENV == 'local' ? 'ils-local' : 'ils-live'); + $db = new Database('ils'); return $db->SelectOne("SELECT * FROM `" . $table . "` WHERE `id` = :in_id", ['in_id' => $value])->Get(); } } \ No newline at end of file