<?php
// src/Controller/TaskController.php
namespace App\Controller\Website;
use App\Message\NewGTFile;
use App\Message\NewGTMessage;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Messenger\MessageBusInterface;
use App\Message\NewInscription;
use Sulu\Bundle\MediaBundle\Collection\Manager\CollectionManager;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class GTInscriptionController extends AbstractController
{
/**
* @var MediaManagerInterface $mediaManager
*/
private MediaManagerInterface $mediaManager;
/**
* @var Security $security
*/
private Security $security;
/**
* @var MessageBusInterface $bus
*/
private MessageBusInterface $bus;
/**
* @var CollectionManager $collectionManager
*/
private CollectionManager $collectionManager;
/**
* @var UrlGeneratorInterface $urlGenerator
*/
private UrlGeneratorInterface $urlGenerator;
public function __construct(
Security $security,
MediaManagerInterface $mediaManager,
MessageBusInterface $bus,
CollectionManager $collectionManager,
UrlGeneratorInterface $urlGenerator
) {
$this->security = $security;
$this->mediaManager = $mediaManager;
$this->bus = $bus;
$this->collectionManager = $collectionManager;
$this->urlGenerator = $urlGenerator;
}
public function addUserToGt(Request $request): Response
{
$uuid = $request->query->get('uuid');
$user = $this->security->getUser();
if (empty($user)) {
return $this->redirect($this->urlGenerator->generate('sulu_community.login'));
}
$user_id = $user->getContact()->getId();
$locale = $request->getLocale();
$array = [
"user_id" => $user_id,
"uuid" => $uuid,
"locale" => $locale
];
$content = json_encode($array);
$this->bus->dispatch(new NewInscription($content));
sleep(3);
exec('rm -rf /var/www/html/var/cache/common/*');
return $this->redirect($request->server->get('HTTP_REFERER'));
}
public function addMessageToGt(Request $request): Response
{
$message = $request->query->get('message');
$message = nl2br(strip_tags($message));
$uuid = $request->query->get('uuid');
$user = $this->security->getUser();
if (empty($user)) {
return $this->redirect($this->urlGenerator->generate('sulu_community.login'));
}
$contactId = $this->security->getUser()->getContact()->getId();
$array = [
"uuid" => $uuid,
"locale" => $request->getLocale(),
"type" => "message",
"settings" => [],
"message_user" => $contactId,
"message_date" => date("Y-m-d\TH:i:s"),
"message_text" => $message
];
$content = json_encode($array);
$this->bus->dispatch(new NewGTMessage($content));
exec('rm -rf /var/www/html/var/cache/common/*');
return $this->json('');
}
public function addFileToGt(Request $request): Response
{
// 1 - j'obtiens tous les données qu'il me faut : uuid, titre du document, locale, fileVersion, fileName, userId
$uuid = $request->request->get('uuid');
$title = $request->request->get('title');
$locale = $request->getLocale();
$file = $request->files->get('fileVersion');
$fileName = strip_tags($file->getClientOriginalName());
$user = $this->security->getUser();
if (empty($user)) {
return $this->redirect($this->urlGenerator->generate('sulu_community.login'));
}
$user_id = $this->security->getUser()->getId();
// 2 - je déclare id = null, car je vais chercher si la collection existe déjà, et si elle n'existe pas, je garde id = null
// sinon, j'applique l'id de la collection trouvé
$id = null;
$collections = $this->collectionManager->get('fr');
// je boucle sur mes collections et je regarde si il y a une collection avec le titre du document
foreach ($collections as $collection) {
if ($title === $collection->getTitle()) {
$id = $collection->getId();
}
}
$data = [
"id" => $id,
"title" => $title,
"locale" => $locale,
'type' => ['id' => 1],
'parent' => null,
'key' => null,
];
$currentCollection = $this->collectionManager->save($data, $user_id, true);
$collectionId = $currentCollection->getId();
// 3 - une fois l'id de la collection obtenu, je vais upload le fichier sur le serveur, puis j'obtiens son ID
$theUploadedFile = $this->mediaManager->save(
$file,
[
'locale' => 'fr',
'collection' => $collectionId,
'title' => $fileName
],
$user_id
);
$theUploadedFileId = $theUploadedFile->getFile()->getId();
// 4 - j'envoie l'id du media, le uuid et la locale au consumer afin de placer l'id du media dans le tableau de gt_files (configuré dans le xml groupe-travail)
$array = [
"media_id" => $theUploadedFileId,
"uuid" => $uuid,
"locale" => $locale
];
$content = json_encode($array);
$this->bus->dispatch(new NewGTFile($content));
sleep(3);
exec('rm -rf /var/www/html/var/cache/common/*');
return $this->redirect($request->server->get('HTTP_REFERER'));
}
}