nineskeletor/src/Controller/ChildController.php

830 lines
31 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Child as Entity;
use App\Entity\Childblog;
use App\Entity\Childpage;
use App\Form\ChildType as Form;
use App\Service\ToolService;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelInterface;
class ChildController extends AbstractController
{
private $data = 'child';
private $route = 'app_child';
private $render = 'Child/';
private $entity = "App\Entity\Child";
private $appKernel;
private $tool;
public function __construct(KernelInterface $appKernel, ToolService $tool)
{
$this->appKernel = $appKernel;
$this->tool = $tool;
}
public function search(Request $request, ManagerRegistry $em)
{
$alpha = $request->get('alpha');
$query = $request->get('query');
$catparents = ($request->get('catparents') ? explode(',', $request->get('catparents')) : null);
$childtypes = ($request->get('childtypes') ? $request->get('childtypes') : '1');
$tags = ($request->get('tags') ? $request->get('tags') : null);
$first = ($request->get('first') ? $request->get('first') : 1);
$maxresult = 50;
$nopagination = in_array(30, explode(',', $childtypes));
// $nopagination = true;
if (!$catparents) {
$catparents = ['map'];
}
if (!$alpha) {
$alpha = 'a';
}
$childmaps = [];
$childblogs = [];
$childpages = [];
foreach ($catparents as $catparent) {
switch ($catparent) {
case 'blog':
$q = $em->createQueryBuilder();
$q->select('child');
$q->from("App\Entity\Blog", 'parent');
$q->from("App\Entity\Childblog", 'relation');
$q->from('App:Child', 'child');
$q->from("App\Entity\Childtype", 'childtype');
$q->andwhere('parent=relation.blog');
$q->andwhere('relation.child=child');
$q->andwhere('child.childtype=childtype');
// Si childtype = photoaerienn = filtre alpha
if (!$query && in_array(30, explode(',', $childtypes))) {
$q->andWhere('parent.name LIKE :alpha OR parent.name LIKE :alphaupper');
$q->setParameter('alpha', $alpha.'%');
$q->setParameter('alphaupper', strtoupper($alpha).'%');
}
// Si filtre par childtype
if ($childtypes) {
$q->andWhere('childtype.id IN(:childtypes)');
$q->setParameter('childtypes', explode(',', $childtypes));
}
// Si filtre par query
if ($query) {
$q->andWhere('LOWER(child.name) LIKE :query OR LOWER(child.subname) LIKE :query');
$q->setParameter('query', strtolower('%'.$query.'%'));
}
// Si filtre par tags
if ($tags) {
$q->from('App:Tag', 'tag');
$q->andWhere('tag.id IN(:tags)');
$q->andWhere('tag MEMBER OF child.tags');
$q->setParameter('tags', explode(',', $tags));
}
$c = $q;
$c->select('count(child.id)');
$count = $c->getQuery()->getSingleScalarResult();
$q->select('child');
$q->addOrderBy('parent.name', 'ASC');
$q->addOrderBy('relation.roworder', 'ASC');
if (!$nopagination) {
$q->setMaxResults($maxresult);
$q->setFirstResult(0 + (($first - 1) * $maxresult));
}
$childblogs = $q->getQuery()->getResult();
foreach ($childblogs as $relation) {
$relation->setCatparent('blog');
$relation->setIdparent($relation->getChildblogs()[0]->getBlog()->getId());
$relation->setNameparent($relation->getChildblogs()[0]->getBlog()->getName());
}
break;
case 'page':
$q = $em->createQueryBuilder();
$q->select('child');
$q->from("App\Entity\Page", 'parent');
$q->from("App\Entity\Childpage", 'relation');
$q->from('App:Child', 'child');
$q->from("App\Entity\Childtype", 'childtype');
$q->andwhere('parent=relation.page');
$q->andwhere('relation.child=child');
$q->andwhere('child.childtype=childtype');
// Si childtype = photoaerienn = filtre alpha
if (!$query && in_array(30, explode(',', $childtypes))) {
$q->andWhere('parent.name LIKE :alpha OR parent.name LIKE :alphaupper');
$q->setParameter('alpha', $alpha.'%');
$q->setParameter('alphaupper', strtoupper($alpha).'%');
}
// Si filtre par childtype
if ($childtypes) {
$q->andWhere('childtype.id IN(:childtypes)');
$q->setParameter('childtypes', explode(',', $childtypes));
}
// Si filtre par query
if ($query) {
$q->andWhere('LOWER(child.name) LIKE :query OR LOWER(child.subname) LIKE :query');
$q->setParameter('query', strtolower('%'.$query.'%'));
}
// Si filtre par tags
if ($tags) {
$q->from('App:Tag', 'tag');
$q->andWhere('tag.id IN(:tags)');
$q->andWhere('tag MEMBER OF child.tags');
$q->setParameter('tags', explode(',', $tags));
}
$c = $q;
$c->select('count(child.id)');
$count = $c->getQuery()->getSingleScalarResult();
$q->select('child');
$q->addOrderBy('parent.name', 'ASC');
$q->addOrderBy('relation.roworder', 'ASC');
if (!$nopagination) {
$q->setMaxResults($maxresult);
$q->setFirstResult(0 + (($first - 1) * $maxresult));
}
$childpages = $q->getQuery()->getResult();
foreach ($childpages as $relation) {
$relation->setCatparent('page');
$relation->setIdparent($relation->getChildpages()[0]->getPage()->getId());
$relation->setNameparent($relation->getChildpages()[0]->getPage()->getName());
}
break;
}
}
$childs = array_merge($childmaps, $childblogs, $childpages);
return $this->render($this->render.'search.html.twig', [
'childs' => $childs,
'catparents' => $catparents,
'lsttags' => $em->getRepository('App:Tag')->findAll(),
'lsttypes' => $em->getRepository("App\Entity\Childtype")->findAll(),
'tags' => explode(',', $tags),
'query' => $query,
'childtypes' => $childtypes,
'isalpha' => (!$query && in_array(30, explode(',', $childtypes))),
'useheader' => true,
'usesidebar' => false,
'maxwidth' => true,
'nopagination' => $nopagination,
'count' => $count,
'first' => $first,
'maxresult' => $maxresult,
]);
}
public function list($catparent, $idparent, ManagerRegistry $em)
{
// En fonction du parent
switch ($catparent) {
case 'blog':
$parent = $em->getRepository("App\Entity\Blog")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
// Récupérer les childtypes
$childtypes = $em->getRepository("App\Entity\Childtype")->findBy([], ['roworder' => 'ASC']);
// Récupérer les childs
$datas = $em->getRepository($this->entity)->findBy(["blog"=>$parent]);
break;
case 'page':
$parent = $em->getRepository("App\Entity\Page")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
// Récupérer les childtypes
$childtypes = $em->getRepository("App\Entity\Childtype")->findBy([], ['roworder' => 'ASC']);
// Récupérer les childs
$datas = $em->getRepository($this->entity)->findBy(["page"=>$parent]);
break;
}
return $this->render($this->render.'list.html.twig', [
'catparent' => $catparent,
'parent' => $parent,
'childtypes' => $childtypes,
$this->data.'s' => $datas,
'useheader' => true,
'usesidebar' => true,
]);
}
public function submit($catparent, $idparent, $idchildtype, Request $request, ManagerRegistry $em)
{
// Récupérer le parent
switch ($catparent) {
case 'blog':
$parent = $em->getRepository("App\Entity\Blog")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
break;
case 'page':
$parent = $em->getRepository("App\Entity\Page")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
break;
}
// Récupérer du childtype
$childtype = $em->getRepository("App\Entity\Childtype")->find($idchildtype);
if (!$childtype) {
throw $this->createNotFoundException('Not exist');
}
// Redirige vers la submitroute
if ('app_child_submit' != $childtype->getSubmitroute()) {
return $this->redirectToRoute($childtype->getSubmitroute(), ['catparent' => $catparent, 'idparent' => $parent->getId(), 'idchildtype' => $idchildtype]);
}
// Récupérer le roworder
switch ($catparent) {
case 'blog':
$last = $em->getRepository("App\Entity\Child")->findOneBy(['blog' => $parent], ['roworder' => 'DESC']);
$lastorder = ($last ? $last->getRoworder() + 1 : 1);
// Initialisation de l'enregistrement
$data = new Entity();
$data->setChildtype($childtype);
$data->setBlog($parent);
$data->setRoworder($lastorder);
if (1 == $childtype->getId()) {
$data->setName($parent->getName());
}
break;
case 'page':
$last = $em->getRepository("App\Entity\Child")->findOneBy(['page' => $parent], ['roworder' => 'DESC']);
$lastorder = ($last ? $last->getRoworder() + 1 : 1);
// Initialisation de l'enregistrement
$data = new Entity();
$data->setChildtype($childtype);
$data->setPage($parent);
$data->setRoworder($lastorder);
if (1 == $childtype->getId()) {
$data->setName($parent->getName());
}
break;
}
// Création du formulaire
$form = $this->createForm(Form::class, $data, ['mode' => 'submit', 'childtype' => $childtype]);
// 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();
$data->setUrl($this->formatEmbed($data->getUrl()));
$em->getManager()->persist($data);
$em->getManager()->flush();
// Retour à la liste
return $this->redirectToRoute($this->route, ['catparent' => $catparent, 'idparent' => $parent->getId()]);
}
// Affichage du formulaire
return $this->render($this->render.'edit.html.twig', [
'useheader' => true,
'usesidebar' => true,
'catparent' => $catparent,
'parent' => $parent,
'childtype' => $childtype,
$this->data => $data,
'mode' => 'submit',
'form' => $form->createView(),
]);
}
public function submitimage($catparent, $idparent, $idchildtype, Request $request, ManagerRegistry $em)
{
// Récupérer le parent
switch ($catparent) {
case 'blog':
$parent = $em->getRepository("App\Entity\Blog")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
break;
case 'page':
$parent = $em->getRepository("App\Entity\Page")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
break;
}
// Récupérer du childtype
$childtype = $em->getRepository("App\Entity\Childtype")->find($idchildtype);
if (!$childtype) {
throw $this->createNotFoundException('Not exist');
}
// Rendu
return $this->render($this->render.'upload.html.twig', [
'useheader' => true,
'usesidebar' => true,
'type' => 'child',
'catparent' => $catparent,
'idparent' => $idparent,
'idchildtype' => $childtype->getId(),
'typeupload' => 'image',
]);
}
public function submitvideo($catparent, $idparent, $idchildtype, Request $request, ManagerRegistry $em)
{
// Récupérer le parent
switch ($catparent) {
case 'blog':
$parent = $em->getRepository("App\Entity\Blog")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
break;
case 'page':
$parent = $em->getRepository("App\Entity\Page")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
break;
}
// Récupérer du childtype
$childtype = $em->getRepository("App\Entity\Childtype")->find($idchildtype);
if (!$childtype) {
throw $this->createNotFoundException('Not exist');
}
// Rendu
return $this->render($this->render.'upload.html.twig', [
'useheader' => true,
'usesidebar' => true,
'type' => 'child',
'catparent' => $catparent,
'idparent' => $idparent,
'idchildtype' => $childtype->getId(),
'typeupload' => 'video',
]);
}
public function submitfile($catparent, $idparent, $idchildtype, Request $request, ManagerRegistry $em)
{
// Récupérer le parent
switch ($catparent) {
case 'blog':
$parent = $em->getRepository("App\Entity\Blog")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
break;
case 'page':
$parent = $em->getRepository("App\Entity\Page")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
break;
}
// Récupérer du childtype
$childtype = $em->getRepository("App\Entity\Childtype")->find($idchildtype);
if (!$childtype) {
throw $this->createNotFoundException('Not exist');
}
// Rendu
return $this->render($this->render.'upload.html.twig', [
'useheader' => true,
'usesidebar' => true,
'type' => 'child',
'catparent' => $catparent,
'idparent' => $idparent,
'idchildtype' => $childtype->getId(),
'typeupload' => 'all',
]);
}
public function update($catparent, $idparent, $idchild, Request $request, ManagerRegistry $em)
{
// Récupérer le parent
switch ($catparent) {
case 'blog':
$parent = $em->getRepository("App\Entity\Blog")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
break;
case 'page':
$parent = $em->getRepository("App\Entity\Page")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
break;
}
$data = $em->getRepository($this->entity)->find($idchild);
$data->setUrl($this->formatEmbed($data->getUrl()));
// Création du formulaire
$form = $this->createForm(Form::class, $data, ['mode' => 'update', 'childtype' => $data->getChildtype()]);
// Récupération des data du formulaire
$form->handleRequest($request);
// Sur erreur
$this->getErrorForm(null, $form, $request, $data, 'update');
// Sur validation
if ($form->get('submit')->isClicked() && $form->isValid()) {
$data = $form->getData();
$em->getManager()->flush();
// Retour à la liste
return $this->redirectToRoute($this->route, ['catparent' => $catparent, 'idparent' => $idparent]);
}
// Affichage du formulaire
return $this->render($this->render.'edit.html.twig', [
'useheader' => true,
'usesidebar' => true,
'catparent' => $catparent,
'parent' => $parent,
'childtype' => $data->getChildtype(),
$this->data => $data,
'mode' => 'update',
'form' => $form->createView(),
]);
}
public function delete($catparent, $idparent, $idchild, Request $request, ManagerRegistry $em)
{
$child = $em->getRepository($this->entity)->find($idchild);
// Récupérer le parent et la relation
switch ($catparent) {
case 'blog':
$parent = $em->getRepository("App\Entity\Blog")->find($idparent);
break;
case 'page':
$parent = $em->getRepository("App\Entity\Page")->find($idparent);
break;
}
// Controle avant suppression
$error = false;
if (!$parent || !$child ) {
$error = true;
}
if ($error) {
return $this->redirectToRoute($this->route.'_update', ['catparent' => $catparent, 'idparent' => $idparent, 'idchild' => $idchild]);
} else {
$em->getManager()->remove($child);
$em->getManager()->flush();
// Retour à la liste
return $this->redirectToRoute($this->route, ['catparent' => $catparent, 'idparent' => $idparent]);
}
}
public function upload($catparent, $idparent, $idchild, $type, Request $request, ManagerRegistry $em)
{
// Récupérer la map
$data = $em->getRepository($this->entity)->find($idchild);
if (!$data) {
throw $this->createNotFoundException('Not exist');
}
// Rendu
return $this->render($this->render.'upload.html.twig', [
'useheader' => true,
'usesidebar' => true,
'type' => 'child',
'catparent' => $catparent,
'idparent' => $idparent,
'idchild' => $idchild,
'idchildtype' => $data->getChildtype()->getId(),
'typeupload' => $type,
]);
}
public function order($catparent, $idparent, Request $request, ManagerRegistry $em)
{
// Récupérer le parent
switch ($catparent) {
case 'blog':
$parent = $em->getRepository("App\Entity\Blog")->find($idparent);
break;
case 'page':
$parent = $em->getRepository("App\Entity\Page")->find($idparent);
break;
}
if ($parent) {
$childids = explode(',', $request->request->get('lstordered'));
$i = 1;
foreach ($childids as $id) {
$child = $em->getRepository($this->entity)->find($id);
$child->setRoworder($i);
if ($child) {
$child->setRoworder($i);
$em->getManager()->flush();
}
++$i;
}
}
return new JsonResponse([]);
}
public function select(Request $request, ManagerRegistry $em)
{
// S'assurer que c'est un appel ajax
if (!$request->isXmlHttpRequest()) {
return new JsonResponse(['message' => 'Interdit'], 400);
}
$output = [];
$page_limit = $request->query->get('page_limit');
$q = $request->query->get('q');
$qb = $em->createQueryBuilder();
$qb->select('table')->from($this->entity, 'table')
->where('table.name LIKE :value')
->setParameter('value', '%'.$q.'%')
->orderBy('table.name');
$datas = $qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
foreach ($datas as $data) {
array_push($output, ['id' => $data->getId(), 'text' => $data->getName()]);
}
$ret_string['results'] = $output;
return new JsonResponse($ret_string);
}
public function view($catparent, $idparent, $idchild, Request $request, ManagerRegistry $em)
{
$framed = boolval($request->get('framed'));
$size = intval($request->get('size'));
// Récupérer le parent
switch ($catparent) {
case 'blog':
$parent = $em->getRepository("App\Entity\Blog")->find($idparent);
break;
case 'page':
$parent = $em->getRepository("App\Entity\Page")->find($idparent);
break;
}
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
// Récupérer le type de parent
switch ($catparent) {
case 'blog':
$typeparent = $parent->getBlogtype()->getName();
break;
case 'page':
$typeparent = $parent->getPagetype()->getName();
break;
}
// Récupérer le child
$child = $em->getRepository("App\Entity\Child")->find($idchild);
$firstchild = $em->getRepository("App\Entity\Child")->findFirstchild($catparent, $idparent);
if (!$child) {
$child = $firstchild;
}
if (!$child) {
throw $this->createNotFoundException('Not exist');
}
switch ($child->getChildtype()->getId()) {
// Image / Image embed
case 12:
case 13:
$imageprev = $em->getRepository($this->entity)->findPrev($catparent, $idparent, [12, 13], $relation->getRoworder());
$imagenext = $em->getRepository($this->entity)->findNext($catparent, $idparent, [12, 13], $relation->getRoworder());
return $this->render($this->render.'image.html.twig', [
'catparent' => $catparent,
'parent' => $parent,
'typeparent' => $typeparent,
'firstchild' => $firstchild,
$this->data => $child,
'useheader' => false,
'usesidebar' => false,
'maxwidth' => true,
'imageprev' => $imageprev,
'imagenext' => $imagenext,
]);
break;
// Video / Video embed
case 14:
case 15:
return $this->render($this->render.'video.html.twig', [
'catparent' => $catparent,
'parent' => $parent,
'typeparent' => $typeparent,
'firstchild' => $firstchild,
$this->data => $child,
'useheader' => (!$framed),
'usesidebar' => false,
'maxwidth' => true,
'framed' => $framed,
'size' => $size,
]);
break;
// Ressource / Ressource embed / Lien externe / Biblio
case 16:
case 17:
case 21:
case 22:
return $this->render($this->render.'file.html.twig', [
'catparent' => $catparent,
'parent' => $parent,
'typeparent' => $typeparent,
'firstchild' => $firstchild,
$this->data => $child,
'useheader' => (!$framed),
'usesidebar' => false,
'maxwidth' => true,
'framed' => $framed,
'size' => $size,
]);
break;
// Lien externe / Bibliographie
case 21:
case 12:
$size = 2;
// no break
default:
$images = $em->getRepository($this->entity)->findOtherchilds($catparent, $idparent, [12, 13], $idchild);
$videos = $em->getRepository($this->entity)->findOtherchilds($catparent, $idparent, [14, 15], $idchild);
$contacts = $em->getRepository($this->entity)->findOtherchilds($catparent, $idparent, [20], $idchild);
$ressources = $em->getRepository($this->entity)->findOtherchilds($catparent, $idparent, [16, 17], $idchild);
$links = $em->getRepository($this->entity)->findOtherchilds($catparent, $idparent, [21], $idchild);
$bibliographys = $em->getRepository($this->entity)->findOtherchilds($catparent, $idparent, [22], $idchild);
return $this->render($this->render.'view.html.twig', [
'useheader' => false,
'usemenu' => true,
'maxsize' => ($catparent=="page" ? 900 : 1200),
'catparent' => $catparent,
'parent' => $parent,
'typeparent' => $typeparent,
'firstchild' => $firstchild,
$this->data => $child,
'usesidebar' => false,
'maxwidth' => true,
'framed' => $framed,
'size' => $size,
'pages' => $em->getRepository($this->entity)->findPages($catparent, $idparent),
'images' => $images,
'videos' => $videos,
'contacts' => $contacts,
'ressources' => $ressources,
'links' => $links,
'bibliographys' => $bibliographys,
]);
break;
}
}
public function restjson($catparent, $idparent, $idchild, Request $request, ManagerRegistry $em)
{
// S'assurer que l'on doit ou non configurer le proxy
$url = 'https://'.$this->getParameter('appWeburl').'/'.$this->getParameter('appAlias');
$clientguzzle = new \GuzzleHttp\Client(['timeout' => 3, 'verify' => false]);
$token = $this->getParameter('appSecret');
try {
$response = $clientguzzle->request('GET', $url);
} catch (RequestException $e) {
if ($e->hasResponse()) {
return 0;
}
}
// Entete
$headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];
if ($token) {
$headers['Authorization'] = 'token '.$token;
}
// Paramétrage unirest
\Unirest\Request::verifyPeer(false);
\Unirest\Request::verifyHost(false);
\Unirest\Request::timeout(5);
$query = ['key' => $token, 'parentcat' => $catparent, 'parentid' => $idparent, 'childid' => $idchild, 'refreshcache' => true];
$query = json_encode($query);
try {
$response = \Unirest\Request::post($url.'/rest/getChild', $headers, $query);
} catch (\Exception $e) {
return false;
}
return new JsonResponse([]);
}
protected function formatEmbed($url)
{
// Formatage youtube
if (false === stripos($url, 'https://www.youtube.com/embed') && false !== stripos($url, 'youtube')) {
$url = str_replace('http://www.youtube.com', 'https://www.youtube.com', $url);
$url = str_replace('https://www.youtube.com/watch?v=', '', $url);
$tmp = explode('&', $url);
$url = 'https://www.youtube.com/embed/'.$tmp[0];
}
if (false === stripos($url, 'https://www.youtube.com/embed') && false !== stripos($url, 'youtu.be')) {
$url = str_replace('http://youtu.be/', 'https://www.youtube.com/embed/', $url);
$url = str_replace('https://youtu.be/', 'https://www.youtube.com/embed/', $url);
}
// Formatage Dalymotion
if (false === stripos($url, 'https://www.dailymotion.com/embed') && false !== stripos($url, 'dailymotion')) {
$url = str_replace('http://www.dailymotion.com', 'https://www.dailymotion.com', $url);
$url = str_replace('https://www.dailymotion.com/video/', '', $url);
$tmp = explode('?', $url);
$url = 'https://www.dailymotion.com/embed/video/'.$tmp[0];
}
// Formatage Peertube
if (false !== stripos($url, '/watch/')) {
$url = str_replace('/watch/', '/embed/', $url);
}
return $url;
}
protected function getErrorForm($id, $form, $request, $data, $mode)
{
if ($form->get('submit')->isClicked() && 'delete' == $mode) {
}
if ($form->get('submit')->isClicked() && 'submit' == $mode) {
}
if ($form->get('submit')->isClicked() && !$form->isValid()) {
$this->get('session')->getFlashBag()->clear();
$errors = $form->getErrors();
foreach ($errors as $error) {
$request->getSession()->getFlashBag()->add('error', $error->getMessage());
}
}
}
}