A lot of things.
This commit is contained in:
64
Service/Club/ApproverService.php
Normal file
64
Service/Club/ApproverService.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace RomhackPlaza\Master\Service\Club;
|
||||
|
||||
use RomhackPlaza\Master\Entity\Club;
|
||||
use XF\App;
|
||||
use XF\Entity\Forum;
|
||||
use XF\Entity\Node;
|
||||
use XF\Service\AbstractService;
|
||||
use XF\Util\File;
|
||||
|
||||
class ApproverService extends AbstractService
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Club
|
||||
*/
|
||||
protected $club;
|
||||
|
||||
public function __construct(App $app, Club $club)
|
||||
{
|
||||
parent::__construct($app);
|
||||
|
||||
$this->club = $club;
|
||||
}
|
||||
|
||||
public function approve(): bool
|
||||
{
|
||||
if( $this->club->club_state !== 'moderated' ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->club->club_state = 'visible';
|
||||
$this->club->save();
|
||||
|
||||
/** @var Node $node */
|
||||
$node = $this->em()->create('XF:Node');
|
||||
|
||||
$node->node_type_id = 'Forum';
|
||||
$node->title = $this->club->title;
|
||||
$node->description = $this->club->description;
|
||||
$node->parent_node_id = \XF::options()->rhpz_club_node_id ?? 0;
|
||||
$node->display_order = 1;
|
||||
$node->save();
|
||||
|
||||
/** @var Forum $forum */
|
||||
$forum = $this->em()->create('XF:Forum');
|
||||
$forum->node_id = $node->node_id;
|
||||
$forum->allow_posting = true;
|
||||
$forum->save();
|
||||
|
||||
$this->club->node_id = $node->node_id;
|
||||
$this->club->save();
|
||||
|
||||
$user = $this->club->User;
|
||||
if( $user ){
|
||||
/** @var ModeratorService $modManage */
|
||||
$modManage = \XF::app()->service('RomhackPlaza\Master:Club\Moderator', $this->club, $user );
|
||||
$modManage->addModerator( $error );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
99
Service/Club/CreatorService.php
Normal file
99
Service/Club/CreatorService.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace RomhackPlaza\Master\Service\Club;
|
||||
|
||||
use RomhackPlaza\Master\Entity\Club;
|
||||
use RomhackPlaza\Master\XF\Entity\Forum;
|
||||
use XF\App;
|
||||
use XF\Entity\Node;
|
||||
use XF\Entity\User;
|
||||
use XF\Http\Upload;
|
||||
use XF\Service\AbstractService;
|
||||
use XF\Service\ValidateAndSavableTrait;
|
||||
use XF\Util\File;
|
||||
|
||||
class CreatorService extends AbstractService
|
||||
{
|
||||
use ValidateAndSavableTrait;
|
||||
|
||||
/**
|
||||
* @var Club
|
||||
*/
|
||||
protected $club;
|
||||
|
||||
/**
|
||||
* @var Upload
|
||||
*/
|
||||
protected $bannerUpload;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
public function __construct(App $app, User $creator)
|
||||
{
|
||||
parent::__construct($app);
|
||||
$this->club = $this->em()->create('RomhackPlaza\Master:Club');
|
||||
$this->setUser($creator);
|
||||
$this->setupDefaults();
|
||||
}
|
||||
|
||||
protected function setupDefaults()
|
||||
{
|
||||
$this->club->club_state = 'moderated';
|
||||
}
|
||||
|
||||
public function getClub()
|
||||
{
|
||||
return $this->club;
|
||||
}
|
||||
|
||||
public function getUser()
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function setUser(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->club->user_id = $this->user->user_id;
|
||||
}
|
||||
|
||||
public function setContent(string $title, string $description )
|
||||
{
|
||||
$this->club->title = $title;
|
||||
$this->club->description = $description;
|
||||
}
|
||||
|
||||
public function setBannerUpload(?Upload $upload = null)
|
||||
{
|
||||
if ($upload) {
|
||||
$upload->requireImage();
|
||||
if($upload->isValid()){
|
||||
$this->bannerUpload = $upload;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function _validate()
|
||||
{
|
||||
$this->club->preSave();
|
||||
return $this->club->getErrors();
|
||||
}
|
||||
|
||||
protected function _save()
|
||||
{
|
||||
$this->club->save();
|
||||
|
||||
if( $this->bannerUpload ){
|
||||
$path = 'data://club_banners/' . $this->club->club_id . '.jpg';
|
||||
if( File::copyFileToAbstractedPath($this->bannerUpload->getFileWrapper()->getFilePath(), $path) ){
|
||||
$this->club->banner_date = \XF::$time;
|
||||
$this->club->save();
|
||||
}
|
||||
}
|
||||
|
||||
return $this->club;
|
||||
}
|
||||
}
|
||||
40
Service/Club/DeleterService.php
Normal file
40
Service/Club/DeleterService.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace RomhackPlaza\Master\Service\Club;
|
||||
|
||||
use RomhackPlaza\Master\Entity\Club;
|
||||
use XF\App;
|
||||
use XF\Entity\Forum;
|
||||
use XF\Entity\Node;
|
||||
use XF\Http\Upload;
|
||||
use XF\Service\AbstractService;
|
||||
use XF\Service\ValidateAndSavableTrait;
|
||||
use XF\Util\File;
|
||||
|
||||
class DeleterService extends AbstractService
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Club
|
||||
*/
|
||||
protected $club;
|
||||
|
||||
public function __construct(App $app, Club $club)
|
||||
{
|
||||
parent::__construct($app);
|
||||
|
||||
$this->club = $club;
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
if( $this->club->Forum && $this->club->Forum->Node ){
|
||||
$this->club->Forum->Node->delete();
|
||||
}
|
||||
|
||||
$path = 'data://club_banners/' . $this->club->club_id . '.jpg';
|
||||
File::deleteFromAbstractedPath($path);
|
||||
|
||||
$this->club->delete();
|
||||
}
|
||||
}
|
||||
73
Service/Club/EditorService.php
Normal file
73
Service/Club/EditorService.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace RomhackPlaza\Master\Service\Club;
|
||||
|
||||
use RomhackPlaza\Master\Entity\Club;
|
||||
use XF\App;
|
||||
use XF\Entity\Forum;
|
||||
use XF\Entity\Node;
|
||||
use XF\Http\Upload;
|
||||
use XF\Service\AbstractService;
|
||||
use XF\Service\ValidateAndSavableTrait;
|
||||
use XF\Util\File;
|
||||
|
||||
class EditorService extends AbstractService
|
||||
{
|
||||
use ValidateAndSavableTrait;
|
||||
|
||||
/**
|
||||
* @var Club
|
||||
*/
|
||||
protected $club;
|
||||
|
||||
public function __construct(App $app, Club $club)
|
||||
{
|
||||
parent::__construct($app);
|
||||
|
||||
$this->club = $club;
|
||||
}
|
||||
|
||||
public function setContent(string $title, string $description)
|
||||
{
|
||||
$this->club->title = $title;
|
||||
$this->club->description = $description;
|
||||
}
|
||||
|
||||
public function setBannerUpload(?Upload $upload = null)
|
||||
{
|
||||
if( $upload ){
|
||||
$upload->requireImage();
|
||||
if( $upload->isValid() ){
|
||||
$path = 'data://club_banners/' . $this->club->club_id . '.jpg';
|
||||
if(File::copyFileToAbstractedPath($upload->getFileWrapper()->getFilePath(), $path)){
|
||||
$this->club->banner_date = \XF::$time;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function syncForumNode()
|
||||
{
|
||||
if( $this->club->Forum && $this->club->Forum->Node )
|
||||
{
|
||||
$node = $this->club->Forum->Node;
|
||||
$node->title = $this->club->title;
|
||||
$node->description = $this->club->description;
|
||||
$node->save();
|
||||
}
|
||||
}
|
||||
|
||||
protected function _validate()
|
||||
{
|
||||
$this->club->preSave();
|
||||
return $this->club->getErrors();
|
||||
}
|
||||
|
||||
protected function _save()
|
||||
{
|
||||
$this->club->save();
|
||||
$this->syncForumNode();
|
||||
|
||||
return $this->club;
|
||||
}
|
||||
}
|
||||
106
Service/Club/ModeratorService.php
Normal file
106
Service/Club/ModeratorService.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user