<?php
namespace App\Utils;
use Doctrine\ORM\EntityManagerInterface;
// use App\Exception\ResourceNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Entity\Category;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\HttpFoundation\RequestStack;
class CategoryHelper
{
protected $entityManager;
protected $requestStack;
public function __construct (EntityManagerInterface $entityManager, RequestStack $requestStack)
{
$this->entityManager = $entityManager;
$this->requestStack = $requestStack;
}
public function getSiteCode() {
$site_code = null;
$request = $this->requestStack->getCurrentRequest();
if($request) {
$session = $request->getSession();
if($session) {
$site_code = $session->get("site");
}
}
return $site_code;
}
public function getUniqueSlug (Category $category)
{
// query the database and make sure the returned slug is unique
}
/*
Renamed to getCategoriesByTaxonomy... does it need to be updated anywhere else before deleting this?
*/
public function getCategoryByTaxonomy ($taxonomy = "")
{
return $this->getCategoriesByTaxonomy($taxonomy);
}
public function getCategoriesByTaxonomy ($taxonomy = "")
{
$category = $this->entityManager
->getRepository(Category::class)
->findBy([
"taxonomy" => $taxonomy,
"status" => 1,
], [
"title" => "ASC", //orderby
]);
if ($category) {
return $category;
}
return [];
//throw new NotFoundHttpException ("Category Taxonomy Not Found: {$taxonomy}");
}
public function getPopulatedCategoriesByTaxonomy ($taxonomy = "")
{
$site_code = $this->getSiteCode();
$category = $this->entityManager
->getRepository(Category::class)
->getPopulatedCategoriesByTaxonomy($taxonomy, $site_code);
if ($category) {
return $category;
}
return [];
//throw new NotFoundHttpException ("Category Taxonomy Not Found: {$taxonomy}");
}
public function getRootCategoriesByTaxonomy ($taxonomy = "")
{
$category = $this->entityManager
->getRepository(Category::class)
->findBy([
"taxonomy" => $taxonomy,
"status" => 1,
"prnt" => null,
], [
"title" => "ASC", //orderby
]);
if ($category) {
return $category;
}
return [];
//throw new NotFoundHttpException ("Category Taxonomy Not Found: {$taxonomy}");
}
//Not filtered by site anymore; categories can be used across sites
// (need to confirm this is how we want to handle categories)
public function getCategoryBySlug ($slug = "")
{
$qb = $this->entityManager->createQueryBuilder();
$category = $qb->select('cat')
->from("App\Entity\Category", "cat")
->where("cat.slug = :slug")
->setParameter("slug", $slug)
->setMaxResults(1)->getQuery()->getResult();
if($category) {
$category = $category[0];
return $category;
}
throw new NotFoundHttpException ("Category Not Found: {$slug}");
}
public function getCategoriesBySlug ($slug = "", $taxonomy = "")
{
$qb = $this->entityManager->createQueryBuilder();
$qb->select('cat')
->from("App\Entity\Category", "cat")
->where("cat.slug = :slug");
if($taxonomy) {
$qb->andWhere("cat.taxonomy = :tax")
->setParameter("tax", $taxonomy);
}
$category = $qb
->setParameter("slug", $slug)
->getQuery()->getResult();
if($category) {
return $category;
}
throw new NotFoundHttpException ("Category Not Found: {$slug}");
}
public function getCategoryExistsBySlug ($slug = "")
{
$qb = $this->entityManager->createQueryBuilder();
$category = $qb->select('cat')
->from("App\Entity\Category", "cat")
->where("cat.slug = :slug")
->setParameter("slug", $slug)
->setMaxResults(1)->getQuery()->getResult();
if($category) {
return true;
}
return false;
}
// To filter by site, or not to filter by site?
/*
//Filtered by site
public function getCategoryBySlug ($slug = "")
{
$site_code = $this->getSiteCode();
$qb = $this->entityManager->createQueryBuilder();
$qb->select('cat')
->from("App\Entity\Category", "cat")
->where("cat.slug = :slug");
if($site_code) {
$qb->join("cat.site", "s", "WITH", "s.id = :site")
->setParameter("site", $site_code);
}
$category = $qb
->setParameter("slug", $slug)
->setMaxResults(1)->getQuery()->getResult();
if($category) {
$category = $category[0];
return $category;
}
throw new NotFoundHttpException ("Category Not Found: {$slug}");
}
//Filtered by site
public function getCategoriesBySlug ($slug = "", $taxonomy = "")
{
$site_code = $this->getSiteCode();
$qb = $this->entityManager->createQueryBuilder();
$qb->select('cat')
->from("App\Entity\Category", "cat")
->where("cat.slug = :slug");
if($site_code) {
$qb->join("cat.site", "s", "WITH", "s.id = :site")
->setParameter("site", $site_code);
}
if($taxonomy) {
$qb->andWhere("cat.taxonomy = :tax")
->setParameter("tax", $taxonomy);
}
$category = $qb
->setParameter("slug", $slug)
->getQuery()->getResult();
if($category) {
return $category;
}
throw new NotFoundHttpException ("Category Not Found: {$slug}");
}
*/
//returns the category and all child categories up to 2 levels deep
public function getChildCategoriesBySlugs($slugs = [])
{
$categorySQL = [];
foreach ($slugs as $slug) {
$categorySQL[] = ":slug" . (count($categorySQL) + 1);
}
$query = implode(" ", array (
"SELECT DISTINCT category1",
"FROM",
"App\Entity\Category category1",
"LEFT JOIN",
"category1.prnt category2",
"LEFT JOIN",
"category2.prnt category3",
"WHERE",
"category1.slug IN (",
implode(", ", $categorySQL),
")",
"OR",
"category2.slug IN (",
implode(", ", $categorySQL),
") ",
"OR",
"category3.slug IN (",
implode(", ", $categorySQL),
") ",
));
$query = $this->entityManager
->createQuery($query);
$pos = 1;
foreach ($slugs as $slug) {
$query->setParameter("slug{$pos}", $slug);
$pos++;
}
$results = $query->getResult();
return $results;
}
public function getCategoryById ($id = 0)
{
$category = $this->entityManager
->getRepository(Category::class)
->findOneBy([
"id" => $id
]);
if ($category) {
return $category;
}
throw new NotFoundHttpException ("Category with Id {$id} - Not Found");
}
public function getPaginatedContent(Category $category, int $limit = 10, int $page = 1)
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq("status", "1"))
->orderBy(array("published_at" => Criteria::DESC))
->setFirstResult($limit * ($page - 1))
->setMaxResults($limit)
;
return $category->getContent()->matching($criteria);
}
public function getCategories()
{
$content = [];
$categories = $this->entityManager
->getRepository(Category::class)
->createQueryBuilder('c')
->where('c.status = :status')
->andWhere('c.taxonomy = :taxonomy or c.taxonomy = :taxonomy2')
->setParameter('status', 1)
->setParameter('taxonomy', 'category')
->setParameter('taxonomy2', 'ebook-category')
->getQuery()
->getResult();
$content['content'] = $categories;
return $content;
}
}