src/Controller/Frontend/SitemapController.php line 33

  1. <?php
  2. namespace App\Controller\Frontend;
  3. use Webbamboo\SpinnersCommon\Entity\City;
  4. use Webbamboo\SpinnersCommon\Entity\Page;
  5. use Webbamboo\SpinnersCommon\Entity\Article;
  6. use Webbamboo\SpinnersCommon\Entity\Company;
  7. use Webbamboo\SpinnersCommon\Entity\Counties;
  8. use Webbamboo\SpinnersCommon\Entity\Generalsettings;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. class SitemapController extends AbstractController
  16. {
  17.     protected EntityManagerInterface $em;
  18.     public function __construct(EntityManagerInterface $em)
  19.     {
  20.         $this->em $em;    
  21.     }
  22.     private function getDTO()
  23.     {
  24.         $dt = new \DateTime();
  25.         return $dt;
  26.     }
  27.     #[Route("/sitemaps/sitemap-general.xml"name"spinner_general_sitemap"methods: ['GET'])]
  28.     public function generalsitemapAction()
  29.     {
  30.         //var_dump($this->calculateCitySitemaps());
  31.         ob_start();
  32.         echo '<?xml version="1.0" encoding="UTF-8"?>';
  33.         echo '<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  34.         //County
  35.         echo '<sitemap>';
  36.         echo '<loc>'.$this->generateUrl('spinner_county_sitemap', array(), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';
  37.         echo '<lastmod>'.$this->getDTO()->createFromFormat('d-m-Y''29-07-2014')->format('Y-m-d').'</lastmod>';
  38.         echo '</sitemap>';
  39.         //City
  40.         for ($i=1;$i<=$this->calculateCitySitemaps();$i++) {
  41.             echo '<sitemap>';
  42.             echo '<loc>'.$this->generateUrl('spinner_city_sitemap', array('part' => $i), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';
  43.             echo '<lastmod>'.$this->getDTO()->createFromFormat('d-m-Y''29-07-2014')->format('Y-m-d').'</lastmod>';
  44.             echo '</sitemap>';
  45.         }
  46.         //Company
  47.         for ($i=1;$i<=$this->calculateCompanySitemaps();$i++) {
  48.             echo '<sitemap>';
  49.             echo '<loc>'.$this->generateUrl('spinner_company_sitemap', array('part' => $i), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';
  50.             echo '<lastmod>'.$this->getDTO()->createFromFormat('d-m-Y''29-07-2014')->format('Y-m-d').'</lastmod>';
  51.             echo '</sitemap>';
  52.         }
  53.         //Page
  54.         echo '<sitemap>';
  55.         echo '<loc>'.$this->generateUrl('spinner_page_sitemap', array(), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';
  56.         echo '<lastmod>'.$this->getDTO()->createFromFormat('d-m-Y''29-07-2014')->format('Y-m-d').'</lastmod>';
  57.         echo '</sitemap>';
  58.         //echo '</sitemapindex>';
  59.         //Article
  60.         echo '<sitemap>';
  61.         echo '<loc>'.$this->generateUrl('spinner_article_sitemap', array(), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';
  62.         echo '<lastmod>'.$this->getDTO()->createFromFormat('d-m-Y''03-12-2014')->format('Y-m-d').'</lastmod>';
  63.         echo '</sitemap>';
  64.         echo '</sitemapindex>';
  65.         $content ob_get_contents();
  66.         ob_end_clean();
  67.         $response = new Response();
  68.         $response->headers->set('Content-Type''text/xml');
  69.         $response->setContent($content);
  70.         return $response;
  71.     }
  72.     #[Route("/sitemaps/sitemap-county.xml"name"spinner_county_sitemap"methods: ['GET'])]
  73.     public function countysitemapAction()
  74.     {
  75.         $counties $this->em->getRepository(Counties::class)->findAll();
  76.         ob_start();
  77.         echo '<?xml version="1.0" encoding="UTF-8"?>';
  78.         echo '   <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  79.         foreach ($counties as $c) {
  80.             echo '      <url>';
  81.             echo '         <loc>'.$this->generateUrl('spinner_front_county', array('countypermalink' => $this->capitalizePermalink($c->getPermalinkP())), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';
  82.             echo '         <lastmod>'.$this->getDTO()->createFromFormat('d-m-Y''29-07-2014')->format('Y-m-d').'</lastmod>';
  83.             echo '         <changefreq>weekly</changefreq>';
  84.             echo '         <priority>0.8</priority>';
  85.             echo '      </url>';
  86.         }
  87.         echo '   </urlset>';
  88.         $content ob_get_contents();
  89.         ob_end_clean();
  90.         $response = new Response();
  91.         $response->headers->set('Content-Type''text/xml');
  92.         $response->setContent($content);
  93.         return $response;
  94.     }
  95.     #[Route("/sitemaps/sitemap-pages.xml"name"spinner_page_sitemap"methods: ['GET'])]
  96.     public function pagesitemapAction()
  97.     {
  98.         $pages $this->em->getRepository(Page::class)->findAll();
  99.         ob_start();
  100.         echo '<?xml version="1.0" encoding="UTF-8"?>';
  101.         echo '   <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  102.         foreach ($pages as $c) {
  103.             echo '      <url>';
  104.             echo '         <loc>'.$this->generateUrl('spinner_front_pages', array('pagepermalink' => $this->capitalizePermalink($c->getPermalinkP())), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';
  105.             echo '         <lastmod>'.$this->getDTO()->createFromFormat('d-m-Y''29-07-2014')->format('Y-m-d').'</lastmod>';
  106.             echo '         <changefreq>weekly</changefreq>';
  107.             echo '         <priority>0.8</priority>';
  108.             echo '      </url>';
  109.         }
  110.         echo '   </urlset>';
  111.         $content ob_get_contents();
  112.         ob_end_clean();
  113.         $response = new Response();
  114.         $response->headers->set('Content-Type''text/xml');
  115.         $response->setContent($content);
  116.         return $response;
  117.     }
  118.     #[Route("/sitemaps/sitemap-articles.xml"name'spinner_article_sitemap'methods: ['GET'])]
  119.     public function articlesitemapAction()
  120.     {
  121.         $articles $this->em->getRepository(Article::class)->findAll();
  122.         ob_start();
  123.         echo '<?xml version="1.0" encoding="UTF-8"?>';
  124.         echo '   <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  125.         foreach ($articles as $c) {
  126.             echo '      <url>';
  127.             echo '         <loc>'.$this->generateUrl('spinner_front_articles_single', array('articlepermalink' => $this->capitalizePermalink($c->getPermalinkP())), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';
  128.             echo '         <lastmod>'.$this->getDTO()->createFromFormat('d-m-Y''29-07-2014')->format('Y-m-d').'</lastmod>';
  129.             echo '         <changefreq>weekly</changefreq>';
  130.             echo '         <priority>0.8</priority>';
  131.             echo '      </url>';
  132.         }
  133.         echo '   </urlset>';
  134.         $content ob_get_contents();
  135.         ob_end_clean();
  136.         $response = new Response();
  137.         $response->headers->set('Content-Type''text/xml');
  138.         $response->setContent($content);
  139.         return $response;
  140.     }
  141.     #[Route("/sitemaps/sitemap-city-{part}.xml"name"spinner_city_sitemap"methods: ['GET'])]
  142.     public function citysitemapAction($part)
  143.     {
  144.         $results 1000;
  145.         $realPart $part-1;
  146.         $offset $realPart*$results;
  147.         $cities $this->em
  148.             ->getRepository(City::class)
  149.             ->findBy(array(), array(), $results$offset);
  150.         ob_start();
  151.         echo '<?xml version="1.0" encoding="UTF-8"?>';
  152.         echo '   <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  153.         foreach ($cities as $c) {
  154.             echo '      <url>';
  155.             echo '         <loc>'.$this->generateUrl('spinner_front_city', array('citypermalink' => $this->capitalizePermalink($c->getPermalinkP()), 'countypermalink'    => $this->capitalizePermalink($c->getCounty()->getPermalinkP())), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';
  156.             echo '         <lastmod>'.$this->getDTO()->createFromFormat('d-m-Y''29-07-2014')->format('Y-m-d').'</lastmod>';
  157.             echo '         <changefreq>weekly</changefreq>';
  158.             echo '         <priority>0.8</priority>';
  159.             echo '      </url>';
  160.         }
  161.         echo '   </urlset>';
  162.         $content ob_get_contents();
  163.         ob_end_clean();
  164.         $response = new Response();
  165.         $response->headers->set('Content-Type''text/xml');
  166.         $response->setContent($content);
  167.         return $response;
  168.     }
  169.     #[Route('/sitemaps/sitemap-company-{part}.xml'name'spinner_company_sitemap'methods: ['GET'])]
  170.     public function companysitemapAction($part)
  171.     {
  172.         $results 1000;
  173.         $realPart $part-1;
  174.         $offset $realPart*$results;
  175.         $companies $this->em
  176.             ->getRepository(Company::class)
  177.             ->findBy(array(), array(), $results$offset);
  178.         ob_start();
  179.         echo '<?xml version="1.0" encoding="UTF-8"?>';
  180.         echo '   <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  181.         foreach ($companies as $c) {
  182.             $city $c->getCityobj();
  183.             if (isset($city)) {
  184.                 $county $c->getCityobj()->getCounty();
  185.             }
  186.             if ($city && isset($county)) {
  187.                 echo '      <url>';
  188.                 echo '         <loc>'.$this->generateUrl('spinner_front_company', array('citypermalink' => $this->capitalizePermalink($city->getPermalinkP()), 'companypermalink' => $this->capitalizePermalink($c->getPermalinkP())), UrlGeneratorInterface::ABSOLUTE_URL).'</loc>';
  189.                 echo '         <lastmod>'.$this->getDTO()->createFromFormat('d-m-Y''29-07-2014')->format('Y-m-d').'</lastmod>';
  190.                 echo '         <changefreq>weekly</changefreq>';
  191.                 echo '         <priority>0.8</priority>';
  192.                 echo '      </url>';
  193.             }
  194.         }
  195.         echo '   </urlset>';
  196.         $content ob_get_contents();
  197.         ob_end_clean();
  198.         $response = new Response();
  199.         $response->headers->set('Content-Type''text/xml');
  200.         $response->setContent($content);
  201.         return $response;
  202.     }
  203.     private function calculateCitySitemaps()
  204.     {
  205.         $qb $this->em->createQueryBuilder();
  206.         $qb->select('count(city.id)');
  207.         $qb->from(City::class, 'city');
  208.         $count ceil($qb->getQuery()->getSingleScalarResult()/1000);
  209.         return (int)$count;
  210.     }
  211.     private function calculateCompanySitemaps()
  212.     {
  213.         $qb $this->em->createQueryBuilder();
  214.         $qb->select('count(company.id)');
  215.         $qb->from(Company::class, 'company');
  216.         $count ceil($qb->getQuery()->getSingleScalarResult()/1000);
  217.         return (int)$count;
  218.     }
  219.     private function capitalizePermalink($permalink)
  220.     {
  221.         return mb_convert_case($permalinkMB_CASE_TITLE"UTF-8");
  222.     }
  223. }