182 lines
6.4 KiB
PHP
182 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace RomhackPlaza\Master\Pub\Controller;
|
|
|
|
use PhpParser\Node\Param;
|
|
use RomhackPlaza\Master\Service\Club\CreatorService;
|
|
use RomhackPlaza\Master\Service\Club\DeleterService;
|
|
use RomhackPlaza\Master\Service\Club\EditorService;
|
|
use RomhackPlaza\Master\Service\Club\ModeratorService;
|
|
use XF\Mvc\ParameterBag;
|
|
use XF\Pub\Controller\AbstractController;
|
|
use XF\Util\File;
|
|
|
|
class Club extends AbstractController
|
|
{
|
|
|
|
public function actionIndex(){
|
|
$clubsForum = \XF::options()->rhpz_club_node_id;
|
|
return $this->redirect('categories/.' . $clubsForum );
|
|
}
|
|
|
|
public function actionSubmit(ParameterBag $params)
|
|
{
|
|
$this->assertRegistrationRequired();
|
|
|
|
return $this->view('RomhackPlaza\Master:Club\Request', 'club_request_form');
|
|
}
|
|
|
|
public function actionEdit(ParameterBag $params)
|
|
{
|
|
$this->assertRegistrationRequired();
|
|
|
|
if( !$params->club_id )
|
|
$this->notFound("Club not found");
|
|
|
|
/** @var \RomhackPlaza\Master\Entity\Club $club */
|
|
$club = $this->assertRecordExists( 'RomhackPlaza\Master:Club', $params->club_id );
|
|
if( $club->user_id !== \XF::visitor()->user_id && !\XF::visitor()->hasAdminPermission('node') ){
|
|
$this->noPermission();
|
|
}
|
|
|
|
$mods = [];
|
|
$modsCount = 0;
|
|
|
|
if( $club->node_id ){
|
|
$mods = $this->finder('XF:ModeratorContent')
|
|
->where('content_type', 'node')
|
|
->where('content_id', $club->node_id)
|
|
->with('User')
|
|
->fetch();
|
|
foreach( $mods as $m ){
|
|
if( $m->user_id !== $club->user_id ){
|
|
$modsCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->view('RomhackPlaza\Master:Club\Edit', 'club_edit_form', [ 'club' => $club, 'moderators' => $mods, 'subModCount' => $modsCount ] );
|
|
}
|
|
|
|
public function actionSave(ParameterBag $params)
|
|
{
|
|
$this->assertRegistrationRequired();
|
|
$this->assertPostOnly();
|
|
|
|
$isEdit = (bool) $params->club_id;
|
|
if( $isEdit ){
|
|
$club = $this->assertRecordExists('RomhackPlaza\Master:Club', $params->club_id );
|
|
if( $club->user_id !== \XF::visitor()->user_id && !\XF::visitor()->hasAdminPermission('node') ){
|
|
$this->noPermission();
|
|
}
|
|
|
|
/** @var EditorService $editor */
|
|
$editor = $this->service('RomhackPlaza\Master:Club\Editor', $club);
|
|
$editor->setContent($this->filter('title','str'), $this->filter('description','str'));
|
|
$editor->setBannerUpload($this->request->getFile('upload_banner', false, false) );
|
|
|
|
if( !$editor->validate($errors) ){
|
|
return $this->error($errors);
|
|
}
|
|
|
|
$editor->save();
|
|
|
|
return $this->redirect( $this->buildLink('clubs'), "Changes saved successfully" );
|
|
|
|
} else {
|
|
|
|
/** @var CreatorService $creator */
|
|
$creator = $this->service('RomhackPlaza\Master:Club\Creator', \XF::visitor());
|
|
$creator->setContent($this->filter('title','str'), $this->filter('description','str'));
|
|
$creator->setBannerUpload($this->request->getFile('upload_banner', false, false));
|
|
|
|
if( !$creator->validate($errors) ){
|
|
return $this->error($errors);
|
|
}
|
|
$creator->save();
|
|
return $this->redirect( $this->buildLink('clubs'), "Your club creation request has been submitted and is awaiting approval by a staff member." );
|
|
}
|
|
}
|
|
|
|
public function actionDelete(ParameterBag $params)
|
|
{
|
|
$this->assertRegistrationRequired();
|
|
|
|
/** @var \RomhackPlaza\Master\Entity\Club $club */
|
|
$club = $this->assertRecordExists('RomhackPlaza\Master:Club', $params->club_id);
|
|
if( $club->user_id !== \XF::visitor()->user_id && !\XF::visitor()->hasAdminPermission('node') ){
|
|
$this->noPermission();
|
|
}
|
|
|
|
if( $this->isPost() ){
|
|
/** @var DeleterService $deleter */
|
|
$deleter = $this->service('RomhackPlaza\Master:Club\Deleter', $club);
|
|
$deleter->delete();
|
|
|
|
return $this->redirect( $this->buildLink('clubs'), "Club deleted successfully." );
|
|
}
|
|
|
|
return $this->view('RomhackPlaza\Master:Club\Delete', 'club_delete_confirm', [ 'club' => $club ] );
|
|
}
|
|
|
|
public function actionAddModerator(ParameterBag $params)
|
|
{
|
|
$this->assertRegistrationRequired();
|
|
$this->assertPostOnly();
|
|
|
|
/** @var \RomhackPlaza\Master\Entity\Club $club */
|
|
$club = $this->assertRecordExists('RomhackPlaza\Master:Club', $params->club_id);
|
|
if( $club->user_id !== \XF::visitor()->user_id && !\XF::visitor()->hasAdminPermission('node') ){
|
|
return $this->noPermission();
|
|
}
|
|
|
|
$username = $this->filter('username','str');
|
|
$user = $this->em()->findOne('XF:User', ['username' => $username]);
|
|
|
|
if( !$user ){
|
|
return $this->error("This user doesn't exist");
|
|
}
|
|
|
|
/** @var ModeratorService $modManage */
|
|
$modManage = \XF::app()->service('RomhackPlaza\Master:Club\Moderator', $club, $user );
|
|
$error = "";
|
|
$modManage->addModerator( $error );
|
|
|
|
if( $error && $error !== "" ){
|
|
return $this->error($error);
|
|
}
|
|
|
|
return $this->redirect( $this->buildLink('clubs/edit', $club), "Moderator added successfully." );
|
|
}
|
|
|
|
public function actionRemoveModerator(ParameterBag $params)
|
|
{
|
|
$this->assertRegistrationRequired();
|
|
$this->assertPostOnly();
|
|
|
|
/** @var \RomhackPlaza\Master\Entity\Club $club */
|
|
$club = $this->assertRecordExists('RomhackPlaza\Master:Club', $params->club_id);
|
|
if( $club->user_id !== \XF::visitor()->user_id && !\XF::visitor()->hasAdminPermission('node') ){
|
|
$this->noPermission();
|
|
}
|
|
|
|
$userId = $this->filter('user_id','uint');
|
|
$user = $this->em()->findOne('XF:User', ['user_id' => $userId]);
|
|
|
|
if( !$user ){
|
|
$this->error("This user doesn't exist");
|
|
}
|
|
|
|
/** @var ModeratorService $modManage */
|
|
$modManage = \XF::app()->service('RomhackPlaza\Master:Club\Moderator', $club, $user );
|
|
$error = "";
|
|
$modManage->deleteModerator( $error );
|
|
|
|
if( $error && $error !== "" ){
|
|
return $this->error($error);
|
|
}
|
|
|
|
return $this->redirect( $this->buildLink('clubs/edit', $club), "Moderator deleted successfully." );
|
|
}
|
|
|
|
} |