ninegate/src/cadolesuser-1.0/src/Cadoles/PortalBundle/Controller/PageController.php

493 lines
19 KiB
PHP

<?php
namespace Cadoles\PortalBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Filesystem\Filesystem;
use Cadoles\PortalBundle\Entity\Page;
use Cadoles\PortalBundle\Form\PageSubmitType;
use Cadoles\PortalBundle\Form\PageUpdateToolType;
use Cadoles\PortalBundle\Form\PageUpdateURLType;
use Cadoles\PortalBundle\Form\PageUpdateWidgetType;
use Cadoles\PortalBundle\Form\PageUpdateEditorType;
use Cadoles\PortalBundle\Form\PageShareType;
class PageController extends Controller
{
private $labelentity="CadolesPortalBundle:Page";
private $routeprimary="cadoles_portal_config_page";
public function listAction()
{
return $this->render($this->labelentity.':list.html.twig',[
'useheader' => true,
'usemenu' => false,
'usesidebar' => true,
]);
}
public function ajaxlistAction(Request $request,$access="config")
{
/*
// S'assurer que c'est un appel ajax
if (!$request->isXmlHttpRequest()) {
return new JsonResponse(array('message' => 'Interdit'), 400);
}
*/
$em = $this->getDoctrine()->getManager();
$start=$request->query->get('start');
$length= $request->query->get('length');
$search= $request->query->get('search');
$draw= $request->query->get('draw');
$order= $request->query->get('order');
$alluser= $request->query->get('alluser');
// Query de base
$qbase=$em->createQueryBuilder()->from($this->labelentity,'table');
$qsearch=$em->createQueryBuilder()->from($this->labelentity,'table');
if($alluser=="false") {
$qbase->where("table.user is null");
$qsearch->where("table.user is null");
}
else {
$qbase->from('CadolesCoreBundle:User','user')
->where("table.user=user");
$qsearch->from('CadolesCoreBundle:User','user')
->where("table.user=user");
}
if($alluser=="false")
$qsearch->andwhere('table.id LIKE :value OR table.name LIKE :value');
else
$qsearch->andWhere('table.id LIKE :value OR table.name LIKE :value OR user.username LIKE :value') ;
$qsearch->setParameter("value", "%".$search["value"]."%");
// Nombre total d'enregistrement
$total = $qbase->select('COUNT(table)')->getQuery()->getSingleScalarResult();
// Nombre d'enregistrement filtré
if($search["value"]=="")
$totalf = $total;
else
$totalf= $qsearch->select('COUNT(table)')->getQuery()->getSingleScalarResult();
// Parcours des Enregistrement
if($search["value"]=="")
$qb = $qbase->select('table');
else
$qb = $qsearch->select('table');
// Order
switch($order[0]["column"]) {
case 1 :
$qb->orderBy('table.roworder',$order[0]["dir"]);
break;
case 2 :
$qb->orderBy('table.name',$order[0]["dir"]);
break;
case 4 :
if($alluser=="true") $qb->orderBy('user.username',$order[0]["dir"]);
break;
}
// Execution de la requete d'affichage
$datas=$qb->setFirstResult($start)->setMaxResults($length)->getQuery()->getResult();
// Construction du tableau de retour
$output = array(
'draw' => $draw,
'recordsFiltered' => $totalf,
'recordsTotal' => $total,
'data' => array(),
);
foreach($datas as $data) {
$route=str_replace("_config_","_".$access."_",$this->routeprimary);
$action = "";
//$action.="<a href='".$this->generateUrl($route.'_update', array('id'=>$data->getId()))."'><i class='fa fa-file fa-fw'></i></a>";
$action.="<a href='".$this->generateUrl($route.'_delete', array('id'=>$data->getId()))."' data-method='delete'><i class='fa fa-trash fa-fw fa-2x'></i></a>";
$action.="<a href='".$this->generateUrl($route.'_view', array('id'=>$data->getId()))."'><i class='fa fa-eye fa-2x'></i></a>";
$user="";
if($data->getUser()) {
$user.="<img src='/".$this->container->getParameter('alias')."/uploads/avatar/".$data->getUser()->getAvatar()."' class='avatar' style='margin:0px 5px 0px 0px;display:inline-block;'>";
$user.=$data->getUser()->getUsername();
}
array_push($output["data"],[
$action,
$data->getRoworder(),
$data->getName(),
$data->getPagecategory()->getName(),
$user
]);
}
// Retour
return new Response(json_encode($output), 200);
}
private function entityForm(Page $entity,$access="config")
{
$route=str_replace("_config_","_".$access."_",$this->routeprimary);
if ($this->getDoctrine()->getManager()->contains($entity)) {
// Type Tools
if($entity->getPagecategory()->getId()<0 ) {
return $this->createForm(PageUpdateToolType::class, $entity, [
"mode" => "update",
"access" => $access
]);
}
// Type URL
if($entity->getPagecategory()->getId()==1 ) {
return $this->createForm(PageUpdateURLType::class, $entity, [
"mode" => "update",
"access" => $access
]);
}
// Type Widget
elseif($entity->getPagecategory()->getId()==2 ) {
return $this->createForm(PageUpdateWidgetType::class, $entity, [
"mode" => "update",
"access" => $access
]);
}
// Type Editeur
elseif($entity->getPagecategory()->getId()==3 ) {
return $this->createForm(PageUpdateEditorType::class, $entity, [
"mode" => "update",
"access" => $access
]);
}
}
else {
return $this->createForm(PageSubmitType::class, $entity, [
"mode" => "update",
"access" => $access
]);
}
}
public function submitAction(Request $request,$access="config")
{
$entity = new Page();
$entity->setMaxwidth(0);
$entity->setRoworder(0);
$form = $this->entityForm($entity,$access);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
if($access=="user") $entity->setUser($this->getUser());
$em->persist($entity);
$em->flush();
$route=str_replace("_config_","_".$access."_",$this->routeprimary);
return $this->redirect($this->generateUrl($route.'_update',["id"=>$entity->getId()]));
}
return $this->render($this->labelentity.':submit.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => ($access=="config"),
'maxwidth' => ($access=="user"),
'entity' => $entity,
'mode' => "submit",
'access' => $access,
'form' => $form->createView()
]);
}
public function updateAction(Request $request, $id,$access="config")
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository($this->labelentity)->find($id);
if (!$entity) throw $this->createNotFoundException('Unable to find entity.');
// On s'assure que l'utilisateur à la permission de modifier
if($access=="user") {
$em->getRepository($this->labelentity)->getPermission($this->getUser(),$entity,$cansee,$canupdate);
if(!$canupdate) throw $this->createNotFoundException('Permission denied');
}
// Création du formulaire
$form = $this->entityForm($entity,$access);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
if($access=="config")
return $this->redirect($this->generateUrl($this->routeprimary.'_view',["id"=>$id]));
else
return $this->redirect($this->generateUrl('cadoles_core_home',["id"=>$id]));
}
// Type Tool
if($entity->getPagecategory()->getId()<0 ) {
return $this->render($this->labelentity.':updatetool.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => ($access=="config"),
'maxwidth' => ($access=="user"),
'entity' => $entity,
'access' => $access,
'mode' => "update",
'form' => $form->createView(),
]);
}
// Type URL
if($entity->getPagecategory()->getId()==1 ) {
return $this->render($this->labelentity.':updateurl.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => ($access=="config"),
'maxwidth' => ($access=="user"),
'entity' => $entity,
'access' => $access,
'mode' => "update",
'form' => $form->createView(),
]);
}
// Type Widget
elseif($entity->getPagecategory()->getId()==2 ) {
return $this->render($this->labelentity.':updatewidget.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => ($access=="config"),
'maxwidth' => ($access=="user"),
'entity' => $entity,
'access' => $access,
'mode' => "update",
'form' => $form->createView(),
]);
}
// Type Editeur
elseif($entity->getPagecategory()->getId()==3 ) {
return $this->render($this->labelentity.':updateeditor.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => ($access=="config"),
'maxwidth' => ($access=="user"),
'entity' => $entity,
'access' => $access,
'mode' => "update",
'form' => $form->createView(),
]);
}
}
public function shareAction(Request $request, $id,$access="config")
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository($this->labelentity)->find($id);
if (!$entity) throw $this->createNotFoundException('Unable to find entity.');
// On s'assure que l'utilisateur à la permission de modifier
if($access=="user") {
$em->getRepository($this->labelentity)->getPermission($this->getUser(),$entity,$cansee,$canupdate);
if(!$canupdate) throw $this->createNotFoundException('Permission denied');
}
// Création du formulaire
$form = $this->createForm(PageShareType::class, $entity, ["access" => $access, "user" => $this->getUser()]);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
if($access=="config")
return $this->redirect($this->generateUrl($this->routeprimary.'_view',["id"=>$id]));
else
return $this->redirect($this->generateUrl('cadoles_core_home',["id"=>$id]));
}
return $this->render($this->labelentity.':share.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => ($access=="config"),
'maxwidth' => ($access=="user"),
'entity' => $entity,
'access' => $access,
'form' => $form->createView(),
]);
}
public function deleteAction(Request $request, $id,$access="config")
{
$em = $this->getDoctrine()->getManager();
$entity = $this->getDoctrine()->getRepository($this->labelentity)->find($id);
if (!$entity) throw $this->createNotFoundException('Unable to find entity.');
// On s'assure que l'utilisateur à la permission de supprimer
if($access=="user") {
$em->getRepository($this->labelentity)->getPermission($this->getUser(),$entity,$cansee,$canupdate);
if(!$canupdate) throw $this->createNotFoundException('Permission denied');
}
// Suppression
$em->remove($entity);
$em->flush();
// Retour
if($access=="config")
return $this->redirect($this->generateUrl($this->routeprimary));
else
return $this->redirect($this->generateUrl("cadoles_core_home"));
}
public function orderAction(Request $request,$access="config")
{
// 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 = $this->getDoctrine()->getRepository($this->labelentity)->find($id);
if (!$entity) throw $this->createNotFoundException('Unable to find entity.');
// On s'assure que l'utilisateur à la permission
if($access=="user") {
$em->getRepository($this->labelentity)->getPermission($this->getUser(),$entity,$cansee,$canupdate);
if(!$canupdate) throw $this->createNotFoundException('Permission denied');
}
$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 viewAction($id,$access=null) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository($this->labelentity)->find($id);
if (!$entity) throw $this->createNotFoundException('Unable to find entity.');
// Permissions
if($access=="config") {
$canupdate = true;
}
else {
// On s'assure que l'utilisateur à la permission de voir
$em->getRepository($this->labelentity)->getPermission($this->getUser(),$entity,$cansee,$canupdate);
if(!$cansee) throw $this->createNotFoundException('Permission denied');
}
// Type Calendrier
if($entity->getPageCategory()->getId()==-100) {
$entity->setUrl($this->generateUrl('cadoles_portal_user_calendar_view'));
return $this->render($this->labelentity.':viewurl.html.twig', [
'useheader' => ($access=="config"),
'usemenu' => false,
'usesidebar' => ($access=="config"),
'entity' => $entity,
'access' => $access,
'canupdate' => $canupdate
]);
}
// Type Blob
if($entity->getPageCategory()->getId()==-90) {
$entity->setUrl($this->generateUrl('cadoles_portal_user_blog_view'));
return $this->render($this->labelentity.':viewurl.html.twig', [
'useheader' => ($access=="config"),
'usemenu' => false,
'usesidebar' => ($access=="config"),
'entity' => $entity,
'access' => $access,
'canupdate' => $canupdate
]);
}
// Type URL
if($entity->getPageCategory()->getId()==1) {
return $this->render($this->labelentity.':viewurl.html.twig', [
'useheader' => ($access=="config"),
'usemenu' => false,
'usesidebar' => ($access=="config"),
'entity' => $entity,
'access' => $access,
'canupdate' => $canupdate
]);
}
// Type Editeur
if($entity->getPageCategory()->getId()==2) {
return $this->render($this->labelentity.':viewwidget.html.twig', [
'useheader' => ($access=="config"),
'usemenu' => false,
'usesidebar' => ($access=="config"),
'entity' => $entity,
'access' => $access,
'canupdate' => $canupdate,
'widgets' => $this->getDoctrine()->getRepository("CadolesPortalBundle:Widget")->findAll()
]);
}
// Type Editeur
if($entity->getPageCategory()->getId()==3) {
return $this->render($this->labelentity.':vieweditor.html.twig', [
'useheader' => ($access=="config"),
'usemenu' => false,
'usesidebar' => ($access=="config"),
'entity' => $entity,
'access' => $access,
'canupdate' => $canupdate
]);
}
}
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);
$funcNum = $request->query->get('CKEditorFuncNum');
$response = "<script type='text/javascript'>\n";
$response.= "window.parent.CKEDITOR.tools.callFunction($funcNum, '$targetUrl', '$message');\n";
$response.= "</script>";
return new Response($response);
}
}