mirror of
https://github.com/claradex/nativegallery.git
synced 2025-03-28 20:28:24 +03:00
models & services
This commit is contained in:
parent
8672508efb
commit
20b34878c6
6 changed files with 109 additions and 39 deletions
|
@ -2,45 +2,61 @@
|
||||||
|
|
||||||
namespace App\Core;
|
namespace App\Core;
|
||||||
|
|
||||||
|
use Latte\Engine;
|
||||||
|
use Exception;
|
||||||
|
use App\Services\Auth;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Services\DB;
|
||||||
|
|
||||||
class Page
|
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])) {
|
$this->latte = new Engine;
|
||||||
self::$cache[$name] = require_once($_SERVER['DOCUMENT_ROOT'] . '/views/components/' . $name . '.php');
|
$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];
|
$this->latte->render($templatePath, $params);
|
||||||
}
|
|
||||||
|
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ class Routes
|
||||||
Router::get('/voting/results', 'ContestsController@results');
|
Router::get('/voting/results', 'ContestsController@results');
|
||||||
Router::get('/voting/waiting', 'ContestsController@waiting');
|
Router::get('/voting/waiting', 'ContestsController@waiting');
|
||||||
Router::get('/comments', 'MainController@comments');
|
Router::get('/comments', 'MainController@comments');
|
||||||
|
Router::get('/messages', 'MessagesController@i');
|
||||||
if (Auth::userid() > 0) {
|
if (Auth::userid() > 0) {
|
||||||
$user = new \App\Models\User(Auth::userid());
|
$user = new \App\Models\User(Auth::userid());
|
||||||
|
|
||||||
|
|
|
@ -5,11 +5,17 @@ use \App\Services\DB;
|
||||||
class Photo {
|
class Photo {
|
||||||
|
|
||||||
public $photoid;
|
public $photoid;
|
||||||
function __construct($user_id) {
|
private $photo;
|
||||||
$this->photoid = $user_id;
|
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) {
|
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) {
|
public static function fetchAll($user_id = NULL) {
|
||||||
if ($user_id != NULL) {
|
if ($user_id != NULL) {
|
||||||
|
|
|
@ -5,11 +5,13 @@ use \App\Services\DB;
|
||||||
class User {
|
class User {
|
||||||
|
|
||||||
public $userid;
|
public $userid;
|
||||||
|
private $user;
|
||||||
function __construct($user_id) {
|
function __construct($user_id) {
|
||||||
$this->userid = $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) {
|
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) {
|
public function content($table) {
|
||||||
$content = json_decode(self::i('content'), true);
|
$content = json_decode(self::i('content'), true);
|
||||||
|
|
45
app/Services/APIResponse.php
Normal file
45
app/Services/APIResponse.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ class DB {
|
||||||
try {
|
try {
|
||||||
self::$pdoInstance = new PDO($dsn, $username, $password);
|
self::$pdoInstance = new PDO($dsn, $username, $password);
|
||||||
self::$pdoInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
self::$pdoInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
} catch (PDOException $ex) {
|
} catch (\PDOException $ex) {
|
||||||
die("Connection failed: " . $ex->getMessage());
|
die("Connection failed: " . $ex->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue