src/Utils/ContentHelper.php line 137

Open in your IDE?
  1. <?php
  2. namespace App\Utils;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  5. use App\Exception\ResourceNotFoundException;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use App\Entity\Content;
  8. use App\Entity\Category;
  9. use App\Entity\Customer;
  10. use App\Entity\User;
  11. use App\Entity\Site;
  12. use Doctrine\Common\Collections\Criteria;
  13. use \Datetime;
  14. use \DateTimeZone;
  15. use App\Utils\ICS;
  16. use App\Utils\EmailHelper;
  17. use Symfony\Component\Security\Core\Security;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  20. use App\Entity\Traffic;
  21. use App\Entity\TrafficData;
  22. use App\Entity\ContentImpression;
  23. use App\Entity\ContentView;
  24. use App\Entity\AdImpression;
  25. use App\Entity\AdClick;
  26. use App\Entity\ContentLinkClick;
  27. use App\Entity\Product;
  28. use App\Entity\ContentAnalyticsDaily;
  29. class ContentHelper 
  30. {
  31.     const DEFAULT_LIMIT 15;
  32.     const FORUM_LIMIT 15;
  33.     
  34.     protected $entityManager;
  35.     protected $emailHelper;
  36.     protected $security;
  37.     protected $requestStack;
  38.     protected $tokenStorage;
  39.     protected $categoryHelper;
  40.     
  41.     public function __construct (
  42.         EntityManagerInterface $entityManager,
  43.         EmailHelper $emailHelper,
  44.         Security $security,
  45.         RequestStack $requestStack,
  46.         TokenStorageInterface $tokenStorage,
  47.         CategoryHelper $categoryHelper
  48.     ) {
  49.         $this->entityManager $entityManager;
  50.         $this->emailHelper $emailHelper;
  51.         $this->security $security;
  52.         $this->requestStack $requestStack;
  53.         $this->tokenStorage $tokenStorage;
  54.         $this->categoryHelper $categoryHelper;
  55.     }
  56.     
  57.     public function getSiteCode() {
  58.         $site_code null;
  59.         $request $this->requestStack->getCurrentRequest();
  60.         if($request) {
  61.             $session $request->getSession();
  62.             if($session) {
  63.                 $site_code $session->get("site");
  64.             }
  65.         }
  66.         return $site_code;
  67.     }
  68.     
  69.     public function slugIsUnique ($slug ""$content ""$site "")
  70.     {
  71.         return $this->entityManager
  72.             ->getRepository(Content::class)
  73.             ->slugIsUnique($slug$content$site);
  74.     }
  75.     
  76.     /*
  77.     * takes a page title or a slug and returns a unique slug
  78.     */
  79.     public function getUniqueSlug ($title ""$content ""$site "")
  80.     {
  81.         return $this->entityManager
  82.             ->getRepository(Content::class)
  83.             ->getUniqueSlug($title$content$site);
  84.     }
  85.     
  86.     /*public function getStaticPageBySlug ($slug = "")
  87.     {
  88.         try{
  89.             return $this->getContentBySlug(Content::STATIC_PAGE, $slug);
  90.         } catch(\Exception $e) {
  91.             return null;
  92.         }
  93.     }*/
  94.     public function getStaticPageBySlug ($slug "") {
  95.         $type 0;
  96.         $content="";
  97.         $site_code $this->getSiteCode();
  98.         
  99.         $qb $this->entityManager->createQueryBuilder();
  100.             
  101.         $qb->select('c')
  102.             ->from("App\Entity\Content""c")
  103.             ->where("c.slug = :slug")
  104.             ->andWhere("c.type = :type")
  105.             ->andWhere("c.status = 1"); //allow 'customer in review' to be linkable
  106.         if($site_code) {
  107.             $qb->join("c.site""s""WITH""s.id = :site")
  108.                 ->setParameter("site"$site_code);
  109.         }
  110.         $content $qb
  111.         ->setParameter("slug"$slug)
  112.         ->setParameter("type"$type)
  113.         ->setMaxResults(1)->getQuery()->getResult();
  114.         
  115.         if($content) {
  116.             $content $content[0];
  117.             return $content;
  118.         }
  119.         
  120.         return null;
  121.     }
  122.     public function getPageBySlug ($slug "")
  123.     {
  124.         //Adding this try catch temporarily to deal with social posts using the old format for posts (/{slug} instead of /post/{slug})
  125.         try{
  126.             return $this->getContentBySlug(Content::PAGE$slug);
  127.         } catch(\Exception $e) {
  128.             
  129.         }
  130.         return $this->getContentBySlug(Content::POST$slug);
  131.     }
  132.     
  133.     public function getPostBySlug ($slug "")
  134.     {
  135.         return $this->getContentBySlug(Content::POST$slug);
  136.     }
  137.     
  138.     public function getClassifiedBySlug ($slug "")
  139.     {
  140.         return $this->getContentBySlug(Content::CLASSIFIED$slug);
  141.     }
  142.     
  143.     public function getDirectoryBySlug ($slug "")
  144.     {
  145.         return $this->getContentBySlug(Content::DIRECTORY$slug);
  146.     }
  147.     
  148.     public function getEventBySlug ($slug "")
  149.     {
  150.         //Exception for webinars
  151.         try{
  152.             return $this->getContentBySlug(Content::EVENT$slug);
  153.         } catch(\Exception $e) {
  154.             
  155.         }
  156.         return $this->getContentBySlug(Content::WEBINAR$slug);
  157.         
  158.         //return $this->getContentBySlug(Content::EVENT, $slug);
  159.     }
  160.     
  161.     public function getForumBySlug ($slug "")
  162.     {
  163.         return $this->getContentBySlug(Content::FORUM$slug);
  164.     }
  165.     
  166.     public function getTopicBySlug ($slug "")
  167.     {
  168.         return $this->getContentBySlug(Content::FORUM_TOPIC$slug);
  169.     }
  170.     
  171.     public function getLandingPageBySlug ($slug "") {
  172.         return $this->getContentBySlug(Content::LANDING_PAGE$slug);
  173.     }
  174.     
  175.     public function getThankYouPageBySlug ($slug "") {
  176.         return $this->getContentBySlug(Content::THANK_YOU_LANDING_PAGE$slug);
  177.     }
  178.     
  179.     public function getContentBySlug ($type ""$slug "") {
  180.         $content="";
  181.         $user $this->security->getUser();
  182.         $site_code $this->getSiteCode();
  183.         
  184.         if($user) {
  185.             //if the user is an admin, return the content regardless of its status
  186.             if($user->isAdmin()) {
  187.                 $qb $this->entityManager->createQueryBuilder();
  188.                 
  189.                 $qb->select('c')
  190.                     ->from("App\Entity\Content""c")
  191.                     ->where("c.slug = :slug")
  192.                     ->andWhere("c.type = :type");
  193.                 if($site_code && (!in_array($type, [Content::DIRECTORYContent::CLASSIFIED]))) { //todo: remove these exceptions when purchasing on MCS is finished.
  194.                     $qb->join("c.site""s""WITH""s.id = :site")
  195.                         ->setParameter("site"$site_code);
  196.                 }
  197.                 $content $qb
  198.                 ->setParameter("slug"$slug)
  199.                 ->setParameter("type"$type)
  200.                 ->setMaxResults(1)->getQuery()->getResult();
  201.                 
  202.                 if($content) {
  203.                     $content $content[0];
  204.                 }
  205.             }
  206.         }
  207.         if(!$content) {
  208.             
  209.             $qb $this->entityManager->createQueryBuilder();
  210.             
  211.             $qb->select('c')
  212.                 ->from("App\Entity\Content""c")
  213.                 ->where("c.slug = :slug")
  214.                 ->andWhere("c.type = :type")
  215.                 ->andWhere("c.status = 1 OR c.status = -5"); //allow 'customer in review' to be linkable
  216.             if($site_code && $type != Content::DIRECTORY) { //todo: update directory content w/ site_code
  217.                 $qb->join("c.site""s""WITH""s.id = :site")
  218.                     ->setParameter("site"$site_code);
  219.             }
  220.             $content $qb
  221.             ->setParameter("slug"$slug)
  222.             ->setParameter("type"$type)
  223.             ->setMaxResults(1)->getQuery()->getResult();
  224.             
  225.             if($content) {
  226.                 $content $content[0];
  227.             }
  228.             
  229.         }
  230.         if ($content) {
  231.             return $content;
  232.         }
  233.         
  234.         throw new NotFoundHttpException("Content Not Found: {$slug}");
  235.         
  236.         
  237.         /*
  238.         //Old way of checking if page is members only. Now, returning the content and handling in the controller.
  239.         $content="";
  240.         $user = $this->security->getUser();
  241.         if($user) {
  242.             //if the user is an admin, return the content regardless of its status
  243.             if($user->isAdmin()) {
  244.                 $content = $this->entityManager
  245.                     ->getRepository(Content::class)
  246.                     ->findOneBy([
  247.                         "slug" => $slug,
  248.                         "type" => $type,
  249.                     ]);
  250.             }
  251.             //if the user is a member, return active content
  252.             elseif($user->isMember()) {
  253.                 $content = $this->entityManager
  254.                 ->getRepository(Content::class)
  255.                 ->findOneBy([
  256.                     "slug" => $slug,
  257.                     "type" => $type,
  258.                     "status" => 1,
  259.                 ]);
  260.             }
  261.         }
  262.         //otherwise, only return active, non-members only content
  263.         if(!$content) {
  264.             $content = $this->entityManager
  265.                 ->getRepository(Content::class)
  266.                 ->findOneBy([
  267.                     "slug" => $slug,
  268.                     "type" => $type,
  269.                     "status" => 1,
  270.                     "members_only" => 0,
  271.                 ]);
  272.         }
  273.         if ($content) {
  274.             return $content;
  275.         }
  276.         else {
  277.             $m_content = $this->entityManager
  278.                 ->getRepository(Content::class)
  279.                 ->findOneBy([
  280.                     "slug" => $slug,
  281.                     "type" => $type,
  282.                     "status" => 1,
  283.                 ]);
  284.                 
  285.                 if($m_content) {
  286.                     //return the "members only" page
  287.                 }
  288.         }
  289.         throw new ResourceNotFoundException ("Content Not Found: {$slug}");
  290.         */
  291.     }
  292.     
  293.     public function getContentBySlugRaw ($slug "") {
  294.         $site_code $this->getSiteCode();
  295.         $qb $this->entityManager->createQueryBuilder();
  296.         
  297.         $qb->select('c')
  298.             ->from("App\Entity\Content""c")
  299.             ->where("c.slug = :slug");
  300.         if($site_code) {
  301.             $qb->join("c.site""s""WITH""s.id = :site")
  302.                 ->setParameter("site"$site_code);
  303.         }
  304.         $content $qb
  305.             ->setParameter("slug"$slug)
  306.             ->setMaxResults(1)->getQuery()->getResult();
  307.         
  308.         if($content) {
  309.             $content $content[0];
  310.             return $content;
  311.         }
  312.         
  313.         throw new ResourceNotFoundException ("Content with Slug {$slug} - Not Found");
  314.     }
  315.     // not site specific (TODO: confirm unique when setting slugs)
  316.     public function getDirectoryBySlugRaw($slug "") {
  317.         $qb $this->entityManager->createQueryBuilder();
  318.         
  319.         $content $qb->select('c')
  320.             ->from("App\Entity\Content""c")
  321.             ->where("c.slug = :slug")
  322.             ->andWhere("c.type = :type")
  323.             ->setParameter("slug"$slug)
  324.             ->setParameter("type"Content::DIRECTORY)
  325.             ->setMaxResults(1)->getQuery()->getResult();
  326.         
  327.         if($content) {
  328.             $content $content[0];
  329.             return $content;
  330.         }
  331.         
  332.         throw new ResourceNotFoundException ("Content with Slug {$slug} - Not Found");
  333.     }
  334.     
  335.     public function getContentByType ($type "") {
  336.         $site_code $this->getSiteCode();
  337.         $qb $this->entityManager->createQueryBuilder();
  338.         
  339.         $qb->select('c')
  340.             ->from("App\Entity\Content""c")
  341.             ->where("c.type = :type")
  342.             ->andWhere("c.status = 1 ");
  343.         if($site_code) {
  344.             $qb->join("c.site""s""WITH""s.id = :site")
  345.                 ->setParameter("site"$site_code);
  346.         }
  347.         $content $qb
  348.             ->setParameter("type"$type)
  349.             ->getQuery()->getResult();
  350.         
  351.         if($content) {
  352.             return $content;
  353.         }
  354.         return [];
  355.     }
  356.     
  357.     public function getContentByOldId ($old_id "") {
  358.         $site_code $this->getSiteCode();
  359.         $qb $this->entityManager->createQueryBuilder();
  360.         
  361.         $qb->select('c')
  362.             ->from("App\Entity\Content""c")
  363.             ->where("c.old_id = :old_id");
  364.         if($site_code) {
  365.             $qb->join("c.site""s""WITH""s.id = :site")
  366.                 ->setParameter("site"$site_code);
  367.         }
  368.         $content $qb
  369.             ->setParameter("old_id"$old_id)
  370.             ->setMaxResults(1)->getQuery()->getResult();
  371.         
  372.         if($content) {
  373.             $content $content[0];
  374.             return $content;
  375.         }
  376.         
  377.         throw new ResourceNotFoundException ("Content Not Found - Old ID: {{$old_id}");
  378.     }
  379.     
  380.     public function updateExpired () {
  381.         
  382.         //TODO: Update for AAR (emails etc. if necessary)
  383.         
  384.         $contents $this->entityManager
  385.             ->createQuery(implode(" ", array (
  386.                 "SELECT content",
  387.                 "FROM",
  388.                     "App\Entity\Content content",
  389.                 "WHERE",
  390.                     "content.expires_at < :now",
  391.                     "AND content.type != 7",
  392.             )))
  393.             ->setParameter("now", new \DateTime())
  394.         ->getResult();
  395.         
  396.         $results $this->entityManager
  397.             ->createQuery(implode(" ", array (
  398.                 "UPDATE",
  399.                     "App\Entity\Content content",
  400.                 "SET",
  401.                     "content.status = -2,",
  402.                     "content.expires_at = null",
  403.                 "WHERE",
  404.                     "content.expires_at < :now",
  405.                     "AND content.type != 7",
  406.             )))
  407.             ->setParameter("now", new \DateTime())
  408.         ->getResult();
  409.         
  410.         
  411.         if($contents) {
  412.             foreach($contents as $content) {
  413.                 $type $content->getType();
  414.                 //only send the email for expired classified / directory listings
  415.                 if($type == Content::CLASSIFIED || $type == Content::DIRECTORY) {
  416.                     $id $content->getId();
  417.                     $title $content->getTitle();
  418.                     $type_text $content->getTypeText();
  419.                     $published $content->getPublishedAt()->format("Y-m-d H:i:s");
  420.                     $expired = new \DateTime();
  421.                     $expired $expired->format("Y-m-d H:i:s");
  422.                     $user $content->getAuthor();
  423.                     if($user) { //should change to send to admins even if no author is set
  424.                         $author_name $user->getDisplayname();
  425.                         
  426.                         $this->emailHelper->sendEmail(
  427.                             [$user->getEmail()],
  428.                             "RoofersCoffeeShop.com: {$type_text} \"{$title}\" Has Expired",
  429.                             [
  430.                                 ["p" => "Dear {$author_name},"],
  431.                                 ["p" => "Your {$type_text} \"{$title}\" on <a href='https://rooferscoffeeshop.com/'>RoofersCoffeeShop.com</a> has Expired."],
  432.                                 ["p" => "To make changes, cancel or renew your {$type_text} please log in and find Your Account in the drop down menu."],
  433.                                 ["p" => "Please contact us at <a href='mailto:info@rooferscoffeshop.com'>info@rooferscoffeshop.com</a> or call us a (877) 419-0477 with any questions"],
  434.                             ],
  435.                             [],
  436.                             "",
  437.                             "",
  438.                             "",
  439.                             [],
  440.                             Content::SITE_RCS
  441.                         );
  442.                     }
  443.                 }
  444.             }
  445.         }
  446.         
  447.         
  448.         // Do the same for ads
  449.         $ads $this->entityManager
  450.             ->createQuery(implode(" ", array (
  451.                 "SELECT ad",
  452.                 "FROM",
  453.                     "App\Entity\MediaGroupItem ad",
  454.                 "JOIN",
  455.                     "ad.mediaGroup mgroup",
  456.                 "WHERE",
  457.                     "ad.expires_at < :now",
  458.                     "AND mgroup.type = 2",
  459.             )))
  460.             ->setParameter("now", new \DateTime())
  461.         ->getResult();
  462.         
  463.         $results $this->entityManager
  464.             ->createQuery(implode(" ", array (
  465.                 "UPDATE",
  466.                     "App\Entity\MediaGroupItem ad",
  467.                 "SET",
  468.                     "ad.status = -2,",
  469.                     "ad.expires_at = null",
  470.                 "WHERE",
  471.                     "ad.expires_at < :now",
  472.             )))
  473.             ->setParameter("now", new \DateTime())
  474.         ->getResult();
  475.         
  476.         if($ads) {
  477.             foreach($ads as $ad) {
  478.                 $id $ad->getId();
  479.                 $title $ad->getTitle();
  480.                 $published $ad->getPublishedAt()->format("Y-m-d H:i:s");
  481.                 $expired = new \DateTime();
  482.                 $expired $expired->format("Y-m-d H:i:s");
  483.                 $this->emailHelper->sendEmail(
  484.                     ["rcs@rooferscoffeeshop.com"],
  485.                     "Ad Expired",
  486.                     [
  487.                         ["p" => "The following ad has expired:"],
  488.                         ["table" => [
  489.                             "Id" => $id,
  490.                             "Title" => $title,
  491.                             "Published Date" => $published,
  492.                             "Expired Date" => $expired,
  493.                         ]],
  494.                     ],
  495.                     [],
  496.                     "",
  497.                     "",
  498.                     "",
  499.                     [],
  500.                     Content::SITE_RCS
  501.                 );
  502.             }
  503.         }
  504.         
  505.     }
  506.     
  507.     //Not site specific
  508.     public function updatePublished ()
  509.     {
  510.         //Select all going live per site
  511.         //For RCS:
  512.         //Get current content on home page - latest Up to the Minute, RCS Original, RCS influencer (no duplicates)
  513.         //After the update query, get the same
  514.         //Considered new on home page if just showing up on the home page now but not previously (AND just published? to prevent issues with expiring old ones etc.)
  515.         //If new - Grab the customers associated with the contact and send email to each contact from directory
  516.         //MCS: MCS Influencers, From the Newsroom
  517.         //AAR: Roofing Blog (Add Roofing Blog to top of "Featured")
  518.         //Todo: hook up with home page so that it doesn't need changed in both places
  519.         //Excluded in dupes
  520.         //{{ show_module({'cache_time': '180', 'type': 'content', 'ctype': '2', 'limit': '2', 'dupes': 'yes', 'exclude_in_dupes': 'yes' })}}
  521.         
  522.         $content $this->entityManager
  523.         ->createQuery(implode(" ", array (
  524.             "SELECT content",
  525.             "FROM",
  526.                 "App\Entity\Content content",
  527.             "WHERE",
  528.                 "content.status = 2",
  529.                 "AND content.published_at < :now",
  530.         )))
  531.         ->setParameter("now", new \DateTime())
  532.         ->getResult();
  533.         $this->entityManager
  534.         ->createQuery(implode(" ", array (
  535.             "UPDATE",
  536.                 "App\Entity\Content content",
  537.             "SET",
  538.                 "content.status = 1",
  539.             "WHERE",
  540.                 "content.status = 2",
  541.                 "AND content.published_at < :now",
  542.         )))
  543.         ->setParameter("now", new \DateTime())
  544.         ->getResult();
  545.         try {
  546.             //-----------------RCS Start-----------------
  547.             //Careful with dupes from these
  548.             //{{ show_module({'type': 'content', 'ctype': '3', 'limit': '3', 'dupes': 'yes'})}}
  549.             //{{ show_module({'type': 'content', 'only_announcements': 'yes', 'limit': '1', 'dupes': 'no' }) }}
  550.             $announcements $this->getContent(
  551.                 "*"//type
  552.                 "*"//category
  553.                 ""//order
  554.                 ""//dir
  555.                 0//page
  556.                 1//limit
  557.                 false//allowDupes
  558.                 "*"//taxonomy
  559.                 false//user_secondary_categories
  560.                 false//members_only,
  561.                 false//$as_gallery,
  562.                 false//$only_featured,
  563.                 ""//customer_id
  564.                 ""//customer_level_or_higher,
  565.                 true//only_announcements,
  566.                 false//excludeInDupes,
  567.                 Content::SITE_RCS //site_override
  568.             );
  569.             //Up to the Minute!
  570.             //{{ show_module({'type': 'content', 'ctype': '2', 'category': 'newsroom', 'limit': '1', 'dupes': 'no'})}}
  571.             $up_to_the_minute $this->getContent(
  572.                 Content::POST//type
  573.                 "newsroom"//category
  574.                 ""//order
  575.                 ""//dir
  576.                 0//page
  577.                 1//limit
  578.                 false//allowDupes
  579.                 "*"//taxonomy
  580.                 false//user_secondary_categories
  581.                 false//members_only,
  582.                 false//$as_gallery,
  583.                 false//$only_featured,
  584.                 ""//customer_id
  585.                 ""//customer_level_or_higher,
  586.                 false//only_announcements,
  587.                 false//excludeInDupes,
  588.                 Content::SITE_RCS //site_override
  589.             );
  590.             //RCS Original
  591.             //{{ show_module({'type': 'content', 'ctype': '2', 'category': 'rcs-blog', 'limit': '1', 'dupes': 'no'})}}
  592.             $rcs_original $this->getContent(
  593.                 Content::POST//type
  594.                 "rcs-blog"//category
  595.                 ""//order
  596.                 ""//dir
  597.                 0//page
  598.                 1//limit
  599.                 false//allowDupes
  600.                 "*"//taxonomy
  601.                 false//user_secondary_categories
  602.                 false//members_only,
  603.                 false//$as_gallery,
  604.                 false//$only_featured,
  605.                 ""//customer_id
  606.                 ""//customer_level_or_higher,
  607.                 false//only_announcements,
  608.                 false//excludeInDupes,
  609.                 Content::SITE_RCS //site_override
  610.             );
  611.             //RCS Influencers
  612.             //{{ show_module({'type': 'content', 'ctype': '2', 'category': 'rcs-influencers', 'limit': '1','dupes': 'no'})}}
  613.             $rcs_influencers $this->getContent(
  614.                 Content::POST//type
  615.                 "rcs-influencers"//category
  616.                 ""//order
  617.                 ""//dir
  618.                 0//page
  619.                 1//limit
  620.                 false//allowDupes
  621.                 "*"//taxonomy
  622.                 false//user_secondary_categories
  623.                 false//members_only,
  624.                 false//$as_gallery,
  625.                 false//$only_featured,
  626.                 ""//customer_id
  627.                 ""//customer_level_or_higher,
  628.                 false//only_announcements,
  629.                 false//excludeInDupes,
  630.                 Content::SITE_RCS//site_override
  631.                 true //clear dupes after (To make sure the next site doesn't limit posts based on dupes of previous site)
  632.             );
  633.             $rcs_home_page_contents array_merge($up_to_the_minute["content"], $rcs_original["content"], $rcs_influencers["content"]);
  634.             //[content type="18" category="partner-podcast" col_image="12" col_text="12" limit="1" class="featured-single" show_full="no" show_dates="no" show_hub_categories="yes"]
  635.             //Latest RCS podcast:
  636.             $rcs_partner_podcasts $this->getContent(
  637.                 Content::PODCAST//type
  638.                 "partner-podcast"//category
  639.                 ""//order
  640.                 ""//dir
  641.                 0//page
  642.                 1//limit
  643.                 false//allowDupes
  644.                 "*"//taxonomy
  645.                 true//user_secondary_categories
  646.                 false//members_only,
  647.                 false//$as_gallery,
  648.                 false//$only_featured,
  649.                 ""//customer_id
  650.                 ""//customer_level_or_higher,
  651.                 false//only_announcements,
  652.                 false//excludeInDupes,
  653.                 Content::SITE_RCS//site_override
  654.                 true //clear dupes after (To make sure the next site doesn't limit posts based on dupes of previous site)
  655.             );
  656.             $rcs_partner_podcasts array_merge($rcs_partner_podcasts["content"]);
  657.             /*
  658.             $content = $this->getContent(
  659.                 Content::POST, //type
  660.                 "*", //category
  661.                 "", //order
  662.                 "", //dir
  663.                 0, //page
  664.             // * 3, //limit
  665.                 false, //allowDupes
  666.                 "*", //taxonomy
  667.                 false, //user_secondary_categories
  668.                 false, //members_only,
  669.                 false, //$as_gallery,
  670.                 false, //$only_featured,
  671.                 "", //customer_id
  672.                 "", //customer_level_or_higher,
  673.                 false, //only_announcements,
  674.                 false, //excludeInDupes,
  675.                 Content::SITE_RCS //site_override
  676.             );
  677.             */
  678.             ////-----------------RCS End-----------------
  679.             ////-----------------MCS Start-----------------
  680.             // show_module({'type': 'content', 'ctype': '2', 'category': 'mcs-influencers',  'intro_length': '24',  'intro-length': '5',  'limit': '1', 'class': 'home-articles-big white', 'dupes': 'no'})
  681.             $mcs_influencers $this->getContent(
  682.                 Content::POST//type
  683.                 "mcs-influencers"//category
  684.                 ""//order
  685.                 ""//dir
  686.                 0//page
  687.                 1//limit
  688.                 false//allowDupes
  689.                 "*"//taxonomy
  690.                 false//user_secondary_categories
  691.                 false//members_only,
  692.                 false//$as_gallery,
  693.                 false//$only_featured,
  694.                 ""//customer_id
  695.                 ""//customer_level_or_higher,
  696.                 false//only_announcements,
  697.                 false//excludeInDupes,
  698.                 Content::SITE_MCS//site_override
  699.                 false //clear dupes after (To make sure the next site doesn't limit posts based on dupes of previous site)
  700.             );
  701.             // show_module({'type': 'content', 'ctype': '2', 'intro_length': '24', 'limit': '1', 'class': 'mcs-home-module', 'title': 'FROM THE NEWSROOM', 'col_image': 12, 'col_text': 12, 'show_dates': 'no', 'visit_text': 'More From The Newsroom',  'visit_link': '/news-room', 'show_readmores': 'no', 'dupes': 'no'})}}
  702.             $mcs_newsroom $this->getContent(
  703.                 Content::POST//type
  704.                 "*"//category
  705.                 ""//order
  706.                 ""//dir
  707.                 0//page
  708.                 1//limit
  709.                 false//allowDupes
  710.                 "*"//taxonomy
  711.                 false//user_secondary_categories
  712.                 false//members_only,
  713.                 false//$as_gallery,
  714.                 false//$only_featured,
  715.                 ""//customer_id
  716.                 ""//customer_level_or_higher,
  717.                 false//only_announcements,
  718.                 false//excludeInDupes,
  719.                 Content::SITE_MCS//site_override
  720.                 true //clear dupes after (To make sure the next site doesn't limit posts based on dupes of previous site)
  721.             );
  722.             $mcs_home_page_contents array_merge($mcs_influencers["content"], $mcs_newsroom["content"]);
  723.             ////-----------------MCS End-----------------
  724.             ////-----------------CCS Start-----------------
  725.             // show_module({'type': 'content', 'ctype': '2', 'category': 'ccs-influencers',  'intro_length': '24',  'intro-length': '5',  'limit': '1', 'class': 'home-articles-big white', 'dupes': 'no'})
  726.             $ccs_influencers $this->getContent(
  727.                 Content::POST//type
  728.                 "ccs-influencers"//category
  729.                 ""//order
  730.                 ""//dir
  731.                 0//page
  732.                 1//limit
  733.                 false//allowDupes
  734.                 "*"//taxonomy
  735.                 false//user_secondary_categories
  736.                 false//members_only,
  737.                 false//$as_gallery,
  738.                 false//$only_featured,
  739.                 ""//customer_id
  740.                 ""//customer_level_or_higher,
  741.                 false//only_announcements,
  742.                 false//excludeInDupes,
  743.                 Content::SITE_CCS//site_override
  744.                 false //clear dupes after (To make sure the next site doesn't limit posts based on dupes of previous site)
  745.             );
  746.             // show_module({'type': 'content', 'ctype': '2', 'intro_length': '24', 'limit': '1', 'class': 'ccs-home-module', 'title': 'FROM THE NEWSROOM', 'col_image': 12, 'col_text': 12, 'show_dates': 'no', 'visit_text': 'More From The Newsroom',  'visit_link': '/news-room', 'show_readmores': 'no', 'dupes': 'no'})}}
  747.             $ccs_newsroom $this->getContent(
  748.                 Content::POST//type
  749.                 "*"//category
  750.                 ""//order
  751.                 ""//dir
  752.                 0//page
  753.                 1//limit
  754.                 false//allowDupes
  755.                 "*"//taxonomy
  756.                 false//user_secondary_categories
  757.                 false//members_only,
  758.                 false//$as_gallery,
  759.                 false//$only_featured,
  760.                 ""//customer_id
  761.                 ""//customer_level_or_higher,
  762.                 false//only_announcements,
  763.                 false//excludeInDupes,
  764.                 Content::SITE_CCS//site_override
  765.                 true //clear dupes after (To make sure the next site doesn't limit posts based on dupes of previous site)
  766.             );
  767.             $ccs_home_page_contents array_merge($ccs_influencers["content"], $ccs_newsroom["content"]);
  768.             ////-----------------CCS End-----------------
  769.             ////-----------------AAR Start-----------------
  770.             //show_module({'type': 'content', 'ctype': '2', 'show_intros': 'no', 'readmore_text': '', 'limit': '1', 'class': 'home-articles-big', 'dupes': 'no'})
  771.             $aar_roofing_blog $this->getContent(
  772.                 Content::POST//type
  773.                 "*"//category
  774.                 ""//order
  775.                 ""//dir
  776.                 0//page
  777.                 1//limit
  778.                 false//allowDupes
  779.                 "*"//taxonomy
  780.                 false//user_secondary_categories
  781.                 false//members_only,
  782.                 false//$as_gallery,
  783.                 false//$only_featured,
  784.                 ""//customer_id
  785.                 ""//customer_level_or_higher,
  786.                 false//only_announcements,
  787.                 false//excludeInDupes,
  788.                 Content::SITE_AAR//site_override
  789.                 true //clear dupes after (To make sure the next site doesn't limit posts based on dupes of previous site)
  790.             );
  791.             $aar_home_page_contents array_merge($aar_roofing_blog["content"]);
  792.             ////-----------------AAR End-----------------
  793.             
  794.             $cids = [];
  795.             $cObjs = [];
  796.             $cidsString "";
  797.             foreach($content as $c) {
  798.                 $cidsString .= $c->getId() . " ";
  799.                 $cids[] = $c->getId();
  800.                 $cObjs[$c->getId()] = $c;
  801.             }
  802.             //TODO: consolidate this so there aren't three copies
  803.             //RCS
  804.             $rcsHcObjs = [];
  805.             $rcsHcidsString "";
  806.             foreach($rcs_home_page_contents as $hc) {
  807.                 $rcsHcidsString .= $hc->getId() . " ";
  808.                 if(in_array($hc->getId(), $cids)) {
  809.                     $rcsHcObjs[] = $hc;
  810.                 }
  811.             }
  812.             $rcsHcObjsStr "";
  813.             foreach($rcsHcObjs as $home_content) {
  814.                 $rcsHcObjsStr .= $home_content->getId() . " ";
  815.                 foreach($home_content->getCustomers() as $hc_cust) {
  816.                     if($hc_cust->getDirectoryContent()) {
  817.                         $recipients = [];
  818.                         $valid_recipients false;
  819.                         foreach(explode(','$hc_cust->getDirectoryContent()->getDirectoryEmailRecipients()) as $recipient) {
  820.                             if ($recipient && filter_var(trim($recipient), FILTER_VALIDATE_EMAIL)) {
  821.                                 $recipients[] = trim($recipient);
  822.                                 $valid_recipients true;
  823.                             }
  824.                         }
  825.                         $recipients_text "none";
  826.                         if($valid_recipients) {
  827.                             $recipients_text implode(", "$recipients);
  828.                         }
  829.                         $account_managers = [];
  830.                         $valid_account_managers false;
  831.                         foreach(explode(','$hc_cust->getDirectoryContent()->getDirectoryAccountManagers()) as $account_manager) {
  832.                             if ($account_manager && filter_var(trim($account_manager), FILTER_VALIDATE_EMAIL)) {
  833.                                 $account_managers[] = trim($account_manager);
  834.                                 $valid_account_managers true;
  835.                             }
  836.                         }
  837.                         $ac_text "none";
  838.                         if($valid_account_managers) {
  839.                             $ac_text implode(", "$account_managers);
  840.                         }
  841.                         if(!$valid_recipients) {
  842.                             continue;
  843.                         }
  844.                         //send email
  845.                         $this->emailHelper->sendEmail(
  846.                             $recipients,
  847.                             //["egreco@intradatech.com", "apost@intradatech.com"],
  848.                             "Your article is now live on the RoofersCoffeeShop home page!",
  849.                             [
  850.                                 ["p" => "Hello " $hc_cust->getTitle() . "!" ],
  851.                                 ["p" => "Your article is now live on RCS!"],
  852.                                 
  853.                                 ["p" => "
  854.                                     <div>
  855.                                         <a href='".$home_content->getFullURL(Content::SITE_RCS)."'>
  856.                                             <img  width=300 style='border:0;' src='https://www.rooferscoffeeshop.com/".$home_content->getFeaturedImageURL()."'>
  857.                                         </a>
  858.                                     </div>
  859.                                     <div>
  860.                                         <div>
  861.                                             <a href='".$home_content->getFullURL(Content::SITE_RCS)."' style='color:#043056;font-weight: 700; font-size:16px;text-decoration:none;'>
  862.                                             ".$home_content->getTitle()."
  863.                                             </a>
  864.                                         </div>
  865.                                         <i style='color:#26679e;'>".str_replace(["AM""PM"], ["a.m.""p.m."], date("F j, Y \\a\\t g:i A"strtotime($home_content->getPublishedAt()->format("Y-m-d H:i:s"))))."</i>
  866.                                         <div class='my-3'>
  867.                                             <span class='sideintrocontent'>".$home_content->getIntroText()."</span>
  868.                                             <a href='".$home_content->getFullURL(Content::SITE_RCS)."' style='color:#26679e;'>
  869.                                                 Read More
  870.                                             </a>
  871.                                         </div>
  872.                                     </div>
  873.                                 "],
  874.                                 ["p" => "Please share!"],
  875.                             ],
  876.                             [], //attachments
  877.                             $account_managers//cc
  878.                             ""//bcc
  879.                             ""//logo
  880.                             [], //media_attachments
  881.                             Content::SITE_RCS
  882.                         );
  883.                         
  884.                     }
  885.                 }
  886.             }
  887.             //RCS podcasts
  888.             $rcsPpObjs = [];
  889.             $rcsPpidsString "";
  890.             foreach($rcs_partner_podcasts as $pp) {
  891.                 $rcsPpidsString .= $pp->getId() . " ";
  892.                 if(in_array($pp->getId(), $cids)) {
  893.                     $rcsPpObjs[] = $pp;
  894.                 }
  895.             }
  896.             $rcsPpObjsStr "";
  897.             foreach($rcsPpObjs as $home_content) {
  898.                 $rcsPpObjsStr .= $home_content->getId() . " ";
  899.                 foreach($home_content->getCustomers() as $hc_cust) {
  900.                     if($hc_cust->getDirectoryContent()) {
  901.                         
  902.                         $recipients = [];
  903.                         $valid_recipients false;
  904.                         foreach(explode(','$hc_cust->getDirectoryContent()->getDirectoryEmailRecipients()) as $recipient) {
  905.                             if ($recipient && filter_var(trim($recipient), FILTER_VALIDATE_EMAIL)) {
  906.                                 $recipients[] = trim($recipient);
  907.                                 $valid_recipients true;
  908.                             }
  909.                         }
  910.                         $recipients_text "none";
  911.                         if($valid_recipients) {
  912.                             $recipients_text implode(", "$recipients);
  913.                         }
  914.                         $account_managers = [];
  915.                         $valid_account_managers false;
  916.                         foreach(explode(','$hc_cust->getDirectoryContent()->getDirectoryAccountManagers()) as $account_manager) {
  917.                             if ($account_manager && filter_var(trim($account_manager), FILTER_VALIDATE_EMAIL)) {
  918.                                 $account_managers[] = trim($account_manager);
  919.                                 $valid_account_managers true;
  920.                             }
  921.                         }
  922.                         $ac_text "none";
  923.                         if($valid_account_managers) {
  924.                             $ac_text implode(", "$account_managers);
  925.                         }
  926.                         if(!$valid_recipients) {
  927.                             continue;
  928.                         }
  929.                         //send email
  930.                         $this->emailHelper->sendEmail(
  931.                             $recipients,
  932.                             //["egreco@intradatech.com", "apost@intradatech.com"],
  933.                             "Your podcast is now live on the RoofersCoffeeShop partner podcasts page!",
  934.                             [
  935.                                 ["p" => "Hello " $hc_cust->getTitle() . "!" ],
  936.                                 ["p" => "Your podcast is now live on the RCS <a href='https://www.rooferscoffeeshop.com/podcasts/partner-podcast'>Partner Podcasts page</a>!"],
  937.                                 
  938.                                 ["p" => "
  939.                                     <div>
  940.                                         <a href='".$home_content->getFullURL(Content::SITE_RCS)."'>
  941.                                             <img  width=300 style='border:0;' src='https://www.rooferscoffeeshop.com/".$home_content->getFeaturedImageURL()."'>
  942.                                         </a>
  943.                                     </div>
  944.                                     <div>
  945.                                         <div>
  946.                                             <a href='".$home_content->getFullURL(Content::SITE_RCS)."' style='color:#043056;font-weight: 700; font-size:16px;text-decoration:none;'>
  947.                                             ".$home_content->getTitle()."
  948.                                             </a>
  949.                                         </div>
  950.                                         <i style='color:#26679e;'>".str_replace(["AM""PM"], ["a.m.""p.m."], date("F j, Y \\a\\t g:i A"strtotime($home_content->getPublishedAt()->format("Y-m-d H:i:s"))))."</i>
  951.                                         <div class='my-3'>
  952.                                             <span class='sideintrocontent'>".$home_content->getIntroText()."</span>
  953.                                             <a href='".$home_content->getFullURL(Content::SITE_RCS)."' style='color:#26679e;'>
  954.                                                 Read More
  955.                                             </a>
  956.                                         </div>
  957.                                     </div>
  958.                                 "],
  959.                                 ["p" => "Please share!"],
  960.                             ],
  961.                             [], //attachments
  962.                             $account_managers//cc
  963.                             ""//bcc
  964.                             ""//logo
  965.                             [], //media_attachments
  966.                             Content::SITE_RCS
  967.                         );
  968.                         
  969.                     }
  970.                 }
  971.             }
  972.             //dev info
  973.             /*
  974.             ["p" => "."],
  975.             ["p" => "."],
  976.             ["p" => "Dev info:"],
  977.             ["p" => "Content ids: " . $cidsString],
  978.             ["p" => "The content ids are on the home page:"],
  979.             ["p" => $rcsHcidsString],
  980.             
  981.             ["p" => "Recipients: " . $recipients_text],
  982.             ["p" => "Account Managers: " . $ac_text],
  983.             ["p" => "Customer: #" . $hc_cust->getID() . " " . $hc_cust->getTitle()],
  984.             ["p" => "Content: #" . $home_content->getID() . " " . $home_content->getTitle()],
  985.             */
  986.             
  987.             //MCS
  988.             $mcsHcObjs = [];
  989.             $mcsHcidsString "";
  990.             foreach($mcs_home_page_contents as $hc) {
  991.                 $mcsHcidsString .= $hc->getId() . " ";
  992.                 if(in_array($hc->getId(), $cids)) {
  993.                     $mcsHcObjs[] = $hc;
  994.                 }
  995.             }
  996.             $mcsHcObjsStr "";
  997.             foreach($mcsHcObjs as $home_content) {
  998.                 $mcsHcObjsStr .= $home_content->getId() . " ";
  999.                 foreach($home_content->getCustomers() as $hc_cust) {
  1000.                     if($hc_cust->getDirectoryContent()) {
  1001.                         
  1002.                         $recipients = [];
  1003.                         $valid_recipients false;
  1004.                         foreach(explode(','$hc_cust->getDirectoryContent()->getDirectoryEmailRecipients()) as $recipient) {
  1005.                             if ($recipient && filter_var(trim($recipient), FILTER_VALIDATE_EMAIL)) {
  1006.                                 $recipients[] = trim($recipient);
  1007.                                 $valid_recipients true;
  1008.                             }
  1009.                         }
  1010.                         $recipients_text "none";
  1011.                         if($valid_recipients) {
  1012.                             $recipients_text implode(", "$recipients);
  1013.                         }
  1014.                         $account_managers = [];
  1015.                         $valid_account_managers false;
  1016.                         foreach(explode(','$hc_cust->getDirectoryContent()->getDirectoryAccountManagers()) as $account_manager) {
  1017.                             if ($account_manager && filter_var(trim($account_manager), FILTER_VALIDATE_EMAIL)) {
  1018.                                 $account_managers[] = trim($account_manager);
  1019.                                 $valid_account_managers true;
  1020.                             }
  1021.                         }
  1022.                         $ac_text "none";
  1023.                         if($valid_account_managers) {
  1024.                             $ac_text implode(", "$account_managers);
  1025.                         }
  1026.                         if(!$valid_recipients) {
  1027.                             continue;
  1028.                         }
  1029.                         //send email
  1030.                         $this->emailHelper->sendEmail(
  1031.                             $recipients,
  1032.                             //["egreco@intradatech.com", "apost@intradatech.com"],
  1033.                             "Your article is now live on the MetalCoffeeShop home page!",
  1034.                             [
  1035.                                 ["p" => "Hello " $hc_cust->getTitle() . "!" ],
  1036.                                 ["p" => "Your article is now live on MCS!"],
  1037.                                 
  1038.                                 ["p" => "
  1039.                                     <div>
  1040.                                         <a href='".$home_content->getFullURL(Content::SITE_MCS)."'>
  1041.                                             <img  width=300 style='border:0;' src='https://www.metalcoffeeshop.com/".$home_content->getFeaturedImageURL()."'>
  1042.                                         </a>
  1043.                                     </div>
  1044.                                     <div>
  1045.                                         <div>
  1046.                                             <a href='".$home_content->getFullURL(Content::SITE_MCS)."' style='color:#043056;font-weight: 700; font-size:16px;text-decoration:none;'>
  1047.                                             ".$home_content->getTitle()."
  1048.                                             </a>
  1049.                                         </div>
  1050.                                         <i style='color:#26679e;'>".str_replace(["AM""PM"], ["a.m.""p.m."], date("F j, Y \\a\\t g:i A"strtotime($home_content->getPublishedAt()->format("Y-m-d H:i:s"))))."</i>
  1051.                                         <div class='my-3'>
  1052.                                             <span class='sideintrocontent'>".$home_content->getIntroText()."</span>
  1053.                                             <a href='".$home_content->getFullURL(Content::SITE_MCS)."' style='color:#26679e;'>
  1054.                                                 Read More
  1055.                                             </a>
  1056.                                         </div>
  1057.                                     </div>
  1058.                                 "],
  1059.                                 ["p" => "Please share!"],
  1060.                             ],
  1061.                             [], //attachments
  1062.                             $account_managers//cc
  1063.                             ""//bcc
  1064.                             ""//logo
  1065.                             [], //media_attachments
  1066.                             Content::SITE_MCS
  1067.                         );
  1068.                         
  1069.                     }
  1070.                 }
  1071.             }
  1072.             //CCS
  1073.             $ccsHcObjs = [];
  1074.             $ccsHcidsString "";
  1075.             foreach($ccs_home_page_contents as $hc) {
  1076.                 $ccsHcidsString .= $hc->getId() . " ";
  1077.                 if(in_array($hc->getId(), $cids)) {
  1078.                     $ccsHcObjs[] = $hc;
  1079.                 }
  1080.             }
  1081.             $ccsHcObjsStr "";
  1082.             foreach($ccsHcObjs as $home_content) {
  1083.                 $ccsHcObjsStr .= $home_content->getId() . " ";
  1084.                 foreach($home_content->getCustomers() as $hc_cust) {
  1085.                     if($hc_cust->getDirectoryContent()) {
  1086.                         
  1087.                         $recipients = [];
  1088.                         $valid_recipients false;
  1089.                         foreach(explode(','$hc_cust->getDirectoryContent()->getDirectoryEmailRecipients()) as $recipient) {
  1090.                             if ($recipient && filter_var(trim($recipient), FILTER_VALIDATE_EMAIL)) {
  1091.                                 $recipients[] = trim($recipient);
  1092.                                 $valid_recipients true;
  1093.                             }
  1094.                         }
  1095.                         $recipients_text "none";
  1096.                         if($valid_recipients) {
  1097.                             $recipients_text implode(", "$recipients);
  1098.                         }
  1099.                         $account_managers = [];
  1100.                         $valid_account_managers false;
  1101.                         foreach(explode(','$hc_cust->getDirectoryContent()->getDirectoryAccountManagers()) as $account_manager) {
  1102.                             if ($account_manager && filter_var(trim($account_manager), FILTER_VALIDATE_EMAIL)) {
  1103.                                 $account_managers[] = trim($account_manager);
  1104.                                 $valid_account_managers true;
  1105.                             }
  1106.                         }
  1107.                         $ac_text "none";
  1108.                         if($valid_account_managers) {
  1109.                             $ac_text implode(", "$account_managers);
  1110.                         }
  1111.                         if(!$valid_recipients) {
  1112.                             continue;
  1113.                         }
  1114.                         //send email
  1115.                         $this->emailHelper->sendEmail(
  1116.                             $recipients,
  1117.                             //["egreco@intradatech.com", "apost@intradatech.com"],
  1118.                             "Your article is now live on the CoatingsCoffeeShop home page!",
  1119.                             [
  1120.                                 ["p" => "Hello " $hc_cust->getTitle() . "!" ],
  1121.                                 ["p" => "Your article is now live on CCS!"],
  1122.                                 
  1123.                                 ["p" => "
  1124.                                     <div>
  1125.                                         <a href='".$home_content->getFullURL(Content::SITE_CCS)."'>
  1126.                                             <img  width=300 style='border:0;' src='https://www.coatingscoffeeshop.com/".$home_content->getFeaturedImageURL()."'>
  1127.                                         </a>
  1128.                                     </div>
  1129.                                     <div>
  1130.                                         <div>
  1131.                                             <a href='".$home_content->getFullURL(Content::SITE_CCS)."' style='color:#043056;font-weight: 700; font-size:16px;text-decoration:none;'>
  1132.                                             ".$home_content->getTitle()."
  1133.                                             </a>
  1134.                                         </div>
  1135.                                         <i style='color:#26679e;'>".str_replace(["AM""PM"], ["a.m.""p.m."], date("F j, Y \\a\\t g:i A"strtotime($home_content->getPublishedAt()->format("Y-m-d H:i:s"))))."</i>
  1136.                                         <div class='my-3'>
  1137.                                             <span class='sideintrocontent'>".$home_content->getIntroText()."</span>
  1138.                                             <a href='".$home_content->getFullURL(Content::SITE_CCS)."' style='color:#26679e;'>
  1139.                                                 Read More
  1140.                                             </a>
  1141.                                         </div>
  1142.                                     </div>
  1143.                                 "],
  1144.                                 ["p" => "Please share!"],
  1145.                             ],
  1146.                             [], //attachments
  1147.                             $account_managers//cc
  1148.                             ""//bcc
  1149.                             ""//logo
  1150.                             [], //media_attachments
  1151.                             Content::SITE_CCS
  1152.                         );
  1153.                         
  1154.                     }
  1155.                 }
  1156.             }
  1157.             //AAR
  1158.             $aarHcObjs = [];
  1159.             $aarHcidsString "";
  1160.             foreach($aar_home_page_contents as $hc) {
  1161.                 $aarHcidsString .= $hc->getId() . " ";
  1162.                 if(in_array($hc->getId(), $cids)) {
  1163.                     $aarHcObjs[] = $hc;
  1164.                 }
  1165.             }
  1166.             $aarHcObjsStr "";
  1167.             foreach($aarHcObjs as $home_content) {
  1168.                 $aarHcObjsStr .= $home_content->getId() . " ";
  1169.                 foreach($home_content->getCustomers() as $hc_cust) {
  1170.                     if($hc_cust->getDirectoryContent()) {
  1171.                         
  1172.                         $recipients = [];
  1173.                         $valid_recipients false;
  1174.                         foreach(explode(','$hc_cust->getDirectoryContent()->getDirectoryEmailRecipients()) as $recipient) {
  1175.                             if ($recipient && filter_var(trim($recipient), FILTER_VALIDATE_EMAIL)) {
  1176.                                 $recipients[] = trim($recipient);
  1177.                                 $valid_recipients true;
  1178.                             }
  1179.                         }
  1180.                         $recipients_text "none";
  1181.                         if($valid_recipients) {
  1182.                             $recipients_text implode(", "$recipients);
  1183.                         }
  1184.                         $account_managers = [];
  1185.                         $valid_account_managers false;
  1186.                         foreach(explode(','$hc_cust->getDirectoryContent()->getDirectoryAccountManagers()) as $account_manager) {
  1187.                             if ($account_manager && filter_var(trim($account_manager), FILTER_VALIDATE_EMAIL)) {
  1188.                                 $account_managers[] = trim($account_manager);
  1189.                                 $valid_account_managers true;
  1190.                             }
  1191.                         }
  1192.                         $ac_text "none";
  1193.                         if($valid_account_managers) {
  1194.                             $ac_text implode(", "$account_managers);
  1195.                         }
  1196.                         if(!$valid_recipients) {
  1197.                             continue;
  1198.                         }
  1199.                         //send email
  1200.                         $this->emailHelper->sendEmail(
  1201.                             $recipients,
  1202.                             //["egreco@intradatech.com", "apost@intradatech.com"],
  1203.                             "Your article is now live on the AskARoofer home page!",
  1204.                             [
  1205.                                 ["p" => "Hello " $hc_cust->getTitle() . "!" ],
  1206.                                 ["p" => "Your article is now live on AAR!"],
  1207.                                 
  1208.                                 ["p" => "
  1209.                                     <div>
  1210.                                         <a href='".$home_content->getFullURL(Content::SITE_AAR)."'>
  1211.                                             <img  width=300 style='border:0;' src='https://www.askaroofer.com/".$home_content->getFeaturedImageURL()."'>
  1212.                                         </a>
  1213.                                     </div>
  1214.                                     <div>
  1215.                                         <div>
  1216.                                             <a href='".$home_content->getFullURL(Content::SITE_AAR)."' style='color:#043056;font-weight: 700; font-size:16px;text-decoration:none;'>
  1217.                                             ".$home_content->getTitle()."
  1218.                                             </a>
  1219.                                         </div>
  1220.                                         <i style='color:#26679e;'>".str_replace(["AM""PM"], ["a.m.""p.m."], date("F j, Y \\a\\t g:i A"strtotime($home_content->getPublishedAt()->format("Y-m-d H:i:s"))))."</i>
  1221.                                         <div class='my-3'>
  1222.                                             <span class='sideintrocontent'>".$home_content->getIntroText()."</span>
  1223.                                             <a href='".$home_content->getFullURL(Content::SITE_AAR)."' style='color:#26679e;'>
  1224.                                                 Read More
  1225.                                             </a>
  1226.                                         </div>
  1227.                                     </div>
  1228.                                 "],
  1229.                                 ["p" => "Please share!"],
  1230.                             ],
  1231.                             [], //attachments
  1232.                             $account_managers//cc
  1233.                             ""//bcc
  1234.                             ""//logo
  1235.                             [], //media_attachments
  1236.                             Content::SITE_AAR
  1237.                         );
  1238.                         
  1239.                     }
  1240.                 }
  1241.             }
  1242.             /*
  1243.             $this->emailHelper->sendEmail(
  1244.                 ["egreco@intradatech.com"],
  1245.                 "RoofersCoffeeShop.com: home page test",
  1246.                 [
  1247.                     ["p" => "The following content ids went live:"],
  1248.                     ["p" => $cidsString],
  1249.                     //["p" => "The following content ids went live and are on the home page:"],
  1250.                     ["p" => "The following content ids are on the home page:"],
  1251.                     ["p" => $hcObjsStr],
  1252.                 ],
  1253.                 [],
  1254.                 "",
  1255.                 "",
  1256.                 "",
  1257.                 [],
  1258.                 Content::SITE_RCS
  1259.             );
  1260.             */
  1261.         }
  1262.         catch(\Exception $e) {
  1263.             $this->emailHelper->sendEmail(
  1264.                 ["egreco@intradatech.com"],
  1265.                 "RoofersCoffeeShop.com: home page email error",
  1266.                 [
  1267.                     ["p" => "Dev message: New home page live content email failed. Check logs."],
  1268.                 ],
  1269.                 [],
  1270.                 "",
  1271.                 "",
  1272.                 "",
  1273.                 [],
  1274.                 Content::SITE_RCS
  1275.             );
  1276.         }
  1277.         
  1278.     }
  1279.     
  1280.     //Not site specific
  1281.     public function updateExpiredAnnouncements() {
  1282.         
  1283.         $contents $this->entityManager
  1284.             ->createQuery(implode(" ", array (
  1285.                 "SELECT content",
  1286.                 "FROM",
  1287.                     "App\Entity\Content content",
  1288.                 "JOIN",
  1289.                     "content.contentmeta contentmeta",
  1290.                 "WHERE",
  1291.                     "content.announcement_sites != '' AND content.announcement_sites != '[]'",
  1292.                     "AND contentmeta.metakey = '_PostAnnouncementEndDate'",
  1293.                     "AND contentmeta.metavalue != ''",
  1294.                     "AND contentmeta.metavalue < :now",
  1295.             )))
  1296.             ->setParameter("now", new \DateTime())
  1297.         ->getResult();
  1298.         
  1299.         if($contents) {
  1300.             foreach($contents as $content) {
  1301.                 $content->setAnnouncement(false);
  1302.                 $content->setAnnouncementSites([]);
  1303.                 $content->setContentmetum("_PostAnnouncementEndDate""");
  1304.                 $this->entityManager->persist($content);
  1305.                 $this->entityManager->flush();
  1306.             }
  1307.         }
  1308.         
  1309.     }
  1310.     
  1311.     //Filtered by site
  1312.     public function getDirectoryListings (
  1313.         $page 0,
  1314.         $category "_",
  1315.         $type "_",
  1316.         $search "_",
  1317.         $location "_",
  1318.         $state "_",
  1319.         $limit ""
  1320.     ) {
  1321.         $site_code $this->getSiteCode();
  1322.         $page = (int) $page;
  1323.         // Temp for ticket #151771. Should update to handle spaces / dashes.
  1324.         $search strtolower($search) == "georgia pacific" "georgia-pacific" $search;
  1325.         
  1326.         if ($category != "_") {
  1327.             
  1328.             // just showing 1 for now...
  1329.             // $category = explode("-", $category);
  1330.             // foreach ($category as $c) {
  1331.             // }
  1332.             
  1333.         } else {
  1334.             $category "";
  1335.         }
  1336.         
  1337.         if ($type != "_") {
  1338.             $type explode("-"$type);
  1339.             $num 0;
  1340.             foreach ($type as $t) {
  1341.                 switch ($t) {
  1342.                     case "products_services":
  1343.                         $num += Customer::TYPE_PRODUCTS_SERVICES;
  1344.                         break;
  1345.                     case "distributors":
  1346.                         $num += Customer::TYPE_DISTRIBUTORS;
  1347.                         break;
  1348.                     case "consultants":
  1349.                         $num += Customer::TYPE_CONSULTANTS;
  1350.                         break;
  1351.                     case "contractors":
  1352.                         $num += Customer::TYPE_CONTRACTORS;
  1353.                         break;
  1354.                     case "associations":
  1355.                         $num += Customer::TYPE_ASSOCIATIONS;
  1356.                         break;
  1357.                     case "manufacturers_reps":
  1358.                         $num += Customer::TYPE_MANUFACTURERS_REP;
  1359.                         break;
  1360.                     case "rcs_partners":
  1361.                         $num += Customer::TYPE_RCS_PARTNERS;
  1362.                         break;
  1363.                 }
  1364.             }
  1365.             $type $num;
  1366.         } else {
  1367.             $type "";
  1368.         }
  1369.         
  1370.         if ($location && $location != "_") {
  1371.             $location_string str_replace("-""%' OR customers.location LIKE '%"$location);
  1372.             $location_string "customers.location LIKE '%" $location_string "%'";
  1373.         } else {
  1374.             $location_string "";
  1375.         }
  1376.         
  1377.         if ($state && $state != "_") {
  1378.             $state_string str_replace("-""%' OR customers.state LIKE '%"$state);
  1379.             $state_string "customers.state LIKE '%" $state_string "%'";
  1380.         } else {
  1381.             $state_string "";
  1382.         }
  1383.         
  1384.         if(!$limit) {
  1385.             $limit self::DEFAULT_LIMIT;
  1386.         }
  1387.         $offset = ($page $limit);
  1388.         
  1389.         $total 0;
  1390.         $query $this->entityManager
  1391.             ->createQuery(($sql implode(" ", array (
  1392.                 "SELECT DISTINCT",
  1393.                     "content",
  1394.                 "FROM",
  1395.                     "App\Entity\Content content",
  1396.                     
  1397.                 "JOIN",
  1398.                     "content.directory customers",
  1399.                     
  1400.                 (($site_code) ? implode(" ", array (
  1401.                     "JOIN",
  1402.                         "customers.site site",
  1403.                     
  1404.                     "LEFT JOIN",
  1405.                         "customers.levels levels",
  1406.                 )) : ""),
  1407.                 
  1408.                 "LEFT JOIN",
  1409.                     "customers.categories categories",
  1410.                     
  1411.                 "WHERE",
  1412.                 
  1413.                     "content.type = 4 AND",
  1414.                     "content.status = 1",
  1415.                     
  1416.                     // if search is provided
  1417.                     (($search) ? implode(" ", array (
  1418.                         "AND (",
  1419.                             //"customers.description LIKE :search1 OR",
  1420.                             "content.content_full LIKE :search1 OR",
  1421.                             "customers.title LIKE :search2 OR",
  1422.                             "customers.keywords LIKE :search3",
  1423.                         ")",
  1424.                     )) : ""),
  1425.                     
  1426.                     (($site_code) ? implode(" ", array (
  1427.                         "AND",
  1428.                             "site.id = {$site_code}",
  1429.                         "AND",
  1430.                             "(levels.site_id = {$site_code} OR levels.site_id IS NULL)",
  1431.                     )) : ""),
  1432.                     
  1433.                     // if type is provided
  1434.                     (($type) ? implode(" ", array (
  1435.                         "AND (",
  1436.                             "BIT_AND( customers.customer_type, :type1 ) != 0",
  1437.                         ")",
  1438.                     )) : ""),
  1439.                     
  1440.                     // if category is provided
  1441.                     (($category) ? implode(" ", array (
  1442.                         "AND (",
  1443.                             "categories.id = :cat1",
  1444.                         ")",
  1445.                     )) : ""),
  1446.                     
  1447.                     // if location is provided
  1448.                     (($location_string) ? implode(" ", array (
  1449.                         "AND (",
  1450.                             $location_string,
  1451.                         ")",
  1452.                     )) : ""),
  1453.                     
  1454.                     // if state is provided
  1455.                     (($state_string) ? implode(" ", array (
  1456.                         "AND (",
  1457.                             $state_string,
  1458.                         ")",
  1459.                     )) : ""),
  1460.                     
  1461.                     
  1462.                 "ORDER BY",
  1463.                     (($site_code == 3) ? implode(" ", array (
  1464.                         "CASE WHEN customers.id = 863 THEN 3
  1465.                              WHEN customers.id = 160 THEN 2
  1466.                               ELSE 0 END DESC,"//Hack to get Sherwin Williams to display first
  1467.                                   // WHEN customers.id = 782 THEN 2
  1468.                                 //WHEN customers.id = 760 THEN 1
  1469.                     )) : ""),
  1470.                     (($site_code == 4) ? implode(" ", array (
  1471.                         "CASE WHEN customers.id = 680 THEN 4
  1472.                              WHEN customers.id = 491 THEN 3
  1473.                              WHEN customers.id = 481 THEN 2
  1474.                               ELSE 0 END DESC,",
  1475.                     )) : ""),
  1476.                               
  1477.                     "content.featured DESC,",
  1478.                     //"customers.customer_level,",
  1479.                     "CASE WHEN levels.level IS NULL THEN customers.customer_level
  1480.                             ELSE levels.level END ASC,",
  1481.                             
  1482.                     "customers.title",
  1483.             ))));
  1484.             
  1485.         if ($search) {
  1486.             $query->setParameter("search1""%{$search}%");
  1487.             $query->setParameter("search2""%{$search}%");
  1488.             $query->setParameter("search3""%{$search}%");
  1489.         }
  1490.         
  1491.         if ($type) {
  1492.             $query->setParameter("type1"$type);
  1493.         }
  1494.         
  1495.         if ($category) {
  1496.             $query->setParameter("cat1"$category);
  1497.         }
  1498.         
  1499.         // pagination
  1500.         $query
  1501.             ->setFirstResult($offset)
  1502.             ->setMaxResults($limit);
  1503.             
  1504.         $listings $query
  1505.             ->getResult();
  1506.         
  1507.         // now count the content
  1508.         $sql str_replace("SELECT DISTINCT content""SELECT COUNT(DISTINCT content.id)"$sql);
  1509.         
  1510.         $query $this->entityManager
  1511.             ->createQuery($sql);
  1512.         
  1513.         if ($search) {
  1514.             $query->setParameter("search1""%{$search}%");
  1515.             $query->setParameter("search2""%{$search}%");
  1516.             $query->setParameter("search3""%{$search}%");
  1517.         }
  1518.         
  1519.         if ($type) {
  1520.             $query->setParameter("type1"$type);
  1521.         }
  1522.         
  1523.         if ($category) {
  1524.             $query->setParameter("cat1"$category);
  1525.         }
  1526.         
  1527.         $count $query
  1528.             ->getResult();
  1529.         
  1530.         return array (
  1531.             "listings" => $listings,
  1532.             "total" => (int) $count[0][1],
  1533.         );
  1534.     }
  1535.     
  1536.     public function getDirectoryCategories ()
  1537.     {
  1538.         return $this->entityManager
  1539.             ->getRepository(\App\Entity\Category::class)
  1540.             ->findBy([
  1541.                 "taxonomy" => "directory-category"
  1542.             ], array ("title" => "ASC"));
  1543.     }
  1544.     
  1545.     //Filtered by site
  1546.     public function getClassifiedListings (
  1547.         $page 0,
  1548.         $keywords "",
  1549.         $address "",
  1550.         $category "",
  1551.         $area "",
  1552.         $group "",
  1553.         $pagingSize self::DEFAULT_LIMIT
  1554.     ) {
  1555.         
  1556.         $site_code $this->getSiteCode();
  1557.         $page = (int) $page;
  1558.         $limit $pagingSize;
  1559.         $offset = ($page $limit);
  1560.         
  1561.         $listings = array ();
  1562.         
  1563.         if($area != "_") {
  1564.             $area explode("-"$area);
  1565.         }
  1566.         
  1567.         $sql implode(" ", array (
  1568.             "SELECT DISTINCT",
  1569.                 "content",
  1570.                 
  1571.             "FROM",
  1572.                 "App\Entity\Content content",
  1573.                 
  1574.             "JOIN",
  1575.                 "content.contentmeta contentmeta",
  1576.             
  1577.             (($site_code) ? implode(" ", array (
  1578.                 "JOIN",
  1579.                     "content.site site",
  1580.             )) : ""),
  1581.                 
  1582.             // join if looking for category
  1583.             (($category != "_") ? 
  1584.                 implode(" ", array (
  1585.                     "JOIN",
  1586.                         "content.secondary_categories categories1",
  1587.                         
  1588.                 )) : ""
  1589.             ),
  1590.             
  1591.             // join if looking for area
  1592.             (($area != "_") ?
  1593.                 implode(" ", array (    
  1594.                     "JOIN",
  1595.                         "content.secondary_categories categories2",
  1596.                 )) : ""
  1597.             ),
  1598.                 
  1599.             // join if looking for special grouping
  1600.             ((!empty($group)) ? 
  1601.                 implode(" ", array (
  1602.                     "JOIN",
  1603.                         "content.secondary_categories categories3",
  1604.                 )) : ""
  1605.             ),
  1606.                 
  1607.             
  1608.             "WHERE",
  1609.                 "content.type = 3 AND",
  1610.                 "content.status = 1",
  1611.                 
  1612.             (($address != "_") ?
  1613.                 implode(" ", array (
  1614.                     "AND (",
  1615.                         "contentmeta.metakey = '_job_location' AND",
  1616.                         "contentmeta.metavalue LIKE :address1",
  1617.                     ")",
  1618.                 )) : ""
  1619.             ),
  1620.             (($site_code) ? implode(" ", array (
  1621.                 "AND",
  1622.                     "site.id = {$site_code}",
  1623.             )) : ""),
  1624.                 
  1625.             (($keywords != "_") ? 
  1626.                 implode(" ", array (
  1627.                     "AND (",
  1628.                         "content.title LIKE :keyword1 OR",
  1629.                         "content.content_full LIKE :keyword2",
  1630.                     ")",
  1631.                 )) : ""
  1632.             ),
  1633.                 
  1634.             (($category != "_") ? 
  1635.                 implode(" ", array (
  1636.                     "AND (",
  1637.                         "categories1.id = :cat1 AND",
  1638.                         "categories1.taxonomy = 'job_listing_category'",
  1639.                     ")",
  1640.                 )) : ""
  1641.             )));
  1642.             
  1643.             if($area != "_") {
  1644.                 $sql .= "AND (categories2.taxonomy = 'job_listing_type' AND (";
  1645.                 $c 0;
  1646.                 foreach($area as $a) {
  1647.                     if($c != 0) { $sql .= " OR "; }
  1648.                     $sql .= "categories2.id = {$a}";
  1649.                     $c++;
  1650.                 }
  1651.                 $sql .= "))";
  1652.             }
  1653.             
  1654.             $sql .= implode(" ", array (
  1655.             ((!empty($group)) ? 
  1656.                 implode(" ", array (
  1657.                     "AND (",
  1658.                         // "categories3.slug = :cat3",
  1659.                         // "categories3.type = ". Category::TYPE_CLASSIFIED_GROUP,
  1660.                         "categories3 = :cat3",
  1661.                     ")",
  1662.                 )) : ""
  1663.             ),
  1664.         
  1665.             "ORDER BY",
  1666.                 "content.featured DESC,",
  1667.                 "content.published_at DESC,",
  1668.                 "content.featured DESC",
  1669.         ));
  1670.         
  1671.         $query $this->entityManager
  1672.             ->createQuery($sql);
  1673.             
  1674.         // var_dump($sql);
  1675.         // exit;
  1676.         
  1677.         // var_dump($group);
  1678.         // exit;
  1679.             
  1680.         if ($keywords != "_") {
  1681.             $query->setParameter("keyword1""%{$keywords}%");
  1682.             $query->setParameter("keyword2""%{$keywords}%");
  1683.         }
  1684.         
  1685.         if ($address != "_") {
  1686.             $query->setParameter("address1""%{$address}%");
  1687.         }
  1688.             
  1689.         if ($category != "_") {
  1690.             $query->setParameter("cat1"$category);
  1691.         }
  1692.         
  1693.         /*
  1694.         if ($area != "_") {
  1695.             $query->setParameter("cat2", $area);
  1696.         }
  1697.         */
  1698.         
  1699.         if (!empty($group)) {
  1700.             $query->setParameter("cat3"$group);
  1701.         }
  1702.             
  1703.         // add pagination
  1704.         $query
  1705.             ->setFirstResult($offset)
  1706.             ->setMaxResults($limit);
  1707.             
  1708.         $listings $query
  1709.             ->getResult();
  1710.                     
  1711.         // now count the content
  1712.         $sql str_replace("SELECT DISTINCT content""SELECT COUNT(DISTINCT content.id)"$sql);
  1713.         
  1714.         $query $this->entityManager
  1715.             ->createQuery($sql);
  1716.             
  1717.         if ($keywords != "_") {
  1718.             $query->setParameter("keyword1""%{$keywords}%");
  1719.             $query->setParameter("keyword2""%{$keywords}%");
  1720.         }
  1721.         
  1722.         if ($address != "_") {
  1723.             $query->setParameter("address1""%{$address}%");
  1724.         }
  1725.             
  1726.         if ($category != "_") {
  1727.             $query->setParameter("cat1"$category);
  1728.         }
  1729.         
  1730.         /*
  1731.         if ($area != "_") {
  1732.             $query->setParameter("cat2", $area);
  1733.         }
  1734.         */
  1735.         
  1736.         if (!empty($group)) {
  1737.             $query->setParameter("cat3"$group);
  1738.         }
  1739.             
  1740.         $count $query
  1741.             ->getResult();
  1742.             
  1743.         return array (
  1744.             "listings" => $listings,
  1745.             "total" => (int) $count[0][1],
  1746.         );
  1747.     }
  1748.     
  1749.     
  1750.     //Filtered by site
  1751.                                                                                                                     //remove these ASAP
  1752.     public function getPaginatedContentByType($type ""int $limit 10int $page 1$search ""$customer ""$category_slug ""$filter_by_site true)
  1753.     {
  1754.         $site_code $this->getSiteCode();
  1755.         
  1756.         //added $search for events, not yet added to other types
  1757.         
  1758.         //If type is event, uses event start date.
  1759.         //Use a positive $page number for upcoming events, negative for past.
  1760.         if($type == 7) {
  1761.             
  1762.             $qb $this->entityManager->createQueryBuilder();
  1763.             $date date("Y-m-d H:i:s");
  1764.             
  1765.             if($page 0) { // upcoming events
  1766.                 $qb->select('c')
  1767.                     ->from("App\Entity\Content""c")
  1768.                     ->join("c.contentmeta""m1""WITH""m1.metakey = '_EventStartDate'")
  1769.                     ->join("c.contentmeta""m2""WITH""m2.metakey = '_EventEndDate' AND m2.metavalue >= '$date'")
  1770.                     ->leftjoin("c.contentmeta""m3""WITH""m3.metakey = '_IsEvent' ");
  1771.                     
  1772.                 if ($customer) {
  1773.                     $cuid $customer->getId();
  1774.                     $qb->join("c.customers""cu""WITH""cu.id = '$cuid'");
  1775.                 }
  1776.                 
  1777.                 if($filter_by_site && $site_code) {
  1778.                     $qb->join("c.site""s""WITH""s.id = {$site_code}");
  1779.                 }
  1780.                 
  1781.                 $qb->where("c.status = 1 AND (c.type = 7 OR m3.metavalue = 'yes') AND m1.metavalue != '' AND m2.metavalue != ''");
  1782.                 
  1783.                 if($search) {
  1784.                     $qb->andWhere("c.content_full LIKE '%{$search}%' OR c.title LIKE '%{$search}%'");
  1785.                 }
  1786.                 
  1787.                 $results $qb->setMaxResults($limit)
  1788.                     ->setFirstResult($limit * ($page 1))
  1789.                     ->addOrderBy("m1.metavalue""ASC")
  1790.                     ->addOrderBy("c.title""ASC")
  1791.                     ->getQuery()->getResult();
  1792.             }
  1793.             else { // past events
  1794.                 $qb->select('c')
  1795.                     ->from("App\Entity\Content""c")
  1796.                     ->join("c.contentmeta""m1""WITH""m1.metakey = '_EventStartDate'")
  1797.                     ->join("c.contentmeta""m2""WITH""m2.metakey = '_EventEndDate' AND m2.metavalue < '$date'")
  1798.                     ->leftjoin("c.contentmeta""m3""WITH""m3.metakey = '_IsEvent' ");
  1799.                 
  1800.                 if ($customer) {
  1801.                     $cuid $customer->getId();
  1802.                     $qb->join("c.customers""cu""WITH""cu.id = '$cuid'");
  1803.                 }
  1804.                 
  1805.                 if($filter_by_site && $site_code) {
  1806.                     $qb->join("c.site""s""WITH""s.id = {$site_code}");
  1807.                 }
  1808.                 
  1809.                 $qb->where("c.status = 1 AND (c.type = 7 OR m3.metavalue = 'yes') AND m1.metavalue != '' AND m2.metavalue != ''");
  1810.                     
  1811.                 if($search) {
  1812.                     $qb->andWhere("c.content_full LIKE '%{$search}%' OR c.title LIKE '%{$search}%'");
  1813.                 }
  1814.                 
  1815.                 $results $qb->setMaxResults($limit)
  1816.                     ->setFirstResult($limit * (-$page 1))
  1817.                     ->addOrderBy("m1.metavalue""DESC")
  1818.                     ->addOrderBy("c.title""DESC")
  1819.                     ->getQuery()->getResult();
  1820.                     
  1821.                 $results array_reverse($results);
  1822.             }
  1823.             
  1824.             return $results;
  1825.         }
  1826.         else {
  1827.             
  1828.             $qb $this->entityManager->createQueryBuilder();
  1829.             $qb->select('c')
  1830.                 ->from("App\Entity\Content""c");
  1831.             
  1832.             if ($customer) {
  1833.                 $cuid $customer->getId();
  1834.                 $qb->join("c.customers""cu""WITH""cu.id = '$cuid'");
  1835.             }
  1836.             
  1837.             if($category_slug) {
  1838.                 if($category_slug == "newsroom") {
  1839.                     $qb->join("c.secondary_categories""cat1")
  1840.                         ->leftJoin("cat1.prnt""cat2")
  1841.                         ->leftJoin("cat2.prnt""cat3")
  1842.                         ->leftJoin("cat3.prnt""cat4")
  1843.                         ;
  1844.                     $qb->leftjoin("c.category""cat""WITH""(cat.slug != 'buy-online' AND cat.slug != 'partner-videos') OR cat IS NULL");
  1845.                 }
  1846.                 else {
  1847.                     $qb->join("c.category""cat""WITH""cat.slug = '{$category_slug}'");
  1848.                 }
  1849.             }
  1850.             else {
  1851.                 //To remove 'buy online' items. We should make a 'buy online' content type, then remove this.
  1852.                 if($type == 2) {
  1853.                     $qb->leftjoin("c.category""cat""WITH""(cat.slug != 'buy-online' AND cat.slug != 'partner-videos') OR cat IS NULL");
  1854.                 }
  1855.             }
  1856.             
  1857.             if($filter_by_site && $site_code) {
  1858.                 $qb->join("c.site""s""WITH""s.id = {$site_code}");
  1859.             }
  1860.             
  1861.             $qb->where("c.type = {$type} AND c.status = 1");
  1862.             if($category_slug == "newsroom") {
  1863.                 $qb->andWhere("(cat1.slug = '{$category_slug}' OR cat2.slug = '{$category_slug}' OR cat3.slug = '{$category_slug}' OR cat4.slug = '{$category_slug}'
  1864.                 OR cat1.slug IN ('rcs-influencers', 'mcs-influencers', 'ccs-influencers') OR cat2.slug IN ('rcs-influencers', 'mcs-influencers', 'ccs-influencers'))"); //temporary
  1865.             }
  1866.             
  1867.             $content $qb->setMaxResults($limit)
  1868.                 ->setFirstResult($limit * ($page 1))
  1869.                 ->addOrderBy("c.published_at""DESC")
  1870.                 ->getQuery()->getResult();
  1871.             
  1872.             
  1873.             if ($content) {
  1874.                 return $content;
  1875.             }
  1876.             else {
  1877.                 return "";
  1878.             }
  1879.             
  1880.             throw new ResourceNotFoundException ("Content Not Found of Type: {$type}");
  1881.         }
  1882.     }
  1883.     
  1884.     //RCS site only - unfiltered
  1885.     public function getPreviousEvent($event)
  1886.     {
  1887.         $qb $this->entityManager->createQueryBuilder();
  1888.         
  1889.         $meta_collection $event->getContentmeta();
  1890.         $content_meta = array();
  1891.         foreach($meta_collection as $meta) {
  1892.             $content_meta[$meta->getMetakey()] = $meta->getMetavalue();
  1893.         }
  1894.         $date $content_meta["_EventStartDate"];
  1895.         $title =  $event->getTitle();
  1896.         
  1897.         $result $qb->select('c')
  1898.             ->from("App\Entity\Content""c")
  1899.             ->join("c.contentmeta""m1""WITH""m1.metakey = '_EventStartDate' AND m1.metavalue <= :date")
  1900.             ->join("c.contentmeta""m2""WITH""m2.metakey = '_EventEndDate'")
  1901.             ->leftjoin("c.contentmeta""m3""WITH""m3.metakey = '_IsEvent' ")
  1902.             ->where("c.status = 1 AND (c.type = 7 OR m3.metavalue = 'yes') AND m1.metavalue != '' AND m2.metavalue != ''")
  1903.             ->andWhere("m1.metavalue < :date OR c.title < :title")
  1904.             ->setMaxResults(1)
  1905.             ->addOrderBy("m1.metavalue""DESC")
  1906.             ->addOrderBy("c.title""DESC")
  1907.             ->getQuery()
  1908.             ->setParameter("date"$date)
  1909.             ->setParameter("title"$title)
  1910.             ->getResult();
  1911.             
  1912.         if($result) {
  1913.             return $result[0];
  1914.         }
  1915.         else {
  1916.             return "";
  1917.         }
  1918.     }
  1919.     
  1920.     //RCS site only - unfiltered
  1921.     public function getNextEvent($event)
  1922.     {
  1923.         $qb $this->entityManager->createQueryBuilder();
  1924.         
  1925.         $meta_collection $event->getContentmeta();
  1926.         $content_meta = array();
  1927.         foreach($meta_collection as $meta) {
  1928.             $content_meta[$meta->getMetakey()] = $meta->getMetavalue();
  1929.         }
  1930.         $date $content_meta["_EventStartDate"];
  1931.         $title =  $event->getTitle();
  1932.         
  1933.         $result $qb->select('c')
  1934.             ->from("App\Entity\Content""c")
  1935.             ->join("c.contentmeta""m1""WITH""m1.metakey = '_EventStartDate' AND m1.metavalue >= :date")
  1936.             ->join("c.contentmeta""m2""WITH""m2.metakey = '_EventEndDate'")
  1937.             ->leftjoin("c.contentmeta""m3""WITH""m3.metakey = '_IsEvent' ")
  1938.             ->where("c.status = 1 AND (c.type = 7 OR m3.metavalue = 'yes') AND m1.metavalue != '' AND m2.metavalue != ''")
  1939.             ->andWhere("m1.metavalue > :date OR c.title > :title")
  1940.             ->setMaxResults(1)
  1941.             ->addOrderBy("m1.metavalue""ASC")
  1942.             ->addOrderBy("c.title""ASC")
  1943.             ->getQuery()
  1944.             ->setParameter("date"$date)
  1945.             ->setParameter("title"$title)
  1946.             ->getResult();
  1947.             
  1948.         if($result) {
  1949.             return $result[0];
  1950.         }
  1951.         else {
  1952.             return "";
  1953.         }
  1954.     }
  1955.     
  1956.     //RCS site only - unfiltered
  1957.     public function countPreviousEvents($search "")
  1958.     {
  1959.         $qb $this->entityManager->createQueryBuilder();
  1960.         $date date("Y-m-d H:i:s");
  1961.         
  1962.         $results $qb->select('c')
  1963.             ->from("App\Entity\Content""c")
  1964.             ->join("c.contentmeta""m1""WITH""m1.metakey = '_EventStartDate'")
  1965.             ->join("c.contentmeta""m2""WITH""m2.metakey = '_EventEndDate' AND m2.metavalue < '$date'")
  1966.             ->leftjoin("c.contentmeta""m3""WITH""m3.metakey = '_IsEvent' ")
  1967.             ->where("c.status = 1 AND (c.type = 7 OR m3.metavalue = 'yes') AND m1.metavalue != '' AND m2.metavalue != ''")
  1968.             ->andWhere("c.content_full LIKE '%{$search}%' OR c.title LIKE '%{$search}%'")
  1969.             ->getQuery()->getResult();
  1970.             
  1971.         return count($results);
  1972.     }
  1973.     
  1974.     //RCS site only - unfiltered
  1975.     public function countUpcomingEvents($search "")
  1976.     {
  1977.         $qb $this->entityManager->createQueryBuilder();
  1978.         $date date("Y-m-d H:i:s");
  1979.         
  1980.         $results $qb->select('c')
  1981.             ->from("App\Entity\Content""c")
  1982.             ->join("c.contentmeta""m1""WITH""m1.metakey = '_EventStartDate'")
  1983.             ->join("c.contentmeta""m2""WITH""m2.metakey = '_EventEndDate' AND m2.metavalue >= '$date'")
  1984.             ->leftjoin("c.contentmeta""m3""WITH""m3.metakey = '_IsEvent' ")
  1985.             ->where("c.status = 1 AND (c.type = 7 OR m3.metavalue = 'yes') AND m1.metavalue != '' AND m2.metavalue != ''")
  1986.             ->andWhere("c.content_full LIKE '%{$search}%' OR c.title LIKE '%{$search}%'")
  1987.             ->getQuery()->getResult();
  1988.             
  1989.         return count($results);
  1990.     }
  1991.     
  1992.     //RCS site only - unfiltered
  1993.     public function getEventsByMonth($month 0$year 0)
  1994.     {
  1995.         if(!$month) { $month date('n'); }
  1996.         if(!$year) { $year date('Y'); }
  1997.         $date date("Y-m-d H:i:s"strtotime("$year-$month-01"));
  1998.         $first_of_month $date;
  1999.         
  2000.         $first_weekday date('w'strtotime($date));
  2001.         $date date("Y-m-d H:i:s"strtotime("-$first_weekday day"strtotime($date)));
  2002.         $num_days_month date('t'strtotime($first_of_month));
  2003.         //$last_weekday = date("w", strtotime("-1 day", strtotime("$year-$month-$num_days_month")));
  2004.         $last_weekday date("w"strtotime("$year-$month-$num_days_month"));
  2005.         $date_eod date("Y-m-d H:i:s"strtotime(date("Y-m-d"strtotime($date)) . " 23:59:59"));
  2006.         
  2007.         
  2008.         $results = array();
  2009.         
  2010.         $x $last_weekday;
  2011.         while($date date("Y-m-d H:i:s"strtotime("+$x day"strtotime("$year-$month-$num_days_month")))) {
  2012.             $qb $this->entityManager->createQueryBuilder();
  2013.             
  2014.             $results[$date] = $qb->select('c')
  2015.                 ->from("App\Entity\Content""c")
  2016.                 ->join("c.contentmeta""m1""WITH""m1.metakey = '_EventStartDate' AND m1.metavalue <= '$date_eod'")
  2017.                 ->join("c.contentmeta""m2""WITH""m2.metakey = '_EventEndDate' AND m2.metavalue >= '$date'")
  2018.                 ->leftjoin("c.contentmeta""m3""WITH""m3.metakey = '_IsEvent' ")
  2019.                 ->where("c.status = 1 AND (c.type = 7 OR m3.metavalue = 'yes') AND m1.metavalue != '' AND m2.metavalue != ''")
  2020.                 ->orderBy("m1.metavalue""ASC")
  2021.                 ->getQuery()->getResult();
  2022.                 
  2023.                 $date date("Y-m-d H:i:s"strtotime("+1 day"strtotime($date)));
  2024.                 $date_eod date("Y-m-d H:i:s"strtotime("+1 day"strtotime($date_eod)));
  2025.         }
  2026.         
  2027.         return $results;
  2028.     }
  2029.     
  2030.     //RCS site only - unfiltered
  2031.     public function getEventsByDay($date$search)
  2032.     {
  2033.         $qb $this->entityManager->createQueryBuilder();
  2034.         $date_eod date("Y-m-d H:i:s"strtotime(date("Y-m-d"strtotime($date)) . " 23:59:59"));
  2035.         
  2036.         $qb->select('c')
  2037.             ->from("App\Entity\Content""c")
  2038.             ->join("c.contentmeta""m1""WITH""m1.metakey = '_EventStartDate' AND m1.metavalue <= '$date_eod'")
  2039.             ->join("c.contentmeta""m2""WITH""m2.metakey = '_EventEndDate' AND m2.metavalue >= '$date'")
  2040.             ->leftjoin("c.contentmeta""m3""WITH""m3.metakey = '_IsEvent' ")
  2041.             ->where("c.status = 1 AND (c.type = 7 OR m3.metavalue = 'yes') AND m1.metavalue != '' AND m2.metavalue != ''");
  2042.             
  2043.         if($search) {
  2044.             $qb->andWhere("c.content_full LIKE '%{$search}%' OR c.title LIKE '%{$search}%'");
  2045.         }
  2046.         
  2047.         $results $qb->orderBy("m1.metavalue""ASC")
  2048.             ->getQuery()->getResult();
  2049.                 
  2050.         return $results;
  2051.     }
  2052.     
  2053.     /*
  2054.     * Takes the content type and the number of results,
  2055.     * and returns the latest X pages, posts, etc.
  2056.     */
  2057.     public function getLatestContent ($type ""$num_results "3")
  2058.     {
  2059.          $content $this->entityManager
  2060.             ->getRepository(Content::class)
  2061.             ->findBy([
  2062.                 "type" => $type,
  2063.                 "status" => 1,
  2064.             ],
  2065.             [
  2066.                 'published_at' => 'DESC'//orderby
  2067.             ],
  2068.             $num_results//limit
  2069.             0              //offset
  2070.             );
  2071.         
  2072.         if ($content) {
  2073.             return $content;
  2074.         }
  2075.         
  2076.         throw new ResourceNotFoundException ("Content Not Found: type = {$type}");
  2077.     }
  2078.     
  2079.     //Not site specific
  2080.     public function getContentById ($id 0)
  2081.     {
  2082.         $content $this->entityManager
  2083.             ->getRepository(Content::class)
  2084.             ->findOneBy([
  2085.                 "id" => $id
  2086.             ]);
  2087.             
  2088.         if ($content) {
  2089.             return $content;
  2090.         }
  2091.         
  2092.         throw new ResourceNotFoundException ("Content with Id {$id} - Not Found");
  2093.     }
  2094.     
  2095.     //Not site specific
  2096.     public function getContentForAnalyticsAggregated($customer$startDate$endDate)
  2097.     {
  2098.         $query implode(" ", array (
  2099.                 "SELECT DISTINCT content",
  2100.                 "FROM",
  2101.                     "App\Entity\Content content",
  2102.                 "JOIN",
  2103.                     "content.customers customer",
  2104.                 "LEFT JOIN",
  2105.                     "customer.prnt prnt",
  2106.                 "JOIN",
  2107.                     "content.content_analytics_daily content_analytics_daily",
  2108.                 "WHERE",
  2109.                     "(customer.id = :id OR prnt.id = :id)",
  2110.                     "AND content_analytics_daily.day BETWEEN :start AND :end",
  2111.                     "AND content.status NOT IN ('0', '-1', '-3', '-4', '-5')",
  2112.             ));
  2113.         
  2114.         $results $this->entityManager
  2115.             ->createQuery($query)
  2116.             ->setParameter("id"$customer->getId())
  2117.             ->setParameter("start"$startDate)
  2118.             ->setParameter("end"$endDate)
  2119.         ->getResult();
  2120.         
  2121.         return $results;
  2122.     }
  2123.     
  2124.     public function countContentImpressions($content$startDate$endDate$site_id="")
  2125.     {
  2126.         $query implode(" ", array (
  2127.                 "SELECT COUNT(impression.traffic_data)",
  2128.                 "FROM",
  2129.                     "App\Entity\ContentImpression impression",
  2130.                 "JOIN",
  2131.                     "impression.content content",
  2132.                 "WHERE",
  2133.                     "content.id = :id",
  2134.                     "AND (impression.requested_at BETWEEN :start AND :end)",
  2135.                     
  2136.                     ($site_id implode(" ", array (
  2137.                         "AND impression.site_id = {$site_id}",
  2138.                     )) : ""),
  2139.                     
  2140.             ));
  2141.         
  2142.         $count $this->entityManager
  2143.             ->createQuery($query)
  2144.             ->setParameter("id"$content->getId())
  2145.             ->setParameter("start"$startDate)
  2146.             ->setParameter("end"$endDate)
  2147.         ->getResult();
  2148.         
  2149.         if (!empty($count)) {
  2150.             return (int) $count[0][1];
  2151.         }
  2152.         
  2153.         return 0;
  2154.     }
  2155.     
  2156.     public function countContentImpressionsGroupedBySite($content$startDate$endDate)
  2157.     {
  2158.         $query implode(" ", array (
  2159.                 "SELECT impression.site_id, COUNT(impression.traffic_data) as impressions",
  2160.                 "FROM",
  2161.                     "App\Entity\ContentImpression impression",
  2162.                 "JOIN",
  2163.                     "impression.content content",
  2164.                 "WHERE",
  2165.                     "content.id = :id",
  2166.                     "AND (impression.requested_at BETWEEN :start AND :end)",
  2167.                 "GROUP BY impression.site_id",
  2168.             ));
  2169.         
  2170.         $results $this->entityManager
  2171.             ->createQuery($query)
  2172.             ->setParameter("id"$content->getId())
  2173.             ->setParameter("start"$startDate)
  2174.             ->setParameter("end"$endDate)
  2175.         ->getResult();
  2176.         
  2177.         return $results;
  2178.     }
  2179.     
  2180.     //Not site specific
  2181.     public function countContentImpressionsAggregated($content$startDate$endDate)
  2182.     {
  2183.         $query implode(" ", array (
  2184.                 "SELECT SUM(content_analytics_daily.impressions)",
  2185.                 "FROM",
  2186.                     "App\Entity\ContentAnalyticsDaily content_analytics_daily",
  2187.                 "JOIN",
  2188.                     "content_analytics_daily.content content",
  2189.                 "WHERE",
  2190.                     "content.id = :id",
  2191.                     "AND content_analytics_daily.day BETWEEN :start AND :end",
  2192.             ));
  2193.         
  2194.         $sum $this->entityManager
  2195.             ->createQuery($query)
  2196.             ->setParameter("id"$content->getId())
  2197.             ->setParameter("start"$startDate)
  2198.             ->setParameter("end"$endDate)
  2199.         ->getResult();
  2200.         
  2201.         if (!empty($sum)) {
  2202.             return (int) $sum[0][1];
  2203.         }
  2204.         
  2205.         return 0;
  2206.     }
  2207.     
  2208.     //Not site specific
  2209.     public function countContentImpressionsAggregatedCorrected($content$startDate$endDate$site_id="")
  2210.     {
  2211.         $query implode(" ", array (
  2212.             "SELECT",
  2213.                 "(",
  2214.                     "SELECT IFNULL(SUM(cad.impressions), 0)",
  2215.                     "FROM content_analytics_daily cad",
  2216.                     "WHERE cad.content_id = :id",
  2217.                     "AND cad.day BETWEEN :start AND :end",
  2218.                     ($site_id implode(" ", [
  2219.                         "AND cad.site_id = {$site_id}",
  2220.                     ]) : ""),
  2221.                 ") +",
  2222.                 "(",
  2223.                     "SELECT IFNULL(SUM(cacd.impressions), 0)",
  2224.                     "FROM content_analytics_correction_daily cacd",
  2225.                     "WHERE cacd.content_id = :id",
  2226.                     "AND cacd.day BETWEEN :start AND :end",
  2227.                     ($site_id implode(" ", [
  2228.                         "AND cacd.site_id = {$site_id}",
  2229.                     ]) : ""),
  2230.                 ") as total",
  2231.         ));
  2232.         
  2233.         $stmt $this->entityManager->getConnection()->prepare($query);
  2234.         $stmt->execute(['id' => $content->getId(), 'start' => $startDate->format("Y-m-d H:i:s"), 'end' => $endDate->format("Y-m-d H:i:s")]);
  2235.         $count $stmt->fetch();
  2236.         
  2237.         if (!empty($count)) {
  2238.             return (int) $count["total"];
  2239.         }
  2240.         
  2241.         return 0;
  2242.     }
  2243.     
  2244.     public function countContentClicks($content$startDate$endDate$site_id="")
  2245.     {
  2246.         $query implode(" ", array (
  2247.                 "SELECT COUNT(view.traffic_data)",
  2248.                 "FROM",
  2249.                     "App\Entity\ContentView view",
  2250.                 "JOIN",
  2251.                     "view.content content",
  2252.                 "WHERE",
  2253.                     "content.id = :id",
  2254.                     "AND (view.requested_at BETWEEN :start AND :end)",
  2255.                     
  2256.                     ($site_id implode(" ", array (
  2257.                         "AND view.site_id = {$site_id}",
  2258.                     )) : ""),
  2259.                     
  2260.             ));
  2261.         
  2262.         $count $this->entityManager
  2263.             ->createQuery($query)
  2264.             ->setParameter("id"$content->getId())
  2265.             ->setParameter("start"$startDate)
  2266.             ->setParameter("end"$endDate)
  2267.         ->getResult();
  2268.         
  2269.         if (!empty($count)) {
  2270.             return (int) $count[0][1];
  2271.         }
  2272.         
  2273.         return 0;
  2274.     }
  2275.             
  2276.     public function countContentClicksGroupedBySite($content$startDate$endDate)
  2277.     {
  2278.         $query implode(" ", array (
  2279.                 "SELECT view.site_id, COUNT(view.traffic_data) as views",
  2280.                 "FROM",
  2281.                     "App\Entity\ContentView view",
  2282.                 "JOIN",
  2283.                     "view.content content",
  2284.                 "WHERE",
  2285.                     "content.id = :id",
  2286.                     "AND (view.requested_at BETWEEN :start AND :end)",
  2287.                 "GROUP BY view.site_id",
  2288.             ));
  2289.         
  2290.         $results $this->entityManager
  2291.             ->createQuery($query)
  2292.             ->setParameter("id"$content->getId())
  2293.             ->setParameter("start"$startDate)
  2294.             ->setParameter("end"$endDate)
  2295.         ->getResult();
  2296.         
  2297.         return $results;
  2298.     }
  2299.     
  2300.     //Not site specific
  2301.     public function countContentClicksAggregated($content$startDate$endDate$site_id="")
  2302.     {
  2303.         $query implode(" ", array (
  2304.                 "SELECT SUM(content_analytics_daily.views)",
  2305.                 "FROM",
  2306.                     "App\Entity\ContentAnalyticsDaily content_analytics_daily",
  2307.                 "JOIN",
  2308.                     "content_analytics_daily.content content",
  2309.                 "WHERE",
  2310.                     "content.id = :id",
  2311.                     "AND content_analytics_daily.day BETWEEN :start AND :end",
  2312.                 ($site_id implode(" ", [
  2313.                     "AND content_analytics_daily.site_id = {$site_id}",
  2314.                 ]) : ""),
  2315.             ));
  2316.         
  2317.         $sum $this->entityManager
  2318.             ->createQuery($query)
  2319.             ->setParameter("id"$content->getId())
  2320.             ->setParameter("start"$startDate)
  2321.             ->setParameter("end"$endDate)
  2322.         ->getResult();
  2323.         
  2324.         if (!empty($sum)) {
  2325.             return (int) $sum[0][1];
  2326.         }
  2327.         
  2328.         return 0;
  2329.     }
  2330.     
  2331.     //Not site specific
  2332.     // Count all the content impressions for a specific customer and time range
  2333.     public function countCustomerContentImpressions($customer$startDate$endDate)
  2334.     {
  2335.         $query implode(" ", array (
  2336.                 "SELECT COUNT(impression.traffic_data)",
  2337.                 "FROM",
  2338.                     "App\Entity\ContentImpressions impression",
  2339.                 "JOIN",
  2340.                     "impression.content content",
  2341.                 "JOIN",
  2342.                     "content.customers customer",
  2343.                 "WHERE",
  2344.                     "customer.id = :id",
  2345.                     "AND (impression.requested_at BETWEEN :start AND :end)",
  2346.                     "AND content.status NOT IN ('0', '-1', '-3', '-4', '-5')",
  2347.             ));
  2348.         
  2349.         $count $this->entityManager
  2350.             ->createQuery($query)
  2351.             ->setParameter("id"$customer->getId())
  2352.             ->setParameter("start"$startDate)
  2353.             ->setParameter("end"$endDate)
  2354.         ->getResult();
  2355.         
  2356.         if (!empty($count)) {
  2357.             return (int) $count[0][1];
  2358.         }
  2359.         
  2360.         return 0;
  2361.     }
  2362.     
  2363.     //Not site specific
  2364.     // Count all the content impressions for a specific customer and time range
  2365.     public function countCustomerContentImpressionsAggregated($customer$startDate$endDate)
  2366.     {
  2367.         $query implode(" ", array (
  2368.                 "SELECT SUM(content_analytics_daily.impressions)",
  2369.                 "FROM",
  2370.                     "App\Entity\ContentAnalyticsDaily content_analytics_daily",
  2371.                 "JOIN",
  2372.                     "content_analytics_daily.content content",
  2373.                 "JOIN",
  2374.                     "content.customers customer",
  2375.                 "WHERE",
  2376.                     "customer.id = :id",
  2377.                     "AND (content_analytics_daily.day BETWEEN :start AND :end)",
  2378.                     "AND content.status NOT IN ('0', '-1', '-3', '-4', '-5')",
  2379.             ));
  2380.         
  2381.         $count $this->entityManager
  2382.             ->createQuery($query)
  2383.             ->setParameter("id"$customer->getId())
  2384.             ->setParameter("start"$startDate)
  2385.             ->setParameter("end"$endDate)
  2386.         ->getResult();
  2387.         
  2388.         if (!empty($count)) {
  2389.             return (int) $count[0][1];
  2390.         }
  2391.         
  2392.         return 0;
  2393.     }
  2394.     
  2395.     //Not site specific
  2396.     // Count all the content impressions for a specific customer and time range
  2397.     public function countCustomerContentImpressionsAggregatedCorrected($customer$startDate$endDate)
  2398.     {
  2399.         $query implode(" ", array (
  2400.                 "SELECT",
  2401.                 "(",
  2402.                     "SELECT IFNULL(SUM(cad.impressions), 0)",
  2403.                     "FROM content_analytics_daily cad",
  2404.                     "JOIN customer_content cc",
  2405.                     "ON cc.content_id = cad.content_id",
  2406.                     "JOIN content content",
  2407.                     "ON cc.content_id = content.id",
  2408.                     "JOIN content_site cs",
  2409.                     "ON cs.content_id = cad.content_id AND cs.site_id = cad.site_id",
  2410.                     "WHERE cc.customer_id = :id",
  2411.                     "AND cad.day BETWEEN :start AND :end",
  2412.                     "AND content.status NOT IN ('0', '-1', '-3', '-4', '-5')",
  2413.                 ") +",
  2414.                 "(",
  2415.                     "SELECT IFNULL(SUM(cacd.impressions), 0)",
  2416.                     "FROM content_analytics_correction_daily cacd",
  2417.                     "JOIN customer_content cc",
  2418.                     "ON cc.content_id = cacd.content_id",
  2419.                     "JOIN content content",
  2420.                     "ON cc.content_id = content.id",
  2421.                     "JOIN content_site cs",
  2422.                     "ON cs.content_id = cacd.content_id AND cs.site_id = cacd.site_id",
  2423.                     "WHERE cc.customer_id = :id",
  2424.                     "AND cacd.day BETWEEN :start AND :end",
  2425.                     "AND content.status NOT IN ('0', '-1', '-3', '-4', '-5')",
  2426.                 ") as total",
  2427.             ));
  2428.         
  2429.         $stmt $this->entityManager->getConnection()->prepare($query);
  2430.         $stmt->execute(['id' => $customer->getId(), 'start' => $startDate->format("Y-m-d H:i:s"), 'end' => $endDate->format("Y-m-d H:i:s")]);
  2431.         $count $stmt->fetch();
  2432.         
  2433.         if (!empty($count)) {
  2434.             return (int) $count["total"];
  2435.         }
  2436.         
  2437.         return 0;
  2438.     }
  2439.     //Not site specific
  2440.     // Count all the content clicks (now called content views in the dashboard) for a specific customer and time range
  2441.     public function countCustomerContentClicks($customer$startDate$endDate)
  2442.     {
  2443.         $query implode(" ", array (
  2444.                 "SELECT COUNT(view.traffic_data)",
  2445.                 "FROM",
  2446.                     "App\Entity\ContentView view",
  2447.                 "JOIN",
  2448.                     "view.content content",
  2449.                 "JOIN",
  2450.                     "content.customers customer",
  2451.                 "WHERE",
  2452.                     "customer.id = :id",
  2453.                     "AND (view.requested_at BETWEEN :start AND :end)",
  2454.                     "AND content.status NOT IN ('0', '-1', '-3', '-4', '-5')",
  2455.             ));
  2456.         
  2457.         $count $this->entityManager
  2458.             ->createQuery($query)
  2459.             ->setParameter("id"$customer->getId())
  2460.             ->setParameter("start"$startDate)
  2461.             ->setParameter("end"$endDate)
  2462.         ->getResult();
  2463.         
  2464.         if (!empty($count)) {
  2465.             return (int) $count[0][1];
  2466.         }
  2467.         
  2468.         return 0;
  2469.     }
  2470.     //Not site specific
  2471.     // Count all the content clicks (now called content views in the dashboard) for a specific customer and time range
  2472.     public function countCustomerContentClicksAggregated($customer$startDate$endDate)
  2473.     {
  2474.         $query implode(" ", array (
  2475.                 "SELECT SUM(content_analytics_daily.views)",
  2476.                 "FROM",
  2477.                     "App\Entity\ContentAnalyticsDaily content_analytics_daily",
  2478.                 "JOIN",
  2479.                     "content_analytics_daily.content content",
  2480.                 "JOIN",
  2481.                     "content.customers customer",
  2482.                 "JOIN",
  2483.                     "content.site site",
  2484.                 "WHERE",
  2485.                     "customer.id = :id",
  2486.                     "AND (content_analytics_daily.day BETWEEN :start AND :end)",
  2487.                     "AND content.status NOT IN ('0', '-1', '-3', '-4', '-5')",
  2488.                     "AND site.id = content_analytics_daily.site_id",
  2489.             ));
  2490.         
  2491.         $count $this->entityManager
  2492.             ->createQuery($query)
  2493.             ->setParameter("id"$customer->getId())
  2494.             ->setParameter("start"$startDate)
  2495.             ->setParameter("end"$endDate)
  2496.         ->getResult();
  2497.         
  2498.         if (!empty($count)) {
  2499.             return (int) $count[0][1];
  2500.         }
  2501.         
  2502.         return 0;
  2503.     }
  2504.     //Not site specific
  2505.     public function countContentLinkClicks($customer$startDate$endDate)
  2506.     {
  2507.         $query implode(" ", array (
  2508.                 "SELECT content.id, click.link_url, COUNT(click.link_url) as count",
  2509.                 "FROM",
  2510.                     "App\Entity\ContentLinkClick click",
  2511.                 "JOIN",
  2512.                     "click.content content",
  2513.                 "JOIN",
  2514.                     "content.customers customer",
  2515.                 "LEFT JOIN",
  2516.                     "customer.prnt prnt",
  2517.                 "WHERE",
  2518.                     "(customer.id = :id OR prnt.id = :id)",
  2519.                     "AND (click.requested_at BETWEEN :start AND :end)",
  2520.                     "AND content.status NOT IN ('0', '-1', '-3', '-4', '-5')",
  2521.                 "GROUP BY click.link_url",
  2522.             ));
  2523.         
  2524.         $results $this->entityManager
  2525.             ->createQuery($query)
  2526.             ->setParameter("id"$customer->getId())
  2527.             ->setParameter("start"$startDate)
  2528.             ->setParameter("end"$endDate)
  2529.         ->getResult();
  2530.         
  2531.         return $results;
  2532.     }
  2533.     
  2534.     //Not site specific
  2535.     public function countCustomerLeads($customer$startDate$endDate)
  2536.     {
  2537.         $query implode(" ", array (
  2538.                 "SELECT content.id, COUNT(lead.id) as count",
  2539.                 "FROM",
  2540.                     "App\Entity\Lead lead",
  2541.                 "JOIN",
  2542.                     "lead.content content",
  2543.                 "JOIN",
  2544.                     "content.customers customer",
  2545.                 "LEFT JOIN",
  2546.                     "customer.prnt prnt",
  2547.                 "WHERE",
  2548.                     "(customer.id = :id OR prnt.id = :id)",
  2549.                     "AND (lead.created_at BETWEEN :start AND :end)",
  2550.             ));
  2551.         
  2552.         $query .= "GROUP BY content.id";
  2553.         
  2554.         $results $this->entityManager
  2555.             ->createQuery($query)
  2556.             ->setParameter("id"$customer->getId())
  2557.             ->setParameter("start"$startDate)
  2558.             ->setParameter("end"$endDate)
  2559.         ->getResult();
  2560.         
  2561.         return $results;
  2562.     }
  2563.     
  2564.     //Not site specific
  2565.     //should replace with a general count function for getPaginatedContentByType results (or return with those results)
  2566.     public function countPostsByCustomer($customer "") {
  2567.         
  2568.         if (!$customer) {
  2569.             return null;
  2570.         }
  2571.         
  2572.         $cuid $customer->getId();
  2573.         
  2574.         $qb $this->entityManager->createQueryBuilder();
  2575.         $results $qb->select('count(c.id)')
  2576.             ->from("App\Entity\Content""c")
  2577.             ->join("c.customers""cu""WITH""cu.id = '$cuid'")
  2578.             ->leftJoin("c.category""cat""WITH""(cat.slug != 'buy-online' AND cat.slug != 'partner-videos') OR cat IS NULL")
  2579.             ->join("c.secondary_categories""cat1")
  2580.                 ->leftJoin("cat1.prnt""cat2")
  2581.                 ->leftJoin("cat2.prnt""cat3")
  2582.                 ->leftJoin("cat3.prnt""cat4")
  2583.             ->where("c.type = 2 AND c.status = 1")
  2584.             ->andWhere("(cat1.slug = 'newsroom' OR cat2.slug = 'newsroom' OR cat3.slug = 'newsroom' OR cat4.slug = 'newsroom')")
  2585.             ->getQuery()->getSingleScalarResult();
  2586.             
  2587.         return $results;
  2588.         
  2589.     }
  2590.     
  2591.     
  2592.     public function countContentByCustomer($type ""$customer ""$filter_by_site true) {
  2593.         $site_code $this->getSiteCode();
  2594.         
  2595.         if (!$customer) {
  2596.             return null;
  2597.         }
  2598.         if (!$type) {
  2599.             return null;
  2600.         }
  2601.         
  2602.         $cuid $customer->getId();
  2603.         
  2604.         $qb $this->entityManager->createQueryBuilder();
  2605.         $qb->select('count(c.id)')
  2606.             ->from("App\Entity\Content""c")
  2607.             ->join("c.customers""cu""WITH""cu.id = '$cuid'");
  2608.         if($filter_by_site && $site_code) {
  2609.             $qb->join("c.site""s""WITH""s.id = {$site_code}");
  2610.         }
  2611.         $results $qb->where("c.type = $type AND c.status = 1")
  2612.             ->getQuery()->getSingleScalarResult();
  2613.             
  2614.         return $results;
  2615.     
  2616.     }
  2617.     
  2618.     //Filtered by site
  2619.     public function getPostsByCategory (
  2620.         $category ""
  2621.         $length 10,
  2622.         $page 1,
  2623.         $sort "",
  2624.         $includeChildren true
  2625.     ) {
  2626.         return $this->entityManager
  2627.             ->getRepository(Content::class)
  2628.             ->getPostsByCategory($category$length$page$sort$includeChildren$this->getSiteCode());
  2629.     }
  2630.     public function getRecommendedPosts (
  2631.         $category null,
  2632.         $length 3,
  2633.         $ignoreID 0
  2634.     ) {
  2635.         $query implode(" ", [
  2636.             "SELECT cad.content_id",
  2637.             "FROM `content_analytics_daily` cad",
  2638.             "LEFT JOIN `content` c ",
  2639.             "ON cad.content_id = c.id ",
  2640.             
  2641.             (!is_null($category) ? implode(" ", [
  2642.                 "LEFT JOIN `content_category` cc",
  2643.                     "ON cc.content_id = c.id",
  2644.                 "WHERE cc.category_id = :category",
  2645.                 "AND ",
  2646.             ]) : "WHERE "),
  2647.             
  2648.             " cad.day > :date",
  2649.             "AND c.status = 1",
  2650.             "GROUP BY cad.content_id",
  2651.             "ORDER BY MAX(cad.`views`) DESC",
  2652.             "limit 20;"
  2653.         ]);
  2654.         $stmt $this->entityManager->getConnection()->prepare($query);
  2655.         $exec_params = ['date' => date_create(date("Y-m-d"))->modify('-10 days')->format('Y-m-d')];
  2656.         if(!is_null($category)) {
  2657.             $exec_params['category'] = $category->getId();
  2658.         }
  2659.         $stmt->execute($exec_params);
  2660.         $hot $stmt->fetchAll(); 
  2661.         
  2662.         $howManyHot count($hot);
  2663.         if ($howManyHot) {
  2664.             $hot $hot[array_rand($hot1)]['content_id'];
  2665.             $hotPost $this->getContentById($hot);
  2666.             $hotPost->hot true;
  2667.         }
  2668.         $site $this->getSiteCode();
  2669.         $result $this->entityManager->createQueryBuilder();
  2670.         $result $result->select('a')->from("App\Entity\Content""a")->join("a.secondary_categories""c")->join("a.site""s");
  2671.         $result $result->where("c.id = :id")->setParameter("id"$category)->distinct();
  2672.         $result $result->andWhere("a.status = 1");
  2673.         if($site) {
  2674.             $result $result->andWhere("s.id = {$site}");
  2675.         }
  2676.         if ($ignoreID) {
  2677.             $result $result->andWhere("c.id != {$ignoreID}")->andWhere("a.id != {$ignoreID}");
  2678.             if ($howManyHot) {
  2679.                 $result $result->andWhere("a.id != {$hot}")->andWhere("c.id != {$hot}");
  2680.             }
  2681.         }
  2682.         
  2683.         if ($howManyHot) {
  2684.             $result $result->andWhere("a.id != {$hot}");
  2685.             if ($howManyHot) {
  2686.                 $result $result->andWhere("c.id != {$hot}");
  2687.             }
  2688.         }
  2689.         $result $result->orderBy("rand()");
  2690.         if ($howManyHot) {
  2691.             $result $result->setMaxResults($length-1);
  2692.         }else{
  2693.             $result $result->setMaxResults($length);
  2694.         }
  2695.         $result $result->getQuery()->getResult();
  2696.         for ($i 0$i count($result); $i++) {
  2697.             $result[$i]->hot false;
  2698.         }
  2699.         if ($howManyHot) {
  2700.             array_unshift($result $hotPost);
  2701.         }
  2702.         shuffle($result);
  2703.         return $result;
  2704.     }
  2705.     
  2706.     //Filtered by site
  2707.     public function getPostsByCategories (
  2708.         $categories ""
  2709.         $length 10,
  2710.         $page 1,
  2711.         $sort "",
  2712.         $includeChildren true
  2713.     ) {
  2714.         return $this->entityManager
  2715.             ->getRepository(Content::class)
  2716.             ->getPostsByCategories($categories$length$page$sort$includeChildren$this->getSiteCode());
  2717.     }
  2718.     //Filtered by Site
  2719.     public function getContentByCategories (
  2720.         $type 0,
  2721.         $categories ""
  2722.         $length 10,
  2723.         $page 1,
  2724.         $sort "",
  2725.         $includeChildren true
  2726.     ) {
  2727.         return $this->entityManager
  2728.             ->getRepository(Content::class)
  2729.             ->getContentByCategories($type$categories$length$page$sort$includeChildren$this->getSiteCode());
  2730.     }
  2731.     
  2732.     //Filtered by site
  2733.     public function countContentByCategories (
  2734.         $type 0,
  2735.         $categories "",
  2736.         $includeChildren true
  2737.     ) {
  2738.         $count $this->entityManager
  2739.             ->getRepository(Content::class)
  2740.             ->countContentByCategories($type$categoriestrue$this->getSiteCode());
  2741.         
  2742.         if (!empty($count)) {
  2743.             return (int) $count[0][1];
  2744.         }
  2745.         
  2746.         return 0;
  2747.     }
  2748.     
  2749.     //Filtered by site
  2750.     public function getDirectoriesByCategory (
  2751.         $category ""
  2752.         $length 10,
  2753.         $page 1,
  2754.         $sort "",
  2755.         $includeChildren true
  2756.     ) {
  2757.         return $this->entityManager
  2758.             ->getRepository(Content::class)
  2759.             ->getDirectoriesByCategory($category$length$page$sort$includeChildren$this->getSiteCode());
  2760.     }
  2761.     
  2762.     //Filtered by site
  2763.     public function countPostsByCategory (
  2764.         $category "",
  2765.         $includeChildren true
  2766.     ) {
  2767.         $count $this->entityManager
  2768.             ->getRepository(Content::class)
  2769.             ->countPostsByCategory($categorytrue$this->getSiteCode());
  2770.         
  2771.         if (!empty($count)) {
  2772.             return (int) $count[0][1];
  2773.         }
  2774.         
  2775.         return 0;
  2776.     }
  2777.     
  2778.     //Filtered by site
  2779.     public function countPostsByCategories (
  2780.         $categories "",
  2781.         $includeChildren true
  2782.     ) {
  2783.         $count $this->entityManager
  2784.             ->getRepository(Content::class)
  2785.             ->countPostsByCategories($categoriestrue$this->getSiteCode());
  2786.         
  2787.         if (!empty($count)) {
  2788.             return (int) $count[0][1];
  2789.         }
  2790.         
  2791.         return 0;
  2792.     }
  2793.     
  2794.     //Not site specific
  2795.     public function countTopicsPostsByForum (
  2796.         $slug ""
  2797.     ) {
  2798.         $count $this->entityManager
  2799.             ->getRepository(Content::class)
  2800.             ->countTopicsPostsByForum($slug);
  2801.         if (!empty($count)) {
  2802.             return [
  2803.                 "num_topics" => (int) $count[0][1],
  2804.                 "num_posts" => (int) $count[0][2] + (int) $count[0][1],
  2805.             ];
  2806.         }
  2807.         
  2808.         return [
  2809.             "num_topics" => 0,
  2810.             "num_posts" => 0,
  2811.         ];
  2812.     }
  2813.     
  2814.     public function getGalleries (
  2815.         $category "",
  2816.         $sort ""
  2817.     ) {
  2818.         return $this->entityManager
  2819.             ->getRepository(Content::class)
  2820.             ->getGalleries($category$sort$this->getSiteCode());
  2821.     }
  2822.     
  2823.     public function getEbooks(
  2824.         $customer ""
  2825.     ){
  2826.         $site_code $this->getSiteCode();    
  2827.         return $this->entityManager
  2828.             ->getRepository(Content::class)
  2829.             ->getEbooks($customer$site_code);
  2830.     }
  2831.     public function getEbooksByCategory(
  2832.         $category ""
  2833.     ){
  2834.         $site_code $this->getSiteCode();    
  2835.         return $this->entityManager
  2836.             ->getRepository(Content::class)
  2837.             ->getEbooksByCategory($category$site_code);
  2838.     }
  2839.     
  2840.     //aarhere
  2841.     public function getHrefById ($id 0)
  2842.     {
  2843.         $content $this->getContentById($id);
  2844.         if ($content) {
  2845.             return $this->getHref($content);
  2846.         }
  2847.         
  2848.         throw new \Exception ("Content with Id {$id} - Not Found");
  2849.     }
  2850.     
  2851.     public function getHref (Content $content)
  2852.     {
  2853.         switch ($content->getType())
  2854.         {
  2855.             case Content::PAGE:
  2856.                 return "/{$content->getSlug()}";
  2857.                 
  2858.             case Content::POST:
  2859.                 // need to grab the primary category
  2860.                 $category "";
  2861.                 return "/post/todo{$category}/{$content->getSlug()}";
  2862.                 
  2863.             case Content::CLASSIFIED:
  2864.                 return "/classified/{$content->getSlug()}";
  2865.                 
  2866.             case Content::DIRECTORY:
  2867.                 return "/directory/{$content->getSlug()}";
  2868.                 
  2869.             case Content::EVENT:
  2870.                 return "/event/{$content->getSlug()}";
  2871.                 
  2872.             case Content::FORUM:
  2873.                 return "/forum/{$content->getSlug()}";
  2874.                 
  2875.             case Content::FORUM_TOPIC:
  2876.                 return "/forum/topic/{$content->getSlug()}";
  2877.                 
  2878.             case Content::FORUM_REPLY:
  2879.                 // grab the topic
  2880.                 $topic "";
  2881.                 return "/forum/topic/{$topic}";
  2882.         }
  2883.         
  2884.         return "";
  2885.     }
  2886.     
  2887.     public function getComments(Content $content)
  2888.     {
  2889.         $criteria Criteria::create()
  2890.             ->where(Criteria::expr()->eq("approved""1"))
  2891.             ->orderBy(array("created_at" => Criteria::ASC))
  2892.         ;
  2893.         
  2894.         return $content->getComments()->matching($criteria);
  2895.     }
  2896.     
  2897.     public function getPaginatedComments(Content $contentint $limit 10int $page 1)
  2898.     {
  2899.         $criteria Criteria::create()
  2900.             ->where(Criteria::expr()->eq("approved""1"))
  2901.             ->orderBy(array("created_at" => Criteria::ASC))
  2902.             ->setFirstResult($limit * ($page 1))
  2903.             ->setMaxResults($limit)
  2904.         ;
  2905.         
  2906.         return $content->getComments()->matching($criteria);
  2907.     }
  2908.     
  2909.     public function getPaginatedChildren(Content $contentint $limit 10int $page 1bool $asc false)
  2910.     {
  2911.         $criteria Criteria::create()
  2912.             ->where(Criteria::expr()->eq("status""1"))
  2913.             ->orderBy(array("published_at" => $asc Criteria::ASC Criteria::DESC))
  2914.             ->setFirstResult($limit * ($page 1))
  2915.             ->setMaxResults($limit)
  2916.         ;
  2917.         
  2918.         return $content->getChildren()->matching($criteria);
  2919.     }
  2920.     
  2921.     //horrible, don't use this
  2922.     public function countChildren(Content $content)
  2923.     {
  2924.         return $content->getActiveChildren()->count();
  2925.     }
  2926.     
  2927.     public function countActiveChildren(Content $content)
  2928.     {
  2929.         
  2930.         $sql implode(" ", array (
  2931.             "SELECT",
  2932.                 "COUNT(c)",
  2933.             "FROM",
  2934.                 "App\Entity\Content c",
  2935.             "WHERE",
  2936.                 "c.status = 1",
  2937.                 "AND c.prnt = :id",
  2938.         ));
  2939.         
  2940.         $query $this->entityManager
  2941.             ->createQuery($sql)
  2942.             ->setParameter("id"$content->getId());
  2943.         
  2944.         $count $query->getResult();
  2945.         $count $count[0][1];
  2946.         return $count;
  2947.     }
  2948.     
  2949.     public function getTopicsByForum(Content $forumint $limit 10int $page 1)
  2950.     {
  2951.         
  2952.         $forumId $forum->getId();
  2953.         $offset $limit * ($page 1);
  2954.         
  2955.         $query implode(" ", array (
  2956.             "SELECT i.id FROM (",
  2957.                 "SELECT t.id, r.published_at",
  2958.                 "FROM content r",
  2959.                 "JOIN content t ",
  2960.                 "ON r.prnt_id = t.id",
  2961.                 "WHERE r.type = 11",
  2962.                 "AND r.status = 1",
  2963.                 "AND t.prnt_id = :forumId",
  2964.                 "UNION",
  2965.                 "SELECT t.id, t.published_at",
  2966.                 "FROM content t",
  2967.                 "WHERE t.type = 10",
  2968.                 "AND t.status = 1",
  2969.                 "AND t.prnt_id = :forumId",
  2970.             ") i",
  2971.             "GROUP BY i.id",
  2972.             "ORDER BY MAX(i.published_at) DESC",
  2973.             "LIMIT {$limit} OFFSET {$offset}",
  2974.         ));
  2975.         
  2976.         $stmt $this->entityManager->getConnection()->prepare($query);
  2977.         $stmt->execute(['forumId' => $forum->getId()]);
  2978.         $contents $stmt->fetchAll();
  2979.         
  2980.         $topics = [];
  2981.         foreach($contents as $c) {
  2982.             $content $this->getContentById($c["id"]);
  2983.             $topics[] = $content;
  2984.         }
  2985.         
  2986.         return $topics;
  2987.     }
  2988.     
  2989.     public function getTopicsByForumOld(Content $forumint $limit 10int $page 1)
  2990.     {
  2991.         $query $this->entityManager
  2992.             ->createQuery(($sql implode(" ", array (
  2993.                 "SELECT",
  2994.                     "topic, (CASE WHEN reply.published_at IS NULL THEN topic.published_at ELSE MAX(reply.published_at) END) AS rdate",
  2995.                 "FROM",
  2996.                     "App\Entity\Content topic",
  2997.                 "LEFT JOIN",
  2998.                     "topic.children reply",
  2999.                 "WHERE",
  3000.                     "topic.type = 10 AND",
  3001.                     "topic.status = 1 AND",
  3002.                     "topic.prnt = {$forum->getId()} AND",
  3003.                     "(reply.status = 1 OR reply.status IS NULL)",
  3004.                 "GROUP BY",
  3005.                     "topic",
  3006.                 "ORDER BY",
  3007.                     "rdate DESC",
  3008.             ))));
  3009.             
  3010.         $query->setFirstResult($limit * ($page 1))
  3011.               ->setMaxResults($limit);
  3012.         
  3013.         $results =  $query->getResult();
  3014.         $results2 = array();
  3015.         foreach($results as $result) {
  3016.             $results2[] = $result[0];
  3017.         }
  3018.         return $results2;
  3019.     }
  3020.     
  3021.     public function getTopicsByForum2(Content $forumint $limit 10int $page 1)
  3022.     {
  3023.         $query $this->entityManager
  3024.             ->createQuery(($sql implode(" ", array (
  3025.                 "SELECT",
  3026.                     "reply",
  3027.                 "FROM",
  3028.                     "App\Entity\Content reply",
  3029.                 "LEFT JOIN",
  3030.                     "reply.prnt topic",
  3031.                 "WHERE",
  3032.                     "reply.type = 11 AND",
  3033.                     "reply.status = 1 AND",
  3034.                     "topic.status = 1 AND",
  3035.                     "topic.prnt = {$forum->getId()}",
  3036.                 "GROUP BY",
  3037.                     "reply.prnt",
  3038.                 "ORDER BY",
  3039.                     "MAX(reply.published_at) DESC,",
  3040.                     "topic.published_at DESC",
  3041.             ))));
  3042.             
  3043.         $query->setFirstResult($limit * ($page 1))
  3044.               ->setMaxResults($limit);
  3045.         
  3046.         $replies $query->getResult();
  3047.         $topics = [];
  3048.         foreach($replies as $reply) {
  3049.             $topics[] = $reply->getPrnt();
  3050.         }
  3051.         return $topics;
  3052.         
  3053.     }
  3054.     
  3055.     /**
  3056.     * Returns forum topics and replies based on their titles and content
  3057.     */
  3058.     public function getForumSearchResults($search ""int $limit 10int $page 1)
  3059.     {
  3060.         $site_code $this->getSiteCode();
  3061.         $qb $this->entityManager->createQueryBuilder();
  3062.         
  3063.         $qb->select('c')
  3064.             ->from("App\Entity\Content""c");
  3065.         if($site_code) {
  3066.             $qb->join("c.site""s""WITH""s.id = {$site_code}");
  3067.         }
  3068.         $qb->where("c.type = 10 OR c.type = 11")
  3069.             ->andWhere("c.status = 1");
  3070.         
  3071.         $results $qb->andWhere("c.content_full LIKE '%{$search}%' OR c.title LIKE '%{$search}%'")
  3072.             ->setMaxResults($limit)
  3073.             ->setFirstResult($limit * ($page 1))
  3074.             ->addOrderBy("c.published_at""DESC")
  3075.             ->getQuery()->getResult();
  3076.             
  3077.         return $results;
  3078.     }
  3079.     //TODO: test
  3080.     public function countForumSearchResults($search "")
  3081.     {
  3082.         $site_code $this->getSiteCode();
  3083.         
  3084.         $query implode(" ", array (
  3085.                 "SELECT COUNT(DISTINCT content)",
  3086.                 "FROM",
  3087.                     "App\Entity\Content content",
  3088.                     
  3089.                 ($site_code implode(" ", array (
  3090.                 "JOIN",
  3091.                     "content.site site",
  3092.                  )) : ""),
  3093.                  
  3094.                 "WHERE",
  3095.                     "(content.type = 10 OR content.type = 11)",
  3096.                     "AND content.status = 1",
  3097.                     ($site_code implode(" ", array (
  3098.                         "AND site.id = {$site_code}",
  3099.                     )) : ""),
  3100.                     "AND (content.content_full LIKE '%{$search}%' OR content.title LIKE '%{$search}%')",
  3101.             ));
  3102.         
  3103.         $count $this->entityManager
  3104.             ->createQuery($query)
  3105.         ->getResult();
  3106.         
  3107.         if (!empty($count)) {
  3108.             return (int) $count[0][1];
  3109.         }
  3110.         
  3111.         return 0;
  3112.     }
  3113.     
  3114.     public function getSidebars(string $slug)
  3115.     {
  3116.         $sidebars = array ();
  3117.         
  3118.         // ...
  3119.         
  3120.         if (strpos($slug"category/") !== false) {
  3121.         
  3122.             // ... include category sidebar modules
  3123.                         
  3124.         }
  3125.         
  3126.         if (strpos($slug".com/") == false) {
  3127.             
  3128.             $sidebars[] =  array(
  3129.                 "type" => "signup",
  3130.                 "title" => "Sign up for our e-news!"
  3131.             );
  3132.             
  3133.             $sidebars[] =  array(
  3134.                 "type" => "latest_classifieds",
  3135.                 "title" => "Latest Classifieds",
  3136.                 "classifieds" => $this->getLatestContent(3)
  3137.             );
  3138.                         
  3139.         }
  3140.         
  3141.         
  3142.         
  3143.         return $sidebars;
  3144.     }
  3145.     
  3146.     /* Original code from https://gist.github.com/chrisknepper/cba89754a222695c37e9
  3147.     Call this with the shown parameters (make sure $time and $end are integers and in Unix timestamp format!)
  3148.     Get a link that will open a new event in Google Calendar with those details pre-filled */
  3149.     public function make_google_calendar_link($name$begin$end$location$details) {
  3150.         $params = array('&dates=''/''&location=''&details=''&sf=true&output=xml');
  3151.         $url 'https://www.google.com/calendar/render?action=TEMPLATE&text=';
  3152.         $arg_list func_get_args();
  3153.         for ($i 0$i count($arg_list); $i++) {
  3154.             $current $arg_list[$i];
  3155.             if(is_int($current)) {
  3156.                 $t = new DateTime('@' $current, new DateTimeZone('UTC'));
  3157.                 $current $t->format('Ymd\THis\Z');
  3158.                 unset($t);
  3159.             }
  3160.             else {
  3161.                 $current urlencode($current);
  3162.             }
  3163.             $url .= (string) $current $params[$i];
  3164.         }
  3165.         return $url;
  3166.     }
  3167.     
  3168.     public function make_ics_file($location$description$dtstart$dtend$summary$url) {
  3169.         $ics = new ICS(array(
  3170.           'location' => $location,
  3171.           'description' => $description,
  3172.           'dtstart' => $dtstart,
  3173.           'dtend' => $dtend,
  3174.           'summary' => $summary,
  3175.           'url' => $url
  3176.         ));
  3177.         return $ics->to_string();
  3178.     }
  3179.     
  3180.     /**
  3181.      * 
  3182.      */
  3183.     public function getContent (
  3184.         $type "*",
  3185.         $category "*",
  3186.         $order "published",
  3187.         $dir "desc",
  3188.         $page 0,
  3189.         $limit 0,
  3190.         $allowDupes false,
  3191.         $taxonomy "*",
  3192.         $use_secondary_categories false//need to explicitly require searching by secondary categories, otherwise just searches by the primary (for optimization)
  3193.         $members_only false// not currently used
  3194.         $require_featured_image false,
  3195.         $only_featured false,
  3196.         $customer_id "",
  3197.         $customer_level_or_higher "",
  3198.         $only_announcements false,
  3199.         $excludeInDupes false//Don't include content in the dupes / 'used_ids' variable. Used for the mobile sidebar (which is at top of page) so that other modules with no duplicates allowed still show this content.
  3200.         $site_override "",
  3201.         $clear_dupes_after_get false
  3202.         //$test = false
  3203.     ) {
  3204.         //$sarray = __METHOD__ . serialize(func_get_args());
  3205.         /*if($test){
  3206.             $cache = new FilesystemAdapter();
  3207.             $test_cache = $cache->getItem("test_cache");
  3208.             if($test_cache->isHit()) {
  3209.                 $results = $test_cache->get();
  3210.                 return $results;
  3211.             }
  3212.         }*/
  3213.         
  3214.         $site_code $this->getSiteCode();
  3215.         if($site_override) {
  3216.             if($site_override == "all") {
  3217.                 $site_code null;
  3218.             }
  3219.             else {
  3220.                 $site_code $site_override;
  3221.             }
  3222.         }
  3223.         
  3224.         $order = ($order == "") ? "published" $order;
  3225.         
  3226.         $user $this->security->getUser();
  3227.         $isMember false;
  3228.         if($user && ($user->isMember() || $user->isAdmin()) ) {
  3229.             //also show members only posts
  3230.             $isMember true;
  3231.         }
  3232.         
  3233.         // prevent showing the same content from different modules
  3234.         static $used_ids;
  3235.         if (empty($used_ids)) {
  3236.             $used_content = array ();
  3237.         }
  3238.         
  3239.         $useCatIds true;
  3240.         $useTaxIds true;
  3241.         
  3242.         // for events
  3243.         $today date("Y-m-d H:i:s");
  3244.         if($type == Content::EVENT) {
  3245.             $order "events";
  3246.             $dir "asc";
  3247.         }
  3248.         
  3249.         // Move to repository class
  3250.         $typenum $type;
  3251.         if ($type == "*") {
  3252.             $type "> 0";
  3253.         } 
  3254.         elseif($type == Content::EVENT) {
  3255.             $type "IN (7, 8)"//Event or Webinar
  3256.         }
  3257.         else {
  3258.             $type = (int) $type;
  3259.             $type "= {$type}";
  3260.         }
  3261.         
  3262.         if ($category != "*") {
  3263.         
  3264.             if (strpos($category",") !== false) {
  3265.                 $category explode(","$category);
  3266.             }
  3267.             else if (!is_array($category)) {
  3268.                 $category = array($category);
  3269.             }
  3270.             foreach($category as $key => $c) {
  3271.                 if(!$this->categoryHelper->getCategoryExistsBySlug($c)) {
  3272.                     unset($category[$key]);
  3273.                 }
  3274.             }
  3275.             if(count($category) == 0) {
  3276.                 return $results = [
  3277.                     "total" => 0,
  3278.                     "content" => [],
  3279.                 ];
  3280.             }
  3281.             
  3282.             $categories $this->categoryHelper->getChildCategoriesBySlugs($category);
  3283.             $category = [];
  3284.             foreach($categories as $c) {
  3285.                 $category[] = $c;
  3286.                 //$category[] = $c->getSlug(); //4/17
  3287.             }
  3288.             
  3289.             $useCatIds true;
  3290.             $categorySQL = array ();
  3291.             foreach ($category as $cat) {
  3292.                 if ($useCatIds && !is_numeric($cat)) {
  3293.                     $useCatIds false;
  3294.                 }
  3295.                 $categorySQL[] = ":cat" . (count($categorySQL) + 1);
  3296.             }
  3297.         }
  3298.         
  3299.         if ($taxonomy != "*") {
  3300.             
  3301.             if (strpos($taxonomy",") !== false) {
  3302.                 $taxonomy explode(","$taxonomy);
  3303.             }
  3304.             
  3305.             else if (!is_array($taxonomy)) {
  3306.                 $taxonomy = array($taxonomy);
  3307.             }
  3308.             
  3309.             $useTaxIds true;
  3310.             $taxonomySQL = array ();
  3311.             foreach ($taxonomy as $tax) {
  3312.                 if (!$useTaxIds && !is_numeric($tax)) {
  3313.                     $useTaxIds false;
  3314.                 }
  3315.                 $taxonomySQL[] = ":tax" . (count($taxonomySQL) + 1);
  3316.             }
  3317.             
  3318.         }
  3319.         
  3320.         $dir = (strtolower($dir) == "asc") ? "ASC" "DESC";
  3321.         
  3322.         $order_by "";
  3323.         
  3324.         // types of sorting 
  3325.         switch (strtolower($order)) {
  3326.             case "created":
  3327.                 $order_by "content.created_at {$dir}";
  3328.                 break;
  3329.             case "published":
  3330.                 $order_by "content.published_at {$dir}";
  3331.                 break;
  3332.             case "expires":
  3333.                 $order_by "content.expires_at {$dir}";
  3334.                 break;
  3335.             case "title":
  3336.                 $order_by "content.title {$dir}";
  3337.                 break;
  3338.             case "random":
  3339.                 $order_by "random";
  3340.                 break;
  3341.             case "events":
  3342.                 $order_by "contentmeta1.metavalue {$dir}";
  3343.                 break;
  3344.         }
  3345.         
  3346.         $sql implode(" ", array (
  3347.         
  3348.             "SELECT DISTINCT",
  3349.                 "content",
  3350.             
  3351.             "FROM",
  3352.                 "App\Entity\Content content",
  3353.             
  3354.             // if we are filtering by categories
  3355.             ($use_secondary_categories || is_array($taxonomy) ? implode(" ", array (
  3356.             "JOIN",
  3357.                 "content.secondary_categories category1",
  3358.             
  3359.             /* 4/17
  3360.             "LEFT JOIN",
  3361.                 "category1.prnt category2",
  3362.                 
  3363.             "LEFT JOIN",
  3364.                 "category2.prnt category3",
  3365.             */
  3366.             )) : ""),
  3367.             
  3368.             // for directory pages
  3369.             ($typenum == Content::DIRECTORY implode(" ", array (
  3370.             "JOIN",
  3371.                 "content.customers customers",
  3372.       )) : ""),
  3373.              
  3374.       ($typenum != Content::DIRECTORY implode(" ", array (
  3375.      "LEFT JOIN",
  3376.        "content.customers customers",
  3377.       )) : ""),
  3378.        
  3379.              // for events pages
  3380.             //($typenum == Content::EVENT ? implode(" ", array (
  3381.             ($order == "events" implode(" ", array (
  3382.             "JOIN",
  3383.                 "content.contentmeta contentmeta1",
  3384.              )) : ""),
  3385.                 
  3386.             //($typenum == Content::EVENT ? implode(" ", array (
  3387.             ($order == "events" implode(" ", array (
  3388.             "JOIN",
  3389.                 "content.contentmeta contentmeta2",
  3390.              )) : ""),
  3391.              
  3392.             //($typenum == Content::EVENT ? implode(" ", array (
  3393.             ($order == "events"implode(" ", array (
  3394.             "LEFT JOIN",
  3395.                 "content.contentmeta contentmetaisevent",
  3396.              )) : ""),
  3397.              
  3398.              ($site_code implode(" ", array (
  3399.                 "JOIN",
  3400.                     "content.site site",
  3401.             )) : ""),
  3402.             
  3403.             "WHERE",
  3404.                 "content.type {$type} AND",
  3405.                 "content.status = 1",
  3406.                 (!empty($used_ids) && !$allowDupes implode(" ", array (
  3407.                     "AND content.id NOT IN (",
  3408.                         implode(", "$used_ids),
  3409.                     ")",
  3410.                 )) : ""),
  3411.                 
  3412.                 ($site_code implode(" ", array (
  3413.                     "AND site.id = {$site_code}",
  3414.                 )) : ""),
  3415.                 
  3416.                 /*
  3417.                 ($members_only ? implode(" ", array (
  3418.                     "AND content.members_only = 1",
  3419.                 )) : ""),
  3420.                 */
  3421.                 ($require_featured_image implode(" ", array (
  3422.                     "AND content.media != ''",
  3423.                 )) : ""),
  3424.                 
  3425.                 (!$isMember implode(" ", array (
  3426.                     "AND content.members_only = 0",
  3427.                 )) : ""),
  3428.                 
  3429.                 ($customer_id implode(" ", array (
  3430.                     "AND customers.id IN ({$customer_id})",
  3431.                 )) : ""),
  3432.                 
  3433.                 /*
  3434.                 ($customer_id ? implode(" ", array (
  3435.                     "AND customers.id = {$customer_id}",
  3436.                 )) : ""),
  3437.                 */
  3438.                 ($customer_level_or_higher implode(" ", array (
  3439.                     "AND customers.customer_level <= {$customer_level_or_higher}",
  3440.                 )) : ""),
  3441.                 
  3442.                 ($only_featured implode(" ", array (
  3443.                     "AND content.featured = 1",
  3444.                 )) : ""),
  3445.                 
  3446.                 (($only_announcements && $site_code) ? implode(" ", array (
  3447.                     "AND content.announcement_sites LIKE '%{$site_code}%'",
  3448.                 )) : ""),
  3449.                 
  3450.                 //Don't include if excluded from list views
  3451.                 "AND content.exclude_from_list = 0",
  3452.                 
  3453.                 // sort by start date, and filter out past events
  3454.                 //($typenum == Content::EVENT ? implode(" ", array (
  3455.                 ($order == "events" implode(" ", array (
  3456.                     //"AND content.expires_at > '{$today}'"
  3457.                     "AND contentmeta1.metakey = '_EventStartDate'",
  3458.                     "AND contentmeta1.metavalue != ''",
  3459.                     
  3460.                     "AND (",
  3461.                         "contentmeta2.metakey = '_EventEndDate' AND",
  3462.                         "contentmeta2.metavalue > '{$today}'",
  3463.                     ") ",
  3464.                     
  3465.                     "AND (",
  3466.                         "content.type = 7 OR ",
  3467.                         "(contentmetaisevent.metakey = '_IsEvent' AND contentmetaisevent.metavalue = 'yes')",
  3468.                     ") ",
  3469.                     
  3470.                     
  3471.                  )) : ""),
  3472.                  
  3473.                  // filter out directory influencers
  3474.                 ($typenum == Content::DIRECTORY implode(" ", array (
  3475.                     "AND content.title NOT LIKE '%RCS Influencer%'",
  3476.                     "AND content.title NOT LIKE '%PHD, Author%'",
  3477.                  )) : ""),
  3478.                 
  3479.                 // if filtering by categories
  3480.                 (is_array($category) ? implode(" "
  3481.                     ($useCatIds ?
  3482.                         array (
  3483.                             "AND (",
  3484.                                 "category1.id IN (",
  3485.                                     implode(", "$categorySQL),
  3486.                                 ")",
  3487.                                 /*4/17, same was below
  3488.                                 "OR",
  3489.                                 "category2.id IN (",
  3490.                                     implode(", ", $categorySQL),
  3491.                                 ") OR",
  3492.                                 "category3.id IN (",
  3493.                                     implode(", ", $categorySQL),
  3494.                                 ")",
  3495.                                 */
  3496.                             ")",
  3497.                         ) :
  3498.                         
  3499.                         
  3500.                         ($use_secondary_categories || is_array($taxonomy) ?
  3501.                         
  3502.                             array (
  3503.                                 "AND (",
  3504.                                     "category1 IN (",
  3505.                                         implode(", "$categorySQL),
  3506.                                     ")",
  3507.                                 ")",
  3508.                             ) :
  3509.                             
  3510.                             array (
  3511.                                 "AND (",
  3512.                                     "content.category IN (",
  3513.                                         implode(", "$categorySQL),
  3514.                                     ")",
  3515.                                 ")",
  3516.                             )
  3517.                         )
  3518.                         
  3519.                         
  3520.                     )
  3521.                 ) : "" ),
  3522.                 
  3523.                 // if filtering by taxonomies
  3524.                 (is_array($taxonomy) ? implode(" ",
  3525.                     // ($useTaxIds ? <- taxonomy does use ids
  3526.                     array (
  3527.                         "AND (",
  3528.                             "category1.taxonomy IN (",
  3529.                                 implode(", "$taxonomySQL),
  3530.                             ")",
  3531.                             /*4/17
  3532.                             "OR",
  3533.                             "category2.taxonomy IN (",
  3534.                                 implode(", ", $taxonomySQL),
  3535.                             ")",
  3536.                             "OR",
  3537.                             "category3.taxonomy IN (",
  3538.                                 implode(", ", $taxonomySQL),
  3539.                             ")",
  3540.                             */
  3541.                         ")",
  3542.                     ) 
  3543.                 ) : "" ),
  3544.                 
  3545.             "ORDER BY",
  3546.             
  3547.             (($order_by != "" && $order_by != "random") ?
  3548.                 $order_by :
  3549.                 "content.published_at DESC"
  3550.             ),
  3551.             
  3552.         ));
  3553.         
  3554.                 
  3555.         if ($order_by == "random") {
  3556.             // first grab all the id's from content that match the query
  3557.             
  3558.             $sql str_replace("SELECT DISTINCT content""SELECT DISTINCT (content.id)"$sql);
  3559.             
  3560.             $query $this->entityManager->createQuery($sql);
  3561.             if (is_array($category)) {
  3562.                 $pos 1;
  3563.                 foreach ($category as $cat) {
  3564.                     $query->setParameter("cat{$pos}"$cat);
  3565.                     $pos++;
  3566.                 }
  3567.             }
  3568.             
  3569.             if (is_array($taxonomy)) {
  3570.                 $pos 1;
  3571.                 foreach ($taxonomy as $tax) {
  3572.                     $query->setParameter("tax{$pos}"$tax);
  3573.                     $pos++;
  3574.                 }
  3575.             }
  3576.             
  3577.             $content $query->getResult();
  3578.             shuffle($content);
  3579.             
  3580.             $temp = array ();
  3581.             for ($i 0$i count($content); $i++) {
  3582.                 $temp[] = $content[$i][1];
  3583.             }
  3584.             $content $temp;
  3585.             if ($limit) {
  3586.                 $content array_slice($content0$limit);
  3587.             }
  3588.             
  3589.             $sql str_replace("SELECT DISTINCT (content.id)""SELECT DISTINCT content"$sql);
  3590.             
  3591.             if (count($content)) {
  3592.                 $sql str_replace("WHERE""WHERE content.id IN ("implode(","$content) . ") AND"$sql);
  3593.             }
  3594.         }
  3595.         
  3596.         $query $this->entityManager->createQuery($sql);
  3597.         
  3598.         // add the parameters
  3599.         if (is_array($category)) {
  3600.             $pos 1;
  3601.             foreach ($category as $cat) {
  3602.                 $query->setParameter("cat{$pos}"$cat);
  3603.                 $pos++;
  3604.             }
  3605.         }
  3606.         
  3607.         if (is_array($taxonomy)) {
  3608.             $pos 1;
  3609.             foreach ($taxonomy as $tax) {
  3610.                 $query->setParameter("tax{$pos}"$tax);
  3611.                 $pos++;
  3612.             }
  3613.         }
  3614.         
  3615.         // pagination
  3616.         if ($limit) {
  3617.             $offset = ($page $limit);
  3618.             $query
  3619.                 ->setFirstResult($offset)
  3620.                 ->setMaxResults($limit);
  3621.         }
  3622.         $content $query->getResult();
  3623.         
  3624.         /* works but won't use the full result set ...
  3625.         if (strtolower($order) == "random") {
  3626.             shuffle($content);
  3627.         }
  3628.         */
  3629.         
  3630.         // store the ids
  3631.         if(!$excludeInDupes) {
  3632.             for ($i 0$i count($content); $i++) {
  3633.                 $used_ids[] = $content[$i]->getId();
  3634.             }
  3635.         }
  3636.         
  3637.         // count the totals
  3638.         $sql str_replace("SELECT DISTINCT content""SELECT COUNT(DISTINCT content.id)"$sql);
  3639.         
  3640.         $query $this->entityManager
  3641.             ->createQuery($sql);
  3642.         
  3643.         // add the parameters
  3644.         if (is_array($category)) {
  3645.             $pos 1;
  3646.             foreach ($category as $cat) {
  3647.                 $query->setParameter("cat{$pos}"$cat);
  3648.                         $pos++;
  3649.             }
  3650.         }
  3651.         
  3652.         if (is_array($taxonomy)) {
  3653.             $pos 1;
  3654.             foreach ($taxonomy as $tax) {
  3655.                 $query->setParameter("tax{$pos}"$tax);
  3656.                         $pos++;
  3657.             }
  3658.         }
  3659.         
  3660.         $total $query->getResult();
  3661.         
  3662.         if ($order_by == "random") {
  3663.             shuffle($content);
  3664.         }
  3665.         
  3666.         $results = [
  3667.             "total" => $total[0][1],
  3668.             "content" => $content,
  3669.         ];
  3670.         /*if($test){
  3671.             $test_cache->set($results);
  3672.             $test_cache->expiresAfter(120);
  3673.             $cache->save($test_cache);
  3674.         }*/    
  3675.         
  3676.         // clear dupes (not generally used)
  3677.         if($clear_dupes_after_get) {
  3678.             $used_ids = [];
  3679.             $used_content = [];
  3680.         }
  3681.         return $results;
  3682.     }
  3683.     
  3684.     //Used to check if something posted to the forum should be considered spam
  3685.     //Currently just checks for Chinese and Korean characters
  3686.     public function isSpam($string) {
  3687.         
  3688.         //Checks for Chinese characters
  3689.         if( preg_match("/\p{Han}+/u"$string) ) {
  3690.             return true;
  3691.         }
  3692.         
  3693.         //Checks for Korean characters
  3694.         if( preg_match('/[\x{3130}-\x{318F}\x{AC00}-\x{D7AF}]/u'$string) ) {
  3695.             return true;
  3696.         }
  3697.         
  3698.         //Checks for Cyrillic letters
  3699.         if( preg_match("/\p{Cyrillic}+/u"$string) ) {
  3700.             return true;
  3701.         }
  3702.         
  3703.         //Checks for Arabic letters
  3704.         if( preg_match("/\p{Arabic}+/u"$string) ) {
  3705.             return true;
  3706.         }
  3707.         
  3708.         //Block .onion links
  3709.         if (strpos($string".onion") !== false) {
  3710.             return true;
  3711.         }
  3712.         
  3713.         //Block certain URLs / strings (Should we make something like the "domain blacklist" where these can be entered by admins?)
  3714.         if (strpos($string"digitalvertex.com") !== false ||
  3715.             strpos($string"mailbanger.com") !== false ||
  3716.             strpos($string"webolowitz.com") !== false ||
  3717.             strpos($string"thissucks.com") !== false ||
  3718.             strpos($string"jumboleadmagnet.com") !== false
  3719.         ) {
  3720.             return true;
  3721.         }
  3722.         // Block Specific email address (Should we make something like the "domain blacklist" where these can be entered by admins?)
  3723.         if (strpos($string"millionsgroup4@gmail.com") !== false) {
  3724.             return true;
  3725.         }
  3726.         return false;
  3727.     }
  3728.     
  3729.     public function getTopicsByReplyDate ()
  3730.     {
  3731.         $sql implode(" ", array (
  3732.             "SELECT DISTINCT",
  3733.                 "topic",
  3734.             "FROM",
  3735.                 "App\Entity\Content topic",
  3736.             "LEFT JOIN",
  3737.                 "topic.children reply",
  3738.             "WHERE",
  3739.                 "topic.type = 10 AND",
  3740.                 "topic.status = 1",
  3741.             "ORDER BY",
  3742.                 "reply.published_at DESC,",
  3743.                 "topic.published_at DESC",
  3744.         ));
  3745.         
  3746.         $query $this->entityManager
  3747.             ->createQuery($sql);
  3748.             
  3749.         return $query->getResult();
  3750.     }
  3751.     
  3752.     public function search (
  3753.         $search "",
  3754.         $page 1
  3755.     ) {
  3756.         $site_code $this->getSiteCode() ?? 1;
  3757.         
  3758.         $limit self::DEFAULT_LIMIT;
  3759.         $offset = ($limit * ($page 1));
  3760.         
  3761.         $sql implode(" ", [
  3762.             "SELECT",
  3763.                 "COUNT(DISTINCT content.id)",
  3764.             "FROM",
  3765.                 "App\Entity\Content content",
  3766.             "JOIN",
  3767.                 "content.site site",
  3768.             "LEFT JOIN",
  3769.                 "content.directory customer",
  3770.             "LEFT JOIN",
  3771.                 "customer.levels levels",
  3772.             "WHERE",
  3773.                 "content.status = 1 AND",
  3774.                 "site.id = {$site_code} AND",
  3775.                 "content.searchable = 1 AND",
  3776.                 "content.type != 6 AND",
  3777.                 "content.type != 9 AND",
  3778.                 "content.type != 10 AND",
  3779.                 "content.type != 11 AND",
  3780.                 "content.type != 12 AND",
  3781.                 "content.type != 13 AND",
  3782.                 "content.type != 16 AND",
  3783.                 "content.type != 17 AND",
  3784.                 "content.type != 21 AND",
  3785.                 "content.type != 22 AND",
  3786.                 "content.type != 23 AND",
  3787.                 "content.type != 24 AND",
  3788.                 "content.type != 27 AND",
  3789.                 "(",
  3790.                     "content.title LIKE :search OR",
  3791.                     "content.content_full LIKE :search OR",
  3792.                     "content.content_builder LIKE :search",
  3793.                 ")",
  3794.                 " AND (levels.site_id = {$site_code} OR levels.site_id IS NULL)",
  3795.             "ORDER BY",
  3796.                 "CASE WHEN :searchExact LIKE :gpString AND content.id = 109945 THEN 1 ELSE 2 END,"//exception for Georgia-Pacific directory (ticket #159817)
  3797.                 "CASE WHEN content.title LIKE :search THEN 1 ELSE 2 END,",
  3798.                 "CASE WHEN content.type = 4 THEN 1 ELSE 2 END,",
  3799.                 "CASE WHEN content.type = 4 AND customer.id IS NOT NULL AND levels.level IS NULL THEN customer.customer_level
  3800.                     WHEN content.type = 4 AND customer.id IS NOT NULL AND levels.level IS NOT NULL THEN levels.level
  3801.                         ELSE 10000 END,",
  3802.                 "content.published_at DESC",
  3803.         ]);
  3804.         
  3805.         $query $this->entityManager
  3806.             ->createQuery($sql)
  3807.             ->setParameter("search""%{$search}%")
  3808.             ->setParameter("searchExact""$search")
  3809.             ->setParameter("gpString""gp");
  3810.             
  3811.         $count $query->getResult();
  3812.         $count $count[0][1];
  3813.         
  3814.         $sql str_replace("COUNT(DISTINCT content.id)""DISTINCT content"$sql);
  3815.         $query $this->entityManager
  3816.             ->createQuery($sql)
  3817.             ->setParameter("search""%{$search}%")
  3818.             ->setParameter("searchExact""$search")
  3819.             ->setParameter("gpString""gp")
  3820.             ->setFirstResult($offset)
  3821.             ->setMaxResults($limit);
  3822.             
  3823.         $content $query->getResult();
  3824.         
  3825.         return [
  3826.             "count" => $count,
  3827.             "content" => $content,
  3828.         ];
  3829.     }
  3830.     
  3831.     public function countImpressions ($content)
  3832.     {
  3833.         $sql implode(" ", array (
  3834.             "SELECT",
  3835.                 "COUNT(t)",
  3836.             "FROM",
  3837.                 "App\Entity\Traffic t",
  3838.             "WHERE",
  3839.                 "t.user_this_path = :url",
  3840.         ));
  3841.         
  3842.         $query $this->entityManager
  3843.             ->createQuery($sql)
  3844.             ->setParameter("url"$content->getURL());
  3845.         
  3846.         $count $query->getResult();
  3847.         $count $count[0][1];
  3848.         return $count;
  3849.     }
  3850.     
  3851.     /*public function enqueueImpression($request, $content="")
  3852.     {
  3853.         $this->impressionQueue(false, $request, $content);
  3854.     }
  3855.     
  3856.     public function storeQueuedImpressions()
  3857.     {
  3858.         $this->impressionQueue(true);
  3859.     }
  3860.     
  3861.     public function impressionQueue($store=false, $request="", $content="")
  3862.     {
  3863.         static $stored_request;
  3864.         static $content_ids;
  3865.         
  3866.         if(empty($stored_request)) {
  3867.             $stored_request = $request;
  3868.         }
  3869.         if(empty($content_ids)) {
  3870.             $content_ids = [];
  3871.         }
  3872.         if($content){
  3873.             $content_ids[] = $content->getId();
  3874.         }
  3875.         if($store && $stored_request && !empty($content_ids)) {
  3876.             $all_content = [];
  3877.             foreach($content_ids as $content_id) {
  3878.                 $all_content[] = $this->getContentById($content_id);
  3879.             }
  3880.             $this->addImpressions($stored_request, $all_content);
  3881.             $content_ids = [];
  3882.         }
  3883.     }*/
  3884.     
  3885.     
  3886.     public function enqueueImpression($request$content="")
  3887.     {
  3888.         $this->impressionQueue(false$request$content);
  3889.     }
  3890.     
  3891.     public function enqueueImpressions($request$contents="")
  3892.     {
  3893.         foreach($contents as $content) {
  3894.             $this->impressionQueue(false$request$content);
  3895.         }
  3896.     }
  3897.     
  3898.     public function storeQueuedImpressions($request)
  3899.     {
  3900.         $this->impressionQueue(true$request);
  3901.     }
  3902.     
  3903.     public function impressionQueue($store=false$request=""$content="")
  3904.     {
  3905.         if(!$request) {
  3906.             return false;
  3907.         }
  3908.         $session $request->getSession();
  3909.         
  3910.         //$stored_request = $session->get("stored_request", "");
  3911.         $content_ids $session->get("queued_content_ids", []);
  3912.         if($content_ids && count($content_ids) > 1000) {
  3913.             $session->remove("queued_content_ids");
  3914.             $content_ids = [];
  3915.         }
  3916.         
  3917.         if($content){
  3918.             $content_ids[] = $content->getId();
  3919.         }
  3920.         if($store && $request && !empty($content_ids)) {
  3921.             $session->remove("queued_content_ids");
  3922.             $all_content = [];
  3923.             foreach($content_ids as $content_id) {
  3924.                 $current_content $this->entityManager
  3925.                     ->getRepository(Content::class)
  3926.                     ->findOneBy([
  3927.                         "id" => $content_id
  3928.                     ]);
  3929.                 if($current_content) {
  3930.                     $all_content[] = $current_content;
  3931.                 }
  3932.             }
  3933.             $this->addImpressions($request$all_content);
  3934.         }
  3935.         else {
  3936.             $session->set("queued_content_ids"$content_ids);
  3937.         }
  3938.     }
  3939.     
  3940.     
  3941.     public function addImpressions($request$contents)
  3942.     {
  3943.         foreach($contents as $content) {
  3944.             $this->addImpression($request$content);
  3945.         }
  3946.     }
  3947.     
  3948.     public function addImpression($request$content=""$link=""$item=""$type="0"$href="")
  3949.     {
  3950.         try {
  3951.             //$request = $event->getRequest();
  3952.             $session $request->getSession();
  3953.             
  3954.             $site_code $this->getSiteCode();
  3955.             
  3956.             $routeName $request->get("_route");
  3957.             $routeParams $request->get("_route_params");
  3958.             $path $request->getPathInfo();
  3959.             
  3960.             if ($this->tokenStorage->getToken()) {
  3961.                 $user $this->tokenStorage->getToken()->getUser();
  3962.             } else {
  3963.                 $user null;
  3964.             }
  3965.             
  3966.             if (is_string($user)) {
  3967.                 $user null;
  3968.             }
  3969.             
  3970.             // ==================================================
  3971.             // Log the traffic request
  3972.             // $temp = $request;
  3973.             $temp $_REQUEST;
  3974.                     
  3975.             // remove any private payment data ...
  3976.             if (isset($temp["payment"])) {
  3977.                 unset($temp["payment"]);
  3978.             }
  3979.             
  3980.             $last_route $session->get("last_route", [
  3981.                 "name" => "",
  3982.                 "params" => "",
  3983.                 "path" => "",
  3984.             ]);
  3985.             $user_token $session->get("user_token"bin2hex(openssl_random_pseudo_bytes(32)));
  3986.             $user_ip $request->getClientIp();
  3987.             $user_ua $request->headers->get("User-Agent");
  3988.             $user_refer = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "";
  3989.             $user_this_path $path;
  3990.             $user_this_route = isset($routeName) ? $routeName "";
  3991.             $user_this_route_params = isset($routeParams) ? json_encode($routeParams) : "";
  3992.             $user_last_path = isset($last_route["path"]) ? $last_route["path"] : "";
  3993.             $user_last_route = isset($last_route["name"]) ? $last_route["name"] : "";
  3994.             $user_last_route_params = isset($last_route["params"]) ? json_encode($last_route["params"]) : "";
  3995.             $user_request_data json_encode($temp);
  3996.             $requested_at = new DateTime("now");
  3997.             
  3998.             $hash md5(implode("", [
  3999.                 $user_token,
  4000.                 $user_ip,
  4001.                 $user_ua,
  4002.                 ($user) ? $user->getId() : "",
  4003.                 $user_refer,
  4004.                 $user_this_path,
  4005.                 $user_this_route,
  4006.                 $user_this_route_params,
  4007.                 $user_last_path,
  4008.                 $user_last_route,
  4009.                 $user_last_route_params,
  4010.                 $user_request_data,
  4011.                 date("Y-m-d H:i:s"),
  4012.                 ($content) ? $content->getId() : "",
  4013.                 ($link) ? $link->getId() : "",
  4014.                 ($item) ? $item->getId() : "",
  4015.                 rand(1,1000),
  4016.             ]));
  4017.             
  4018.             $traffic_data = new TrafficData();
  4019.             $traffic_data->setHash($hash);
  4020.             $traffic_data->setUserToken($user_token);
  4021.             $traffic_data->setUser($user);
  4022.             $traffic_data->setUserIp($user_ip);
  4023.             $traffic_data->setUserUa($user_ua);
  4024.             $traffic_data->setUserRefer($user_refer);
  4025.             $traffic_data->setUserThisPath($user_this_path);
  4026.             $traffic_data->setUserThisRoute($user_this_route);
  4027.             $traffic_data->setUserThisRouteParams($user_this_route_params);
  4028.             $traffic_data->setUserLastPath($user_last_path);
  4029.             $traffic_data->setUserLastRoute($user_last_route);
  4030.             $traffic_data->setUserLastRouteParams($user_last_route_params);
  4031.             $traffic_data->setUserRequestData($user_request_data);
  4032.             $traffic_data->setRequestedAt($requested_at);
  4033.             
  4034.             if($type == 3) {
  4035.                 if($content) {
  4036.                     $content_link_click = new ContentLinkClick();
  4037.                     $content_link_click->setTrafficData($traffic_data);
  4038.                     $content_link_click->setContent($content);
  4039.                     $content_link_click->setRequestedAt($requested_at);
  4040.                     $content_link_click->setSiteId($site_code);
  4041.                     $content_link_click->setLinkUrl($href);
  4042.                 }
  4043.             }
  4044.             else {
  4045.                 if($content) {
  4046.                     $content_impression = new ContentImpression();
  4047.                     $content_impression->setTrafficData($traffic_data);
  4048.                     $content_impression->setContent($content);
  4049.                     $content_impression->setRequestedAt($requested_at);
  4050.                     $content_impression->setSiteId($site_code);
  4051.                     
  4052.                     //$content->incrementTotalImpressions();
  4053.                 }
  4054.                 elseif($link) {
  4055.                     /*
  4056.                     $traffic->setType(Traffic::TYPE_LINK_CLICK);
  4057.                     $traffic->setLink($link);
  4058.                     */
  4059.                 }
  4060.                 elseif($item) {
  4061.                     if($type == 1) {
  4062.                         $ad_impression = new AdImpression();
  4063.                         $ad_impression->setTrafficData($traffic_data);
  4064.                         $ad_impression->setMediaGroupItem($item);
  4065.                         $ad_impression->setRequestedAt($requested_at);
  4066.                         $ad_impression->setSiteId($site_code);
  4067.                         
  4068.                         //$item->incrementTotalImpressions();
  4069.                     }
  4070.                     elseif($type == 2) {
  4071.                         $ad_click = new AdClick();
  4072.                         $ad_click->setTrafficData($traffic_data);
  4073.                         $ad_click->setMediaGroupItem($item);
  4074.                         $ad_click->setRequestedAt($requested_at);
  4075.                         $ad_click->setSiteId($site_code);
  4076.                         
  4077.                         //$item->incrementTotalClicks();
  4078.                     }
  4079.                 }
  4080.             }
  4081.             
  4082.             try{
  4083.                 /*
  4084.                 $this->entityManager->persist($traffic);
  4085.                 $this->entityManager->flush();
  4086.                 */
  4087.                 
  4088.                 if(($type == 3) && $content ) {
  4089.                     $this->entityManager->persist($content_link_click);
  4090.                     $this->entityManager->persist($traffic_data);
  4091.                     $this->entityManager->flush();
  4092.                 }
  4093.                 elseif($content) {
  4094.                     $this->entityManager->persist($content);
  4095.                     $this->entityManager->persist($content_impression);
  4096.                     $this->entityManager->persist($traffic_data);
  4097.                     $this->entityManager->flush();
  4098.                 }
  4099.                 elseif($item) {
  4100.                     $this->entityManager->persist($item);
  4101.                     $this->entityManager->persist($traffic_data);
  4102.                     if($type == 1) {
  4103.                         $this->entityManager->persist($ad_impression);
  4104.                     }
  4105.                     elseif($type == 2) {
  4106.                         $this->entityManager->persist($ad_click);
  4107.                     }
  4108.                     $this->entityManager->flush();
  4109.                 }
  4110.             } catch(\Exception $e) {
  4111.                 try{
  4112.                     //Exception may occur if inserting a duplicate primary key. Should log this somewhere.
  4113.                     $errorlog fopen(__DIR__ "/../../var/log/traffic_error.txt""a") or die("Unable to open file!");
  4114.                     $txt 'Caught exception (ContentHelper.php addImpression()): '.  $e->getMessage() . "\n";
  4115.                     fwrite($errorlog$txt);
  4116.                     fclose($errorlog);
  4117.                 } catch(\Exception $e) {
  4118.                     // Error writing to file
  4119.                 }
  4120.             }
  4121.             
  4122.             $session->set("user_token"$user_token);
  4123.         } catch(\Exception $e) {
  4124.             try{
  4125.                 $errorlog fopen(__DIR__ "/../../var/log/traffic_error.txt""a") or die("Unable to open file!");
  4126.                 $txt 'Caught exception (ContentHelper.php addImpression(), 1st loops before persisting): '.  $e->getMessage() . "\n";
  4127.                 fwrite($errorlog$txt);
  4128.                 fclose($errorlog);
  4129.             } catch(\Exception $e) {
  4130.                 // Error writing to file
  4131.             }
  4132.         }
  4133.     }
  4134.     
  4135.     public function likeContent($content$cuser)
  4136.     {
  4137.         $content->addContentMetumKV("_like_user_id"$cuser->getId());
  4138.     }
  4139.     
  4140.     public function unlikeContent($content$cuser)
  4141.     {
  4142.         $content->removeContentMetumKV("_like_user_id"$cuser->getId());
  4143.     }
  4144.     
  4145.     public function countLikes($content)
  4146.     {
  4147.         //return $content->countContentMetum("_like_user_id");
  4148.         return $content->countLikes();
  4149.     }
  4150.     
  4151.     //move this to its own helper
  4152.     public function getProductById ($id "")
  4153.     {
  4154.         $product $this->entityManager
  4155.             ->getRepository(Product::class)
  4156.             ->findOneBy([
  4157.                 "id" => $id
  4158.             ]);
  4159.             
  4160.         if ($product) {
  4161.             return $product;
  4162.         }
  4163.         
  4164.         throw new ResourceNotFoundException ("Product with id {$id} - Not Found");
  4165.     }
  4166.     public function getDirectoryConentSiteMap($slug "") {
  4167.         $directory $this->getDirectoryBySlug($slug);
  4168.         $customer $directory->getDirectory();
  4169.         $customerID $customer->getId();
  4170.         $qb $this->entityManager->createQueryBuilder();
  4171.         $qb->select('c')
  4172.             ->from('App\Entity\Content''c')
  4173.             ->join("c.customers""cu""WITH""cu.id = '$customerID'")
  4174.             ->andWhere('c.status = 1')
  4175.             ->orderBy('c.published_at''DESC');
  4176.         $content $qb->getQuery()->getResult();
  4177.         
  4178.         // split up by content types
  4179.         $typemap = [
  4180.             => "PAGE",
  4181.             => "POST",
  4182.             => "CLASSIFIED",
  4183.             => "GALLERY",
  4184.             => "LANDING_PAGE",
  4185.             => "EVENT",
  4186.             => "WEBINAR",
  4187.             => "FORUM",
  4188.             10 => "FORUM_TOPIC",
  4189.             11 => "FORUM_REPLY",
  4190.             12 => "MODULE",
  4191.             13 => "HTML_AD",
  4192.             14 => "PROMOS_REBATES",
  4193.             15 => "THE_HUB",
  4194.             16 => "EVENT_ORGANIZER",
  4195.             17 => "EVENT_VENUE",
  4196.             18 => "PODCAST",
  4197.             19 => "EBOOK",
  4198.             20 => "CONTEST_GAMES",
  4199.             21 => "ACF_FIELD_GROUP",
  4200.             22 => "ACF_FIELD",
  4201.             23 => "TRADE_ASSOCIATIONS",
  4202.             24 => "THANK_YOU_LANDING_PAGE",
  4203.             25 => "AWARD",
  4204.             26 => "VIDEO",
  4205.             27 => "R_CLUB_PERK",
  4206.             28 => "SCHOLARSHIP",
  4207.             999 => "BUY_ONLINE",
  4208.             998 => "SPANISH",
  4209.             997 => "INTERNATIONAL",
  4210.             996 => "CANADA"
  4211.         ];
  4212.         $types = [
  4213.             $PAGE = [], // type = 1;
  4214.             $POST = [], // type = 2;
  4215.             $CLASSIFIED = [], // type = 3;
  4216.             $GALLERY = [], // type = 5;
  4217.             $LANDING_PAGE = [], // type = 6;
  4218.             $EVENT = [], // type = 7;
  4219.             $WEBINAR = [], // type = 8;
  4220.             $FORUM = [], // type = 9;
  4221.             $FORUM_TOPIC = [], // type = 10;
  4222.             $FORUM_REPLY = [], // type = 11;
  4223.             $MODULE = [], // type = 12;
  4224.             $HTML_AD = [], // type = 13;
  4225.             $PROMOS_REBATES = [], // type = 14;
  4226.             $THE_HUB = [], // type = 15;
  4227.             $RLW = [], // type = 15;
  4228.             $EVENT_ORGANIZER = [], // type = 16;
  4229.             $EVENT_VENUE = [], // type = 17;
  4230.             $PODCAST = [], // type = 18;
  4231.             $EBOOK = [], // type = 19;
  4232.             $CONTEST_GAMES = [], // type = 20;
  4233.             $ACF_FIELD_GROUP = [], // type = 21;
  4234.             $ACF_FIELD = [], // type = 22;
  4235.             $TRADE_ASSOCIATIONS = [], // type = 23;
  4236.             $THANK_YOU_LANDING_PAGE = [], // type = 24;
  4237.             $AWARD = [], // type = 25;
  4238.             $VIDEO = [], // type = 26;
  4239.             $R_CLUB_PERK = [], // type = 27;
  4240.             $SCHOLARSHIP = [], // type = 28;
  4241.             $buyonline = [], // type = 999;
  4242.             $spanish = [], // type = 998;
  4243.             $international = [], // type = 997;
  4244.             $canada = [], // type = 996;
  4245.         ];
  4246.         // split content up by type
  4247.         foreach ($content as $con) {
  4248.             $content_type $con->getType();
  4249.             if (!array_key_exists($content_type$typemap)) {
  4250.                 continue;
  4251.             }
  4252.             $maptype $typemap[$content_type];            
  4253.             $types{$maptype}[] = $con;
  4254.         }
  4255.         // $types buy online
  4256.         $types["buyonline"] = $this->getContent(
  4257.             "*",
  4258.             "buy-online",
  4259.             "published",
  4260.             "desc",
  4261.             0,
  4262.             0,
  4263.             true,
  4264.             "*",
  4265.             true,
  4266.             false,
  4267.             false,
  4268.             false,
  4269.             $customerID
  4270.         );
  4271.         
  4272.         if ($types["buyonline"]["content"]){
  4273.             $types["buyonline"] = $types["buyonline"]['content'];
  4274.         }else{
  4275.             $types["buyonline"] = [];
  4276.         }
  4277.         $types["spanish"] = $this->getContent(
  4278.             "*",
  4279.             "en-espanol",
  4280.             "published",
  4281.             "desc",
  4282.             0,
  4283.             0,
  4284.             true,
  4285.             "*",
  4286.             true//secondary categories
  4287.             false,
  4288.             false,
  4289.             false,
  4290.             $customerID
  4291.         );
  4292.         if ($types["spanish"]["content"]){
  4293.             $types["spanish"] = $types["spanish"]['content'];
  4294.         }else{
  4295.             $types["spanish"] = [];
  4296.         }
  4297.         $types["international"] = $this->getContent(
  4298.             "*",
  4299.             "international",
  4300.             "published",
  4301.             "desc",
  4302.             0,
  4303.             0,
  4304.             true,
  4305.             "*",
  4306.             true//secondary categories
  4307.             false,
  4308.             false,
  4309.             false,
  4310.             $customerID
  4311.         );
  4312.         if ($types["international"]["content"]){
  4313.             $types["international"] = $types["international"]['content'];
  4314.         }else{
  4315.             $types["international"] = [];
  4316.         }
  4317.         $types["canada"] = $this->getContent(
  4318.             "*",
  4319.             "canada",
  4320.             "published",
  4321.             "desc",
  4322.             0,
  4323.             0,
  4324.             true,
  4325.             "*",
  4326.             true//secondary categories
  4327.             false,
  4328.             false,
  4329.             false,
  4330.             $customerID
  4331.         );
  4332.         if ($types["canada"]["content"]) {
  4333.             $types["canada"] = $types["canada"]['content'];
  4334.         }else{
  4335.             $types["canada"] = [];
  4336.         }
  4337.         $urls = [];
  4338.         // loop over types and count each sub arrawy
  4339.         foreach ($types as $key=>$tp){
  4340.             $chuncked array_chunk($tp10);
  4341.             $chunchedCount count($chuncked);
  4342.             // get type number from maptype
  4343.             $typenum array_search($key$typemap);
  4344.             // if typenum is less than 900
  4345.             if ($key != "buyonline" && $key != "spanish" && $key != "international" && $key != "canada") {            
  4346.                 if ($chunchedCount == 1) {
  4347.                     if ($typenum == 2){
  4348.                         $urls[] = "/directory/$slug/posts/";
  4349.                         $urls[] = "/directory/$slug/posts/1";
  4350.                     }
  4351.                     $urls[] = "/directory/$slug/content/$typenum";
  4352.                 } else {
  4353.                     for ($i 0$i $chunchedCount$i++) {
  4354.                         if ($i == 0) {
  4355.                             if ($typenum == 2){
  4356.                                 $urls[] = "/directory/$slug/posts/";
  4357.                                 $urls[] = "/directory/$slug/posts/1";
  4358.                             }
  4359.                             $urls[] = "/directory/$slug/content/$typenum";
  4360.                         }else{
  4361.                             if ($typenum == 2){
  4362.                                 $urls[] = "/directory/$slug/posts/" . ($i 1);
  4363.                             }
  4364.                             $urls[] = "/directory/$slug/content/$typenum/" . ($i 1);
  4365.                         }
  4366.                     }
  4367.                 }
  4368.             }else{
  4369.                 if ($key == "buyonline"){
  4370.                     $key "buy-online";
  4371.                 }
  4372.                 // $urls[] = "/directory/$slug/content/$key";
  4373.                 if ($chunchedCount == 1) {
  4374.                     $urls[] = "/directory/$slug/content/$key";
  4375.                 } else {
  4376.                     for ($i 0$i $chunchedCount$i++) {
  4377.                         if ($i == 0) {
  4378.                             $urls[] = "/directory/$slug/content/$key";
  4379.                         }else{
  4380.                             $urls[] = "/directory/$slug/content/$key/" . ($i 1);
  4381.                         }
  4382.                     }
  4383.                 }
  4384.                 
  4385.             }
  4386.             
  4387.         }
  4388.         return $urls;
  4389.     }
  4390.     
  4391. }