<?php
namespace App\Controller\Front;
use App\Domain\Entity\Project;
use App\Domain\Entity\User;
use App\Service\FolderService;
use App\Service\MailService;
use App\Service\ProjectLogService;
use App\Service\ProjectService;
use App\Service\UserService;
use Exception;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class InvestorController extends AbstractController
{
/**
*
* @Route("/investor", name="investor_home")
* @param FolderService $folderService
* @return Response
*/
public function investorHome(FolderService $folderService, UserService $userService): Response
{
/** @var User $user */
$user = $this->getUser();
if (!$user->isAcceptedPolicy()) {
return $this->redirectToRoute('investor_privacy');
}
//Check if impersonating user
$data = [];
if ($this->isGranted('ROLE_ADMIN')) {
$userId = $this->get('session')->get('impersonated_user');
if (!empty($userId)) {
$user = $userService->getOneById($userId);
if (empty($user)) {
$user = $this->getUser();
}
$this->addFlash('success', 'You\'re seeing the app as ' . $user->getName() . ' ' . $user->getSurname() . ' (' . $user->getOrganization() . ') now <a href="' . $this->generateUrl('admin_user_impersonalize', ['id' => -1]) . '" style="float: right;">Stop impersonating</a>');
$data['impUser'] = $user;
}
}
return $this->render('investor/home.html.twig', array_merge($data, [ 'folders' => $folderService->getFoldersOfUserTree($user)]));
}
/**
*
* @Route("/investor/my-account", name="investor_my_account")
* @return Response
*/
public function investorMyAccount(): Response
{
/** @var User $user */
$user = $this->getUser();
if (!$user->isAcceptedPolicy()) {
return $this->redirectToRoute('investor_privacy');
}
return $this->render('investor/my-account.html.twig', [ 'user' => $user]);
}
/**
*
* @Route("/investor/privacy", name="investor_privacy")
* @param UserService $userService
* @return Response
*/
public function investorPrivacy(UserService $userService): Response
{
/** @var User $user */
$user = $this->getUser();
return $this->render('investor/privacy.html.twig', [ 'user' => $user]);
}
/**
*
* @Route("/investor/accept-privacy", name="investor_accept_privacy")
* @param Request $request
* @param UserService $userService
* @return Response
*/
public function investorAcceptPrivacy(Request $request, UserService $userService): Response
{
/** @var User $user */
$user = $this->getUser();
$userIP = $request->getClientIp();
$userService->acceptPrivacy($user, $userIP);
return $this->redirectToRoute('investor_home');
}
/**
*
* @Route("/investor/request-new-password", name="investor_request_new_password")
* @param UserService $userService
* @return Response
*/
public function investorRequestNewPassword(UserService $userService,): Response
{
/** @var User $user */
$user = $this->getUser();
$userService->requestNewPassword($user);
$this->addFlash('info', 'Password requested successfully');
return $this->redirectToRoute('investor_my_account');
}
/**
* @Route("/investor/history/{type}/{projectId}", name="investor_history_view_download")
* @param ProjectService $projectService
* @param ProjectLogService $projectLogService
* @param UserService $userService
* @param string $type
* @param string $projectId
* @return Response
* @throws Exception
*/
public function investorHistoryDownloadOrView(
ProjectService $projectService,
ProjectLogService $projectLogService,
UserService $userService,
string $type,
string $projectId): Response
{
/** @var Project $project */
$project = $projectService->getOneById($projectId);
/** @var User $user */
$user = $this->getUser();
if (!$user->isAcceptedPolicy()) {
return $this->redirectToRoute('investor_privacy');
}
if ($project) {
$logs = $projectLogService->getLogs($project, $user, $type);
return $this->render('investor/partials/logs.html.twig', [
'logs' => $logs, 'title' => ucfirst($type . 's'), 'projectTitle' => $project->getName()]);
}
throw new Exception('File or project not found');
}
}