src/Repository/ContentRepository.php line 666

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Content;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. /**
  7.  * @method Content|null find($id, $lockMode = null, $lockVersion = null)
  8.  * @method Content|null findOneBy(array $criteria, array $orderBy = null)
  9.  * @method Content[]    findAll()
  10.  * @method Content[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  11.  */
  12. class ContentRepository extends ServiceEntityRepository
  13. {
  14.     public function __construct(ManagerRegistry $registry)
  15.     {
  16.         parent::__construct($registryContent::class);
  17.     }
  18.     
  19.     //TODO: test this and other functions after site changes
  20.     public function slugIsUnique ($slug ""$content ""$site "")
  21.     {
  22.         $count = [];
  23.         if(empty($content)) {
  24.             $count $this->createQueryBuilder("a")
  25.                 ->select("count(a.id)");
  26.             
  27.             if($site) {
  28.                 $count $count->join("a.site""s");
  29.             }
  30.             
  31.             $count $count->where("a.slug = :slug");
  32.             
  33.             if($site) {
  34.                 $count $count->andWhere("s.id IN (:sites)")
  35.                     ->setParameter("sites", [$site]);
  36.             }
  37.             
  38.             $count $count->setParameter("slug"$slug)
  39.                 ->getQuery()
  40.                 ->getResult();
  41.         }
  42.         else {
  43.             $count $this->createQueryBuilder("a")
  44.                 ->select("count(a.id)")
  45.                 ->join("a.site""s")
  46.                 ->where("a.id != :id")
  47.                 ->andWhere("a.slug = :slug")
  48.                 //->andWhere("s.id = :site")
  49.                 ->andWhere("s.id IN (:sites)")
  50.                 
  51.                 ->setParameter("id"$content->getId())
  52.                 ->setParameter("slug"$slug)
  53.                 ->setParameter("sites"$content->getSiteIds())
  54.                 
  55.                 ->getQuery()
  56.                 ->getResult();
  57.         }
  58.         
  59.         if (!empty($count)) {
  60.             return ( ((int) $count[0][1]) < );
  61.         }
  62.         else {
  63.             return true;
  64.         }
  65.         
  66.     }
  67.     
  68.     /*
  69.     * takes a page title or a slug and returns a unique slug
  70.     */
  71.     public function getUniqueSlug ($title ""$content ""$site "")
  72.     {
  73.         $slug trim(trim(strtolower($title)), '-');
  74.         $slug str_replace(' ''-'$slug);
  75.         $slug preg_replace('/-+/''-'$slug);
  76.         $slug preg_replace('/[^a-z0-9-]/'''$slug);
  77.         
  78.         $final_slug $slug;
  79.         $x 2;
  80.         while(!$this->slugIsUnique($final_slug$content$site)) {
  81.             $final_slug $slug '-' $x++;
  82.         }
  83.         return $final_slug;
  84.     }
  85.     
  86.     public function getPostsByCategory (
  87.         $category "",
  88.         $length 10,
  89.         $page 1,
  90.         $sort "",
  91.         $includeChildren true,
  92.         $site ""
  93.     ) {
  94.         static $content_ids;
  95.         if (is_null($content_ids)) {
  96.             $content_ids = array ();
  97.         }
  98.         
  99.         // always include children for now ...
  100.         
  101.         $result $this->createQueryBuilder("a")
  102.             ->join("a.secondary_categories""c");
  103.         if($site) {
  104.             $result $result->join("a.site""s");
  105.         }
  106.         $result $result->leftJoin("c.prnt""d")
  107.             ->leftJoin("d.prnt""e")
  108.             ->leftJoin("e.prnt""f")
  109.             ->where("c.id = :id")
  110.             ->orWhere("d.id = :id")
  111.             ->orWhere("e.id = :id")
  112.             ->orWhere("f.id = :id")            
  113.             
  114.             ->setParameter("id"$category)
  115.             ->distinct()
  116.             
  117.             ->andWhere("a.type = 2")
  118.             ->andWhere("a.status = 1");
  119.         if($site) {
  120.             $result $result->andWhere("s.id = {$site}");
  121.         }
  122.         $result $result->orderBy("a.published_at""DESC")
  123.             ->setFirstResult($length * ($page 1))
  124.             ->setMaxResults($length)
  125.             ->getQuery()
  126.             ->getResult();
  127.         
  128.         for ($i 0$i count($result); $i++) {
  129.             $content_ids[] = $result[$i]->getId();
  130.         }
  131.         
  132.         return $result;
  133.         
  134.     }
  135.     public function recommendedContent (
  136.         $category "",
  137.         $length 3,
  138.         $ignoreID 0,
  139.         $site ""
  140.     ) {
  141.         static $content_ids;
  142.         if (is_null($content_ids)) {
  143.             $content_ids = array ();
  144.         }
  145.         
  146.         // // always include children for now ...
  147.         
  148.         // $result = $this->createQueryBuilder("a")
  149.         //     ->join("a.secondary_categories", "c");
  150.         // if($site) {
  151.         //     $result = $result->join("a.site", "s");
  152.         // }
  153.         // $result = $result->leftJoin("c.prnt", "d")
  154.         //     ->leftJoin("d.prnt", "e")
  155.         //     ->leftJoin("e.prnt", "f")
  156.         //     ->where("c.id = :id")
  157.         //     ->orWhere("d.id = :id")
  158.         //     ->orWhere("e.id = :id")
  159.         //     ->orWhere("f.id = :id")            
  160.             
  161.         //     ->setParameter("id", $category)
  162.         //     ->distinct()
  163.             
  164.         //     ->andWhere("a.type = 2")
  165.         //     ->andWhere("a.status = 1");
  166.         // if($site) {
  167.         //     $result = $result->andWhere("s.id = {$site}");
  168.         // }
  169.         // if ($ignoreID) {
  170.         //     $result = $result->andWhere("a.id != {$ignoreID}");
  171.         // }
  172.         // $result = $result->orderBy("a.published_at", "DESC")
  173.         //     ->setMaxResults($length)
  174.         //     ->getQuery()
  175.         //     ->getResult();
  176.         
  177.         // for ($i = 0; $i < count($result); $i++) {
  178.         //     $content_ids[] = $result[$i]->getId();
  179.         // }
  180.         // $hot = $this->createQueryBuilder("a")->join("a.secondary_categories", "c")->join("a.site", "s")->join('a.content_analytics_daily', 'd');
  181.         // $hot = $hot->where("c.id = :id")->setParameter("id", $category)->distinct();
  182.         // if($site) {
  183.         //     $hot = $hot->andWhere("s.id = {$site}");
  184.         // }
  185.         // if ($ignoreID) {
  186.         //     $hot = $hot->andWhere("c.id != {$ignoreID}")->andWhere("a.id != {$ignoreID}");
  187.         // }
  188.         // $hot = $hot->orderBy('a.total_impressions', 'DESC')->setMaxResults(1)->getQuery()->getResult();
  189.         // $hot[0]->hot = true;
  190.         // $hot = $this->createQueryBuilder("a")->join("a.secondary_categories", "c")->join("a.site", "s")->join('a.content_analytics_daily', 'd');
  191.         // $tenDaysAgo = date_create(date("Y-m-d"))->modify('-10 days')->format('Y-m-d');
  192.         // $hot = $hot->where("d.day > {$tenDaysAgo}");
  193.         // $hot = $hot->orderBy('d.views', 'DESC')->setMaxResults(20)->getQuery()->getResult();
  194.         // $howManyHot = count($hot);
  195.         $query implode(" ", array("SELECT cad.content_id, MAX(cad.impressions), MAX(cad.views)",
  196.             "FROM `content_analytics_daily` cad",
  197.             "LEFT JOIN `content` c ",
  198.             "ON cad.content_id = c.id ",
  199.             "LEFT JOIN `content_category` cc",
  200.             "ON cc.content_id = c.id",
  201.             "WHERE cc.category_id = 19",
  202.             "AND cad.day > '2022-08-07'",
  203.             "GROUP BY cad.content_id",
  204.             "ORDER BY MAX(cad.`views`) DESC;"));
  205.         $stmt $this->entityManager->getConnection()->prepare($query);
  206.         
  207.         $hot '';
  208.         // if ($howManyHot) {
  209.         //     $hotIndex = array_rand($hot, 1);
  210.         //     $hot[$hotIndex]->hot = true;
  211.         //     $hot = $hot[$hotIndex];
  212.         // }
  213.         $result $this->createQueryBuilder("a")->join("a.secondary_categories""c")->join("a.site""s");
  214.         $result $result->where("c.id = :id")->setParameter("id"$category)->distinct();
  215.         if($site) {
  216.             $result $result->andWhere("s.id = {$site}");
  217.         }
  218.         // if ($ignoreID) {
  219.         //     $result = $result->andWhere("c.id != {$ignoreID}")->andWhere("a.id != {$ignoreID}")->andWhere("a.id != {$hot->getId()}")->andWhere("c.id != {$hot->getId()}");
  220.         // }
  221.         
  222.         // if ($howManyHot) {
  223.         //     $result = $result->andWhere("a.id != {$hot->getId()}")->andWhere("c.id != {$hot->getId()}");
  224.         // }
  225.         $result $result->orderBy("rand()");
  226.         // if ($howManyHot) {
  227.         //     $result = $result->setMaxResults($length-1);
  228.         // }else{
  229.             $result $result->setMaxResults($length);
  230.         // }
  231.         $result $result->getQuery()->getResult();
  232.         for ($i 0$i count($result); $i++) {
  233.             $result[$i]->hot false;
  234.         }
  235.         
  236.         // array_unshift($result , $hot);
  237.         shuffle($result);
  238.         return $result;
  239.         
  240.     }
  241.     
  242.     public function getPostsByCategories (
  243.         $categories "",
  244.         $length 10,
  245.         $page 1,
  246.         $sort "",
  247.         $includeChildren true,
  248.         $site ""
  249.     ) {
  250.         static $content_ids;
  251.         if (is_null($content_ids)) {
  252.             $content_ids = array ();
  253.         }
  254.         
  255.         $category_ids = [];
  256.         foreach($categories as $category) {
  257.             $category_ids[] = $category->getId();
  258.         }
  259.         
  260.         // always include children for now ...
  261.         
  262.         $result $this->createQueryBuilder("a")
  263.             ->join("a.secondary_categories""c");
  264.         if($site) {
  265.             $result $result->join("a.site""s");
  266.         }
  267.         $result $result->leftJoin("c.prnt""d")
  268.             ->leftJoin("d.prnt""e")
  269.             ->leftJoin("e.prnt""f")
  270.             ->where("c.id IN (:ids)")
  271.             ->orWhere("d.id IN (:ids)")
  272.             ->orWhere("e.id IN (:ids)")
  273.             ->orWhere("f.id IN (:ids)")
  274.             
  275.             ->setParameter("ids"$category_ids)
  276.             
  277.             ->distinct()
  278.             
  279.             ->andWhere("a.type = 2")
  280.             ->andWhere("a.status = 1");
  281.         if($site) {
  282.             $result $result->andWhere("s.id = {$site}");
  283.         }
  284.         $result $result->orderBy("a.published_at""DESC")
  285.             ->setFirstResult($length * ($page 1))
  286.             ->setMaxResults($length)
  287.             ->getQuery()
  288.             ->getResult();
  289.         
  290.         for ($i 0$i count($result); $i++) {
  291.             $content_ids[] = $result[$i]->getId();
  292.         }
  293.         
  294.         return $result;
  295.         
  296.     }
  297.     
  298.     public function getContentByCategories (
  299.         $type 0,
  300.         $categories "",
  301.         $length 10,
  302.         $page 1,
  303.         $sort "",
  304.         $includeChildren true,
  305.         $site ""
  306.     ) {
  307.         static $content_ids;
  308.         if (is_null($content_ids)) {
  309.             $content_ids = array ();
  310.         }
  311.         
  312.         $category_ids = [];
  313.         foreach($categories as $category) {
  314.             $category_ids[] = $category->getId();
  315.         }
  316.         
  317.         // always include children for now ...
  318.         
  319.         $result $this->createQueryBuilder("a")
  320.             ->join("a.secondary_categories""c");
  321.         if($site) {
  322.             $result $result->join("a.site""s");
  323.         }
  324.         $result $result->leftJoin("c.prnt""d")
  325.             ->leftJoin("d.prnt""e")
  326.             ->leftJoin("e.prnt""f")
  327.             ->where("c.id IN (:ids)")
  328.             ->orWhere("d.id IN (:ids)")
  329.             ->orWhere("e.id IN (:ids)")
  330.             ->orWhere("f.id IN (:ids)")
  331.             
  332.             ->setParameter("ids"$category_ids)
  333.             
  334.             ->distinct()
  335.             
  336.             ->andWhere("a.status = 1");
  337.             
  338.         if($type) {
  339.             $result $result->andWhere("a.type = {$type}");
  340.         }
  341.         if($site) {
  342.             $result $result->andWhere("s.id = {$site}");
  343.         }
  344.         $result $result->orderBy("a.published_at""DESC")
  345.             ->setFirstResult($length * ($page 1))
  346.             ->setMaxResults($length)
  347.             ->getQuery()
  348.             ->getResult();
  349.         
  350.         for ($i 0$i count($result); $i++) {
  351.             $content_ids[] = $result[$i]->getId();
  352.         }
  353.         
  354.         return $result;
  355.         
  356.     }
  357.     
  358.     public function getDirectoriesByCategory (
  359.         $category "",
  360.         $length 10,
  361.         $page 1,
  362.         $sort "",
  363.         $includeChildren true,
  364.         $site ""
  365.     ) {
  366.         static $content_ids;
  367.         if (is_null($content_ids)) {
  368.             $content_ids = array ();
  369.         }
  370.         
  371.         // always include children for now ...
  372.         
  373.         $result $this->createQueryBuilder("a")
  374.             ->join("a.category""c");
  375.         if($site) {
  376.             $result $result->join("a.site""s");
  377.         }
  378.         $result $result->where("c.id = :id")    
  379.             
  380.             ->setParameter("id"$category)
  381.             ->distinct()
  382.             
  383.             ->andWhere("a.type = 4")
  384.             ->andWhere("a.status = 1");
  385.         if($site) {
  386.             $result $result->andWhere("s.id = {$site}");
  387.         }
  388.         $result $result->orderBy("a.published_at""DESC")
  389.             ->setFirstResult($length * ($page 1))
  390.             ->setMaxResults($length)
  391.             ->getQuery()
  392.             ->getResult();
  393.         
  394.         for ($i 0$i count($result); $i++) {
  395.             $content_ids[] = $result[$i]->getId();
  396.         }
  397.         
  398.         return $result;
  399.         
  400.         
  401.     }
  402.     
  403.     public function countPostsByCategory (
  404.         $category "",
  405.         $includeChildren true,
  406.         $site ""
  407.     ) {
  408.         
  409.         $result $this->createQueryBuilder("a")
  410.             ->select("count(a.id)")
  411.             
  412.             ->join("a.secondary_categories""c");
  413.         if($site) {
  414.             $result $result->join("a.site""s");
  415.         }
  416.         $result $result->leftJoin("c.prnt""d")
  417.             ->leftJoin("d.prnt""e")
  418.             ->leftJoin("e.prnt""f")
  419.             
  420.             ->where("c.id = :id")
  421.             ->orWhere("d.id = :id")
  422.             ->orWhere("e.id = :id")
  423.             ->orWhere("f.id = :id")
  424.             
  425.             ->setParameter("id"$category)
  426.             ->distinct()
  427.             
  428.             ->andWhere("a.type = 2")
  429.             ->andWhere("a.status = 1");
  430.         if($site) {
  431.             $result $result->andWhere("s.id = {$site}");
  432.         }
  433.         $result $result->orderBy("a.created_at""DESC")
  434.             ->getQuery()
  435.             ->getResult();
  436.         
  437.         return $result;
  438.             
  439.     }
  440.     
  441.     public function countPostsByCategories (
  442.         $categories "",
  443.         $includeChildren true,
  444.         $site ""
  445.     ) {
  446.         
  447.         foreach($categories as $category) {
  448.             $category_ids[] = $category->getId();
  449.         }
  450.         
  451.         $result $this->createQueryBuilder("a")
  452.             ->select("count(a.id)")
  453.             
  454.             ->join("a.secondary_categories""c");
  455.         if($site) {
  456.             $result $result->join("a.site""s");
  457.         }
  458.         $result $result->leftJoin("c.prnt""d")
  459.             ->leftJoin("d.prnt""e")
  460.             ->leftJoin("e.prnt""f")
  461.             
  462.             ->where("c.id IN (:ids)")
  463.             ->orWhere("d.id IN (:ids)")
  464.             ->orWhere("e.id IN (:ids)")
  465.             ->orWhere("f.id IN (:ids)")
  466.             
  467.             ->setParameter("ids"$category_ids)
  468.             ->distinct()
  469.             
  470.             ->andWhere("a.type = 2")
  471.             ->andWhere("a.status = 1");
  472.         if($site) {
  473.             $result $result->andWhere("s.id = {$site}");
  474.         }
  475.         $result $result->orderBy("a.created_at""DESC")
  476.             ->getQuery()
  477.             ->getResult();
  478.         
  479.         return $result;
  480.             
  481.     }
  482.     
  483.     public function countContentByCategories (
  484.         $type 0,
  485.         $categories "",
  486.         $includeChildren true,
  487.         $site ""
  488.     ) {
  489.         
  490.         foreach($categories as $category) {
  491.             $category_ids[] = $category->getId();
  492.         }
  493.         
  494.         $result $this->createQueryBuilder("a")
  495.             ->select("count(a.id)")
  496.             ->join("a.secondary_categories""c");
  497.         if($site) {
  498.             $result $result->join("a.site""s");
  499.         }
  500.         $result $result->leftJoin("c.prnt""d")
  501.             ->leftJoin("d.prnt""e")
  502.             ->leftJoin("e.prnt""f")
  503.             
  504.             ->where("c.id IN (:ids)")
  505.             ->orWhere("d.id IN (:ids)")
  506.             ->orWhere("e.id IN (:ids)")
  507.             ->orWhere("f.id IN (:ids)")
  508.             
  509.             ->setParameter("ids"$category_ids)
  510.             ->distinct()
  511.             
  512.             ->andWhere("a.status = 1");
  513.         if($type) {
  514.             $result $result->andWhere("a.type = {$type}");
  515.         }
  516.         if($site) {
  517.             $result $result->andWhere("s.id = {$site}");
  518.         }
  519.         $result $result->orderBy("a.created_at""DESC")
  520.             ->getQuery()
  521.             ->getResult();
  522.         
  523.         return $result;
  524.             
  525.     }
  526.     
  527.     public function getGalleries (
  528.         $category "",
  529.         $sort "",
  530.         $site ""
  531.     ) {
  532.         //just sorting by title for now
  533.         
  534.         $result $this->createQueryBuilder("a")
  535.             ->join("a.category""c");
  536.         if($site) {
  537.             $result $result->join("a.site""s");
  538.         }
  539.         $result $result->where("c.slug = :slug")
  540.             
  541.             ->setParameter("slug"$category)
  542.             ->distinct()
  543.             
  544.             ->andWhere("a.type = 5")
  545.             ->andWhere("a.status = 1");
  546.         if($site) {
  547.             $result $result->andWhere("s.id = {$site}");
  548.         }
  549.             //->orderBy("a.created_at", "DESC")
  550.         $result $result->orderBy("a.title""ASC")
  551.             ->getQuery()
  552.             ->getResult();
  553.             
  554.         return $result;
  555.     }
  556.     
  557.     //TODO: filter by site
  558.     public function getEbooks(
  559.         $customer "",
  560.         $site ""
  561.     ){
  562.         $result $this->createQueryBuilder("a")
  563.         ->join("a.customers""c");
  564.         if($site) {
  565.             $result $result->join("a.site""s");
  566.         }
  567.         $result $result->distinct()
  568.         ->Where("a.type = 19")
  569.         ->andWhere("a.status = 1");
  570.         if($customer){
  571.             $result $result->andWhere("c.id = {$customer}");
  572.         }
  573.         if($site) {
  574.             $result $result->andWhere("s.id = {$site}");
  575.         }
  576.         $result $result
  577.         ->getQuery()
  578.         ->getResult();
  579.         return $result;
  580.     }
  581.     public function getEbooksByCategory(
  582.         $category "",
  583.         $site ""
  584.     ){
  585.         $result $this->createQueryBuilder("a")
  586.         ->join("a.secondary_categories""sc");
  587.         if($site) {
  588.             $result $result->join("a.site""s");
  589.         }
  590.         $result $result->where("sc.id = :category")
  591.         ->andWhere("a.status = 1");
  592.         if($site) {
  593.             if($site == Content::SITE_AAR) {
  594.                 $result $result->andWhere("sc.taxonomy = 'aar_ebook-category'");
  595.             }
  596.             else {
  597.                 $result $result->andWhere("sc.taxonomy = 'ebook-category'");
  598.             }
  599.             $result $result->andWhere("s.id = {$site}");
  600.         }
  601.         $result $result->setParameter("category"$category->getId())
  602.         ->getQuery()
  603.         ->getResult();
  604.         return $result;
  605.     }
  606.     
  607.     public function countTopicsPostsByForum (
  608.         $slug ""
  609.     ) {
  610.         $result $this->createQueryBuilder("a")
  611.             ->select("COUNT(DISTINCT b.id), COUNT(c.id)")
  612.             
  613.             ->leftJoin("a.children""b")
  614.             ->leftJoin("b.children""c")
  615.             
  616.             ->where("a.slug = :slug")
  617.             
  618.             ->setParameter("slug"$slug)
  619.             ->andWhere("a.type = 9")
  620.             ->andWhere("a.status = 1")
  621.             ->andWhere("b.type = 10")
  622.             ->andWhere("b.status = 1")
  623.             ->andWhere("c.type = 11 OR c.status IS NULL")
  624.             ->andWhere("c.status = 1 OR c.status IS NULL")
  625.             ->getQuery()
  626.             ->getResult();
  627.         
  628.         return $result;
  629.     }
  630.     
  631.     
  632.     
  633.     public function getContentByUrl (
  634.         $url ""$site 1
  635.     ) {
  636.         $pos strrpos($url'/');
  637.         $slug $pos === false $url substr($url$pos 1);
  638.         
  639.         $results $this->createQueryBuilder("a")
  640.             ->join("a.site""s")
  641.             ->where("a.slug = :slug")
  642.             ->andWhere("s.id = :site")
  643.             ->setParameter("slug"$slug)
  644.             ->setParameter("site"$site)
  645.             ->getQuery()
  646.             ->getResult();
  647.             
  648.         foreach($results as $result) {
  649.             if($url == $result->getURL()) {
  650.                 return $result;
  651.             }
  652.         }
  653.             
  654.         return null;
  655.         
  656.     }
  657.     
  658. }