<?php
namespace App\Controller\Admin;
use App\Domain\Entity\Folder;
use App\Domain\Entity\FolderUser;
use App\Domain\Entity\Project;
use App\Domain\Entity\User;
use App\Service\FolderService;
use App\Service\FolderUserService;
use App\Service\ProjectService;
use App\Service\UserService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
/**
* @Route("/", methods={"GET"}, name="home")
*/
public function home(): Response
{
return $this->redirectToRoute('investor_home');
}
/**
* @Route("/admin", methods={"GET"}, name="admin_home")
*/
public function homeAdmin(
ProjectService $projectService,
UserService $userService,
FolderService $folderService): Response
{
$this->denyAccessUnlessGranted(User::ROLE_SIMPLE_ADMIN);
$projectsPending = [];
$allProjects = $projectService->getProjects();
foreach ($allProjects as $p) {
/** @var Project $p */
//If status not in [Project::STATUS_REVOKED, Project::STATUS_APPROVED]
if (!in_array($p->getStatus(), [Project::STATUS_REVOKED, Project::STATUS_APPROVED])) {
$projectsPending[] = $p;
continue;
}
//if has users to be validated
if ($p->getPendingCount() > 0) {
$projectsPending[] = $p;
continue;
}
}
return $this->render('admin/home.html.twig',
[
'generalTitle' => 'Squircle Capital Admin',
'baseTitle' => 'Dashboard',
'projectCount' => $projectService->getProjectsCount(),
'pendingProjects' => $projectsPending,
'pendingFolders' => $folderService->getFoldersPending(),
'userCount' => $userService->getUsersCount(),
'projectCount' => $projectService->getProjectsCount(),
'folderCount' => $folderService->getFolderCount()
]);
}
/**
* @Route("/admin/folder-manage/{action}", name="admin_home_folder_manage_investors_grant_revoke", methods={"POST"})
* @param Request $request
* @param string $action
* @param ProjectService $projectService
* @return Response
*/
public function manageHomeFoldersStatus(
Request $request,
string $action,
ProjectService $projectService): Response
{
$this->denyAccessUnlessGranted(User::ROLE_SIMPLE_ADMIN);
/** @var User $user */
$user = $this->getUser();
//we are receiving folders but in this case, they are projects ids.
$projects = $request->get('folders');
foreach ($projects as $projectId) {
$project = $projectService->getOneById($projectId);
if ($project) {
$projectService->updateStatus($project, $action, $user);
}
}
$this->addFlash('success','Projects updated!');
$response = new Response();
$response->setContent(json_encode([
'message' => 'Folders status edited ok',
]));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}