From c05abad02fab7ac7e7fc176e78bea6984951f8ca Mon Sep 17 00:00:00 2001 From: ghost Date: Tue, 26 Sep 2023 04:49:32 +0300 Subject: [PATCH] update user profile module dependencies --- src/app/controller/index.php | 108 +++++++++++++++++++++++--- src/app/controller/module/profile.php | 38 ++++++--- src/app/controller/page.php | 6 +- src/app/model/database.php | 19 +---- src/config/bootstrap.php | 20 ++++- 5 files changed, 151 insertions(+), 40 deletions(-) diff --git a/src/app/controller/index.php b/src/app/controller/index.php index 00e8c0f..381d63d 100644 --- a/src/app/controller/index.php +++ b/src/app/controller/index.php @@ -2,19 +2,103 @@ class AppControllerIndex { - private $_user; - - public function __construct() + private $_database; + private $_validator; + private $_website; + private $_session; + + public function __construct( + AppModelDatabase $database, + AppModelValidator $validator, + AppModelWebsite $website, + AppModelSession $session + ) { - require_once __DIR__ . '/user.php'; + $this->_database = $database; + $this->_validator = $validator; + $this->_website = $website; + $this->_session = $session; + } - $this->_user = new AppControllerUser( - $_SERVER['REMOTE_ADDR'] - ); + private function _initUser(string $address) + { + $error = []; + if (!$this->_validator->host($address, $error)) + { + $this->_response( + sprintf( + _('Error - %s'), + $this->_website->getName() + ), + _('406'), + $error, + 406 + ); + } + + try + { + $this->_database->beginTransaction(); + + $user = $this->_database->getUser( + $this->_database->initUserId( + $address, + $this->_website->getDefaultUserStatus(), + $this->_website->getDefaultUserApproved(), + time() + ) + ); + + $this->_database->commit(); + } + + catch (Exception $error) + { + $this->_database->rollback(); + + $this->_response( + sprintf( + _('Error - %s'), + $this->_website->getName() + ), + _('500'), + $error, + 500 + ); + } + + // Access denied + if (!$user->status) + { + $this->_response( + sprintf( + _('Error - %s'), + $this->_website->getName() + ), + _('403'), + _('Access denied'), + 403 + ); + } + + // Require account type selection + if (is_null($user->public)) + { + header( + sprintf( + 'Location: %s/welcome', + trim($this->_website->getUrl(), '/') + ) + ); + } } public function render() { + $user = $this->_initUser( + $this->_session->getAddress() + ); + $page = isset($_GET['page']) ? (int) $_GET['page'] : 1; $pages = []; @@ -26,16 +110,16 @@ class AppControllerIndex require_once __DIR__ . '/module/head.php'; $appControllerModuleHead = new AppControllerModuleHead( - Environment::config('website')->url, + $this->_website->getUrl(), $page > 1 ? sprintf( _('Page %s - BitTorrent Registry for Yggdrasil - %s'), $page, - Environment::config('website')->name + $this->_website->getName() ) : sprintf( _('%s - BitTorrent Registry for Yggdrasil'), - Environment::config('website')->name + $this->_website->getName() ), [ [ @@ -60,7 +144,9 @@ class AppControllerIndex require_once __DIR__ . '/module/profile.php'; $appControllerModuleProfile = new AppControllerModuleProfile( - $this->_user + $this->_database, + $this->_website, + $this->_session ); require_once __DIR__ . '/module/header.php'; diff --git a/src/app/controller/module/profile.php b/src/app/controller/module/profile.php index 9300539..f6852aa 100644 --- a/src/app/controller/module/profile.php +++ b/src/app/controller/module/profile.php @@ -2,27 +2,43 @@ class AppControllerModuleProfile { - private $_user; + private $_database; + private $_website; + private $_session; - public function __construct(AppControllerUser $user) + public function __construct( + AppModelDatabase $database, + AppModelWebsite $website, + AppModelSession $session) { - $this->_user = $user; + $this->_database = $database; + $this->_website = $website; + $this->_session = $session; } public function render() { $route = isset($_GET['_route_']) ? (string) $_GET['_route_'] : ''; - $stars = $this->_user->findUserPageStarsDistinctTotalByValue(true); - $views = $this->_user->findUserPageViewsDistinctTotal(); - $downloads = $this->_user->findUserPageDownloadsDistinctTotal(); - $comments = $this->_user->findUserPageCommentsDistinctTotal(); - $editions = $this->_user->findUserPageEditionsDistinctTotal(); + $user = $this->_database->getUser( + $this->_database->initUserId( + $this->_session->getAddress(), + $this->_website->getDefaultUserStatus(), + $this->_website->getDefaultUserApproved(), + time() + ) + ); - $identicon = $this->_user->getIdenticon(24); + $stars = $this->_database->findUserPageStarsDistinctTotalByValue($user->userId, true); + $views = $this->_database->findUserPageViewsDistinctTotal($user->userId); + $downloads = 0; // @TODO $this->_database->findUserPageDownloadsDistinctTotal($user->userId); + $comments = $this->_database->findUserPageCommentsDistinctTotal($user->userId); + $editions = 0; // @TODO $this->_database->findUserPageEditionsDistinctTotal($user->userId); - $public = $this->_user->getPublic(); - $address = $this->_user->getAddress(); + $identicon = false; // @TODO $this->_database->getIdenticon(24); + + $public = $user->public; + $address = $user->address; include __DIR__ . '../../../view/theme/default/module/profile.phtml'; } diff --git a/src/app/controller/page.php b/src/app/controller/page.php index 6fc2746..f47fea0 100644 --- a/src/app/controller/page.php +++ b/src/app/controller/page.php @@ -13,7 +13,7 @@ class AppControllerPage AppModelValidator $validator, AppModelLocale $locale, AppModelWebsite $website, - AppModelSession $session, + AppModelSession $session ) { $this->_database = $database; @@ -334,7 +334,9 @@ class AppControllerPage require_once __DIR__ . '/module/profile.php'; $appControllerModuleProfile = new AppControllerModuleProfile( - $user + $this->_database, + $this->_website, + $this->_session ); require_once __DIR__ . '/module/header.php'; diff --git a/src/app/model/database.php b/src/app/model/database.php index 2d218a7..a8582d0 100644 --- a/src/app/model/database.php +++ b/src/app/model/database.php @@ -639,11 +639,11 @@ class AppModelDatabase return $query->rowCount(); } - public function findUserPageStarsDistinctTotal(int $userId, bool $value) : int { + public function findUserPageStarsDistinctTotalByValue(int $userId, bool $value) : int { $this->_debug->query->select->total++; - $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `userPageStar` WHERE `userId` = ? AND `value` = ?'); + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `pageStar` WHERE `userId` = ? AND `value` = ?'); $query->execute([$userId, (int) $value]); @@ -654,18 +654,7 @@ class AppModelDatabase $this->_debug->query->select->total++; - $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `userPageView` WHERE `userId` = ?'); - - $query->execute([$userId]); - - return $query->fetch()->result; - } - - public function findUserPageDownloadsDistinctTotal(int $userId) : int { - - $this->_debug->query->select->total++; - - $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `userPageDownload` WHERE `userId` = ?'); + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `pageView` WHERE `userId` = ?'); $query->execute([$userId]); @@ -676,7 +665,7 @@ class AppModelDatabase $this->_debug->query->select->total++; - $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `userPageComment` WHERE `userId` = ?'); + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `pageComment` WHERE `userId` = ?'); $query->execute([$userId]); diff --git a/src/config/bootstrap.php b/src/config/bootstrap.php index f5b5a3a..daa8dc3 100644 --- a/src/config/bootstrap.php +++ b/src/config/bootstrap.php @@ -156,9 +156,27 @@ if (isset($request['_route_'])) else { + require_once __DIR__ . '/../app/model/database.php'; + require_once __DIR__ . '/../app/model/validator.php'; + require_once __DIR__ . '/../app/model/website.php'; + require_once __DIR__ . '/../app/model/session.php'; + require_once __DIR__ . '/../app/controller/index.php'; - $appControllerIndex = new AppControllerIndex(); + $appControllerIndex = new AppControllerIndex( + new AppModelDatabase( + Environment::config('database') + ), + new AppModelValidator( + Environment::config('validator') + ), + new AppModelWebsite( + Environment::config('website') + ), + new AppModelSession( + $_SERVER['REMOTE_ADDR'] + ) + ); $appControllerIndex->render(); }