diff options
Diffstat (limited to 'web/_model')
-rw-r--r-- | web/_model/apps/error.php | 35 | ||||
-rw-r--r-- | web/_model/apps/home.php | 22 | ||||
-rw-r--r-- | web/_model/cache.php | 37 | ||||
-rw-r--r-- | web/_model/format.php | 45 | ||||
-rw-r--r-- | web/_model/main.php | 84 | ||||
-rw-r--r-- | web/_model/request.php | 40 |
6 files changed, 263 insertions, 0 deletions
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 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Error_model extends Model { + + function __construct($load) { + parent::__construct($load); + } + + private function get_msg(&$data) { + if (!array_key_exists('code', $_GET)) { + $data['msg'] = lang('error'); + $data['title'] = '500'; + } else { + $code = $_GET['code']; + $data['title'] = $code; + switch ($code) { + case '404': + $data['msg'] = lang('error_404'); + break; + case '500': + $data['msg'] = lang('error_500'); + break; + default: + $data['msg'] = lang('error'); + break; + } + } + } + + public function get_data(): array { + $data = parent::get_data(); + $this->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 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Home_model extends Model { + + function __construct($load) { + parent::__construct($load); + } + + private function get_posts(): array { + return $this->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 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Cache_model extends Model { + + // the user cache + private $users; + + function __construct($load) { + parent::__construct($load); + $this->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 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Format_model extends Modal { + + function __construct($load) { + parent::__construct($load); + } + + /** + * Formats a users's name + * @param array $user - the $user + * @returns the user's formatted display name + */ + public function name($user) { + $name = ''; + if ($user['first_name']) { + $name .= $user['first_name']; + } + if ($user['middle_name']) { + if ($name != '') { + $name .= ' '; + } + $name .= $user['middle_name']; + } + if ($user['last_name']) { + if ($name != '') { + $name .= ' '; + } + $name .= $user['last_name']; + } + if ($name == '') { + $name = '@' . $user['username']; + } + return $name; + } + + /** + * Formats a date + * @param string $date - the data in RFC3999 format + * @returns the formatted date + */ + public function date($date) { + return $date; + } + +} diff --git a/web/_model/main.php b/web/_model/main.php new file mode 100644 index 0000000..f72a2f3 --- /dev/null +++ b/web/_model/main.php @@ -0,0 +1,84 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Main_model { + + // the website database + public $db; + + // the current user session (can be NULL) + public $session; + + // current loaded users + private $users; + + // stores the current request info + public $info; + + /** + * Loads the main model + * @param Loader $load - the main loader object + */ + function __construct($load) { + /// load the database helper + $this->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 '<link rel="stylesheet" href="/public/' . $path . '">'; + } + + /** + * Loads a js html link + * @param string $path - the path to the js file + */ + public function link_js($path) { + return '<script src="/public/'. $path . '"></script>'; + } + + /** + * 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 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Request_model extends Model { + + function __construct($load) { + parent::__construct($load); + } + + /** + * Loads a string from the GET request + * @param string $key - the name for the query param + * @param string $default - the default value if not exists + */ + public function get_str($key, $default = NULL): string | NULL { + if (!array_key_exists($key, $_GET)) { + return $default; + } else { + return $_GET[$key]; + } + } + + /** + * Loads a number from the GET request + * @param string $key - the name for the query param + * @param int $default - the default value if not exists + */ + public function get_int($key, $default = NULL): int | NULL { + if (!array_key_exists($key, $_GET)) { + return $default; + } else { + $val = $_GET[$key]; + $val = intval($val); + if ($val < 0) { + return 0; + } else { + return $val; + } + } + } + +} |