src/Controller/PageController.php line 70
<?phpnamespace App\Controller;use App\Entity\Article;use App\Entity\Category;use App\Entity\Form;use App\Entity\FormField;use App\Entity\Page;use App\Entity\Site;use App\Entity\SiteSection;use App\Service\SiteContentSvc;use Doctrine\Common\Collections\ArrayCollection;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\RequestStack;use Symfony\Component\HttpKernel\Exception\HttpException;use Twig\Environment;/*** Controller used to manage all page requests*/class PageController extends AbstractController {protected $request;protected $requestStack;protected ?string $projectDir = "";protected ?string $publicDir = "";protected ?string $templatesDir = "";protected ?string $route = "";protected ?array $params = [];protected ?EntityManagerInterface $em;protected ?Environment $twig;protected ?SiteContentSvc $contentSvc;protected ?Site $site;protected ?string $pronoun;protected ?string $timeframe;protected ?string $newsletterTopMsg;protected static array $states = ["AK" => "Alaska", "AL" => "Alabama", "AR" => "Arkansas", "AZ" => "Arizona", "CA" => "California", "CO" => "Colorado", "CT" => "Connecticut", "DC" => "D.C.", "DE" => "Delaware", "FL" => "Florida", "GA" => "Georgia", "HI" => "Hawaii", "IA" => "Iowa", "ID" => "Idaho", "IL" => "Illinois", "IN" => "Indiana", "KS" => "Kansas", "KY" => "Kentucky", "LA" => "Louisiana", "MA" => "Massachusetts", "MD" => "Maryland", "ME" => "Maine", "MI" => "Michigan", "MN" => "Minnesota", "MO" => "Missouri", "MS" => "Mississippi", "MT" => "Montana", "NC" => "N. Carolina", "ND" => "N. Dakota", "NE" => "Nebraska", "NH" => "New Hampshire", "NJ" => "New Jersey", "NM" => "New Mexico", "NV" => "Nevada", "NY" => "New York", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "PA" => "Pennsylvania", "PR" => "Puerto Rico", "RI" => "Rhode Island", "SC" => "S. Carolina", "SD" => "S. Dakota", "TN" => "Tennessee", "TX" => "Texas", "UT" => "Utah", "VA" => "Virginia", "VI" => "Virgin Islands", "VT" => "Vermont", "WA" => "Washington", "WI" => "Wisconsin", "WV" => "W. Virginia", "WY" => "Wyoming"];public function __construct(RequestStack $requestStack, EntityManagerInterface $em, Environment $twig, SiteContentSvc $contentSvc) {$this->em = $em;$this->twig = $twig;$this->contentSvc = $contentSvc;$this->requestStack = $requestStack;$this->request = $requestStack->getCurrentRequest();$this->route = $this->request->attributes->get("_route");$this->params = $this->request->attributes->get("_route_params");$this->templatesDir = preg_replace("/public/", "templates", getcwd());if (!empty($_ENV["SITE_ID"])) {$this->site = $em->getRepository(Site::class)->find(["siteId" => $_ENV["SITE_ID"]]);$this->contentSvc->setSite($this->site);}$this->pronoun = (!empty($_ENV["PRONOUN"])) ? $_ENV["PRONOUN"] : null;$this->timeframe = (!empty($_ENV["TIMEFRAME"])) ? $_ENV["TIMEFRAME"] : "monthly";$this->newsletterTopMsg = (!empty($_ENV["NEWSLETTER_TOP_MSG"])) ? $_ENV["NEWSLETTER_TOP_MSG"] : null;} // end function __construct/*** render index page* @Route("/", name="index")* @Route("/index.htm")* @Route("/index")*/public function index(Request $request) {$pagePath = "index";if (!$this->isSiteSet()) return $this->pageNotFound();// redirect requests to /index and /index.htm to /if (str_contains($request->getPathInfo(), $pagePath)) return $this->redirect("/");return new Response($this->contentSvc->getIndexTemplate()->render(["page_title" => $this->contentSvc->getPageTitle($pagePath) ?? null,"page_mkeys" => $this->contentSvc->getPageMkeys($pagePath) ?? null,"page_mdesc" => $this->contentSvc->getPageMdesc($pagePath) ?? null,"site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->contentSvc->getPageContent($pagePath) ?? null,]));}/*** render a page** @return Response*/public function view(string $pagePath, Request $request) {if (!$this->isSiteSet() || (!$this->contentSvc->hasPage($pagePath) && !$this->contentSvc->hasForm($pagePath))) return $this->pageNotFound($pagePath);// if the requested path ends in .htm, do a 301 redirect to the same path without the .htm endingif (str_contains($request->getPathInfo(), ".htm")) {return $this->redirect(str_replace(".htm", "", $request->getPathInfo()), Response::HTTP_MOVED_PERMANENTLY);}if ($this->contentSvc->hasForm($pagePath)) {return $this->showForm($pagePath);}if ($this->contentSvc->hasPage($pagePath)) {return $this->showPage($pagePath);}// if this point in the method has been reached, the page cannot be renderedreturn $this->pageNotFound($pagePath);}/*** render articles page* @Route("/articles", name="articles")* @Route("/articles.htm")* @Route("/content_library.htm")* @Route("/content-library", name="content-library")*/public function articles(Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();// render a custom articles page, if one existsif ($this->contentSvc->hasPage("articles")) {return $this->showPage("articles");}// render the default articles page, shared between all sitesreturn new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("articles/articles.html.twig", ["categories" => $this->em->getRepository(Category::class)->findBy(["parentId" => 'q4acvcqn'], ["categoryName" => "ASC"])])->getContent(),]));}/*** render article category page* @Route("/articles/{categorySlug}", name="article_category")* @Route("/content-library/{categorySlug}", name="content-library-category")*/public function articleCategory(string $categorySlug, Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();$categories = new ArrayCollection($this->em->getRepository(Category::class)->findBy(["parentId" => 'q4acvcqn'], ["categoryName" => "ASC"]));$category = $categories->filter(function($cat) use($categorySlug) {return $cat->getSlug() == $categorySlug;})->first();$articles = new ArrayCollection($this->em->getRepository(Article::class)->findBy([], ["articleUpdated" => "DESC", "articleTitle" => "ASC"]));$articles = $articles->filter(function($article) use($category) {return $article->categories->contains($category);});if(!($category) instanceof Category) {$this->addFlash("warning", "Unable to find the requested category");$this->redirectToRoute("articles");}// render the default article category page, shared between all sitesreturn new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("articles/category.html.twig", ["category" => $category,"articles" => $articles])->getContent(),]));}/*** render individual article page* @Route("/articles/{categorySlug}/{articleSlug}", name="article")* @Route("/content-library/{categorySlug}/{articleSlug}", name="content-library-article")*/public function article(string $categorySlug, string $articleSlug, Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();$categories = new ArrayCollection($this->em->getRepository(Category::class)->findBy(["parentId" => 'q4acvcqn'], ["categoryName" => "ASC"]));$category = $categories->filter(function($cat) use($categorySlug) {return $cat->getSlug() == $categorySlug;})->first();$articles = new ArrayCollection($this->em->getRepository(Article::class)->findBy([], ["articleTitle" => "ASC"]));$article = $articles->filter(function($article) use($articleSlug) {return $article->getSlug() == $articleSlug;})->first();if(!($article) instanceof Article) {$this->addFlash("warning", "Unable to find the requested article");$this->redirectToRoute("articles");}// render the default article page, shared between all sitesreturn new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("articles/article.html.twig", ["category" => $category,"article" => $article])->getContent(),]));}/*** render taxcenter page* @Route("/taxcenter", name="taxcenter")* @Route("/taxcenter.htm")*/public function taxcenter(Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();// render a custom taxcenter page, if one existsif ($this->contentSvc->hasPage("taxcenter")) {return $this->showPage("taxcenter");}// render the default taxcenter page, shared between all sitesreturn new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("taxcenter/taxcenter.html.twig")->getContent(),]));}/*** render taxcenter state refund page* @Route("/state_refund", name="taxcenter_state_refund")*/public function stateRefund(Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();return new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("taxcenter/state_refund.html.twig", ["states" => self::$states])->getContent(),]));}/*** render taxcenter state forms page* @Route("/state_forms", name="taxcenter_state_forms")*/public function stateForms(Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();return new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("taxcenter/state_forms.html.twig", ["states" => self::$states])->getContent(),]));}/*** render taxcenter calculators page* @Route("/calculators", name="taxcenter_calculators")*/public function calculators(Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();return new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("taxcenter/calculators.html.twig", ["states" => self::$states])->getContent(),]));}/*** render taxcenter retentionguide page* @Route("/retentionguide", name="taxcenter_retentionguide")*/public function retentionguide(Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();return new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("taxcenter/retentionguide.html.twig", ["states" => self::$states])->getContent(),]));}/*** render taxcenter subpage* @Route("/taxcenter/{page}", name="taxcenter_page")*/public function taxcenterPage(Request $request, string $page) {if (!$this->isSiteSet() || !file_exists($this->templatesDir . "/taxcenter/{$page}.html.twig")) return $this->pageNotFound();// render the default taxcenter subpage, shared between all sitesreturn new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("taxcenter/{$page}.html.twig", ["states" => self::$states])->getContent(),]));}/*** render contact page* @Route("/contact", name="contact")* @Route("/contact.htm")*/public function contact(Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();// render a custom contact form, if one existsif ($this->contentSvc->hasForm("contact")) {return $this->showForm("contact");}// render a custom contact page, if one existsif ($this->contentSvc->hasPage("contact")) {return $this->showPage("contact");}// render the default contact page/form, shared between all sitesreturn new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("page/contact.html.twig", ["offices" => $this->site->getOffices(), "site" => $this->site, "pronoun" => $this->pronoun])->getContent(),]));}/*** render consultation page* @Route("/consultation", name="consultation")* @Route("/consultation.htm")*/public function consultation(Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();// render a custom consultation form, if one existsif ($this->contentSvc->hasForm("consultation")) {return $this->showForm("consultation");}// render a custom consultation page, if one existsif ($this->contentSvc->hasPage("consultation")) {return $this->showPage("consultation");}// render the default consultation page/form, shared between all sites, OR a custom consultation form that submits to the default consultation form processingif ($this->em->getRepository(Form::class)->findOneBy(["site" => $this->site, "formConsultIntercept" => 1])) {$form = $this->em->getRepository(Form::class)->findOneBy(["site" => $this->site, "formConsultIntercept" => 1]);$formFields = $this->em->getRepository(FormField::class)->findBy(["form" => $form], ["formFieldIndex" => "ASC"]);return new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("page/customConsultation.html.twig", ["site" => $this->site, "form" => $form, "formFields" => $formFields])->getContent(),]));} else {return new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("page/consultation.html.twig", ["site" => $this->site])->getContent(),]));}}/*** render newsletter page* @Route("/newsletter", name="newsletter")* @Route("/newsletter.htm")*/public function newsletter(Request $request) {if (!$this->isSiteSet()) return $this->pageNotFound();// render a custom newsletter form, if one existsif ($this->contentSvc->hasForm("newsletter")) {return $this->showForm("newsletter");}// render a custom newsletter page, if one existsif ($this->contentSvc->hasPage("newsletter")) {return $this->showPage("newsletter");}// render the default newsletter page/form, shared between all sitesreturn new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("page/newsletter.html.twig", ["site" => $this->site, "pronoun" => $this->pronoun, "timeframe" => $this->timeframe, "newsletterTopMsg" => $this->newsletterTopMsg])->getContent(),]));}/*** @Route("css/site_images/{file}", name= "site_get_image")*/public function getImg(string $file): Response {if (!$this->isSiteSet()) return $this->pageNotFound();$imgFile = getcwd() . "/site_images/" . $file;if (!file_exists($imgFile)) throw new HttpException("Image not found");$mimeType = mime_content_type($imgFile);$response = new Response(file_get_contents($imgFile));$response->headers->set('Content-Type', $mimeType);return $response;}/*** @Route("css/{cssFile}.css", name= "site_get_css", requirements={"cssFile"=".+"})*/public function getCss(string $cssFile): Response {if (!$this->isSiteSet()) return $this->pageNotFound();$response = new Response();$response->headers->set('Content-Type', 'text/css');$response->setContent($this->contentSvc->getCssContent($cssFile));return $response;}/*** @Route("js/{jsFile}", name= "site_get_js", requirements={"jsFile"=".+"})*/public function getJs(string $jsFile): Response {if (!$this->isSiteSet()) return $this->pageNotFound();$response = new Response();$response->headers->set('Content-Type', 'text/js');$response->setContent($this->contentSvc->getJsContent($jsFile));return $response;}public function pageNotFound(?string $pagePath = null): Response {return new Response($this->contentSvc->getInnerTemplate()->render(["page_title" => !empty($pagePath) && !empty($this->contentSvc->getPageTitle($pagePath)) ? $this->contentSvc->getPageTitle($pagePath) : "404 - Page Not Found | {$this->site->getSiteName()}","page_mkeys" => !empty($pagePath) && !empty($this->contentSvc->getPageMkeys($pagePath)) ? $this->contentSvc->getPageMkeys($pagePath) : null,"page_mdesc" => !empty($pagePath) && !empty($this->contentSvc->getPageMdesc($pagePath)) ? $this->contentSvc->getPageMdesc($pagePath) : null,"site" => $this->site,"layout" => $this->site->getLayout(),"content" => "<div id=\"notfound\" style=\"display: table; width: 100%; height: 100%; text-align: center; transition: all 0.6s;\"><div class=\"missing\" style=\"display: table-cell; vertical-align: middle;\"><h1 style=\"font-size: 50px; display: inline-block; padding-right: 12px; animation: type .5s alternate infinite;\">Error 404:</h1><h2>Page not found</h2><p>Looks like that page was removed, renamed, or does not exist.</p><h3><a href=\"/\">Click to Return Home</a></h3></div></div>"]));//return $this->render("page/404.html.twig", ["pagePath" => $pagePath]);}protected function isSiteSet(): bool {return $this->contentSvc->isSiteSet();}protected function showPage($pagePath): Response {return new Response($this->contentSvc->getInnerTemplate()->render(["page_title" => !empty($this->contentSvc->getPageTitle($pagePath)) ? $this->contentSvc->getPageTitle($pagePath) : "{$pagePath} | {$this->site->getSiteName()}","page_mkeys" => !empty($this->contentSvc->getPageMkeys($pagePath)) ? $this->contentSvc->getPageMkeys($pagePath) : null,"page_mdesc" => !empty($this->contentSvc->getPageMdesc($pagePath)) ? $this->contentSvc->getPageMdesc($pagePath) : null,"site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->contentSvc->getPageContent($pagePath)]));}protected function showForm($formPath): Response {$form = $this->contentSvc->getForm($formPath);return new Response($this->contentSvc->getInnerTemplate()->render(["site" => $this->site,"layout" => $this->site->getLayout(),"content" => $this->render("page/customForm.html.twig", ["site" => $this->site, "form" => $form, "formFields" => $form->getFormFields()])->getContent(),]));}}