2015-10-10 21:17:50 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the rank object class.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-03 22:22:56 +00:00
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-10-10 21:17:50 +00:00
|
|
|
namespace Sakura;
|
|
|
|
|
2015-12-29 21:52:19 +00:00
|
|
|
use Sakura\Perms;
|
|
|
|
use Sakura\Perms\Site;
|
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Serves Rank data.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2015-10-18 19:06:30 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-10-18 19:06:30 +00:00
|
|
|
*/
|
2015-10-10 21:17:50 +00:00
|
|
|
class Rank
|
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* ID of the rank.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $id = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the rank.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $name = 'Rank';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Global hierarchy of the rank.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $hierarchy = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Text that should be append to the name to make it address multiple.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $multiple = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The rank's username colour.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $colour = 'inherit';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Description of the rank.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $description = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User title of the rank.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $title = '';
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates if this rank should be hidden.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var bool
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
private $hidden = true;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Permission container.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var Perms
|
|
|
|
*/
|
2015-12-29 21:52:19 +00:00
|
|
|
private $permissions;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance cache container.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2015-12-29 21:52:19 +00:00
|
|
|
protected static $_rankCache = [];
|
2016-02-05 11:20:33 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Cached constructor.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $rid ID of the rank.
|
|
|
|
* @param bool $forceRefresh Force a cache refresh.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return Rank The requested rank object.
|
|
|
|
*/
|
2016-01-02 17:55:31 +00:00
|
|
|
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];
|
|
|
|
}
|
2015-10-10 21:17:50 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $rid ID of the rank that should be constructed.
|
|
|
|
*/
|
2015-12-29 21:52:19 +00:00
|
|
|
private function __construct($rid)
|
2015-10-14 19:35:16 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
// Get the rank database row
|
2016-02-18 23:28:44 +00:00
|
|
|
$rankRow = DB::prepare('SELECT * FROM `{prefix}ranks` WHERE `rank_id` = :id');
|
|
|
|
$rankRow->execute([
|
|
|
|
'id' => $rid,
|
|
|
|
]);
|
|
|
|
$rankRow = $rankRow->fetch();
|
2015-10-14 19:35:16 +00:00
|
|
|
|
|
|
|
// Check if the rank actually exists
|
2016-01-17 01:58:31 +00:00
|
|
|
if ($rankRow) {
|
2016-02-18 23:28:44 +00:00
|
|
|
$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-10-14 19:35:16 +00:00
|
|
|
}
|
2015-12-29 21:52:19 +00:00
|
|
|
|
|
|
|
// Init the permissions
|
|
|
|
$this->permissions = new Perms(Perms::SITE);
|
2015-10-14 19:35:16 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get the name of the rank.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param bool $multi Should the multiple sense be appended?
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return string The rank's name.
|
|
|
|
*/
|
2015-10-14 19:35:16 +00:00
|
|
|
public function name($multi = false)
|
|
|
|
{
|
2016-01-17 01:58:31 +00:00
|
|
|
return $this->name . ($multi ? $this->multiple : null);
|
2015-10-14 19:35:16 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Indicates if the rank is hidden.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return bool Hidden status.
|
|
|
|
*/
|
2015-10-14 19:35:16 +00:00
|
|
|
public function hidden()
|
|
|
|
{
|
2016-01-17 01:58:31 +00:00
|
|
|
return $this->hidden || $this->permission(Site::DEACTIVATED) || $this->permission(Site::RESTRICTED);
|
2015-10-14 19:35:16 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Check permissions.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $flag Permission flag that should be checked.
|
2016-02-05 11:20:33 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return bool Success indicator.
|
|
|
|
*/
|
2015-12-29 21:52:19 +00:00
|
|
|
public function permission($flag)
|
2015-10-14 19:35:16 +00:00
|
|
|
{
|
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);
|
2016-02-05 11:20:33 +00:00
|
|
|
|
2015-12-29 21:52:19 +00:00
|
|
|
return $this->permissions->check($flag, $perm);
|
2015-10-14 19:35:16 +00:00
|
|
|
}
|
2016-02-05 11:20:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all users that are part of this rank.
|
|
|
|
*
|
|
|
|
* @param bool $justIds Makes this function only return the user ids when set to a positive value.
|
|
|
|
*
|
|
|
|
* @return array Either just the user IDs of the users or with objects.
|
|
|
|
*/
|
|
|
|
public function users($justIds = false)
|
|
|
|
{
|
|
|
|
// Fetch all users part of this rank
|
2016-02-18 23:28:44 +00:00
|
|
|
$fetch = DB::prepare('SELECT `user_id` FROM `{prefix}user_ranks` WHERE `rank_id` = :id');
|
|
|
|
$fetch->execute([
|
|
|
|
'id' => $this->id,
|
|
|
|
]);
|
|
|
|
$userIds = array_column($fetch->fetchAll(\PDO::FETCH_ASSOC), 'user_id');
|
2016-02-05 11:20:33 +00:00
|
|
|
|
|
|
|
// Just return that if we were asked for just the ids
|
|
|
|
if ($justIds) {
|
|
|
|
return $userIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the storage array
|
|
|
|
$users = [];
|
|
|
|
|
|
|
|
// Create User objects and store
|
|
|
|
foreach ($userIds as $id) {
|
|
|
|
$users[$id] = User::construct($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the array
|
|
|
|
return $users;
|
|
|
|
}
|
2015-10-10 21:17:50 +00:00
|
|
|
}
|