<?php
namespace App\Controller;
use App\Entity\Feedback;
use App\Entity\Import;
use App\Entity\Notice;
use App\Entity\Project;
use App\Entity\User;
use App\Form\Type\FeedbackLoggedInType;
use App\Form\Type\FeedbackLoggedOutType;
use App\Form\Type\NoticeType;
use App\Service\MessageService;
use Psr\Log\LoggerInterface;
use Swift_Mailer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use DateTime;
use Exception;
use Swift_Message;
class NoticeController extends AbstractController
{
/**
* @Route("/notice", name="notice")
* @param Request $request
* @param MessageService $messageService
* @return Response
* @throws Exception
*/
public function index(Request $request, MessageService $messageService)
{
$user_id = $this->getUser()->getId();
$entityManager = $this->getDoctrine()->getManager();
$context = "global scope";
// load project id and import id from given source_uri string
$project_id = $request->get("project_id", 0);
$import_id = $request->get("import_id", 0);
// load notice if one exists for context global
$notice = null;
if ($project_id == 0 && $import_id == 0) {
$notice = $entityManager->getRepository(Notice::class)->findOneBy([
"user_id" => $user_id,
"project_id" => $project_id,
"import_id" => $import_id,
]);
}
// if we have an project id load project notice
else if ($project_id > 0 && $import_id == 0) {
$project = $entityManager->getRepository(Project::class)->find($project_id);
$context = " project <b>" . $project->getName() . "</b>";
$notice = $entityManager->getRepository(Notice::class)->findOneBy([
"project_id" => $project_id,
"import_id" => $import_id,
]);
}
// if we even have an import id load import notice
else if ($project_id > 0 && $import_id > 0) {
$project = $entityManager->getRepository(Project::class)->find($project_id);
$context = " project <b>" . $project->getName() . "</b>";
$import = $entityManager->getRepository(Import::class)->find($import_id);
$context = " import <b>" . $import->getDescription() . "</b> @ " . $context;
$notice = $entityManager->getRepository(Notice::class)->findOneBy([
"project_id" => $project_id,
"import_id" => $import_id,
]);
}
// if no notice exists create an empty one
if(!$notice) {
$notice = new Notice();
}
$createdBy = "";
$modifiedBy = "";
if ($notice->getCreatedBy() > 0){
$user = $entityManager->getRepository(User::class)->find($notice->getCreatedBy());
if ($user) {
$createdBy = $user->getEmail();
}
}
if ($notice->getModifiedBy() > 0){
$user = $entityManager->getRepository(User::class)->find($notice->getModifiedBy());
if ($user) {
$modifiedBy = $user->getEmail();
}
}
$form = $this->createForm(NoticeType::class, $notice);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$notice = $form->getData();
$notice->setUserId($user_id);
$notice->setProjectId($project_id);
$notice->setImportId($import_id);
$notice->setModified(new DateTime());
$notice->setModifiedBy($user_id);
// set created only once
if (!$notice->getCreatedBy()) {
$notice->setCreated(new DateTime());
$notice->setCreatedBy($user_id);
}
$entityManager->persist($notice);
$entityManager->flush();
$messageService->setMessage("Your notice was saved successfully.");
return $this->render("notice.sent.html.twig");
}
return $this->render("notice.html.twig", array(
"form" => $form->createView(),
"focus" => $context,
"notice" => $notice,
"created_by" => $createdBy,
"modified_by" => $modifiedBy,
));
}
/**
* @Route("/get-notice-count", name="get_notice_count")
* @param Request $request
* @param MessageService $messageService
* @return Response
* @throws Exception
*/
public function getNoticeCount(Request $request, MessageService $messageService)
{
if (!$this->getUser()) {
return new JsonResponse([
"result" => false,
"data" => null,
]);
}
$user_id = $this->getUser()->getId();
$entityManager = $this->getDoctrine()->getManager();
$project_id = $request->get("project_id", 0);
$import_id = $request->get("import_id", 0);
// load notice if one exists for context global
$notice = null;
if ($project_id == 0 && $import_id == 0) {
$notice = $entityManager->getRepository(Notice::class)->findOneBy([
"user_id" => $user_id,
"project_id" => $project_id,
"import_id" => $import_id,
]);
}
// if we have an project id load project notice
else if ($project_id > 0 && $import_id == 0) {
$notice = $entityManager->getRepository(Notice::class)->findOneBy([
"project_id" => $project_id,
"import_id" => $import_id,
]);
}
// if we even have an import id load import notice
else if ($project_id > 0 && $import_id > 0) {
$notice = $entityManager->getRepository(Notice::class)->findOneBy([
"project_id" => $project_id,
"import_id" => $import_id,
]);
}
$count = 0;
if ($notice) {
$count++;
}
return new JsonResponse([
"result" => true,
"data" => $count,
]);
}
}