src/Controller/MovieDataController.php line 97

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. use Knp\Component\Pager\PaginatorInterface;
  9. use \Pimcore\Model\DataObject;
  10. use Pimcore\Model\DataObject\Movies;
  11. use App\Model\Movie;
  12. use Pimcore\Model\DataObject\Actor;
  13. use Pimcore\Model\Listing;
  14. use Pimcore\Twig\Extension\Templating\HeadTitle;
  15. use Pimcore\Twig\Extension\Templating\Placeholder;
  16. use FFI\Exception;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Pimcore\Model\DataObject\Data\Hotspotimage;
  19. class MovieDataController extends FrontendController
  20. {
  21.     public static $perPage 60;
  22.     public static $similarCount 12//multiple of 4 preferably
  23.     /**
  24.      * @Template
  25.      */
  26.     public function defaultAction(Request $request)
  27.     {
  28.         return [];
  29.     }
  30.     /**
  31.      * @param Request $request
  32.      *
  33.      * @return Response
  34.      *
  35.      * @throws \Exception
  36.      */
  37.     public function movieDataAction(Request $requestPaginatorInterface $paginator$searchTerm "")
  38.     {
  39.         
  40.         // get a list of Movies objects and order them by name
  41.         $movieList = new Movies\Listing();
  42.         if($request->get('latest'))
  43.         {
  44.             $movieList->setOrderKey('o_modificationDate');
  45.             $movieList->setOrder('DESC');
  46.         }
  47.         else
  48.         {
  49.         $movieList->setOrderKey('title');
  50.         $movieList->setOrder('ASC');
  51.         }
  52.         $movieList->setLocale("de");
  53.         if($searchTerm)
  54.             $movieList->setCondition("title LIKE ?", ["%".$searchTerm."%"]);
  55.         $paginator $paginator->paginate(
  56.             $movieList,
  57.             $request->get('page'1),
  58.             self::$perPage
  59.         );
  60.         $fallbackImage \Pimcore\Model\Asset::getById(414);
  61.         return $this->render('movie/movies.html.twig', [
  62.             'movies' => $movieList,
  63.             'paginationVariables' => $paginator->getPaginationData(),
  64.             'fallbackImage' => $fallbackImage,
  65.             'searchTerm' => $searchTerm,
  66.             'sortByLatest' => $request->get('latest')
  67.         ]);
  68.         // $this->render('movie/movies.html.twig');
  69.     }
  70.     #[Route('/import-image/{id}')]
  71.     public function movieImportAction(Request $request): Response
  72.     {
  73.         try
  74.         {
  75.         $id $request->get('id');
  76.         $object Movies::getById($id) == null ? throw new Exception() : Movies::getByID($id)->getTitle("de");
  77.         $object == null ? throw new Exception() : 0;        
  78.         }
  79.         catch(Exception $e)
  80.         {
  81.             echo "This ID does not exist or could not be found";
  82.             return new Response();
  83.         }
  84.         
  85.         $movie = new Movie();
  86.         $movieList = new Movies\Listing();
  87.         Movie::$DebugMode true;
  88.         $movieList->setCondition("oo_id LIKE ?", ["%".$id."%"]);
  89.         $movie->getMovieInformationPerLanguage($movieList'de-DE'true);
  90.         $movie->getMovieInformationPerLanguage($movieList'en-EN'true);   
  91.         Movie::$DebugMode false;         
  92.         return $this->render('simple-page/importInfo.twig',[
  93.                 'title' => $object
  94.     ]);
  95.     }
  96.     #[Route('/movie-detail/{id}')]
  97.     public function movieDetailAction(Request $request): Response
  98.     {
  99.         $id $request->get('id');
  100.         $movie Movies::getById($id);
  101.         $actors $movie->getActors();
  102.         $similarMovies $this->getSimilarMovies($movie);
  103.         
  104.         return $this->render('movie/movies-detail.html.twig',
  105.         [
  106.             'movie' => $movie,
  107.             'actors' => $actors,
  108.             'similarmovies' => $similarMovies
  109.         ]);
  110.     }   
  111.     #[Route('/actor-detail/{id}')]
  112. public function actorDetailAction(Request $request): Response
  113. {
  114.     $id $request->get('id');
  115.     $actor Actor::getById($id);
  116.     $movies $this->getMoviesForActor($actor);
  117.     $similarActors $this->getSimilarActors($actor);
  118.     
  119.     return $this->render('movie/actors-detail.html.twig',
  120.     [
  121.         'actor' => $actor,
  122.         'movies' => $movies,
  123.         'similarActors' => $similarActors
  124.     ]);
  125. }
  126.     public function getSimilarMovies(Movies $movie)
  127.     {
  128.         $movieList = new Movies\Listing();
  129.         $movieList->setOrderKey("RAND()",false);
  130.         $movieList->setOrder('ASC');
  131.         $movieList->setLocale("de");
  132.         $movieList->setCondition("genre LIKE ?", ["%".$movie->getGenre()."%"]);
  133.         $movieList->setCondition("oo_id != ?", ["%".$movie->getId()."%"]);
  134.         $movieList->setLimit(self::$similarCount);
  135.         return $movieList;
  136.     }
  137.     public function getSimilarActors(Actor $Actor)
  138.     {
  139.         $actorList = new Actor\Listing();
  140.         $actorList->setOrderKey("RAND()"false);
  141.         $actorList->setOrder('ASC');
  142.         $actorList->setLocale("de");
  143.         
  144.         $actorList->setLimit(self::$similarCount);
  145.         
  146.         return $actorList;
  147.     }
  148.     public function getMoviesForActor(Actor $actor)
  149.     {
  150.         $movieList = new Movies\Listing();
  151.         $movieList->setOrderKey("RAND()"false);
  152.         $movieList->setOrder('ASC');
  153.         $movieList->setLocale("de");
  154.         
  155.         // Assuming there is a method in the Movies class to filter by actor ID
  156.         $movieList->setCondition("actors LIKE ?", ["%".$actor->getId()."%"]);
  157.         
  158.         $movieList->setLimit(self::$similarCount);
  159.         
  160.         return $movieList;
  161.     }
  162.     /**
  163.      * @Route("/movies/add-new", name="movie_add_form", methods={"GET"})
  164.      */
  165.     public function addForm(): Response
  166.     {
  167.         return $this->render('movie/add-new-movie.html.twig');
  168.     }
  169.     /**
  170.      * @Route("/movies/add-new", name="movie_add", methods={"POST"})
  171.      */
  172.     public function addAction(Request $request): Response
  173.     {
  174.         $title $request->request->get('title');
  175.         $type $request->request->get('type');
  176.         $status $request->request->get('status');
  177.         $image[] = $request->files->get('captureImage');
  178.         $image[] = $request->files->get('uploadImage');
  179.         if(DataObject::getByPath('/Movies/' \Pimcore\File::getValidFilename($title)))
  180.             return new Response('Movie already exists - nothing added');
  181.         $movie = new Movie();
  182.         $movie->setKey(\Pimcore\File::getValidFilename($title)); // Ensure a valid filesystem name
  183.         $movie->setParentId(3171); // Set the parent ID to a suitable location in your object tree
  184.         $movie->setTitle($title,"de");
  185.         if($type == "Other")
  186.             $typeArray[] = "Sonstiges";
  187.         else
  188.             $typeArray[] = $type;
  189.         $movie->setDisc_Type($typeArray);
  190.         $statusArray[] = $status;
  191.         $movie->setStatus($statusArray);
  192.         $movie->setPublished(true);
  193.         $movie->setIsSteelbook(true);
  194.         $movie->setOriginalTitle($title"de");
  195.         $movie->setTitle($title,"en");
  196.         $movie->setOriginalTitle($title"en");
  197.         if ($image) {
  198.             $galleryArray = [];
  199.             $it 0;
  200.         
  201.             foreach($image as $i) { // Corrected the loop to properly iterate over each image
  202.                 if ($i != null)
  203.                 {
  204.                 $asset = new \Pimcore\Model\Asset\Image();
  205.                 $asset->setParentId(418); // Adjust the parent ID as needed
  206.                 $asset->setData(file_get_contents($i->getPathname())); // Corrected to use $i for the current image in the loop
  207.                  // Append a timestamp to the title to avoid duplicate filenames
  208.                 $timestamp date('YmdHis'); // Generates a timestamp in the format YearMonthDayHourMinuteSecond
  209.                 $filename \Pimcore\File::getValidFilename($title '_' $timestamp) . $it '.png'// Append the timestamp to the title
  210.                 $asset->setFilename($filename);
  211.                 $asset->save();
  212.         
  213.                 $it++;
  214.                 $gimage = new HotspotImage();
  215.                 $gimage->setImage($asset);
  216.                 $galleryArray[] = $gimage// Add each asset to the gallery
  217.                 }
  218.             }
  219.             $movie->setSteelbook_Images(new \Pimcore\Model\DataObject\Data\ImageGallery($galleryArray)); 
  220.         }
  221.         $movie->getDatabaseInformation($movie"de-DE");
  222.         $movie->getDatabaseInformation($movie,"en-EN");
  223.         $movie->save();
  224.         return new RedirectResponse('/movie-detail/' $movie->getId());
  225.     }
  226. }