src/Controller/DefaultController.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\API\FattureInCloud\Request\DocumentoDettagliRequest;
  4. use App\Entity\FattureInCloud\DocumentoDettagliato;
  5. use App\Entity\Gallery;
  6. use App\Entity\GalleryView;
  7. use App\Message\Gallery\GalleryViewNotification;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpClient\HttpClient;
  11. use Symfony\Component\HttpClient\RetryableHttpClient;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\Messenger\MessageBusInterface;
  15. use Symfony\Component\Mime\Email;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use App\API\FattureInCloud\Request\DocumentiListaRequest;
  19. use App\API\FattureInCloud\Documenti;
  20. use FattureInCloud\Configuration;
  21. use Kigkonsult\Icalcreator\Vcalendar;
  22. use App\Service\UrlRsrc;
  23. use ICal\ICal;
  24. class DefaultController extends AbstractController
  25. {
  26.     /**
  27.      * @Route(name="homeOldApi", path="/homeOldApi")
  28.      *
  29.      * @return Response
  30.      */
  31.     public function old(EntityManagerInterface $entityManager): Response
  32.     {
  33.         $client = new RetryableHttpClient(HttpClient::create());
  34.         $documentsListRequest = new DocumentiListaRequest();
  35.         $documentsListRequest->setAnno(2022);
  36.         // fatture
  37.         // proforma
  38.         // ordini
  39.         $documentType 'ordini';
  40.         $documentsApi = new Documenti($client);
  41.         $documentiListaResponse $documentsApi->lista($documentType$documentsListRequest);
  42.         if ($documentiListaResponse->isSucces()) {
  43.             foreach($documentiListaResponse->getListaDocumenti() as $documentiListaElementoResponse) {
  44. //                dump($documentiListaElementoResponse);
  45.                 $documentoDettagliRequest = new DocumentoDettagliRequest();
  46.                 $documentoDettagliRequest->setId($documentiListaElementoResponse->getId());
  47.                 // TODO: add righe, pagamenti, eccetera, al momento li ho commentati nel costruttore
  48.                 $documentoDettagliatoResponse $documentsApi->dettagli($documentType$documentoDettagliRequest);
  49. //                dump($documentoDettagliatoResponse);
  50.                 // if document not exists in db, add it, else skip it
  51.                 $document $entityManager->find(DocumentoDettagliato::class, $documentoDettagliatoResponse->getDettagliDocumento()->getId());
  52.                 if(!$document) {
  53.                     // mancano gli articoli e i pagamenti
  54.                     $document = new DocumentoDettagliato($documentoDettagliatoResponse);
  55.                 }
  56.                 $entityManager->persist($document);
  57.             }
  58.             $entityManager->flush();
  59.         }
  60.         return new Response('importazione eseguita');
  61.     }
  62.     /**
  63.      * @Route(name="home", path="/")
  64.      *
  65.      * @return Response
  66.      */
  67.     public function index(EntityManagerInterface $entityManager): Response
  68.     {
  69.         return $this->render('home.html.twig');
  70.     }
  71.     /**
  72.      * @Route(name="calendar", path="/calendar")
  73.      *
  74.      * @return Response
  75.      */
  76.     public function calendar(): Response
  77.     {
  78.         // get iCal contents from URL resource
  79.         $url 'https://gpzoboli:[email protected]/remote.php/dav/calendars/gpzoboli/seltzlab?export';
  80. //        $iCalContent = UrlRsrc::getContent($url);
  81. //        $iCalContent = file_get_contents($this->getParameter('dir.downloads').'pitagora.ics');
  82.         $ical = new ICal($url, array('httpUserAgent' => 'A Different User Agent'));
  83.         dump($ical->eventsFromRange('2023-01-01 00:00:00''2023-05-31 23:59:59'));
  84.         return new Response('ciao');
  85.     }
  86.     /**
  87.      * @Route(name="show", path="/show/{slug}")
  88.      *
  89.      * @return Response
  90.      */
  91.     public function showSlug(string $slugEntityManagerInterface $entityManagerRequest $requestMessageBusInterface $bus): Response
  92.     {
  93.         $timestamp = new \DateTimeImmutable('now');
  94.         $message '';
  95.         $subject 'New view for slug '.$slug.'!';
  96.         $file 'assets/gallery/placeholder.png';
  97.         $gallery $entityManager->getRepository(Gallery::class)->findOneBy(['slug' => $slug]);
  98.         if (!$gallery) {
  99.             $message 'Condivisione non trovata';
  100.             $subject 'New view for unfound slug '.$slug.'!';
  101.         } else {
  102.             if (!$gallery->isActive()) {
  103.                 $message 'Condivisione non attiva';
  104.                 $subject 'New view for inactive slug '.$slug.'!';
  105.             } else {
  106.                 $file $gallery->getFilename();
  107.                 if ($gallery->getExpiration() !== null) {
  108.                     if ($timestamp $gallery->getExpiration()) {
  109.                         $file 'assets/gallery/placeholder.png';
  110.                         $message 'Condivisione scaduta';
  111.                         $subject 'New view for expired slug ' $slug '!';
  112.                     } elseif ($gallery->getAllowedViews() > && $gallery->getAllowedViews() -$gallery->getViews()->count()) {
  113.                         $file 'assets/gallery/placeholder.png';
  114.                         $message 'Viste condivisione esaurite';
  115.                     }
  116.                 } elseif ($gallery->getAllowedViews() > && $gallery->getAllowedViews() -$gallery->getViews()->count()) {
  117.                         $file 'assets/gallery/placeholder.png';
  118.                         $message 'Viste condivisione esaurite';
  119.                 }
  120.             }
  121.         }
  122.         $bus->dispatch(new GalleryViewNotification($timestamp$slug$request->getClientIp(), $request->headers->get('user-agent'), $subject));
  123.         // ad ogni vista aggiungere un log e mandare mail (o notifica)
  124.         return $this->render('controller/default/showSlug.html.twig', [
  125.             'file' => $file,
  126.             'message' => $message,
  127.         ]);
  128.     }
  129. }