src/Utils/CategoryHelper.php line 141

Open in your IDE?
  1. <?php
  2. namespace App\Utils;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. // use App\Exception\ResourceNotFoundException;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use App\Entity\Category;
  7. use Doctrine\Common\Collections\Criteria;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class CategoryHelper 
  10. {
  11.     
  12.     protected $entityManager;
  13.     protected $requestStack;
  14.     
  15.     public function __construct (EntityManagerInterface $entityManagerRequestStack $requestStack)
  16.     {
  17.         $this->entityManager $entityManager;
  18.         $this->requestStack $requestStack;
  19.     }
  20.     
  21.     public function getSiteCode() {
  22.         $site_code null;
  23.         $request $this->requestStack->getCurrentRequest();
  24.         if($request) {
  25.             $session $request->getSession();
  26.             if($session) {
  27.                 $site_code $session->get("site");
  28.             }
  29.         }
  30.         return $site_code;
  31.     }
  32.     
  33.     public function getUniqueSlug (Category $category)
  34.     {
  35.         // query the database and make sure the returned slug is unique
  36.     }
  37.     
  38.     /*
  39.     Renamed to getCategoriesByTaxonomy... does it need to be updated anywhere else before deleting this?
  40.     */
  41.     public function getCategoryByTaxonomy ($taxonomy "")
  42.     {
  43.         return $this->getCategoriesByTaxonomy($taxonomy);
  44.     }
  45.     
  46.     public function getCategoriesByTaxonomy ($taxonomy "")
  47.     {
  48.         $category $this->entityManager
  49.             ->getRepository(Category::class)
  50.             ->findBy([
  51.                 "taxonomy" => $taxonomy,
  52.                 "status" => 1,
  53.             ], [
  54.                 "title" => "ASC"//orderby
  55.             ]);
  56.         
  57.         if ($category) {
  58.             return $category;
  59.         }
  60.         return [];
  61.         //throw new NotFoundHttpException ("Category Taxonomy Not Found: {$taxonomy}");
  62.     }
  63.     
  64.     public function getPopulatedCategoriesByTaxonomy ($taxonomy "")
  65.     {
  66.         $site_code $this->getSiteCode();
  67.         
  68.         $category $this->entityManager
  69.             ->getRepository(Category::class)
  70.             ->getPopulatedCategoriesByTaxonomy($taxonomy$site_code);
  71.             
  72.         if ($category) {
  73.             return $category;
  74.         }
  75.         return [];
  76.         //throw new NotFoundHttpException ("Category Taxonomy Not Found: {$taxonomy}");
  77.     }
  78.     
  79.     public function getRootCategoriesByTaxonomy ($taxonomy "")
  80.     {
  81.         $category $this->entityManager
  82.             ->getRepository(Category::class)
  83.             ->findBy([
  84.                 "taxonomy" => $taxonomy,
  85.                 "status" => 1,
  86.                 "prnt" => null,
  87.             ], [
  88.                 "title" => "ASC"//orderby
  89.             ]);
  90.             
  91.         if ($category) {
  92.             return $category;
  93.         }
  94.         return [];
  95.         //throw new NotFoundHttpException ("Category Taxonomy Not Found: {$taxonomy}");
  96.     }
  97.     
  98.   //Not filtered by site anymore; categories can be used across sites 
  99.   // (need to confirm this is how we want to handle categories)
  100.       public function getCategoryBySlug ($slug "")
  101.       {
  102.           $qb $this->entityManager->createQueryBuilder();
  103.           
  104.           $category $qb->select('cat')
  105.               ->from("App\Entity\Category""cat")
  106.               ->where("cat.slug = :slug")
  107.               ->setParameter("slug"$slug)
  108.               ->setMaxResults(1)->getQuery()->getResult();
  109.           
  110.           if($category) {
  111.               $category $category[0];
  112.               return $category;
  113.           }
  114.           
  115.           throw new NotFoundHttpException ("Category Not Found: {$slug}");
  116.       }
  117.       
  118.       public function getCategoriesBySlug ($slug ""$taxonomy "")
  119.       {
  120.           $qb $this->entityManager->createQueryBuilder();
  121.           
  122.           $qb->select('cat')
  123.               ->from("App\Entity\Category""cat")
  124.               ->where("cat.slug = :slug");
  125.           if($taxonomy) {
  126.               $qb->andWhere("cat.taxonomy = :tax")
  127.                   ->setParameter("tax"$taxonomy);
  128.           }
  129.           $category $qb
  130.               ->setParameter("slug"$slug)
  131.               ->getQuery()->getResult();
  132.           
  133.           if($category) {
  134.               return $category;
  135.           }
  136.           
  137.           throw new NotFoundHttpException ("Category Not Found: {$slug}");
  138.       }
  139.     public function getCategoryExistsBySlug ($slug "")
  140.     {
  141.         $qb $this->entityManager->createQueryBuilder();
  142.         $category $qb->select('cat')
  143.             ->from("App\Entity\Category""cat")
  144.             ->where("cat.slug = :slug")
  145.             ->setParameter("slug"$slug)
  146.             ->setMaxResults(1)->getQuery()->getResult();
  147.         if($category) {
  148.             return true;
  149.         }
  150.         return false;
  151.     }
  152.   
  153.   // To filter by site, or not to filter by site?
  154.   
  155.   /*
  156.     //Filtered by site
  157.     public function getCategoryBySlug ($slug = "")
  158.     {
  159.         $site_code = $this->getSiteCode();
  160.         
  161.         $qb = $this->entityManager->createQueryBuilder();
  162.         
  163.         $qb->select('cat')
  164.             ->from("App\Entity\Category", "cat")
  165.             ->where("cat.slug = :slug");
  166.         if($site_code) {
  167.             $qb->join("cat.site", "s", "WITH", "s.id = :site")
  168.                 ->setParameter("site", $site_code);
  169.         }
  170.         $category = $qb
  171.             ->setParameter("slug", $slug)
  172.             ->setMaxResults(1)->getQuery()->getResult();
  173.         
  174.         if($category) {
  175.             $category = $category[0];
  176.             return $category;
  177.         }
  178.         
  179.         throw new NotFoundHttpException ("Category Not Found: {$slug}");
  180.     }
  181.     
  182.     //Filtered by site
  183.     public function getCategoriesBySlug ($slug = "", $taxonomy = "")
  184.     {
  185.         $site_code = $this->getSiteCode();
  186.         
  187.         $qb = $this->entityManager->createQueryBuilder();
  188.         
  189.         $qb->select('cat')
  190.             ->from("App\Entity\Category", "cat")
  191.             ->where("cat.slug = :slug");
  192.         if($site_code) {
  193.             $qb->join("cat.site", "s", "WITH", "s.id = :site")
  194.                 ->setParameter("site", $site_code);
  195.         }
  196.         if($taxonomy) {
  197.             $qb->andWhere("cat.taxonomy = :tax")
  198.                 ->setParameter("tax", $taxonomy);
  199.         }
  200.         $category = $qb
  201.             ->setParameter("slug", $slug)
  202.             ->getQuery()->getResult();
  203.         
  204.         if($category) {
  205.             return $category;
  206.         }
  207.         
  208.         throw new NotFoundHttpException ("Category Not Found: {$slug}");
  209.     }
  210.     */
  211.     
  212.     //returns the category and all child categories up to 2 levels deep
  213.     public function getChildCategoriesBySlugs($slugs = [])
  214.     {
  215.         $categorySQL = [];
  216.         foreach ($slugs as $slug) {
  217.             $categorySQL[] = ":slug" . (count($categorySQL) + 1);
  218.         }
  219.         
  220.         $query implode(" ", array (
  221.                 "SELECT DISTINCT category1",
  222.                 "FROM",
  223.                     "App\Entity\Category category1",
  224.                 "LEFT JOIN",
  225.                     "category1.prnt category2",
  226.                 "LEFT JOIN",
  227.                     "category2.prnt category3",
  228.                 "WHERE",
  229.                     "category1.slug IN (",
  230.                         implode(", "$categorySQL),
  231.                     ")",
  232.                     "OR",
  233.                     "category2.slug IN (",
  234.                         implode(", "$categorySQL),
  235.                     ") ",
  236.                     "OR",
  237.                     "category3.slug IN (",
  238.                         implode(", "$categorySQL),
  239.                     ") ",
  240.             ));
  241.         
  242.         $query $this->entityManager
  243.             ->createQuery($query);
  244.         
  245.         $pos 1;
  246.         foreach ($slugs as $slug) {
  247.             $query->setParameter("slug{$pos}"$slug);
  248.             $pos++;
  249.         }
  250.         
  251.         $results $query->getResult();
  252.         
  253.         return $results;
  254.     }
  255.     
  256.     public function getCategoryById ($id 0)
  257.     {
  258.         $category $this->entityManager
  259.             ->getRepository(Category::class)
  260.             ->findOneBy([
  261.                 "id" => $id
  262.             ]);
  263.         
  264.         if ($category) {
  265.             return $category;
  266.         }
  267.         
  268.         throw new NotFoundHttpException ("Category with Id {$id} - Not Found");
  269.     }
  270.     
  271.     public function getPaginatedContent(Category $categoryint $limit 10int $page 1)
  272.     {
  273.         $criteria Criteria::create()
  274.             ->where(Criteria::expr()->eq("status""1"))
  275.             ->orderBy(array("published_at" => Criteria::DESC))
  276.             ->setFirstResult($limit * ($page 1))
  277.             ->setMaxResults($limit)
  278.         ;
  279.         
  280.         return $category->getContent()->matching($criteria);
  281.     }
  282.     public function getCategories()
  283.     {
  284.         $content = [];
  285.         $categories $this->entityManager
  286.             ->getRepository(Category::class)
  287.             ->createQueryBuilder('c')
  288.             ->where('c.status = :status')
  289.             ->andWhere('c.taxonomy = :taxonomy or c.taxonomy = :taxonomy2')
  290.             ->setParameter('status'1)
  291.             ->setParameter('taxonomy''category')
  292.             ->setParameter('taxonomy2''ebook-category')
  293.             ->getQuery()
  294.             ->getResult();
  295.         
  296.         $content['content'] = $categories;
  297.         return $content;
  298.     }
  299.     
  300. }