<?php
namespace App\Controller\IRMS;
use App\Controller\BaseController;
use App\Controller\SecurePageInterface;
use DateTime;
use Empire\Access\Access;
use Empire\Access\Key;
use Empire\Content\Category;
use Empire\Content\Content;
use Empire\Core\Approval;
use Empire\Core\Branch;
use Empire\Core\Core;
use Empire\Core\Event;
use Empire\Core\MeetingTime;
use Empire\Core\News;
use Empire\Core\Password;
use Empire\Core\Page;
use Empire\Core\PageCategory;
use Empire\Core\Position;
use Empire\Core\Rank;
use Empire\Core\Unit;
use Empire\Core\User;
use Empire\Legacy\members;
use Exception;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class IrmsController
* @package App\Controller
* @Route("/irms")
*/
class IrmsController extends BaseController implements SecurePageInterface
{
/**
* @Route("", name="irms")
* @param Request $request
* @return Response
* @throws Exception
*/
public function portal(Request $request): Response
{
$eventNumber = 15;
$page = $request->query->get('e', 0);
$offset = $page * $eventNumber;
return $this->render('irms/portal.twig', [
'gejs' => ['ajax', 'inplace', 'events', 'award'],
'news' => News::getLatestNews(News::NEWS_TYPE_DEV),
'events' => Event::getUnitEvents(Core::user(), true, $offset, $eventNumber),
'page' => $page,
'ranks' => Rank::getLatest(Core::user()),
'awards' => members::getLatestAwards(5),
'meeting' => new MeetingTime(),
]);
}
/**
* @return Response
* @throws Exception
* @Route("/menu")
*/
public function menu(): Response
{
$irms = PageCategory::loadAll(Page::PAGE_TYPE_SECURE);
$ing = PageCategory::loadAll(Page::PAGE_TYPE_PUBLIC);
return $this->render('irms/menu.twig', [
'gejs' => ['ajax', 'switcher'],
'irms' => $irms,
'ing' => $ing,
]);
}
/**
* @return Response
* @throws Exception
* @Route("/content", methods={"GET"})
*/
public function content(): Response
{
$ccats = Category::loadAll();
$clist = [];
foreach ($ccats as $category) {
$clist[] = ['id' => $category->getID(), 'name' => $category->getName()];
}
return $this->render('irms/content.twig', [
'gejs' => ['ajax', 'switcher'],
'ccats' => $ccats,
'clist' => json_encode($clist),
'admin' => Core::user()->hasAccess(['content' => 2]),
'title' => 'Content Management',
]);
}
/**
* @param Request $request
* @return string
* @throws
* @Route("/content", methods={"POST"})
*/
public function content_post(Request $request): string
{
if ($request->get('mode') === 'page-edit') {
$page = Content::load([$request->get('id')]);
if (Content::isValidPageSlug($request->get('url'))) {
$page->updateFields([
'CCAT_ID' => $request->get('category'),
'CONT_PAGE' => Content::sanitiseUrl($request->get('url')),
'CONT_UPDATED' => (new DateTime())->format('Y-m-d H:i:s')]);
} else {
$this->addFlash('error', 'While attempting to update page <b>' . $page->getName() . '</b> the following error occured:<br>Invalid characters for the url slug. Allowed characters are <code>a-z A-Z 0-9 . _ -</code>.');
}
} elseif ($request->get('mode') === 'add-page') {
try {
Content::addPage($request->get('title'), $request->get('url'), $request->get('category'));
} catch (Exception $e) {
$this->addFlash('error', 'Trying to add page <b>' . $request->get('title') . '</b>, but an error occurred.<br><pre>' . $e->getMessage() . '</pre>');
}
} elseif ($request->get('mode') === 'add-category') {
Category::addCategory($request->get('name'));
} elseif ($request->get('mode') === 'add-position') {
Access::setPositionKeys(Position::load([$request->get('position')]), [['!content', $request->get('category')]], false);
} elseif ($request->get('mode') === 'delete-position') {
Access::unsetPositionKeys(Position::load([$request->get('position')]), [['!content', $request->get('category')]]);
} else {
var_dump($_REQUEST);
return "";
}
return $this->redirect("content");
}
/**
* @return Response
* @Route("/relations")
*/
public function relations(): Response
{
return $this->render('irms/relations.twig', [
'gejs' => ['ajax'],
'title' => "Manage Diplomatic Relations"
]);
}
/**
* @return Response
* @Route("/structure")
*/
public function structure(): Response
{
$branches = Branch::loadAll();
$tree = Unit::treeify(Unit::loadEditable(Core::user()));
if (Access::isDev(Core::user())) {
$keys = Key::loadAll(Key::ORDER_BY_CAT);
} else {
$keys = Access::userKeys(Core::user());
}
return $this->render('irms/structure.twig', [
'gejs' => ['ajax'],
'title' => 'Organisational Structure Management',
'branches' => $branches,
'tree' => $tree,
'tiers' => Position::POSITION_TIER_NAMES,
'keys' => $keys,
'regions' => Unit::REGIONS,
]);
}
/**
* @return Response
* @Route("/units")
*/
public function units(): Response
{
$branches = Branch::loadAll();
$tree = Unit::treeify(Unit::loadEditable(Core::user()));
if (Access::isDev(Core::user())) {
$keys = Key::loadAll(Key::ORDER_BY_CAT);
} else {
$keys = Access::userKeys(Core::user());
}
return $this->render('irms/units.twig', [
'gejs' => ['ajax'],
'title' => 'Units',
'branches' => $branches,
'tree' => $tree,
'tiers' => Position::POSITION_TIER_NAMES,
'keys' => $keys,
'regions' => Unit::REGIONS,
]);
}
/**
* @return Response
* @Route("/units/addMember", methods={"GET"})
*/
public function unitsAddMember(): Response
{
$branches = Branch::loadAll();
$tree = Unit::treeify(Unit::loadEditable(Core::user()));
return $this->render('irms/unitadd.twig', [
'gejs' => ['ajax'],
'title' => 'Add Member to Unit',
'branches' => $branches,
'tree' => $tree
]);
}
/**
* @param Request $request
* @return Response
* @throws Exception
* @Route("/units/addMember", methods={"POST"})
*/
public function unitsAddMember_post(Request $request): Response
{
$name = $request->request->get('name');
if(User::exists($name)){
$position = $request->request->get('position');
$user = User::loadByName($name);
$uid = $user->getID();
$user->addPosition(intval($position));
$this->addFlash('success', 'Added to position!');
return $this->redirect("/irms/profile~$uid");
} else {
$this->addFlash('error', 'User does not exist!');
return $this->redirect("/irms/units/addMember");
}
}
/**
* @param Request $request
* @return Response
* @throws Exception
* @Route("/adminevents", methods={"GET"})
*/
public function adminevents(Request $request): Response
{
$type = $request->get('type') ? $request->get('type') : Event::EVENTTYPES['ADMIN'];
$min = $request->get('min') ? $request->get('min') : 0;
$max = $request->get('max') ? $request->get('max') : 100;
$events = Event::getByType(intval($type), intval($min), intval($max));
return $this->render('irms/events.twig', [
'gejs' => ['ajax'],
'title' => "Admin Events",
'events' => $events,
'types' => Event::EVENTTYPES,
]);
}
/**
* @return Response
* @Route("/library")
* @throws Exception
*/
public function library(): Response
{
$branches = [
Branch::loadByName("Imperial Throne"),
Branch::loadByName("COMPNOR"),
Branch::loadByName("Imperial Army"),
Branch::loadByName("Imperial Navy"),
Branch::loadByName("Regional Government"),
Branch::loadByName("Ministry of Industry")
];
$branches[0]->name = "Empire";
return $this->render('irms/doclib.twig', [
'gejs' => ['ajax'],
'title' => "Document Library Management",
'branches' => $branches
]);
}
/**
* @return Response
* @Route("/forum")
*/
public function forum(): Response
{
return $this->render('irms/forum.twig', [
'gejs' => ['ajax'],
'title' => 'Forum Access Management'
]);
}
/**
* @param int $member
* @return Response
* @Route("/adminresetpassword~{id}")
*/
public function adminResetPassword(int $id)
{
if(!Core::user()->hasAccess('dev')) {
$this->addFlash('error', 'Unauthorised access!');
return $this->render('irms/403.twig', ['gejs' => ['ajax']]);
}
$user = User::load([$id]);
Password::forceResetPassword($user, $this);
return $this->redirect('/irms');
}
/**
* @return Response
* @Route("/notifications", methods={"GET"}, name="notifications")
* @throws Exception
*/
public function notifications(): Response
{
return $this->render('irms/notifications.twig', [
'gejs' => ['ajax'],
'title' => "Notifications",
'notifications' => Core::user()->getNotifications(),
'oldNotifications' => Core::user()->getOldNotifications(),
]);
}
/**
* @return Response
* @Route("/approvals", methods={"GET"}, name="approvals")
* @throws Exception
*/
public function approvals(): Response
{
return $this->render('irms/approvals.twig', [
'gejs' => ['ajax'],
'title' => "Approvals",
'approvals' => Core::user()->getApprovals(),
'oldApprovals' => Core::user()->getOldApprovals(),
]);
}
}