<?php
// src/Controller/UserController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Service\MessageService;
use App\Service\UserService;
class AdminController extends AbstractController
{
/**
* @Route("/admin", name="adminLocaleRedirect")
* @Route("/vendeur", name="admin")
* @Security("has_role('ROLE_ADMIN')")
* Redirect the user to the right entry point depending of it's current locale
*/
public function adminEntryLocaleRedirect(Request $request, UrlGeneratorInterface $router, UserService $userService){
$locale = $request->getSession()->get('_locale');
if(empty($locale))
$locale = 'fr';
if(count($userService->getUser()->getCompanies()) > 0){
$cId = $userService->getUser()->getCompanies()->first()->getId();
$newRoute = $router->generate( 'dashboardCompany', array ('_locale' => $locale, 'id' => $cId));
}else
return $this->redirectToRoute('home');
header("Location: ".$newRoute);
die();
}
/**
* @Route("/pull/json/updateInterface/admin",
* options = { "expose" = true },
* name = "getUpdatesForAdminInterface"
* )
*/
public function updateAdminInterface(UserService $userServ, MessageService $messageServ){
$return = array();
$response = new JsonResponse();
if(!$userServ->getUser()){
return $response;
}
if(!$userServ->getUser())
return new JsonResponse(array());
//Début des messages
$notif = $userServ->getUser()->getNotifications();
$notifContent = array();
$isThereNewNotif = false;
foreach ($notif as $n){
if(!$n->getViewed()){
$notifContent[] = array(
'id' => $n->getId(),
'title' => $n->getTitle(),
'icon' => $n->getIcon(),
'iconColor' => $n->getIconColor(),
'text' => $n->getText(),
'dateSince' => $n->getDateSince()
);
}
}
if(count($notifContent) > 0){
$isThereNewNotif = true;
}
$return['notifications'] = array(
'new' => $isThereNewNotif,
'amount' => count($notifContent),
'content' => $notifContent
);
//Fin des notifications
//Debut des messages
$messages = $messageServ->getNewMessagesIn('inbox');
$isThereNewMessages = false;
$messagesContent = array();
foreach($messages as $message){
$messagesContent[] = array(
'id' => $message->getId(),
'from' => $message->getUserFrom()->getDisplayName(),
'fromId' => $message->getUserFrom()->getId(),
'subject' => $message->getSubject(),
'message' => $message->getCleanMessage(),
'dateSent' => $message->getDateSent()->format("j/m/y \\à\\ g:ia")
);
}
if(count($messagesContent) > 0){
$isThereNewMessages = true;
}
$return['messages'] = array(
'new' => $isThereNewMessages,
'amount' => count($messagesContent),
'content' => $messagesContent
);
//Fin des messages
$response->setData($return);
return $response;
}
}