Implemented the new Rank class.

Signed-off-by: Flashwave <me@flash.moe>
This commit is contained in:
flash 2015-10-14 21:35:16 +02:00
parent 69410d0e33
commit 4afd50d766
4 changed files with 115 additions and 57 deletions

View file

@ -7,5 +7,91 @@ namespace Sakura;
class Rank 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);
}
} }

View file

@ -41,13 +41,13 @@ class User
// Get the rows for all the ranks // Get the rows for all the ranks
foreach ($ranks as $rank) { foreach ($ranks as $rank) {
// Store the database row in the array // 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 // Check if ranks were set
if (empty($this->ranks)) { if (empty($this->ranks)) {
// If not assign the fallback rank // 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 // 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 // Check if the main rank is the specified rank
if (in_array($this->mainRank['rank_id'], $ranks)) { if ($this->mainRank->id() === $ranks) {
return true; return true;
} }
@ -85,7 +85,7 @@ class User
public function colour() 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() 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'];
} }

View file

@ -813,27 +813,7 @@ class Users
public static function checkIfUserHasRanks($ranks, $userid, $userIdIsUserData = false) public static function checkIfUserHasRanks($ranks, $userid, $userIdIsUserData = false)
{ {
// Get the specified user return $userIdIsUserData ? $userid->checkIfUserHasRanks($ranks) : (new User($userid))->checkIfUserHasRanks($ranks);
$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;
} }
@ -1151,16 +1131,8 @@ class Users
public static function getRank($id) 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 // If rank was found return rank data
return $rank; return (new Rank($id))->data;
} }
@ -1198,7 +1170,7 @@ class Users
foreach ($users as $user) { foreach ($users as $user) {
// If so store the user's row in the array // If so store the user's row in the array
if (self::checkIfUserHasRanks([$rankId], $user, true) if (self::checkIfUserHasRanks([$rankId], $user, true)
&& ($excludeAbyss ? $user['password_algo'] != 'nologin' : true)) { && ($excludeAbyss ? $user->data['password_algo'] != 'nologin' : true)) {
$rank[] = $user; $rank[] = $user;
} }
} }
@ -1230,7 +1202,7 @@ class Users
continue; continue;
} }
$users[$user['user_id']] = $user; $users[$user['user_id']] = new User($user['user_id']);
} }
// and return an array with the users // and return an array with the users
@ -1250,7 +1222,7 @@ class Users
// Reorder shit // Reorder shit
foreach ($getRanks as $rank) { 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 // and return an array with the ranks

View file

@ -1,18 +1,18 @@
{% extends 'global/master.tpl' %} {% extends 'global/master.tpl' %}
{% set rankTitle %} {% 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 %} {% endset %}
{% set rankDescription %} {% 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 %} {% endset %}
{% block title %}{{ rankTitle }}{% endblock %} {% block title %}{{ rankTitle }}{% endblock %}
{% block content %} {% block content %}
<div class="headerNotify" style="margin-bottom: 1px;"> <div class="headerNotify" style="margin-bottom: 1px;">
<h1 style="text-shadow: 0px 0px 5px #555;{% if page.active %} color: {{ page.ranks[page.active].rank_colour }};{% endif %}">{{ rankTitle }}</h1> <h1 style="{% if page.active %}text-shadow: 0px 0px 5px {{ page.ranks[page.active].colour }}; color: {{ page.ranks[page.active].colour }};{% else %}text-shadow: 0px 0px 5px #555;{% endif %}">{{ rankTitle }}</h1>
<h3>{{ rankDescription }}</h3> <h3>{{ rankDescription }}</h3>
</div> </div>
<div class="membersPage" style="min-height: 500px;"> <div class="membersPage" style="min-height: 500px;">
@ -21,8 +21,8 @@
<a class="dropDownDesc">Rank:</a> <a class="dropDownDesc">Rank:</a>
<a href="{% if page.page and page.sort %}{{ urls.format('MEMBERLIST_SORT_PAGE', [page.sort, (page.page + 1)]) }}{% elseif page.sort %}{{ urls.format('MEMBERLIST_SORT', [page.sort]) }}{% elseif page.page %}{{ urls.format('MEMBERLIST_PAGE', [(page.page + 1)]) }}{% else %}{{ urls.format('MEMBERLIST_INDEX') }}{% endif %}"{% if not page.active %} class="dropDownSelected"{% endif %}>All members</a> <a href="{% if page.page and page.sort %}{{ urls.format('MEMBERLIST_SORT_PAGE', [page.sort, (page.page + 1)]) }}{% elseif page.sort %}{{ urls.format('MEMBERLIST_SORT', [page.sort]) }}{% elseif page.page %}{{ urls.format('MEMBERLIST_PAGE', [(page.page + 1)]) }}{% else %}{{ urls.format('MEMBERLIST_INDEX') }}{% endif %}"{% if not page.active %} class="dropDownSelected"{% endif %}>All members</a>
{% for rank in page.ranks %} {% for rank in page.ranks %}
{% if not rank.rank_hidden or (rank.rank_hidden and page.active == rank.rank_id) %} {% if not rank.hidden or (rank.hidden and page.active == rank.id) %}
<a href="{% if page.sort %}{{ urls.format('MEMBERLIST_SORT_RANK', [page.sort, rank.rank_id]) }}{% else %}{{ urls.format('MEMBERLIST_RANK', [rank.rank_id]) }}{% endif %}" style="color: {{ rank.rank_colour }};"{% if page.active == rank.rank_id %} class="dropDownSelected"{% endif %}>{{ rank.rank_name }}{{ rank.rank_multiple }}</a> <a href="{% if page.sort %}{{ urls.format('MEMBERLIST_SORT_RANK', [page.sort, rank.id]) }}{% else %}{{ urls.format('MEMBERLIST_RANK', [rank.id]) }}{% endif %}" style="color: {{ rank.colour }};"{% if page.active == rank.id %} class="dropDownSelected"{% endif %}>{{ rank.name(true) }}</a>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</div> </div>
@ -66,19 +66,19 @@
#{{ page.active ? count + 1 : count }} #{{ page.active ? count + 1 : count }}
</td> </td>
<td> <td>
<a href="{{ urls.format('USER_PROFILE', [user.user_id]) }}" class="default" style="font-weight: bold; color: {{ page.ranks[user.rank_main].rank_colour }};">{{ user.username }}</a> <a href="{{ urls.format('USER_PROFILE', [user.data.user_id]) }}" class="default" style="font-weight: bold; color: {{ user.colour }}; text-shadow: 0 0 5px {{ user.colour }};">{{ user.data.username }}</a>
</td>
<td title="{{ user.data.user_registered|date(sakura.dateFormat) }}">
{{ user.elapsed.joined }}
</td>
<td title="{% if user.data.user_last_online == 0 %}Never logged in.{% else %}{{ user.data.user_last_online|date(sakura.dateFormat) }}{% endif %}">
{% if user.data.user_last_online == 0 %}<i>Never logged in.</i>{% else %}{{ user.elapsed.lastOnline }}{% endif %}
</td> </td>
<td> <td>
{{ user.regdate|date(sakura.dateFormat) }} {{ user.userTitle }}
</td> </td>
<td> <td>
{% if user.user_last_online == 0 %}<i>Never logged in.</i>{% else %}{{ user.user_last_online|date(sakura.dateFormat) }}{% endif %} <img src="{{ sakura.contentPath }}/images/flags/{{ user.country.short|lower }}.png" alt="{% if user.country.short|lower == 'xx' %}?{% else %}{{ user.country.long }}{% endif %}" title="{% if user.country.short|lower == 'xx' %}Unknown{% else %}{{ user.country.long }}{% endif %}" />
</td>
<td>
{% if not user.user_title %}<i>{{ page.ranks[user.rank_main].rank_title }}</i>{% else %}{{ user.user_title }}{% endif %}
</td>
<td>
<img src="{{ sakura.contentPath }}/images/flags/{{ user.user_country|lower }}.png" alt="{% if user.user_country|lower == 'eu' %}?{% else %}{{ user.user_country }}{% endif %}" />
</td> </td>
</tr> </tr>
</tbody> </tbody>
@ -86,11 +86,11 @@
</table> </table>
{% else %} {% else %}
{% for user in page.users[page.page] %} {% for user in page.users[page.page] %}
<a href="{{ urls.format('USER_PROFILE', [user.user_id]) }}">{# These comment tags are here to prevent the link extending too far <a href="{{ urls.format('USER_PROFILE', [user.data.user_id]) }}">{# These comment tags are here to prevent the link extending too far
#}<div class="userBox" id="u{{ user.user_id }}">{# #}<div class="userBox" id="u{{ user.data.user_id }}">{#
#}<img src="{{ sakura.contentPath }}/pixel.png" alt="{{ user.username }}" style="background: url('{{ urls.format('IMAGE_AVATAR', [user.user_id]) }}') no-repeat center / contain;" />{# #}<img src="{{ sakura.contentPath }}/pixel.png" alt="{{ user.data.username }}" style="background: url('{{ urls.format('IMAGE_AVATAR', [user.data.user_id]) }}') no-repeat center / contain;" />{#
#}<span class="userBoxUserName"{% if page.sort == page.sorts[1] %} style="color: {{ page.ranks[user.rank_main].rank_colour }};"{% endif %}>{# #}<span class="userBoxUserName" style="color: {{ user.colour }};">{#
#}{{ user.username }}{# #}{{ user.data.username }}{#
#}</span>{# #}</span>{#
#}</div>{# #}</div>{#
#}</a> #}</a>