From 4afd50d766763fd80c3a7dd67ef8233664befc42 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 14 Oct 2015 21:35:16 +0200 Subject: [PATCH] Implemented the new Rank class. Signed-off-by: Flashwave --- _sakura/components/Rank.php | 86 +++++++++++++++++++++ _sakura/components/User.php | 10 +-- _sakura/components/Users.php | 38 ++------- _sakura/templates/yuuno/main/memberlist.tpl | 38 ++++----- 4 files changed, 115 insertions(+), 57 deletions(-) diff --git a/_sakura/components/Rank.php b/_sakura/components/Rank.php index add8c94..dcc3e13 100755 --- a/_sakura/components/Rank.php +++ b/_sakura/components/Rank.php @@ -7,5 +7,91 @@ namespace Sakura; class Rank { + // Rank data + public $data = []; + // Initialise the rank object + public function __construct($rid) + { + + // Get the rank database row + $this->data = Database::fetch( + 'ranks', + false, + [ + 'rank_id' => [$rid, '=', true], + ] + ); + + // Check if the rank actually exists + if (empty($this->data)) { + // If not assign as the fallback rank + $this->data = Users::$emptyRank; + } + + } + + // Get the rank id + public function id() + { + + return $this->data['rank_id']; + + } + + // Get the rank hierarchy + public function hierarchy() + { + + return $this->data['rank_hierarchy']; + + } + + // Get the rank name + public function name($multi = false) + { + + return $this->data['rank_name'] . ($multi ? $this->data['rank_multiple'] : null); + + } + + // Get the rank title + public function title() + { + + return $this->data['rank_title']; + + } + + // Get the rank description + public function description() + { + + return $this->data['rank_description']; + + } + + // Get the rank colour + public function colour() + { + + return $this->data['rank_colour']; + + } + + // Check if the rank is hidden + public function hidden() + { + + return $this->data['rank_hidden'] || $this->checkPermission('SITE', 'DEACTIVATED') || $this->checkPermission('SITE', 'RESTRICTED'); + + } + + // Check if the rank has the proper permissions + public function checkPermission($layer, $action) + { + + return Permissions::check($layer, $action, [$this->id()], 2); + + } } diff --git a/_sakura/components/User.php b/_sakura/components/User.php index 72d7d80..a047022 100755 --- a/_sakura/components/User.php +++ b/_sakura/components/User.php @@ -41,13 +41,13 @@ class User // Get the rows for all the ranks foreach ($ranks as $rank) { // Store the database row in the array - $this->ranks[$rank] = Database::fetch('ranks', false, ['rank_id' => [$rank, '=']]); + $this->ranks[$rank] = new Rank($rank); } // Check if ranks were set if (empty($this->ranks)) { // If not assign the fallback rank - $this->ranks[0] = Users::$emptyRank; + $this->ranks[0] = new Rank(0); } // Assign the user's main rank to a special variable since we'll use it a lot @@ -64,7 +64,7 @@ class User { // Check if the main rank is the specified rank - if (in_array($this->mainRank['rank_id'], $ranks)) { + if ($this->mainRank->id() === $ranks) { return true; } @@ -85,7 +85,7 @@ class User public function colour() { - return empty($this->data['user_colour']) ? $this->mainRank['rank_colour'] : $this->data['user_colour']; + return empty($this->data['user_colour']) ? $this->mainRank->colour() : $this->data['user_colour']; } @@ -93,7 +93,7 @@ class User public function userTitle() { - return empty($this->data['user_title']) ? $this->mainRank['rank_title'] : $this->data['user_title']; + return empty($this->data['user_title']) ? $this->mainRank->title() : $this->data['user_title']; } diff --git a/_sakura/components/Users.php b/_sakura/components/Users.php index d1414f9..eb9d941 100755 --- a/_sakura/components/Users.php +++ b/_sakura/components/Users.php @@ -813,27 +813,7 @@ class Users public static function checkIfUserHasRanks($ranks, $userid, $userIdIsUserData = false) { - // Get the specified user - $user = $userIdIsUserData ? $userid : self::getUser($userid); - - // Check if the main rank is the specified rank - if (in_array($user['rank_main'], $ranks)) { - return true; - } - - // Decode the json for the user's ranks - $uRanks = json_decode($user['user_ranks'], true); - - // If not go over all ranks and check if the user has them - foreach ($ranks as $rank) { - // We check if $rank is in $user['ranks'] and if yes return true - if (in_array($rank, $uRanks)) { - return true; - } - } - - // If all fails return false - return false; + return $userIdIsUserData ? $userid->checkIfUserHasRanks($ranks) : (new User($userid))->checkIfUserHasRanks($ranks); } @@ -1151,16 +1131,8 @@ class Users public static function getRank($id) { - // Execute query - $rank = Database::fetch('ranks', false, ['rank_id' => [$id, '=']]); - - // Return false if no rank was found - if (empty($rank)) { - return self::$emptyRank; - } - // If rank was found return rank data - return $rank; + return (new Rank($id))->data; } @@ -1198,7 +1170,7 @@ class Users foreach ($users as $user) { // If so store the user's row in the array if (self::checkIfUserHasRanks([$rankId], $user, true) - && ($excludeAbyss ? $user['password_algo'] != 'nologin' : true)) { + && ($excludeAbyss ? $user->data['password_algo'] != 'nologin' : true)) { $rank[] = $user; } } @@ -1230,7 +1202,7 @@ class Users continue; } - $users[$user['user_id']] = $user; + $users[$user['user_id']] = new User($user['user_id']); } // and return an array with the users @@ -1250,7 +1222,7 @@ class Users // Reorder shit foreach ($getRanks as $rank) { - $ranks[$rank['rank_id']] = $rank; + $ranks[$rank['rank_id']] = new Rank($rank['rank_id']); } // and return an array with the ranks diff --git a/_sakura/templates/yuuno/main/memberlist.tpl b/_sakura/templates/yuuno/main/memberlist.tpl index af77285..d549b50 100755 --- a/_sakura/templates/yuuno/main/memberlist.tpl +++ b/_sakura/templates/yuuno/main/memberlist.tpl @@ -1,18 +1,18 @@ {% extends 'global/master.tpl' %} {% set rankTitle %} -{% if page.notfound %}Not found{% else %}{% if not page.active %}All members{% else %}{{ page.ranks[page.active].rank_name }}{{ page.ranks[page.active].rank_multiple }}{% endif %}{% endif %} +{% if page.notfound %}Not found{% else %}{% if not page.active %}All members{% else %}{{ page.ranks[page.active].name(true) }}{% endif %}{% endif %} {% endset %} {% set rankDescription %} -{% if page.notfound %}The requested rank could not be found!{% else %}{% if not page.active %}The entire user list.{% else %}{{ page.ranks[page.active].rank_description }}{% endif %}{% endif %} +{% if page.notfound %}The requested rank could not be found!{% else %}{% if not page.active %}The entire user list.{% else %}{{ page.ranks[page.active].description }}{% endif %}{% endif %} {% endset %} {% block title %}{{ rankTitle }}{% endblock %} {% block content %}
-

{{ rankTitle }}

+

{{ rankTitle }}

{{ rankDescription }}

@@ -21,8 +21,8 @@ Rank: All members {% for rank in page.ranks %} - {% if not rank.rank_hidden or (rank.rank_hidden and page.active == rank.rank_id) %} - {{ rank.rank_name }}{{ rank.rank_multiple }} + {% if not rank.hidden or (rank.hidden and page.active == rank.id) %} + {{ rank.name(true) }} {% endif %} {% endfor %}
@@ -66,19 +66,19 @@ #{{ page.active ? count + 1 : count }} - {{ user.username }} + {{ user.data.username }} + + + {{ user.elapsed.joined }} + + + {% if user.data.user_last_online == 0 %}Never logged in.{% else %}{{ user.elapsed.lastOnline }}{% endif %} - {{ user.regdate|date(sakura.dateFormat) }} + {{ user.userTitle }} - {% if user.user_last_online == 0 %}Never logged in.{% else %}{{ user.user_last_online|date(sakura.dateFormat) }}{% endif %} - - - {% if not user.user_title %}{{ page.ranks[user.rank_main].rank_title }}{% else %}{{ user.user_title }}{% endif %} - - - {% if user.user_country|lower == 'eu' %}?{% else %}{{ user.user_country }}{% endif %} + {% if user.country.short|lower == 'xx' %}?{% else %}{{ user.country.long }}{% endif %} @@ -86,11 +86,11 @@ {% else %} {% for user in page.users[page.page] %} - {# These comment tags are here to prevent the link extending too far - #}