models & services

This commit is contained in:
themohooks 2025-02-25 23:02:05 +03:00
parent 8672508efb
commit 20b34878c6
6 changed files with 109 additions and 39 deletions

View file

@ -2,45 +2,61 @@
namespace App\Core;
use Latte\Engine;
use Exception;
use App\Services\Auth;
use App\Models\User;
use App\Services\DB;
class Page
{
private static $cache = [];
private $latte;
private $layouts = [
'dashboard' => 'dashboard.latte',
'login' => null,
];
private $defaultLayout = '@layout.latte';
private $user;
public static function component($name)
public function __construct()
{
if (!isset(self::$cache[$name])) {
self::$cache[$name] = require_once($_SERVER['DOCUMENT_ROOT'] . '/views/components/' . $name . '.php');
$this->latte = new Engine;
$this->latte->setTempDirectory($_SERVER['DOCUMENT_ROOT'] . '/cdn/lattecache');
$this->user = new User(Auth::userid());
}
private function get_current_git_commit($branch = 'main')
{
if ($hash = file_get_contents(sprintf($_SERVER['DOCUMENT_ROOT'] . '/.git/refs/heads/%s', $branch))) {
return mb_strimwidth($hash, 0, 7, "");
} else {
return false;
}
}
public function render($template, $params = [])
{
$templatePath = $_SERVER['DOCUMENT_ROOT'] . "/views/pages/" . $template . ".latte";
if (!file_exists($templatePath)) {
throw new Exception("Template not found: " . $templatePath);
}
$params['ngallery'] = NGALLERY;
$params['user_id'] = Auth::userid();
$params['user'] = $this->user;
$params['nonreviewedimgs'] = DB::query('SELECT COUNT(*) FROM photos WHERE moderated=0')[0]['COUNT(*)'];
$params['lastcommit'] = self::get_current_git_commit();
$params['mysqlversion'] = DB::query('SELECT VERSION()')[0]['VERSION()'];
$params['db'] = \App\Services\DB::class;
$layout = $this->layouts[$template] ?? $this->defaultLayout;
if ($layout) {
$params['layout'] = $_SERVER['DOCUMENT_ROOT'] . "/views/components/" . $layout;
}
return self::$cache[$name];
}
public static function rewrite($search, $replace, $rootUrl)
{
return str_ireplace($search, $replace, $rootUrl);
}
public static function set($name)
{
if (!isset(self::$cache[$name])) {
self::$cache[$name] = require_once($_SERVER['DOCUMENT_ROOT'] . '/views/pages/' . $name . '.php');
}
return self::$cache[$name];
}
public static function render($name)
{
return self::set($name)();
}
public static function method()
{
return $_SERVER['REQUEST_METHOD'];
}
public static function exists($name)
{
return file_exists($_SERVER['DOCUMENT_ROOT'] . '/views/pages/' . $name . '.php');
$this->latte->render($templatePath, $params);
}
}

View file

@ -40,6 +40,7 @@ class Routes
Router::get('/voting/results', 'ContestsController@results');
Router::get('/voting/waiting', 'ContestsController@waiting');
Router::get('/comments', 'MainController@comments');
Router::get('/messages', 'MessagesController@i');
if (Auth::userid() > 0) {
$user = new \App\Models\User(Auth::userid());

View file

@ -5,11 +5,17 @@ use \App\Services\DB;
class Photo {
public $photoid;
function __construct($user_id) {
$this->photoid = $user_id;
private $photo;
function __construct($photo_id) {
$this->photoid = $photo_id;
$this->photo = DB::query("SELECT * FROM photos WHERE id=:id", array(':id'=>$this->photoid))[0];
}
public function i($table) {
return DB::query("SELECT * FROM photos WHERE id=:id", array(':id'=>$this->photoid))[0][$table];
return $this->photo[$table];
}
public function exists(): bool
{
return $this->i('id') !== null;
}
public static function fetchAll($user_id = NULL) {
if ($user_id != NULL) {

View file

@ -5,11 +5,13 @@ use \App\Services\DB;
class User {
public $userid;
private $user;
function __construct($user_id) {
$this->userid = $user_id;
$this->user = DB::query("SELECT * FROM users WHERE id=:id", array(':id'=>$this->userid))[0];
}
public function i($table) {
return DB::query("SELECT * FROM users WHERE id=:id", array(':id'=>$this->userid))[0][$table];
return $this->user[$table];
}
public function content($table) {
$content = json_decode(self::i('content'), true);

View file

@ -0,0 +1,45 @@
<?php
namespace App\Services;
class APIResponse {
private array $data;
private int $httpCode;
public function __construct() {
$this->data = [];
$this->httpCode = 200;
}
public static function data(array $data, int $httpCode = 200): self {
$instance = new self();
$instance->data = $data;
$instance->httpCode = $httpCode;
return $instance;
}
public function setData(array $data): self {
$this->data = $data;
return $this;
}
public function setHttpCode(int $httpCode): self {
$this->httpCode = $httpCode;
return $this;
}
public function addField(string $key, mixed $value): self {
$this->data[$key] = $value;
return $this;
}
public function toJson(): string {
return json_encode($this->data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
public function send(): void {
http_response_code($this->httpCode);
header('Content-Type: application/json');
echo $this->toJson();
exit;
}
}

View file

@ -16,7 +16,7 @@ class DB {
try {
self::$pdoInstance = new PDO($dsn, $username, $password);
self::$pdoInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $ex) {
} catch (\PDOException $ex) {
die("Connection failed: " . $ex->getMessage());
}
}