From 1f04b83be337cc91a3fabcf4e574e2306f3d2eaa Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Sat, 30 Mar 2024 12:14:42 -0400 Subject: refactor --- web/_model/apps/error.php | 35 ++++++++++++++++++++ web/_model/apps/home.php | 22 +++++++++++++ web/_model/cache.php | 37 +++++++++++++++++++++ web/_model/format.php | 45 +++++++++++++++++++++++++ web/_model/main.php | 84 +++++++++++++++++++++++++++++++++++++++++++++++ web/_model/request.php | 40 ++++++++++++++++++++++ 6 files changed, 263 insertions(+) create mode 100644 web/_model/apps/error.php create mode 100644 web/_model/apps/home.php create mode 100644 web/_model/cache.php create mode 100644 web/_model/format.php create mode 100644 web/_model/main.php create mode 100644 web/_model/request.php (limited to 'web/_model') diff --git a/web/_model/apps/error.php b/web/_model/apps/error.php new file mode 100644 index 0000000..ad72b28 --- /dev/null +++ b/web/_model/apps/error.php @@ -0,0 +1,35 @@ +get_msg($data); + return $data; + } +} +?> diff --git a/web/_model/apps/home.php b/web/_model/apps/home.php new file mode 100644 index 0000000..82fbf26 --- /dev/null +++ b/web/_model/apps/home.php @@ -0,0 +1,22 @@ +db + ->select('*') + ->from('admin.post') + ->limit(20) + ->rows(); + } + + public function get_data(): array { + $data = parent::get_data(); + $data['title'] = lang('title'); + $data['posts'] = $this->get_posts(); + return $data; + } +} diff --git a/web/_model/cache.php b/web/_model/cache.php new file mode 100644 index 0000000..6cf9924 --- /dev/null +++ b/web/_model/cache.php @@ -0,0 +1,37 @@ +users = array(); + } + + /** + * Gets a array of users + */ + public function get_users($objs) { + $ids = array(); + foreach ($objs as $obj) { + $id = $obj['user_id']; + if (!array_key_exists($id, $this->users)) { + array_push($ids, intval($id)); + } + } + if (!empty($ids)) { + $result = $this->main->db + ->select('*') + ->from('api.user') + ->where_in('id', $ids) + ->rows(); + foreach ($result as $user) { + $id = $user['id']; + $this->users[$id] = $user; + } + } + return $this->users; + } + +} diff --git a/web/_model/format.php b/web/_model/format.php new file mode 100644 index 0000000..d8c7480 --- /dev/null +++ b/web/_model/format.php @@ -0,0 +1,45 @@ +db = new DatabaseHelper(); + /// load the current session + if (array_key_exists('jwt', $_SESSION)) { + $this->get_session($_SESSION['jwt']); + } else { + $this->session = NULL; + }; + /// init other vars + $this->users = array(); + } + + /** + * Loads current session + * @param string $jwt - the user provided JWT + */ + private function get_session($jwt) { + $query = $this->db + ->select("_api.verify_jwt('" . $jwt . "') AS user_id;"); + $result = $query->row(); + $user_id = $result['user_id']; + if ($user_id) { + $this->session = array( + 'id' => $user_id, + 'jwt' => $jwt + ); + } + } + + /** + * Loads a css html link + * @param string $path - the path to the css file + */ + public function link_css($path) { + return ''; + } + + /** + * Loads a js html link + * @param string $path - the path to the js file + */ + public function link_js($path) { + return ''; + } + + /** + * Gets the current user + */ + public function user() { + if ($this->session) { + return $this->db + ->select('*') + ->from('api.user') + ->where('id') + ->eq($this->session['id']) + ->row(); + } else { + return NULL; + } + } + +} + +?> diff --git a/web/_model/request.php b/web/_model/request.php new file mode 100644 index 0000000..4cce07a --- /dev/null +++ b/web/_model/request.php @@ -0,0 +1,40 @@ +