106 lines
3.1 KiB
PHP
106 lines
3.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace RomhackPlaza\Master\Service\Club;
|
||
|
|
|
||
|
|
use RomhackPlaza\Master\Entity\Club;
|
||
|
|
use XF\App;
|
||
|
|
use XF\Entity\User;
|
||
|
|
use XF\Service\AbstractService;
|
||
|
|
|
||
|
|
class ModeratorService extends AbstractService
|
||
|
|
{
|
||
|
|
|
||
|
|
public const int MAX_MODS = 5;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var Club
|
||
|
|
*/
|
||
|
|
protected $club;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var User
|
||
|
|
*/
|
||
|
|
protected $user;
|
||
|
|
|
||
|
|
public function __construct(App $app, Club $club, User $moderator)
|
||
|
|
{
|
||
|
|
parent::__construct($app);
|
||
|
|
$this->club = $club;
|
||
|
|
$this->user = $moderator;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function countMods( int $nodeId, int $excludeUserId = 0 )
|
||
|
|
{
|
||
|
|
return $this->finder('XF:ModeratorContent')
|
||
|
|
->where('content_type', 'node')
|
||
|
|
->where('content_id', $nodeId)
|
||
|
|
->where('user_id', '!=', $excludeUserId)
|
||
|
|
->total();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function addModerator( string &$error ): bool
|
||
|
|
{
|
||
|
|
if( !$this->club->Forum || !$this->club->Forum->Node ) {
|
||
|
|
$error = "No node linked to this club.";
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if( $this->countMods( $this->club->node_id, $this->club->user_id ) > self::MAX_MODS ) {
|
||
|
|
$error = "The limit of 5 moderators has been reached.";
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$existingMod = $this->em()->findOne('XF:ModeratorContent', [
|
||
|
|
'content_type' => 'node',
|
||
|
|
'content_id' => $this->club->node_id,
|
||
|
|
'user_id' => $this->user->user_id
|
||
|
|
]);
|
||
|
|
if( $existingMod ){
|
||
|
|
$error = "This user is already a moderator.";
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$modContent = $this->em()->create('XF:ModeratorContent');
|
||
|
|
$modContent->content_type = 'node';
|
||
|
|
$modContent->content_id = $this->club->node_id;
|
||
|
|
$modContent->user_id = $this->user->user_id;
|
||
|
|
$modContent->save();
|
||
|
|
|
||
|
|
$permissionUpdater = \XF::app()->service('XF:UpdatePermissions');
|
||
|
|
$permissionUpdater->setContent('node', $this->club->node_id);
|
||
|
|
$permissionUpdater->setUser($this->user);
|
||
|
|
$permissionUpdater->updatePermissions(Club::MOD_PERMISSIONS);
|
||
|
|
|
||
|
|
\XF::app()->jobManager()->enqueueUnique('permissionRebuild', 'XF:PermissionRebuild');
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function deleteModerator( string &$error ): bool
|
||
|
|
{
|
||
|
|
if( !$this->club->Forum || !$this->club->Forum->Node ) {
|
||
|
|
$error = "No node linked to this club.";
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if( $this->user->user_id === $this->club->user_id ){
|
||
|
|
$error = "You cannot delete yourself.";
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$mod = $this->em()->findOne('XF:ModeratorContent', [
|
||
|
|
'content_type' => 'node',
|
||
|
|
'content_id' => $this->club->node_id,
|
||
|
|
'user_id' => $this->user->user_id
|
||
|
|
]);
|
||
|
|
|
||
|
|
if( $mod ){
|
||
|
|
$mod->delete();
|
||
|
|
|
||
|
|
\XF::db()->delete('xf_permission_entry_content', 'content_type = ? AND content_id = ? AND user_id = ?', ['node', $this->club->node_id, $this->user->user_id]);
|
||
|
|
|
||
|
|
\XF::app()->jobManager()->enqueueUnique('permissionRebuild', 'XF:PermissionRebuild');
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|