287 lines
9.7 KiB
PHP
287 lines
9.7 KiB
PHP
<?php
|
|
|
|
namespace Cadoles\PortalBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\Form\FormError;
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
|
|
use Cadoles\PortalBundle\Entity\Alert;
|
|
use Cadoles\PortalBundle\Form\AlertType;
|
|
|
|
class AlertController extends Controller
|
|
{
|
|
private $labelroute = 'cadoles_portal_config_alert';
|
|
private $labelentity = 'CadolesPortalBundle:Alert';
|
|
private $labeldata = 'alert';
|
|
private $labeldatas = 'alerts';
|
|
|
|
public function ajaxseleclistAction(Request $request)
|
|
{
|
|
// S'assurer que c'est un appel ajax
|
|
if (!$request->isXmlHttpRequest()) {
|
|
return new JsonResponse(array('message' => 'Interdit'), 400);
|
|
}
|
|
|
|
$output=array();
|
|
$em = $this->getDoctrine()->getManager();
|
|
$page_limit=$request->query->get('page_limit');
|
|
$q=$request->query->get('q');
|
|
|
|
$qb = $em->createQueryBuilder();
|
|
$qb->select('table')->from("CadolesPortalBundle:Alert",'table')
|
|
->where('table.title LIKE :value')
|
|
->setParameter("value", "%".$q."%")
|
|
->orderBy('table.title');
|
|
|
|
$datas=$qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
|
|
foreach($datas as $data) {
|
|
array_push($output,array("id"=>$data->getId(),"text"=>$data->getTitle()));
|
|
}
|
|
|
|
$response = new Response(json_encode($output));
|
|
$response->headers->set('Content-Type', 'application/json');
|
|
return $response;
|
|
}
|
|
|
|
public function listAction()
|
|
{
|
|
$alerts = $this->getDoctrine()->getRepository('CadolesPortalBundle:Alert')->findBy([], ['rowOrder' => 'asc']);
|
|
$alertcategorys = $this->getDoctrine()->getRepository('CadolesPortalBundle:Alertcategory')->findAll();
|
|
$groups = $this->getDoctrine()->getRepository('CadolesCoreBundle:Group')->findBy([], ['label' => 'asc']);
|
|
|
|
return $this->render($this->labelentity.':list.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
'alerts' => $alerts,
|
|
'alertcategorys' => $alertcategorys,
|
|
'groups' => $groups
|
|
]);
|
|
}
|
|
|
|
public function submitAction(Request $request)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = new Alert();
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(AlertType::class,$data,array("mode"=>"submit","labelniveau01" => $this->GetParameter("labelsniveau01")));
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur erreur
|
|
$this->getErrorForm(null,$form,$request,$data,"submit");
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
// Sauvegarde
|
|
$em->persist($data);
|
|
$em->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute($this->labelroute);
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->labelentity.':edit.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
$this->labeldata => $data,
|
|
'mode' => 'submit',
|
|
'icons' => $this->getDoctrine()->getRepository("CadolesPortalBundle:Icon")->findAll(),
|
|
'form' => $form->createView()
|
|
]);
|
|
}
|
|
|
|
public function updateAction($id,Request $request)
|
|
{
|
|
// Récupération de l'enregistrement courant
|
|
$data=$this->getData($id);
|
|
|
|
// Vérifier que cet enregistrement est modifiable
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(AlertType::class,$data,array(
|
|
"mode" => "update",
|
|
"labelniveau01" => $this->GetParameter("labelsniveau01")
|
|
));
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur erreur
|
|
$this->getErrorForm($id,$form,$request,$data,"update");
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
$data = $form->getData();
|
|
|
|
// Si non masquable on s'assure qu'il n'y a pas de reader
|
|
if(!$data->getFghideable()) {
|
|
$readers=$data->getReaders();
|
|
foreach($readers as $reader) {
|
|
$data->removeReader($reader);
|
|
}
|
|
}
|
|
|
|
// Sauvegarde
|
|
$em->persist($data);
|
|
$em->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute($this->labelroute);
|
|
}
|
|
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->labelentity.':edit.html.twig', [
|
|
'useheader' => true,
|
|
'usemenu' => false,
|
|
'usesidebar' => true,
|
|
$this->labeldata => $data,
|
|
'mode' => 'update',
|
|
'form' => $form->createView()
|
|
]);
|
|
}
|
|
|
|
public function deleteAction($id,Request $request)
|
|
{
|
|
// Récupération de l'enregistrement courant
|
|
$data=$this->getData($id);
|
|
|
|
// Vérifier que cet enregistrement est supprimable
|
|
|
|
// Supprimer la donnée
|
|
$em = $this->getDoctrine()->getManager();
|
|
$em->remove($data);
|
|
$em->flush();
|
|
|
|
return $this->redirectToRoute($this->labelroute);
|
|
}
|
|
|
|
public function orderAction(Request $request)
|
|
{
|
|
// S'assurer que c'est un appel ajax
|
|
if (!$request->isXmlHttpRequest()) {
|
|
return new JsonResponse(array('message' => 'Interdit'), 400);
|
|
}
|
|
|
|
$output=array();
|
|
$id=$request->request->get('id');
|
|
$order=$request->request->get('order');
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
$entity = $em->getRepository($this->labelentity)->find($id);
|
|
if (!$entity) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
$entity->setRoworder($order);
|
|
$em->persist($entity);
|
|
$em->flush();
|
|
|
|
$response = new Response(json_encode($output));
|
|
$response->headers->set('Content-Type', 'application/json');
|
|
return $response;
|
|
}
|
|
|
|
public function uploadAction(Request $request,$access=null) {
|
|
// Fichier temporaire uploadé
|
|
$tmpfile = $request->files->get('upload');
|
|
$extention = $tmpfile->getClientOriginalExtension();
|
|
|
|
// Répertoire de Destination
|
|
$fs = new Filesystem();
|
|
$rootdir = $this->get('kernel')->getRootDir()."/../web";
|
|
$fs->mkdir($rootdir."/uploads/ckeditor");
|
|
|
|
// Fichier cible
|
|
$targetName = uniqid().".".$extention;
|
|
$targetFile = $rootdir."/uploads/ckeditor/".$targetName;
|
|
$targetUrl = "/".$this->getParameter('alias')."/uploads/ckeditor/".$targetName;
|
|
$message = "";
|
|
|
|
move_uploaded_file($tmpfile,$targetFile);
|
|
|
|
$output["uploaded"]=1;
|
|
$output["fileName"]=$targetName;
|
|
$output["url"]=$targetUrl;
|
|
|
|
return new Response(json_encode($output));
|
|
|
|
}
|
|
|
|
public function readAction(Request $request) {
|
|
$output=array();
|
|
$id=$request->request->get('id');
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
$entity = $em->getRepository($this->labelentity)->find($id);
|
|
if (!$entity) {
|
|
throw $this->createNotFoundException('Unable to find entity.');
|
|
}
|
|
|
|
if(!$entity->getReaders()->contains($this->getUser())) {
|
|
$entity->addReader($this->getUser());
|
|
$em->persist($entity);
|
|
$em->flush();
|
|
}
|
|
|
|
$response = new Response(json_encode($output));
|
|
$response->headers->set('Content-Type', 'application/json');
|
|
return $response;
|
|
}
|
|
|
|
protected function getDatas()
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$datas = $em->getRepository($this->labelentity)->findAll();
|
|
return $datas;
|
|
}
|
|
|
|
protected function getData($id)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$data = $em->getRepository($this->labelentity)->find($id);
|
|
|
|
if (!$data) {
|
|
throw $this->createNotFoundException('Unable to find '.$this->labeldata);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
protected function getErrorForm($id,$form,$request,$data,$mode) {
|
|
if ($form->get('submit')->isClicked()&&$mode=="delete") {
|
|
}
|
|
|
|
if ($form->get('submit')->isClicked() && $mode=="submit") {
|
|
}
|
|
|
|
if ($form->get('submit')->isClicked() && !$form->isValid()) {
|
|
$this->get('session')->getFlashBag()->clear();
|
|
$validator = $this->get('validator');
|
|
$errors = $validator->validate($data);
|
|
foreach( $errors as $error ) {
|
|
$request->getSession()->getFlashBag()->add("error", $error->getMessage());
|
|
}
|
|
|
|
$errors = $form->getErrors();
|
|
foreach( $errors as $error ) {
|
|
$request->getSession()->getFlashBag()->add("error", $error->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|