<?php
namespace App\Controller\Application;
use App\Library\Symfony\Controller,
App\Library\Symfony\Annotation\Breadcrumb,
App\Library\Symfony\Annotation\Lang;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request,
Symfony\Component\Routing\Annotation\Route;
use App\Entity\Quotation\Quote,
App\Entity\Inventory\Purchase,
App\Entity\Application\Message;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Contracts\Translation\TranslatorInterface;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
/**
* @Lang(domain="application", default_key="dashboard")
*/
class DefaultController extends Controller
{
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* @var ManagerRegistry
*/
protected $doctrine;
public function __construct(
TranslatorInterface $translator = null,
ManagerRegistry $doctrine = null )
{
parent::__construct($translator, $doctrine);
}
/**
* @Route("/", name="home")
* @Breadcrumb(parent=false)
*/
public function home(Request $request)
{
$user = $this->getUser();
$isNewMessage = false;
$message = $this->doctrine
->getRepository(Message::class)
->getList(1,1);
$message = $message ? $message[0] : [];
if($request->isMethod('POST')) {
$messageInput = $request->request->get('home-message');
$isNewMessage = true;
$em = $this->doctrine->getManager();
$newMessage = new Message();
$newMessage->setMessage($messageInput);
if(!$message){
$em->persist($newMessage);
$em->flush();
} else if($messageInput != $message->getMessage()){
$em->persist($newMessage);
$em->flush();
}
}
$notifications = $this->getNotifications();
$unseenNotifications = [];
foreach ($notifications as $notification) {
if(!$notification['isSeen']){
$unseenNotifications[] = $notification;
}
}
$parameters['notifications'] = $notifications;
$parameters['unseenNotifications'] = $unseenNotifications;
return $this->render('application/home.html.twig', array(
'user' => $this->getUser(),
'notifications' => $notifications,
'unseenNotifications' => $unseenNotifications,
'message' => $isNewMessage ? $messageInput : ($message ? $message->getMessage() : ""),
));
}
/**
* @Route("/ajax/get/recent", name="ajax_get_recent")
*/
public function ajaxGetRecent(Request $request){
$type = $request->request->get('type');
$item = $request->request->get('item');
$maxResults = $this->data('application.dashboard.config.list.items_shown');
$userParam = $type === "mine" ? ["user" => $this->getUser()] : [] ;
if($item === "project" || $item === "quote"){
$quoteRepository = $this->doctrine->getRepository(Quote::class);
$items = $quoteRepository->{$item === "project" ? "getProjectsList" : "getQuotesList"}(1, $maxResults, [], $userParam, 'updatedAt');
}
else if($item === "purchase" || $item === "rental"){
$inventoryRepository = $this->doctrine->getRepository(Purchase::class);
$items = $inventoryRepository->getList(1, $maxResults, [], $item === "purchase" ? "purchase" : "rental", $userParam, 'updatedAt');
}
else{
$items = [];
}
return $this->renderHtml('application/home-items.html.twig', [
"items" => $items,
"itemType" => $item,
]);
}
/**
* @Route("/ajax/notifications/clear", name="clear_notifications")
*/
public function clearNotifications($module = "", $id = null){
parent::clearNotifications($module, $id);
}
}