src/Trinity/BlogBundle/Controller/BlogController.php line 728

Open in your IDE?
  1. <?php
  2. namespace App\Trinity\BlogBundle\Controller;
  3. use App\CmsBundle\Controller\StorageController;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\Form\Extension\Core\Type\TextType;
  11. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  13. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  14. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  15. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  16. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  17. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  18. use Symfony\Component\Serializer\Serializer;
  19. use Doctrine\Common\Collections\ArrayCollection;
  20. use App\CmsBundle\Util\Mailer;
  21. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  22. use Twig\Environment;
  23. class BlogController extends StorageController
  24. {
  25.     /**
  26.      * @Route("/admin/blog", name="admin_mod_blog")
  27.      */
  28.     public function indexAction(Request $request$id null)
  29.     {
  30.         parent::init($request);
  31.         $this->breadcrumbs->addRouteItem($this->trans("Nieuws"'cms'), "admin_mod_blog");
  32.         $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->findOneBy([
  33.             'language' => $this->language,
  34.             'settings' => $this->Settings,
  35.         ]);
  36.         if (empty($Blog)) {
  37.             // Create default blog
  38.             $Blog = new \App\Trinity\BlogBundle\Entity\Blog();
  39.             $Blog->setLanguage($this->language);
  40.             $Blog->setSettings($this->Settings);
  41.             $Blog->setLabel($this->trans('Algemeen''cms'));
  42.             $em $this->getDoctrine()->getManager();
  43.             $em->persist($Blog);
  44.             $em->flush();
  45.         }
  46.         return $this->redirect($this->generateUrl('admin_mod_blog_entry', ['id' => $Blog->getId()]));
  47.     }
  48.     /**
  49.      * @Route("/admin/blog/edit/{id}", name="admin_mod_blog_edit")
  50.      * @Template()
  51.      */
  52.     public function editAction(Request $request$id null)
  53.     {
  54.         parent::init($request);
  55.         $this->breadcrumbs->addRouteItem($this->trans("Nieuws"'cms'), "admin_mod_blog");
  56.         $new false;
  57.         if ((int)$id 0) {
  58.             // Edit
  59.             $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->find($id);
  60.             $this->breadcrumbs->addRouteItem($this->trans("Wijzigen"'cms'), "admin_mod_blog_edit");
  61.         } else {
  62.             // Add
  63.             $new true;
  64.             $Blog = new \App\Trinity\BlogBundle\Entity\Blog();
  65.             $Blog->setLanguage($this->language);
  66.             $Blog->setSettings($this->Settings);
  67.             $this->breadcrumbs->addRouteItem($this->trans("Toevoegen"'cms'), "admin_mod_blog_edit");
  68.         }
  69.         $saved false;
  70.         $form $this->createFormBuilder($Blog)
  71.             ->add('label'TextType::class, array('label' => $this->trans('Titel''cms')))
  72.             ->add('info'TextareaType::class, array('label' => $this->trans('Info''cms'), 'attr' => ['class' => 'ckeditor']))
  73.             ->setMethod('post')
  74.             ->getForm();
  75.         $form->handleRequest($request);
  76.         if ($form->isSubmitted() && $form->isValid()) {
  77.             // Store in database
  78.             $em $this->getDoctrine()->getManager();
  79.             $em->persist($Blog);
  80.             $em->flush();
  81.             return $this->redirect($this->generateUrl('admin_mod_blog_entry', ['id' => $Blog->getId()]));
  82.         }
  83.         return $this->attributes(array(
  84.             'form' => $form->createView(),
  85.             'Blog' => $Blog,
  86.             'saved' => (bool)$saved
  87.         ));
  88.     }
  89.     /**
  90.      * @Route("/admin/blog/delete/{id}", name="admin_mod_blog_delete")
  91.      * @Template()
  92.      */
  93.     public function deleteAction(Request $request$id null)
  94.     {
  95.         parent::init($request);
  96.         $em $this->getDoctrine()->getManager();
  97.         $Blog $em->getRepository('TrinityBlogBundle:Blog')->find($id);
  98.         if (!is_null($Blog)) {
  99.             $Blog->setSettings(null);
  100.             $em->remove($Blog);
  101.             $em->flush();
  102.         }
  103.         return $this->redirect($this->generateUrl('admin_mod_blog'));
  104.     }
  105.     public function wpredirectAction(Request $request){
  106.         $this->init($request);
  107.         $entryId $request->get('entryId');
  108.         $redirect $this->generateUrl('homepage');
  109.         try{
  110.             if(!empty($entryId)){
  111.                 $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneById($entryId);
  112.                 if(!empty($Entry)){
  113.                     $page null;
  114.                     $blocks $this->getDoctrine()->getRepository('CmsBundle:PageBlock')->findByData('TrinityBlogBundle''is_overview');
  115.                     foreach($blocks as $Block){
  116.                         $p $Block->getWrapper()->getPage();
  117.                         if($p->getLanguage() == $this->language){
  118.                             $page $p;
  119.                             break;
  120.                         }
  121.                     }
  122.                     if($page){
  123.                         $redirect $this->generateUrl($page->getSlugKey());
  124.                         $slug '/' $Entry->getId() . '/' $Entry->getDefaultSlug();
  125.                         if(!empty($Entry->getSlug())){
  126.                             $slug '/' $Entry->getSlug();
  127.                         }
  128.                         $redirect $redirect $slug;
  129.                     }
  130.                 }
  131.             }
  132.         }catch(\Exception $e){}
  133.         return $this->redirect($redirect);
  134.     }
  135.     public function showAction($config$params = array(), $request null)
  136.     {
  137.         parent::init($request);
  138.         $related = [];
  139.         if(!empty($params[0]) && empty($params[1])){
  140.             $category $this->getDoctrine()->getRepository('TrinityBlogBundle:Category')->findOneBy([
  141.                 'language' => $this->language,
  142.                 'slug' =>  $params[0]
  143.             ]);
  144.             if(!empty($category)){
  145.                 $Blog $category->getBlog();
  146.                 $Entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedByCategoryNow($this->language, [$category->getId()], 1);
  147.                 $Entry $Entries[0];
  148.                 $replies $this->getDoctrine()->getRepository('TrinityBlogBundle:Reply')->findBy(['entry' => $Entry'reply' => null'approved' => true], ['date' => 'desc']);
  149.                 $replies_count $this->getDoctrine()->getRepository('TrinityBlogBundle:Reply')->count(['entry' => $Entry'approved' => true], ['date' => 'desc']);
  150.                 
  151.                 if($Entry->getCategory()->count()){
  152.                     $C $Entry->getCategory()->first();
  153.                     $related $C->getEntry();
  154.                 }
  155.                 if (isset($config['show_anchors']) && $config['show_anchors'] == 1) {
  156.                     $this->getDoctrine()->getManager()->detach($Entry);
  157.                     //Get the innerHtml all of the <h2> tags from the body of the entry and store their text in an array to be used as anchors
  158.                     $h2s = [];
  159.                     preg_match_all('/<h2>(.*?)<\/h2>/'$Entry->getBody(), $h2s);
  160.     
  161.                     //Get all the h2 tags from the Entry body and give them an incrementing id attribute
  162.                     $h2s[0] = array_unique($h2s[0]);
  163.                     $i 0;
  164.                     foreach($h2s[0] as $h2){
  165.                         $Entry->setBody(str_replace($h2'<h2 id="anchor-' $i '">' $h2s[1][$i] . '</h2>'$Entry->getBody()));
  166.                         $i++;
  167.                     }
  168.     
  169.                     $h2s $h2s[1];
  170.                 } else {
  171.                     $h2s = [];
  172.                 }
  173.                 return $this->renderView('@TrinityBlog/default/category.html.twig'$this->attributes(array(
  174.                     'Entry' => $Entry,
  175.                     'Category' => $category,
  176.                     'config' => $config,
  177.                     'Entry' => $Entry,
  178.                     'Blog' => $Blog,
  179.                     'Anchors' => $h2s,
  180.                     'recentFour' => [],
  181.                     'replies' => $replies,
  182.                     'replies_count' => $replies_count,
  183.                     'popular' => [],
  184.                     'related' => $category,
  185.                     'products' => [],
  186.                     'settings' => $this->Settings,
  187.                     'most_recent' => [],
  188.                     'installed' => $this->installed,
  189.                     'WebshopSettings' => null,
  190.                     'error' => '',
  191.                     'inline_error' => '',
  192.                 )));
  193.             }
  194.         }
  195.         if(!empty($config['uri'])){
  196.             $config['uri'] = preg_replace('/^\//'''$config['uri']);
  197.         }
  198.         if(!empty($config['uri_overview'])){
  199.             $config['uri_overview'] = preg_replace('/^\//'''$config['uri_overview']);
  200.         }
  201.         if(!empty($config['type']) && $config['type'] == 'categories' && empty($params[0])){
  202.             #Get all blog categories by language and settings
  203.             $Blogs $this->getDoctrine()->getRepository('TrinityBlogBundle:Category')->findBy([
  204.                 'language' => $this->language
  205.             ]);
  206.             return $this->renderView('@TrinityBlog/default/categories.html.twig'$this->attributes(array(
  207.                 'config' => $config,
  208.                 'Blogs' => $Blogs
  209.             )));
  210.         }
  211.         if(!empty($config['type']) && $config['type'] == 'popular'){
  212.             $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->findOneById($config['id']);
  213.             $html '<ul>';
  214.             $page null;
  215.             // Add check for TrinityBlogBundle for legacy support
  216.             $blocks $this->getDoctrine()->getRepository('CmsBundle:PageBlock')->findByData('TrinityBlogBundle''is_overview');
  217.             foreach($blocks as $Block){
  218.                 $p $Block->getWrapper()->getPage();
  219.                 if($p->getLanguage() == $this->language){
  220.                     $page $p;
  221.                     break;
  222.                 }
  223.             }
  224.             $blocks $this->getDoctrine()->getRepository('CmsBundle:PageBlock')->findByData('TrinityBlogBundle''is_overview');
  225.             foreach($blocks as $Block){
  226.                 $p $Block->getWrapper()->getPage();
  227.                 if($p->getLanguage() == $this->language){
  228.                     $page $p;
  229.                     break;
  230.                 }
  231.             }
  232.             $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedNow($Blog4);
  233.             foreach($entries as $Entry){
  234.                 if($page){
  235.                     $baseUrl $this->generateUrl($page->getSlugKey());
  236.                     $slug '/' $Entry->getId() . '/' $Entry->getDefaultSlug();
  237.                     if(!empty($Entry->getSlug())){
  238.                         $slug '/' $Entry->getSlug();
  239.                     }
  240.                     $url $baseUrl $slug;
  241.                 }else{
  242.                     if(!empty($Entry->getSlug())){
  243.                         $url $this->generateUrl($request->get('_route'), ['param1' => $Entry->getSlug()]);
  244.                     }else{
  245.                         $url $this->generateUrl($request->get('_route'), ['param1' => $config['id'], 'param2' => $Entry->getDefaultSlug()]);
  246.                     }
  247.                 }
  248.                 $html .= '<li><a href="' $url '">' $Entry->getLabel() . '</a></li>';
  249.             }
  250.             $html .= '</ul>';
  251.             return $html;
  252.         }
  253.         $message '';
  254.         if($params[0] == 'prefs'){
  255.             $emailhash openssl_decrypt($params[1], 'aes-128-cbc''blogdata.0010000'null'blogdata.0010000');
  256.             $Mailreply null;
  257.             if($emailhash){
  258.                 $Mailreply $this->getDoctrine()->getRepository('TrinityBlogBundle:Mailreply')->findOneByEmail($emailhash);
  259.                 if(empty($Mailreply)){
  260.                     $Mailreply = new \App\Trinity\BlogBundle\Entity\Mailreply();
  261.                     $Mailreply->setEmail($emailhash);
  262.                     $Mailreply->setEnabled(true);
  263.                 }
  264.                 $Form $this->createFormBuilder($Mailreply)
  265.                         ->add('enabled'CheckboxType::class, ['label' => $this->trans('E-mail reacties ontvangen''cms')])
  266.                         ->add('notify_new_blog'CheckboxType::class, ['label' => $this->trans('E-mail update ontvangen bij nieuwe berichten''cms')])
  267.                         ->setMethod('post')
  268.                         ->getForm();
  269.                 $Form->handleRequest($request);
  270.                 if ($Form->isSubmitted() && $Form->isValid())
  271.                 {
  272.                     $em $this->getDoctrine()->getManager();
  273.                     $em->persist($Mailreply);
  274.                     $em->flush();
  275.                     $message $this->trans('De voorkeuren zijn opgeslagen.''cms');
  276.                 }
  277.             }
  278.             return $this->renderView('@TrinityBlog/default/prefs.html.twig', [
  279.                 'Form'      => ($Mailreply $Form->createView() : null),
  280.                 'emailhash' => $emailhash,
  281.                 'message'   => $message,
  282.             ]);
  283.         }
  284.         if (!empty($_GET['id'])) {
  285.             $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneById($_GET['id']);
  286.             if ($Entry) {
  287.                 if(!empty($Entry->getSlug())){
  288.                     $url $this->generateUrl($request->get('_route'), ['param1' => $Entry->getSlug()]);
  289.                 }else{
  290.                     $url $this->generateUrl($request->get('_route'), ['param1' => $_GET['id'], 'param2' => $Entry->getDefaultSlug()]);
  291.                 }
  292.             } else {
  293.                 $url $this->generateUrl($request->get('_route'));
  294.             }
  295.             header("HTTP/1.1 301 Moved Permanently");
  296.             header('Location: ' $url);
  297.             exit;
  298.         }
  299.         if (!empty($params[0])) {
  300.             $inline_error false;
  301.             if (!isset($config['notblogspecific']))
  302.             {
  303.                 $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->findOneById($config['id']);
  304.                 if(!is_numeric($params[0])){
  305.                     //If the show_anchors is set to true, load the blog in a different way
  306.                     if(isset($config['show_anchors']) && $config['show_anchors'] == true && !empty($params[1])){
  307.                         $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneBy(['slug' => $params[1], 'blog' => $Blog]);
  308.                     }else{
  309.                         $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneBy(['slug' => $params[0], 'blog' => $Blog]);
  310.                     }
  311.                 }else{
  312.                     $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneBy(['id' => $params[0], 'blog' => $Blog]);
  313.                     if(!empty($Entry) && !empty($Entry->getSlug())){
  314.                         $url $this->generateUrl($request->get('_route'), ['param1' => $Entry->getSlug()]);
  315.                         header("HTTP/1.1 301 Moved Permanently");
  316.                         header('Location: ' $url);
  317.                         exit;
  318.                     }
  319.                 }
  320.             } else {
  321.                 $Blog null;
  322.                 // FIXME Blog restriction here?
  323.                 if(!is_numeric($params[0])){
  324.                     if(isset($config['show_anchors']) && $config['show_anchors'] == true && !empty($params[1])){
  325.                         $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneBy(['slug' => $params[1]]);
  326.                     }else{
  327.                         $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneBy(['slug' => $params[0]]);
  328.                     }
  329.                 }else{
  330.                     $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneBy(['id' => $params[0]]);
  331.                     if(!empty($Entry) && !empty($Entry->getSlug())){
  332.                         $url $this->generateUrl($request->get('_route'), ['param1' => $Entry->getSlug()]);
  333.                         header("HTTP/1.1 301 Moved Permanently");
  334.                         header('Location: ' $url);
  335.                         exit;
  336.                     }
  337.                 }
  338.             }
  339.             if(empty($Entry))
  340.             {
  341.                 header("HTTP/1.1 301 Moved Permanently");
  342.                 header("location: " $this->generateUrl($request->get('_route')));
  343.                 exit;
  344.             }
  345.             $em $this->getDoctrine()->getManager();
  346.             $Entry->setReadcount($Entry->getReadcount() + 1);
  347.             $em->persist($Entry);
  348.             $em->flush();
  349.             if($Entry->getCategory()->count()){
  350.                 $C $Entry->getCategory()->first();
  351.                 $related $C->getEntry();
  352.             }
  353.         
  354.             $Reply = new \App\Trinity\BlogBundle\Entity\Reply();
  355.             
  356.             $Form $this->createFormBuilder($Reply)
  357.                     ->add('firstname'TextType::class, ['data' => ($this->getUser() ? $this->getUser()->getFirstname() : '')])
  358.                     ->add('lastname'TextType::class, ['data' => ($this->getUser() ? $this->getUser()->getLastname() : '')])
  359.                     ->add('email'EmailType::class, ['data' => ($this->getUser() ? $this->getUser()->getEmail() : '')])
  360.                     ->add('comment'TextareaType::class, ['required' => true])
  361.                     ->setMethod('post')
  362.                     ->getForm();
  363.             $Form->handleRequest($request);
  364.             // View entry details
  365.             //if (!empty($_POST)) {
  366.             if ($Form->isSubmitted() && $Form->isValid())
  367.             {
  368.                 $validCaptcha $this->Settings->validateGoogleRecaptcha($request->request->get('g-recaptcha-response'));
  369.                 
  370.                 if(($validCaptcha))
  371.                 {
  372.                     if(!empty($Form->getData()->getComment())){
  373.                         if(!empty($_POST['replyto'])){
  374.                             $Parent $this->getDoctrine()->getRepository('TrinityBlogBundle:Reply')->find($_POST['replyto']);
  375.                             if($Parent){
  376.                                 $Reply->setReply($Parent);
  377.                                 $Mailreply $this->getDoctrine()->getRepository('TrinityBlogBundle:Mailreply')->findOneByEmail($Parent->getEmail());
  378.                                 if(!empty($Parent->getEmail()) && ($Mailreply && $Mailreply->getEnabled()) || empty($Mailreply)){
  379.                                     $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  380.                                     $emailToHash openssl_encrypt($Parent->getEmail(), 'aes-128-cbc''blogdata.0010000'null'blogdata.0010000');
  381.                                     // $emailToHash = urlencode($emailToHash);
  382.                                     $message =
  383.                                             $this->trans('<p>Beste %firstname%,</p>' "\n".
  384.                                                 '<p>Een bezoeker heeft gereageerd op je reactie:</p>' "\n".
  385.                                                 '<div style="border:solid 1px black;border-width:1px 0;padding: 10px 0;margin: 10px 0;">%comment%</div>'.
  386.                                                 '<p style="text-align:center;border-top: dashed 1px #ddd;padding-top:40px;margin-top:40px;"><a href="%entryurl%">Klik hier</a> om de reactie te bekijken.<br/>'.
  387.                                                 '<a href="%prefsurl%">Klik hier</a> om uw voorkeuren voor deze geautomatiseerde e-mails te beheren.</p>',
  388.                                             'cms',
  389.                                             [
  390.                                                 '%firstname%' => $Parent->getFirstname(),
  391.                                                 '%comment%' => $Reply->getcomment(),
  392.                                                 '%entryurl%' => $baseurl $this->generateUrl($request->get('_route')) . '/' . ($Entry->getSlug() ? $Entry->getSlug() : $Entry->getId() . '/' $Entry->getDefaultSlug()),
  393.                                                 '%prefsurl%' => $baseurl $this->generateUrl($request->get('_route')) . '/prefs/' $emailToHash,
  394.                                             ]);
  395.                                     
  396.                                     /*ob_start();
  397.                                     $htmlDebug = $twig->render('/emails/notify.html.twig', [
  398.                                         'label' => '',
  399.                                         'message' => $message
  400.                                     ]);
  401.                                     die($htmlDebug);*/
  402.                                     // Send notification to person replied to
  403.                                     $mailer = clone $this->mailer;
  404.                                     $mailer->init();
  405.                                     $mailer->setSubject($this->trans('Nieuwe reactie op je reactie bij het blog bericht''cms') . ' ' $Entry->getLabel())
  406.                                             ->setTo($Parent->getEmail())
  407.                                             ->setTwigBody('/emails/notify.html.twig', [
  408.                                                 'label' => '',
  409.                                                 'message' => $message
  410.                                             ])
  411.                                             ->setPlainBody(strip_tags($message));
  412.                                     $status $mailer->execute_forced();
  413.                                 }else{
  414.                                     // NOT WANT EMAIL
  415.                                 }
  416.                             }
  417.                         }
  418.                         $Mailreply $this->getDoctrine()->getRepository('TrinityBlogBundle:Mailreply')->findOneByEmail($Reply->getEmail());
  419.                         if(empty($Mailreply)){
  420.                             // Mailreply setting doesnt exist yet, make it - enabled.
  421.                             $Mailreply = new \App\Trinity\BlogBundle\Entity\Mailreply();
  422.                             $Mailreply->setEmail($Reply->getEmail());
  423.                         }
  424.                         $Mailreply->setEnabled(!empty($_POST['optin']['reply']));
  425.                         $Mailreply->setNotifyNewBlog(!empty($_POST['optin']['new']));
  426.                         $em->persist($Mailreply);
  427.                         // Check if already approved
  428.                         $approved false;
  429.                         /*foreach($this->getDoctrine()->getRepository('TrinityBlogBundle:Reply')->findBy(['email' => $Reply->getEmail()]) as $R){
  430.                             if(!empty($R)){
  431.                                 if($R->getApproved() != true){
  432.                                     $Mailreply = $this->getDoctrine()->getRepository('TrinityBlogBundle:Mailreply')->findOneByEmail($R->getEmail());
  433.                                     if($Mailreply && $Mailreply->getApproved()){
  434.                                         $approved = true;
  435.                                     }else{
  436.                                         $approved = false;
  437.                                     }
  438.                                 }
  439.                             }
  440.                         }*/
  441.                         $Reply->setApproved($approved);
  442.                         $Reply->setEntry($Entry);
  443.                         $Reply->setIp($_SERVER['REMOTE_ADDR']);
  444.                         $Reply->setDate(new \Datetime('now'));
  445.                         $em->persist($Reply);
  446.                         $em->flush();
  447.                         $inline_error $this->trans('Uw reactie is opgeslagen.''cms');
  448.                     }else{
  449.                         $inline_error $this->trans('Uw reactie is niet opgeslagen.''cms');
  450.                     }
  451.                     
  452.                     $Reply = new \App\Trinity\BlogBundle\Entity\Reply();
  453.                     $Form $this->createFormBuilder($Reply)
  454.                         ->add('firstname'TextType::class, ['data' => ($this->getUser() ? $this->getUser()->getFirstname() : '')])
  455.                         ->add('lastname'TextType::class, ['data' => ($this->getUser() ? $this->getUser()->getLastname() : '')])
  456.                         ->add('email'EmailType::class, ['data' => ($this->getUser() ? $this->getUser()->getEmail() : '')])
  457.                         ->add('comment'TextareaType::class, ['data' => '','required' => true])
  458.                         ->setMethod('post')
  459.                         ->getForm();
  460.                         
  461.                     // Redirect to the same page, this prevents that the form can be submitted multiple times by refreshing the page.
  462.                     //header_remove();
  463.                     //header("HTTP/1.1 301 Moved Permanently");
  464.                     //header('Location: ' . $request->getUri());
  465.                     //exit;
  466.                 } else {
  467.                     $inline_error $this->trans('Ongeldige reCAPTCHA, uw reactie is niet opgeslagen.''cms');
  468.                 }
  469.             }
  470.             $replies $this->getDoctrine()->getRepository('TrinityBlogBundle:Reply')->findBy(['entry' => $Entry'reply' => null'approved' => true], ['date' => 'desc']);
  471.             $replies_count $this->getDoctrine()->getRepository('TrinityBlogBundle:Reply')->count(['entry' => $Entry'approved' => true], ['date' => 'desc']);
  472.             if (!isset($config['notblogspecific'])) {
  473.                 // FIXME There function should also check category is needed?
  474.                 $popular $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedNow($Blog);
  475.                 $recentFour $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findRecentEntriesPublishedNow($Blog3$Entry->getId());
  476.                 // $recentFour = $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findBy(['blog' => $Blog], ['datePublish' => 'desc'], 4);
  477.             } else {
  478.                 if (!isset($config['category_id'])) {
  479.                     $categories null;
  480.                 } else {
  481.                     $categories $config['category_id'];
  482.                 }
  483.                 $popular $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedByCategoryNow($this->language$categories);
  484.                 $recentFour $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedByCategoryNow($this->language$categories4);
  485.             }
  486.             $products = [];
  487.             $WebshopSettings null;
  488.             if(!empty($Entry->getProducts())){
  489.                 foreach($Entry->getProducts() as $id){
  490.                     $products[$id] = $this->getDoctrine()->getRepository('TrinityWebshopBundle:Product')->find($id);
  491.                 }
  492.                 $WebshopSettings $this->getDoctrine()->getRepository('TrinityWebshopBundle:Settings')->find(1);
  493.             }
  494.             $content $Entry->getBody();
  495.             if(preg_match_all('/\[caption\s(.*?)\](.*?)\[\/caption\]/'$content$m)){
  496.                 if(!empty($m)){
  497.                     foreach($m[1] as $i => $params){
  498.                         $src $m[0][$i];
  499.                         $inner $m[2][$i];
  500.                         $inner preg_replace("/ width=\"\d+\"/"''$inner);
  501.                         $inner preg_replace("/ height=\"\d+\"/"''$inner);
  502.                         $inner preg_replace("/ class=\".*?\"/"''$inner);
  503.                         $config = [];
  504.                         $params explode(' '$params);
  505.                         foreach($params as $p){
  506.                             $p explode('='$p);
  507.                             $config[$p[0]] = str_replace('"'''$p[1]);
  508.                         }
  509.                         $widget '<div class="caption-widget ' . (!empty($config['align']) ? $config['align'] : '') . '">
  510.                             ' preg_replace('/Bron(.*?)http/''Bron$1<br/>http'$inner) . '
  511.                         </div>';
  512.                         $content str_replace($src$widget$content);
  513.                     }
  514.                 }
  515.             }
  516.             // $content = '<h1>:\'-)</h1>';
  517.             // $content            
  518.             $Entry->setBody($content);
  519.             $most_recent $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findBy(array('blog' => $Entry->getBlog()), array('datePublish' => 'desc'), 5);
  520.             if($Entry->getIsExternal() && !empty($Entry->getExternalUrl())){
  521.                 header("HTTP/1.1 301 Moved Permanently");
  522.                 header('Location: ' $Entry->getExternalUrl());
  523.                 exit;
  524.             }
  525.             if (isset($config['show_anchors']) && $config['show_anchors'] == 1) {
  526.                 $this->getDoctrine()->getManager()->detach($Entry);
  527.                 //Get the innerHtml all of the <h2> tags from the body of the entry and store their text in an array to be used as anchors
  528.                 $h2s = [];
  529.                 preg_match_all('/<h2>(.*?)<\/h2>/'$Entry->getBody(), $h2s);
  530.                 //Get all the h2 tags from the Entry body and give them an incrementing id attribute
  531.                 $h2s[0] = array_unique($h2s[0]);
  532.                 $i 0;
  533.                 foreach($h2s[0] as $h2){
  534.                     $Entry->setBody(str_replace($h2'<h2 id="anchor-' $i '">' $h2s[1][$i] . '</h2>'$Entry->getBody()));
  535.                     $i++;
  536.                 }
  537.                 $h2s $h2s[1];
  538.             } else {
  539.                 $h2s = [];
  540.             }
  541.             return $this->renderView('@TrinityBlog/default/entry.html.twig'$this->attributes(array(
  542.                 'config' => $config,
  543.                 'Entry' => $Entry,
  544.                 'Blog' => $Blog,
  545.                 'Anchors' => $h2s,
  546.                 'recentFour' => $recentFour,
  547.                 'replies' => $replies,
  548.                 'replies_count' => $replies_count,
  549.                 'popular' => $popular,
  550.                 'related' => $related,
  551.                 'Form' => $Form->createView(),
  552.                 'products' => $products,
  553.                 'settings' => $this->Settings,
  554.                 'most_recent' => $most_recent,
  555.                 'installed' => $this->installed,
  556.                 'WebshopSettings' => $WebshopSettings,
  557.                 'error' => '',
  558.                 'inline_error' => $inline_error,
  559.             )));
  560.         } else {
  561.             // View index
  562.             if (!isset($config['notblogspecific'])) {
  563.                 $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->findOneById($config['id']);
  564.                 if ((int)$config['limit'] > 0) {
  565.                     $page = isset($_GET['page']) && (int)$_GET['page'] > $_GET['page'] : 1;
  566.                     $offset = (($page 1) * (int)$config['limit']);
  567.                 } else {
  568.                     $page 1;
  569.                     $offset 0;
  570.                 }
  571.                 $limit = !empty($config['limit']) ? $config['limit'] : null;
  572.                 if (isset($config['category_id']) && !empty($config['category_id']) && !empty(reset($config['category_id'])))
  573.                 {
  574.                     $count count($this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findEntriesByCategory($Blog$config['category_id']));
  575.                     $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findEntriesByCategory($Blog$config['category_id'], false$limit$offset);
  576.                 } else {
  577.                     $count count($this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findEntriesPublishedNow($Blogfalse));
  578.                     $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findEntriesPublishedNow($Blogfalse$limit$offset);
  579.                 }
  580.                 if ((int)$config['limit'] > 0) {
  581.                     $pages ceil($count / (int)$config['limit']);
  582.                 } else {
  583.                     $pages 0;
  584.                 }
  585.                 $popular $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedNow($Blog);
  586.             } else {
  587.                 $Blog null;
  588.                     
  589.                 if ((int)$config['limit'] > 0) {
  590.                     $page = isset($_GET['page']) && (int)$_GET['page'] > $_GET['page'] : 1;
  591.                     $offset = (($page 1) * (int)$config['limit']);
  592.                 } else {
  593.                     $page 1;
  594.                     $offset 0;
  595.                 }
  596.                 $limit = !empty($config['limit']) ? $config['limit'] : null;
  597.                 if (isset($config['category_id']) && !empty($config['category_id']))
  598.                 {
  599.                     $count count($this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->FindEntriesByLanguage($this->language$config['category_id']));
  600.                     $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->FindEntriesByLanguage($this->language$config['category_id'], $limit$offset);
  601.                 } else {
  602.                     $count count($this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->FindEntriesByLanguage($this->language));
  603.                     $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->FindEntriesByLanguage($this->languagenull$limit$offset);
  604.                 }
  605.                 if ((int)$config['limit'] > 0) {
  606.                     $pages ceil($count / (int)$config['limit']);
  607.                 } else {
  608.                     $pages 0;
  609.                 }
  610. // XXX implimenteren
  611.                 $popular null//$this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedNow($Blog);
  612.             }
  613.             if (isset($config['show_slider']) && isset($config['limit']) && $config['limit'] > 0) {
  614.                 return $this->renderView('@TrinityBlog/default/slider.html.twig'$this->attributes(array(
  615.                     'config' => $config,
  616.                     'Blog' => $Blog,
  617.                     'entries' => $entries,
  618.                     'page' => $page,
  619.                     'pages' => $pages,
  620.                 )));
  621.             } else {
  622.                 return $this->renderView('@TrinityBlog/default/blog.html.twig'$this->attributes(array(
  623.                     'config' => $config,
  624.                     'Blog' => $Blog,
  625.                     'entries' => $entries,
  626.                     'popular' => $popular,
  627.                     'page' => $page,
  628.                     'limit' => $limit,
  629.                     'offset' => $offset,
  630.                     'pages' => $pages,
  631.                 )));
  632.             }
  633.         }
  634.     }
  635.     public static function metatagHandler($em$params$bundledata$request){
  636.         if(!is_numeric($params[0])){
  637.             $Entry $em->getRepository('TrinityBlogBundle:Entry')->findOneBy(['slug' => $params[0]]);
  638.         }else{
  639.             $Entry $em->getRepository('TrinityBlogBundle:Entry')->findOneBy(['id' => $params[0]]);
  640.         }
  641.         $entryMetatags = [];
  642.         $ignoreTags    = ['description'];
  643.         $metatags      $em->getRepository('CmsBundle:Metatag')->findBy(['system' => false]);
  644.         if($Entry){
  645.             foreach($metatags as $index => $Metatag){
  646.                 if(!in_array($Metatag->getKey(), $ignoreTags)){
  647.                     $EntryMetatag $em->getRepository('TrinityBlogBundle:Metatag')->findOneBy(['metatag' => $Metatag'entry' => $Entry]);
  648.                     if(empty($EntryMetatag)){
  649.                         $EntryMetatag = new \App\Trinity\BlogBundle\Entity\Metatag();
  650.                         $EntryMetatag->setMetatag($Metatag);
  651.                     }
  652.                     if(empty($EntryMetatag->getValue())){
  653.                         if($EntryMetatag->getMetatag()->getKey() == 'keywords'){
  654.                             if(!empty($Entry->getSeoKeywords())){
  655.                                 $EntryMetatag->setValue($Entry->getSeoKeywords());
  656.                             }
  657.                         }
  658.                         if($EntryMetatag->getMetatag()->getKey() == 'og:title'){
  659.                             if(!empty($Entry->getSeoTitle())){
  660.                                 $EntryMetatag->setValue($Entry->getSeoTitle());
  661.                             }else{
  662.                                 $EntryMetatag->setValue($Entry->getLabel());
  663.                             }
  664.                         }
  665.                         if($EntryMetatag->getMetatag()->getKey() == 'og:description'){
  666.                             if(!empty($Entry->getSeoDescription())){
  667.                                 $EntryMetatag->setValue($Entry->getSeoDescription());
  668.                             }else{
  669.                                 $EntryMetatag->setValue($Entry->getIntro());
  670.                             }
  671.                         }
  672.                         if($EntryMetatag->getMetatag()->getKey() == 'og:image'){
  673.                             if($Entry->getMedia()){
  674.                                 foreach($Entry->getMedia() as $Media){
  675.                                     $EntryMetatag->setValue('/' $Media->getWebPath());
  676.                                     break;
  677.                                 }
  678.                             }
  679.                         }
  680.                     }
  681.                         if($EntryMetatag->getMetatag()->getKey() == 'og:url'){
  682.                             $Settings $Entry->getBlog()->getSettings();
  683.                             if ($Settings->getForceHttps()) {
  684.                                 $url 'https://';
  685.                             } else {
  686.                                 $url 'http://';
  687.                             }
  688.                             $url $url $Settings->getHost();
  689.                             $url $url $request->getRequestUri();
  690.                             $EntryMetatag->setValue($url);
  691.                         }
  692.                     if(!empty($EntryMetatag->getValue())){
  693.                         $entryMetatags[] = $EntryMetatag;
  694.                     }
  695.                 }
  696.             }
  697.         }
  698.         return $entryMetatags;
  699.     }
  700.     public static function resourcesHandler($Settings, array $bundledatastring $projectDir) : ?string
  701.     {
  702.         $resources null;
  703.         $layoutKey = !empty($Settings->getOverrideKey()) ? trim($Settings->getOverrideKey()) . '/' '';
  704.         $resource_file 'resources.json';
  705.         if (isset($bundledata['allow_replies']) && $bundledata['allow_replies'] == 1) {
  706.             $resource_file 'resources_replies.json';
  707.         }
  708.         // check if file exists or build array in code and return that.
  709.         $file __DIR__ "/../Resources/views/default/" $resource_file;
  710.         $override $projectDir '/public/custom/' $layoutKey 'blog/' $resource_file;
  711.         if (file_exists($override)) {
  712.             $resources $override;
  713.         } else if (file_exists($file)) {
  714.             $resources $file;
  715.         }
  716.         return $resources;
  717.     }
  718.     /**
  719.      * @Route("/ajax/blog/category/{id}/{uri}/{catid}", name="mod_blog_category_ajax")
  720.      */
  721.     public function getCategory(Request $request$id$uri ""$catid 0)
  722.     {
  723.         $blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->find($id);
  724.         $current_category $this->getDoctrine()->getRepository('TrinityBlogBundle:Category')->find($catid);
  725.         if ($catid == 0) {
  726.             $current_category $blog;
  727.         }
  728.         $content '';
  729.         if ($current_category == $blog) {
  730.             $entries_tmp $current_category->getEntries();
  731.         } else {
  732.             $entries_tmp $current_category->getEntry();
  733.         }
  734.         $entries = new ArrayCollection();
  735.         for ($i $entries_tmp->count(); $i > -1$i--) {
  736.             $e $entries_tmp->get($i);
  737.             if ($e)
  738.                 $entries->add($e);
  739.         }
  740.         $first false;
  741.         foreach ($entries as $entry) {
  742.             $url $request->getBaseUrl() . '/' urldecode($uri) . '/' $entry->getId() . '/' $entry->getSlug();
  743.             if ($entry->getImage()) {
  744.                 if (!$first) {
  745.                     if ($entry->getImage()->getHeight() < 600) {
  746.                         $imgsize 'catimg';
  747.                     } else {
  748.                         $imgsize 'catimg-full-width';
  749.                     }
  750.                     $headimgcontent '
  751.                     <div class="img-wrap">
  752.                         <a class="imageEntry" href="' $url '">
  753.                             <div class="imgbackgr header-img ' $imgsize ' " style="background-color:#DCDCDC; background-image: url(/' $entry->getImage()->getWebPath() . '); background-repeat: no-repeat; background-size:cover; background-position:center;">
  754.                             </div>
  755.                             <div class="title-holder">
  756.                                 <h4 class="h-post-title">' $entry->getLabel() . '</h4>
  757.                                 <span class="icon-holder h-post-title">
  758.                                     <i class="fa fa-thumbs-up"></i>' $entry->getLikes() . '
  759.                                     <i class="fa fa-eye"></i> ' $entry->getReadCount() . '
  760.                                 </span>
  761.                             </div>
  762.                             <div class="overlay"></div>
  763.                         </a>
  764.                     </div>';
  765.                     $first true;
  766.                 } else {
  767.                     if ($entry->getBlog() == $blog) {
  768.                         $content .= '
  769.                         <div class="pull-left" style="padding-top:10px;">
  770.                             <div class="row">
  771.                                 <div class="col-md-6">
  772.                                     <div class="img-wrap">
  773.                                     <a class="imageEntry" href="' $url '">
  774.                                         <div class="imgbackgr" style="height:100px; padding:5px; background-image:url(/' $entry->getImage()->getWebPath() . ');background-repeat: no-repeat; background-size:cover; background-position:center;"></div>
  775.                                     </a>
  776.                                     </div>
  777.                                 </div>
  778.                                 <div class="col-md-6">
  779.                                     <div class="imgcontent pull-left">
  780.                                         <a class="cat-label" href="' $url '">
  781.                                             ' $entry->getlabel() . '
  782.                                         </a>
  783.                                     </div><br>
  784.                                     <div class="pull-left">
  785.                                         <p style="color:grey; font-size:14px;;"><i class="fa fa-eye"></i> ' $entry->getReadcount() . '
  786.                                            <i class="fa fa-thumbs-up"></i>' $entry->formatLikes($entry->getLikes()) . '
  787.                                         </p>
  788.                                     </div>
  789.                                 </div>
  790.                             </div>
  791.                         </div>';
  792.                     }
  793.                 }
  794.             }
  795.         }
  796.         return new JsonResponse(['content' => $content,
  797.             'headercontent' => $headimgcontent,]);
  798.     }
  799.     /**
  800.      * @Route("/ajax/blog/{id}/{show_amount}/{page}", name="mod_pagination_ajax")
  801.      */
  802.     public function paginationAction(Request $request$id null$page ''$show_amount '')
  803.     {
  804.         parent::init($request);
  805.         $pagipage $page && (int)$page $page 1;
  806.         // blog
  807.         $blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->find($id);
  808.         //entries
  809.         $entry $blog->getEntries();
  810.         if (empty($show_amount)) {
  811.             $limit $entry->count();
  812.         } else {
  813.             $limit $show_amount;
  814.         }
  815.         $countimg = [];
  816.         foreach ($entry as $Entry) {
  817.             if (!empty($Entry->getImage())) {
  818.                 $countimg[] = $Entry;
  819.             }
  820.         }
  821.         $count count($countimg);
  822.         $offset = (($pagipage $limit) - $limit);
  823.         $pages = (int)ceil($count $limit);
  824.         $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findByNot(['blog' => $id], ['image' => null], [], $limit$offset);
  825.         $likes = [];
  826.         foreach ($entries as $Entry) {
  827.             $likes[] = $Entry->formatLikes($Entry->getLikes());
  828.         }
  829.         $encoders = array(new XmlEncoder(), new JsonEncoder());
  830.         $normalizer = new ObjectNormalizer();
  831.         $normalizer->setCircularReferenceLimit(1);
  832.         $normalizer->setCircularReferenceHandler(function ($object) {
  833.             return $object->getId();
  834.         });
  835.         $normalizer->setIgnoredAttributes(['dateAdd''dateEdit''blog''intro''body''replies''category''user''datePublish''media''page''tags''content''language''metatags''versions']);
  836.         $normalizers = array($normalizer);
  837.         $serializer = new Serializer($normalizers$encoders);
  838.         $jsonContent $serializer->serialize($entries'json');
  839.         $likesContent $serializer->serialize($likes'json');
  840.         $entriesdata json_decode($jsonContentTRUE);
  841.         $likesdata json_decode($likesContentTRUE);
  842.         return new JsonResponse([
  843.             'count' => $count,
  844.             'pages' => $pages,
  845.             'page' => $pagipage,
  846.             'jsonContent' => $entriesdata,
  847.             'pagilikes' => $likesdata,
  848.         ]);
  849.     }
  850.     /**
  851.      * @Route("/ajax/blog/{id}/like/{entryid}/{action}", name="mod_like_dislike_ajax")
  852.      */
  853.     public function likeAction(Request $request$id null$entryid null$action '')
  854.     {
  855.         parent::init($request);
  856.         $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->find($id);
  857.         $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->find($entryid);
  858.         if ($action == 'likes') {
  859.             $em $this->getDoctrine()->getManager();
  860.             $likes_count $Entry->setLikes($Entry->getLikes() + 1);
  861.             $em->persist($likes_count);
  862.             $em->flush();
  863.         }
  864.         if ($action == 'dislikes') {
  865.             $em $this->getDoctrine()->getManager();
  866.             $dislikes_count $Entry->setLikes($Entry->getLikes() - 1);
  867.             $em->persist($dislikes_count);
  868.             $em->flush();
  869.         }
  870.         $all_likes $Entry->getLikes();
  871.         if ($all_likes == null) {
  872.             $all_likes 0;
  873.         }
  874.         return new JsonResponse([
  875.             'all_likes' => $Entry->formatLikes($Entry->getLikes()),
  876.         ]);
  877.     }
  878.     /**
  879.      * Return link data when required within the link form
  880.      *
  881.      * @param  object  Doctrine object
  882.      *
  883.      * @return array   Array with config options
  884.      */
  885.     public function getLinkData($em$language$container$settings)
  886.     {
  887.         $blogs $em->getRepository('TrinityBlogBundle:Blog')->findBy(['language' => $language'settings' => $settings]);
  888.         $categories $em->getRepository('TrinityBlogBundle:Category')->findBy(['language' => $language]);
  889.         return array(
  890.             'blogs' => $blogs,
  891.             'categories' => $categories,
  892.         );
  893.     }
  894.     /**
  895.      * Show dashboard blocks
  896.      *
  897.      * @return array List of blocks
  898.      */
  899.     public function dashboardBlocks()
  900.     {
  901.         $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->findOneBy(array(), array(
  902.             'id' => 'asc'
  903.         ));
  904.         $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findBy(array(), array(
  905.             'dateAdd' => 'desc'
  906.         ), 10);
  907.         $responses '';
  908.         foreach ($entries as $Entry) {
  909.             $responses .= '<tr>
  910.                 <td style="text-align:left;"><a href="' $this->generateUrl('admin_mod_blog_entry_edit', array('id' => $Entry->getId())) . '">' $Entry->getLabel() . '</a></td>
  911.                 <td style="text-align:center;">' $Entry->getReadCount() . '</td>
  912.             </tr>';
  913.         }
  914.         return array(
  915.             array(
  916.                 'title' => $this->trans('Laatste blog berichten''cms'),
  917.                 'class' => '',
  918.                 'content' => '<table><tr><th style="text-align:left;">' $this->trans('Post''cms') .'</th><th style="width:60px;text-align:center;">' $this->trans('Gelezen''cms').'</th></tr>' $responses '</table>' .
  919.                     ($Blog '<div style="text-align:center;padding:20px 0 10px 0;"><a href="' $this->generateUrl('admin_mod_blog_entry_edit', array('id' => $Blog->getId())) . '" class="btn">'.$this->trans('Nieuw bericht''cms').'</a></div>' '')
  920.             ),
  921.         );
  922.     }
  923. }