This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/libraries/Rank.php

95 lines
2.4 KiB
PHP
Raw Normal View History

<?php
/*
* Rank Class
*/
namespace Sakura;
2015-12-29 21:52:19 +00:00
use Sakura\Perms;
use Sakura\Perms\Site;
/**
* Class Rank
* @package Sakura
*/
class Rank
{
2016-01-17 01:58:31 +00:00
// Variables
public $id = 0;
public $name = 'Rank';
public $hierarchy = 0;
public $multiple = '';
public $colour = 'inherit';
public $description = '';
public $title = '';
private $hidden = true;
2015-12-29 21:52:19 +00:00
private $permissions;
protected static $_rankCache = [];
// Static initialiser
public static function construct($rid, $forceRefresh = false)
{
2015-12-29 21:52:19 +00:00
// Check if a rank object isn't present in cache
if ($forceRefresh || !array_key_exists($rid, self::$_rankCache)) {
// If not create a new object and cache it
self::$_rankCache[$rid] = new Rank($rid);
}
// Return the cached object
return self::$_rankCache[$rid];
}
// Initialise the rank object
2015-12-29 21:52:19 +00:00
private function __construct($rid)
{
// Get the rank database row
2016-01-17 01:58:31 +00:00
$rankRow = Database::fetch(
'ranks',
false,
[
'rank_id' => [$rid, '=', true],
]
);
// Check if the rank actually exists
2016-01-17 01:58:31 +00:00
if ($rankRow) {
$this->id = $rankRow['rank_id'];
$this->name = $rankRow['rank_name'];
$this->hierarchy = $rankRow['rank_hierarchy'];
$this->multiple = $rankRow['rank_multiple'];
$this->hidden = (bool) $rankRow['rank_hidden'];
$this->colour = $rankRow['rank_colour'];
$this->description = $rankRow['rank_description'];
$this->title = $rankRow['rank_title'];
}
2015-12-29 21:52:19 +00:00
// Init the permissions
$this->permissions = new Perms(Perms::SITE);
}
// Get the rank name
public function name($multi = false)
{
2016-01-17 01:58:31 +00:00
return $this->name . ($multi ? $this->multiple : null);
}
// Check if the rank is hidden
public function hidden()
{
2016-01-17 01:58:31 +00:00
return $this->hidden || $this->permission(Site::DEACTIVATED) || $this->permission(Site::RESTRICTED);
}
// Check if the rank has the proper permissions
2015-12-29 21:52:19 +00:00
public function permission($flag)
{
2015-12-29 21:52:19 +00:00
// Set default permission value
$perm = 0;
// Bitwise OR it with the permissions for this forum
2016-01-17 01:58:31 +00:00
$perm = $perm | $this->permissions->rank($this->id);
2015-12-29 21:52:19 +00:00
return $this->permissions->check($flag, $perm);
}
}