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

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)
  136.     {
  137.         parent::init($request);
  138.         $related = [];
  139.         if(!empty($config['uri'])){
  140.             $config['uri'] = preg_replace('/^\//'''$config['uri']);
  141.         }
  142.         if(!empty($config['uri_overview'])){
  143.             $config['uri_overview'] = preg_replace('/^\//'''$config['uri_overview']);
  144.         }
  145.         if(!empty($config['type']) && $config['type'] == 'popular'){
  146.             $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->findOneById($config['id']);
  147.             $html '<ul>';
  148.             $page null;
  149.             // Add check for TrinityBlogBundle for legacy support
  150.             $blocks $this->getDoctrine()->getRepository('CmsBundle:PageBlock')->findByData('TrinityBlogBundle''is_overview');
  151.             foreach($blocks as $Block){
  152.                 $p $Block->getWrapper()->getPage();
  153.                 if($p->getLanguage() == $this->language){
  154.                     $page $p;
  155.                     break;
  156.                 }
  157.             }
  158.             $blocks $this->getDoctrine()->getRepository('CmsBundle:PageBlock')->findByData('TrinityBlogBundle''is_overview');
  159.             foreach($blocks as $Block){
  160.                 $p $Block->getWrapper()->getPage();
  161.                 if($p->getLanguage() == $this->language){
  162.                     $page $p;
  163.                     break;
  164.                 }
  165.             }
  166.             $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedNow($Blog4);
  167.             foreach($entries as $Entry){
  168.                 if($page){
  169.                     $baseUrl $this->generateUrl($page->getSlugKey());
  170.                     $slug '/' $Entry->getId() . '/' $Entry->getDefaultSlug();
  171.                     if(!empty($Entry->getSlug())){
  172.                         $slug '/' $Entry->getSlug();
  173.                     }
  174.                     $url $baseUrl $slug;
  175.                 }else{
  176.                     if(!empty($Entry->getSlug())){
  177.                         $url $this->generateUrl($request->get('_route'), ['param1' => $Entry->getSlug()]);
  178.                     }else{
  179.                         $url $this->generateUrl($request->get('_route'), ['param1' => $config['id'], 'param2' => $Entry->getDefaultSlug()]);
  180.                     }
  181.                 }
  182.                 $html .= '<li><a href="' $url '">' $Entry->getLabel() . '</a></li>';
  183.             }
  184.             $html .= '</ul>';
  185.             return $html;
  186.         }
  187.         $message '';
  188.         if($params[0] == 'prefs'){
  189.             $emailhash openssl_decrypt($params[1], 'aes-128-cbc''blogdata.0010000'null'blogdata.0010000');
  190.             $Mailreply null;
  191.             if($emailhash){
  192.                 $Mailreply $this->getDoctrine()->getRepository('TrinityBlogBundle:Mailreply')->findOneByEmail($emailhash);
  193.                 if(empty($Mailreply)){
  194.                     $Mailreply = new \App\Trinity\BlogBundle\Entity\Mailreply();
  195.                     $Mailreply->setEmail($emailhash);
  196.                     $Mailreply->setEnabled(true);
  197.                 }
  198.                 $Form $this->createFormBuilder($Mailreply)
  199.                         ->add('enabled'CheckboxType::class, ['label' => $this->trans('E-mail reacties ontvangen''cms')])
  200.                         ->add('notify_new_blog'CheckboxType::class, ['label' => $this->trans('E-mail update ontvangen bij nieuwe berichten''cms')])
  201.                         ->setMethod('post')
  202.                         ->getForm();
  203.                 $Form->handleRequest($request);
  204.                 if ($Form->isSubmitted() && $Form->isValid())
  205.                 {
  206.                     $em $this->getDoctrine()->getManager();
  207.                     $em->persist($Mailreply);
  208.                     $em->flush();
  209.                     $message $this->trans('De voorkeuren zijn opgeslagen.''cms');
  210.                 }
  211.             }
  212.             return $this->renderView('@TrinityBlog/default/prefs.html.twig', [
  213.                 'Form'      => ($Mailreply $Form->createView() : null),
  214.                 'emailhash' => $emailhash,
  215.                 'message'   => $message,
  216.             ]);
  217.         }
  218.         if (!empty($_GET['id'])) {
  219.             $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneById($_GET['id']);
  220.             if ($Entry) {
  221.                 if(!empty($Entry->getSlug())){
  222.                     $url $this->generateUrl($request->get('_route'), ['param1' => $Entry->getSlug()]);
  223.                 }else{
  224.                     $url $this->generateUrl($request->get('_route'), ['param1' => $_GET['id'], 'param2' => $Entry->getDefaultSlug()]);
  225.                 }
  226.             } else {
  227.                 $url $this->generateUrl($request->get('_route'));
  228.             }
  229.             header("HTTP/1.1 301 Moved Permanently");
  230.             header('Location: ' $url);
  231.             exit;
  232.         }
  233.         if (!empty($params[0])) {
  234.             $inline_error false;
  235.             if (!isset($config['notblogspecific']))
  236.             {
  237.                 $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->findOneById($config['id']);
  238.                 if(!is_numeric($params[0])){
  239.                     $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneBy(['slug' => $params[0], 'blog' => $Blog]);
  240.                 }else{
  241.                     $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneBy(['id' => $params[0], 'blog' => $Blog]);
  242.                     if(!empty($Entry->getSlug())){
  243.                         $url $this->generateUrl($request->get('_route'), ['param1' => $Entry->getSlug()]);
  244.                         header("HTTP/1.1 301 Moved Permanently");
  245.                         header('Location: ' $url);
  246.                         exit;
  247.                     }
  248.                 }
  249.             } else {
  250.                 $Blog null;
  251.                 // FIXME Blog restriction here?
  252.                 if(!is_numeric($params[0])){
  253.                     $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneBy(['slug' => $params[0]]);
  254.                 }else{
  255.                     $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findOneBy(['id' => $params[0]]);
  256.                     if(!empty($Entry) && !empty($Entry->getSlug())){
  257.                         $url $this->generateUrl($request->get('_route'), ['param1' => $Entry->getSlug()]);
  258.                         header("HTTP/1.1 301 Moved Permanently");
  259.                         header('Location: ' $url);
  260.                         exit;
  261.                     }
  262.                 }
  263.             }
  264.             if(empty($Entry))
  265.             {
  266.                 header("HTTP/1.1 301 Moved Permanently");
  267.                 header("location: " $this->generateUrl($request->get('_route')));
  268.                 exit;
  269.             }
  270.             $em $this->getDoctrine()->getManager();
  271.             $Entry->setReadcount($Entry->getReadcount() + 1);
  272.             $em->persist($Entry);
  273.             $em->flush();
  274.             if($Entry->getCategory()->count()){
  275.                 $C $Entry->getCategory()->first();
  276.                 $related $C->getEntry();
  277.             }
  278.             $Reply = new \App\Trinity\BlogBundle\Entity\Reply();
  279.             
  280.             $Form $this->createFormBuilder($Reply)
  281.                     ->add('firstname'TextType::class, ['data' => ($this->getUser() ? $this->getUser()->getFirstname() : '')])
  282.                     ->add('lastname'TextType::class, ['data' => ($this->getUser() ? $this->getUser()->getLastname() : '')])
  283.                     ->add('email'EmailType::class, ['data' => ($this->getUser() ? $this->getUser()->getEmail() : '')])
  284.                     ->add('comment'TextareaType::class, ['required' => true])
  285.                     ->setMethod('post')
  286.                     ->getForm();
  287.             $Form->handleRequest($request);
  288.             // View entry details
  289.             //if (!empty($_POST)) {
  290.             if ($Form->isSubmitted() && $Form->isValid())
  291.             {
  292.                 $validCaptcha $this->Settings->validateGoogleRecaptcha($request->request->get('g-recaptcha-response'));
  293.                 if(($validCaptcha))
  294.                 {
  295.                     if(!empty($_POST['replyto'])){
  296.                         $Parent $this->getDoctrine()->getRepository('TrinityBlogBundle:Reply')->find($_POST['replyto']);
  297.                         if($Parent){
  298.                             $Reply->setReply($Parent);
  299.                             $Mailreply $this->getDoctrine()->getRepository('TrinityBlogBundle:Mailreply')->findOneByEmail($Parent->getEmail());
  300.                             if(!empty($Parent->getEmail()) && ($Mailreply && $Mailreply->getEnabled()) || empty($Mailreply)){
  301.                                 $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  302.                                 $emailToHash openssl_encrypt($Parent->getEmail(), 'aes-128-cbc''blogdata.0010000'null'blogdata.0010000');
  303.                                 // $emailToHash = urlencode($emailToHash);
  304.                                 $message =
  305.                                         $this->trans('<p>Beste %firstname%,</p>' "\n".
  306.                                             '<p>Een bezoeker heeft gereageerd op je reactie:</p>' "\n".
  307.                                             '<div style="border:solid 1px black;border-width:1px 0;padding: 10px 0;margin: 10px 0;">%comment%</div>'.
  308.                                             '<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/>'.
  309.                                             '<a href="%prefsurl%">Klik hier</a> om uw voorkeuren voor deze geautomatiseerde e-mails te beheren.</p>',
  310.                                         'cms',
  311.                                         [
  312.                                             '%firstname%' => $Parent->getFirstname(),
  313.                                             '%comment%' => $Reply->getcomment(),
  314.                                             '%entryurl%' => $baseurl $this->generateUrl($request->get('_route')) . '/' . ($Entry->getSlug() ? $Entry->getSlug() : $Entry->getId() . '/' $Entry->getDefaultSlug()),
  315.                                             '%prefsurl%' => $baseurl $this->generateUrl($request->get('_route')) . '/prefs/' $emailToHash,
  316.                                         ]);
  317.                                 
  318.                                 /*ob_start();
  319.                                 $htmlDebug = $twig->render('/emails/notify.html.twig', [
  320.                                     'label' => '',
  321.                                     'message' => $message
  322.                                 ]);
  323.                                 die($htmlDebug);*/
  324.                                 // Send notification to person replied to
  325.                                 $mailer = clone $this->mailer;
  326.                                 $mailer->init();
  327.                                 $mailer->setSubject($this->trans('Nieuwe reactie op je reactie bij het blog bericht''cms') . ' ' $Entry->getLabel())
  328.                                         ->setTo($Parent->getEmail())
  329.                                         ->setTwigBody('/emails/notify.html.twig', [
  330.                                             'label' => '',
  331.                                             'message' => $message
  332.                                         ])
  333.                                         ->setPlainBody(strip_tags($message));
  334.                                 $status $mailer->execute_forced();
  335.                             }else{
  336.                                 // NOT WANT EMAIL
  337.                             }
  338.                         }
  339.                     }
  340.                     $Mailreply $this->getDoctrine()->getRepository('TrinityBlogBundle:Mailreply')->findOneByEmail($Reply->getEmail());
  341.                     if(empty($Mailreply)){
  342.                         // Mailreply setting doesnt exist yet, make it - enabled.
  343.                         $Mailreply = new \App\Trinity\BlogBundle\Entity\Mailreply();
  344.                         $Mailreply->setEmail($Reply->getEmail());
  345.                     }
  346.                     $Mailreply->setEnabled(!empty($_POST['optin']['reply']));
  347.                     $Mailreply->setNotifyNewBlog(!empty($_POST['optin']['new']));
  348.                     $em->persist($Mailreply);
  349.                     // Check if already approved
  350.                     $approved false;
  351.                     /*foreach($this->getDoctrine()->getRepository('TrinityBlogBundle:Reply')->findBy(['email' => $Reply->getEmail()]) as $R){
  352.                         if(!empty($R)){
  353.                             if($R->getApproved() != true){
  354.                                 $Mailreply = $this->getDoctrine()->getRepository('TrinityBlogBundle:Mailreply')->findOneByEmail($R->getEmail());
  355.                                 if($Mailreply && $Mailreply->getApproved()){
  356.                                     $approved = true;
  357.                                 }else{
  358.                                     $approved = false;
  359.                                 }
  360.                             }
  361.                         }
  362.                     }*/
  363.                     $Reply->setApproved($approved);
  364.                     $Reply->setEntry($Entry);
  365.                     $Reply->setIp($_SERVER['REMOTE_ADDR']);
  366.                     $Reply->setDate(new \Datetime('now'));
  367.                     $em->persist($Reply);
  368.                     $em->flush();
  369.                     // Redirect to the same page, this prevents that the form can be submitted multiple times by refreshing the page.
  370.                     header_remove();
  371.                     header("HTTP/1.1 301 Moved Permanently");
  372.                     header('Location: ' $request->getUri());
  373.                     exit;
  374.                 } else {
  375.                     $inline_error $this->trans('Ongeldige reCAPTCHA, uw reactie is niet opgeslagen.''cms');
  376.                 }
  377.             }
  378.             $replies $this->getDoctrine()->getRepository('TrinityBlogBundle:Reply')->findBy(['entry' => $Entry'reply' => null'approved' => true], ['date' => 'desc']);
  379.             $replies_count $this->getDoctrine()->getRepository('TrinityBlogBundle:Reply')->count(['entry' => $Entry'approved' => true], ['date' => 'desc']);
  380.             if (!isset($config['notblogspecific'])) {
  381.                 // FIXME There function should also check category is needed?
  382.                 $popular $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedNow($Blog);
  383.                 $recentFour $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findRecentEntriesPublishedNow($Blog3$Entry->getId());
  384.                 // $recentFour = $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findBy(['blog' => $Blog], ['datePublish' => 'desc'], 4);
  385.             } else {
  386.                 if (!isset($config['category_id'])) {
  387.                     $categories null;
  388.                 } else {
  389.                     $categories $config['category_id'];
  390.                 }
  391.                 $popular $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedByCategoryNow($this->language$categories);
  392.                 $recentFour $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedByCategoryNow($this->language$categories4);
  393.             }
  394.             $products = [];
  395.             $WebshopSettings null;
  396.             if(!empty($Entry->getProducts())){
  397.                 foreach($Entry->getProducts() as $id){
  398.                     $products[$id] = $this->getDoctrine()->getRepository('TrinityWebshopBundle:Product')->find($id);
  399.                 }
  400.                 $WebshopSettings $this->getDoctrine()->getRepository('TrinityWebshopBundle:Settings')->find(1);
  401.             }
  402.             $content $Entry->getBody();
  403.             if(preg_match_all('/\[caption\s(.*?)\](.*?)\[\/caption\]/'$content$m)){
  404.                 if(!empty($m)){
  405.                     foreach($m[1] as $i => $params){
  406.                         $src $m[0][$i];
  407.                         $inner $m[2][$i];
  408.                         $inner preg_replace("/ width=\"\d+\"/"''$inner);
  409.                         $inner preg_replace("/ height=\"\d+\"/"''$inner);
  410.                         $inner preg_replace("/ class=\".*?\"/"''$inner);
  411.                         $config = [];
  412.                         $params explode(' '$params);
  413.                         foreach($params as $p){
  414.                             $p explode('='$p);
  415.                             $config[$p[0]] = str_replace('"'''$p[1]);
  416.                         }
  417.                         $widget '<div class="caption-widget ' . (!empty($config['align']) ? $config['align'] : '') . '">
  418.                             ' preg_replace('/Bron(.*?)http/''Bron$1<br/>http'$inner) . '
  419.                         </div>';
  420.                         $content str_replace($src$widget$content);
  421.                     }
  422.                 }
  423.             }
  424.             // $content = '<h1>:\'-)</h1>';
  425.             // $content            
  426.             $Entry->setBody($content);
  427.             $most_recent $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findBy(array('blog' => $Entry->getBlog()), array('datePublish' => 'desc'), 5);
  428.             if($Entry->getIsExternal() && !empty($Entry->getExternalUrl())){
  429.                 header("HTTP/1.1 301 Moved Permanently");
  430.                 header('Location: ' $Entry->getExternalUrl());
  431.                 exit;
  432.             }
  433.             return $this->renderView('@TrinityBlog/default/entry.html.twig'$this->attributes(array(
  434.                 'config' => $config,
  435.                 'Entry' => $Entry,
  436.                 'Blog' => $Blog,
  437.                 'recentFour' => $recentFour,
  438.                 'replies' => $replies,
  439.                 'replies_count' => $replies_count,
  440.                 'popular' => $popular,
  441.                 'related' => $related,
  442.                 'Form' => $Form->createView(),
  443.                 'products' => $products,
  444.                 'settings' => $this->Settings,
  445.                 'most_recent' => $most_recent,
  446.                 'installed' => $this->installed,
  447.                 'WebshopSettings' => $WebshopSettings,
  448.                 'error' => '',
  449.                 'inline_error' => $inline_error,
  450.             )));
  451.         } else {
  452.             // View index
  453.             if (!isset($config['notblogspecific'])) {
  454.                 $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->findOneById($config['id']);
  455.                 // Tmp fix for concept being 'null'
  456.                 /*$em = $this->getDoctrine()->getManager();
  457.                 $tmpEntries = $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findBy(array('blog' => $Blog, 'concept' => null));
  458.                 foreach($tmpEntries as $Entry){
  459.                     if(!$Entry->getConcept()){
  460.                         $Entry->setConcept(false);
  461.                         $em->persist($Entry);
  462.                     }
  463.                     $em->flush();
  464.                 }*/
  465.                 if ((int)$config['limit'] > 0) {
  466.                     $page = isset($_GET['page']) && (int)$_GET['page'] > $_GET['page'] : 1;
  467.                     $offset = (($page 1) * (int)$config['limit']);
  468.                 } else {
  469.                     $page 1;
  470.                     $offset 0;
  471.                 }
  472.                 $limit = !empty($config['limit']) ? $config['limit'] : null;
  473.                 if (isset($config['category_id']) && !empty($config['category_id']))
  474.                 {
  475. //                    $category = $this->getDoctrine()->getRepository('TrinityBlogBundle:Category')->find($config['category_id']);
  476.                     $count count($this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findEntriesByCategory($Blog$config['category_id']));
  477.                     $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findEntriesByCategory($Blog$config['category_id'], false$limit$offset);
  478.                 } else {
  479.                     $count count($this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findEntriesPublishedNow($Blogfalse));
  480.                     $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findEntriesPublishedNow($Blogfalse$limit$offset);
  481.                 }
  482.                 if ((int)$config['limit'] > 0) {
  483.                     $pages ceil($count / (int)$config['limit']);
  484.                 } else {
  485.                     $pages 0;
  486.                 }
  487.                 $popular $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedNow($Blog);
  488.             } else {
  489.                 $Blog null;
  490.                     
  491.                 // Tmp fix for concept being 'null'
  492. /*
  493.                 $em = $this->getDoctrine()->getManager();
  494.                 $tmpEntries = $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findBy(array('blog' => $Blog, 'concept' => null));
  495.                 foreach($tmpEntries as $Entry){
  496.                     if(!$Entry->getConcept()){
  497.                         $Entry->setConcept(false);
  498.                         $em->persist($Entry);
  499.                     }
  500.                     $em->flush();
  501.                 }
  502. */
  503.                 if ((int)$config['limit'] > 0) {
  504.                     $page = isset($_GET['page']) && (int)$_GET['page'] > $_GET['page'] : 1;
  505.                     $offset = (($page 1) * (int)$config['limit']);
  506.                 } else {
  507.                     $page 1;
  508.                     $offset 0;
  509.                 }
  510.                 $limit = !empty($config['limit']) ? $config['limit'] : null;
  511.                 if (isset($config['category_id']) && !empty($config['category_id']))
  512.                 {
  513.                     $count count($this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->FindEntriesByLanguage($this->language$config['category_id']));
  514.                     $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->FindEntriesByLanguage($this->language$config['category_id'], $limit$offset);
  515.                 } else {
  516.                     $count count($this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->FindEntriesByLanguage($this->language));
  517.                     $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->FindEntriesByLanguage($this->languagenull$limit$offset);
  518.                 }
  519.                 if ((int)$config['limit'] > 0) {
  520.                     $pages ceil($count / (int)$config['limit']);
  521.                 } else {
  522.                     $pages 0;
  523.                 }
  524. // XXX implimenteren
  525.                 $popular null//$this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findPopularEntriesPublishedNow($Blog);
  526.             }
  527.             if (isset($config['show_slider']) && isset($config['limit']) && $config['limit'] > 0) {
  528.                 return $this->renderView('@TrinityBlog/default/slider.html.twig'$this->attributes(array(
  529.                     'config' => $config,
  530.                     'Blog' => $Blog,
  531.                     'entries' => $entries,
  532.                     'page' => $page,
  533.                     'pages' => $pages,
  534.                 )));
  535.             } else {
  536.                 return $this->renderView('@TrinityBlog/default/blog.html.twig'$this->attributes(array(
  537.                     'config' => $config,
  538.                     'Blog' => $Blog,
  539.                     'entries' => $entries,
  540.                     'popular' => $popular,
  541.                     'page' => $page,
  542.                     'limit' => $limit,
  543.                     'offset' => $offset,
  544.                     'pages' => $pages,
  545.                 )));
  546.             }
  547.         }
  548.     }
  549.     public static function metatagHandler($em$params$bundledata$request){
  550.         if(!is_numeric($params[0])){
  551.             $Entry $em->getRepository('TrinityBlogBundle:Entry')->findOneBy(['slug' => $params[0]]);
  552.         }else{
  553.             $Entry $em->getRepository('TrinityBlogBundle:Entry')->findOneBy(['id' => $params[0]]);
  554.         }
  555.         $entryMetatags = [];
  556.         $ignoreTags    = ['description'];
  557.         $metatags      $em->getRepository('CmsBundle:Metatag')->findBy(['system' => false]);
  558.         if($Entry){
  559.             foreach($metatags as $index => $Metatag){
  560.                 if(!in_array($Metatag->getKey(), $ignoreTags)){
  561.                     $EntryMetatag $em->getRepository('TrinityBlogBundle:Metatag')->findOneBy(['metatag' => $Metatag'entry' => $Entry]);
  562.                     if(empty($EntryMetatag)){
  563.                         $EntryMetatag = new \App\Trinity\BlogBundle\Entity\Metatag();
  564.                         $EntryMetatag->setMetatag($Metatag);
  565.                     }
  566.                     if(empty($EntryMetatag->getValue())){
  567.                         if($EntryMetatag->getMetatag()->getKey() == 'keywords'){
  568.                             if(!empty($Entry->getSeoKeywords())){
  569.                                 $EntryMetatag->setValue($Entry->getSeoKeywords());
  570.                             }
  571.                         }
  572.                         if($EntryMetatag->getMetatag()->getKey() == 'og:title'){
  573.                             if(!empty($Entry->getSeoTitle())){
  574.                                 $EntryMetatag->setValue($Entry->getSeoTitle());
  575.                             }else{
  576.                                 $EntryMetatag->setValue($Entry->getLabel());
  577.                             }
  578.                         }
  579.                         if($EntryMetatag->getMetatag()->getKey() == 'og:description'){
  580.                             if(!empty($Entry->getSeoDescription())){
  581.                                 $EntryMetatag->setValue($Entry->getSeoDescription());
  582.                             }else{
  583.                                 $EntryMetatag->setValue($Entry->getIntro());
  584.                             }
  585.                         }
  586.                         if($EntryMetatag->getMetatag()->getKey() == 'og:image'){
  587.                             if($Entry->getMedia()){
  588.                                 foreach($Entry->getMedia() as $Media){
  589.                                     $EntryMetatag->setValue('/' $Media->getWebPath());
  590.                                     break;
  591.                                 }
  592.                             }
  593.                         }
  594.                     }
  595.                         if($EntryMetatag->getMetatag()->getKey() == 'og:url'){
  596.                             $Settings $Entry->getBlog()->getSettings();
  597.                             if ($Settings->getForceHttps()) {
  598.                                 $url 'https://';
  599.                             } else {
  600.                                 $url 'http://';
  601.                             }
  602.                             $url $url $Settings->getHost();
  603.                             $url $url $request->getRequestUri();
  604.                             $EntryMetatag->setValue($url);
  605.                         }
  606.                     if(!empty($EntryMetatag->getValue())){
  607.                         $entryMetatags[] = $EntryMetatag;
  608.                     }
  609.                 }
  610.             }
  611.         }
  612.         return $entryMetatags;
  613.     }
  614.     public static function resourcesHandler($Settings, array $bundledatastring $projectDir) : ?string
  615.     {
  616.         $resources null;
  617.         $layoutKey = !empty($Settings->getOverrideKey()) ? trim($Settings->getOverrideKey()) . '/' '';
  618.         $resource_file 'resources.json';
  619.         if (isset($bundledata['allow_replies']) && $bundledata['allow_replies'] == 1) {
  620.             $resource_file 'resources_replies.json';
  621.         }
  622.         // check if file exists or build array in code and return that.
  623.         $file __DIR__ "/../Resources/views/default/" $resource_file;
  624.         $override $projectDir '/public/custom/' $layoutKey 'blog/' $resource_file;
  625.         if (file_exists($override)) {
  626.             $resources $override;
  627.         } else if (file_exists($file)) {
  628.             $resources $file;
  629.         }
  630.         return $resources;
  631.     }
  632.     /**
  633.      * @Route("/ajax/blog/category/{id}/{uri}/{catid}", name="mod_blog_category_ajax")
  634.      */
  635.     public function getCategory(Request $request$id$uri ""$catid 0)
  636.     {
  637.         $blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->find($id);
  638.         $current_category $this->getDoctrine()->getRepository('TrinityBlogBundle:Category')->find($catid);
  639.         if ($catid == 0) {
  640.             $current_category $blog;
  641.         }
  642.         $content '';
  643.         if ($current_category == $blog) {
  644.             $entries_tmp $current_category->getEntries();
  645.         } else {
  646.             $entries_tmp $current_category->getEntry();
  647.         }
  648.         $entries = new ArrayCollection();
  649.         for ($i $entries_tmp->count(); $i > -1$i--) {
  650.             $e $entries_tmp->get($i);
  651.             if ($e)
  652.                 $entries->add($e);
  653.         }
  654.         $first false;
  655.         foreach ($entries as $entry) {
  656.             $url $request->getBaseUrl() . '/' urldecode($uri) . '/' $entry->getId() . '/' $entry->getSlug();
  657.             if ($entry->getImage()) {
  658.                 if (!$first) {
  659.                     if ($entry->getImage()->getHeight() < 600) {
  660.                         $imgsize 'catimg';
  661.                     } else {
  662.                         $imgsize 'catimg-full-width';
  663.                     }
  664.                     $headimgcontent '
  665.                     <div class="img-wrap">
  666.                         <a class="imageEntry" href="' $url '">
  667.                             <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;">
  668.                             </div>
  669.                             <div class="title-holder">
  670.                                 <h4 class="h-post-title">' $entry->getLabel() . '</h4>
  671.                                 <span class="icon-holder h-post-title">
  672.                                     <i class="fa fa-thumbs-up"></i>' $entry->getLikes() . '
  673.                                     <i class="fa fa-eye"></i> ' $entry->getReadCount() . '
  674.                                 </span>
  675.                             </div>
  676.                             <div class="overlay"></div>
  677.                         </a>
  678.                     </div>';
  679.                     $first true;
  680.                 } else {
  681.                     if ($entry->getBlog() == $blog) {
  682.                         $content .= '
  683.                         <div class="pull-left" style="padding-top:10px;">
  684.                             <div class="row">
  685.                                 <div class="col-md-6">
  686.                                     <div class="img-wrap">
  687.                                     <a class="imageEntry" href="' $url '">
  688.                                         <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>
  689.                                     </a>
  690.                                     </div>
  691.                                 </div>
  692.                                 <div class="col-md-6">
  693.                                     <div class="imgcontent pull-left">
  694.                                         <a class="cat-label" href="' $url '">
  695.                                             ' $entry->getlabel() . '
  696.                                         </a>
  697.                                     </div><br>
  698.                                     <div class="pull-left">
  699.                                         <p style="color:grey; font-size:14px;;"><i class="fa fa-eye"></i> ' $entry->getReadcount() . '
  700.                                            <i class="fa fa-thumbs-up"></i>' $entry->formatLikes($entry->getLikes()) . '
  701.                                         </p>
  702.                                     </div>
  703.                                 </div>
  704.                             </div>
  705.                         </div>';
  706.                     }
  707.                 }
  708.             }
  709.         }
  710.         return new JsonResponse(['content' => $content,
  711.             'headercontent' => $headimgcontent,]);
  712.     }
  713.     /**
  714.      * @Route("/ajax/blog/{id}/{show_amount}/{page}", name="mod_pagination_ajax")
  715.      */
  716.     public function paginationAction(Request $request$id null$page ''$show_amount '')
  717.     {
  718.         parent::init($request);
  719.         $pagipage $page && (int)$page $page 1;
  720.         // blog
  721.         $blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->find($id);
  722.         //entries
  723.         $entry $blog->getEntries();
  724.         if (empty($show_amount)) {
  725.             $limit $entry->count();
  726.         } else {
  727.             $limit $show_amount;
  728.         }
  729.         $countimg = [];
  730.         foreach ($entry as $Entry) {
  731.             if (!empty($Entry->getImage())) {
  732.                 $countimg[] = $Entry;
  733.             }
  734.         }
  735.         $count count($countimg);
  736.         $offset = (($pagipage $limit) - $limit);
  737.         $pages = (int)ceil($count $limit);
  738.         $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findByNot(['blog' => $id], ['image' => null], [], $limit$offset);
  739.         $likes = [];
  740.         foreach ($entries as $Entry) {
  741.             $likes[] = $Entry->formatLikes($Entry->getLikes());
  742.         }
  743.         $encoders = array(new XmlEncoder(), new JsonEncoder());
  744.         $normalizer = new ObjectNormalizer();
  745.         $normalizer->setCircularReferenceLimit(1);
  746.         $normalizer->setCircularReferenceHandler(function ($object) {
  747.             return $object->getId();
  748.         });
  749.         $normalizer->setIgnoredAttributes(['dateAdd''dateEdit''blog''intro''body''replies''category''user''datePublish''media''page''tags''content''language''metatags''versions']);
  750.         $normalizers = array($normalizer);
  751.         $serializer = new Serializer($normalizers$encoders);
  752.         $jsonContent $serializer->serialize($entries'json');
  753.         $likesContent $serializer->serialize($likes'json');
  754.         $entriesdata json_decode($jsonContentTRUE);
  755.         $likesdata json_decode($likesContentTRUE);
  756.         return new JsonResponse([
  757.             'count' => $count,
  758.             'pages' => $pages,
  759.             'page' => $pagipage,
  760.             'jsonContent' => $entriesdata,
  761.             'pagilikes' => $likesdata,
  762.         ]);
  763.     }
  764.     /**
  765.      * @Route("/ajax/blog/{id}/like/{entryid}/{action}", name="mod_like_dislike_ajax")
  766.      */
  767.     public function likeAction(Request $request$id null$entryid null$action '')
  768.     {
  769.         parent::init($request);
  770.         $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->find($id);
  771.         $Entry $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->find($entryid);
  772.         if ($action == 'likes') {
  773.             $em $this->getDoctrine()->getManager();
  774.             $likes_count $Entry->setLikes($Entry->getLikes() + 1);
  775.             $em->persist($likes_count);
  776.             $em->flush();
  777.         }
  778.         if ($action == 'dislikes') {
  779.             $em $this->getDoctrine()->getManager();
  780.             $dislikes_count $Entry->setLikes($Entry->getLikes() - 1);
  781.             $em->persist($dislikes_count);
  782.             $em->flush();
  783.         }
  784.         $all_likes $Entry->getLikes();
  785.         if ($all_likes == null) {
  786.             $all_likes 0;
  787.         }
  788.         return new JsonResponse([
  789.             'all_likes' => $Entry->formatLikes($Entry->getLikes()),
  790.         ]);
  791.     }
  792.     /**
  793.      * Return link data when required within the link form
  794.      *
  795.      * @param  object  Doctrine object
  796.      *
  797.      * @return array   Array with config options
  798.      */
  799.     public function getLinkData($em$language$container$settings)
  800.     {
  801.         $blogs $em->getRepository('TrinityBlogBundle:Blog')->findBy(['language' => $language'settings' => $settings]);
  802.         $categories $em->getRepository('TrinityBlogBundle:Category')->findBy(['language' => $language]);
  803.         return array(
  804.             'blogs' => $blogs,
  805.             'categories' => $categories,
  806.         );
  807.     }
  808.     /**
  809.      * Show dashboard blocks
  810.      *
  811.      * @return array List of blocks
  812.      */
  813.     public function dashboardBlocks()
  814.     {
  815.         $Blog $this->getDoctrine()->getRepository('TrinityBlogBundle:Blog')->findOneBy(array(), array(
  816.             'id' => 'asc'
  817.         ));
  818.         $entries $this->getDoctrine()->getRepository('TrinityBlogBundle:Entry')->findBy(array(), array(
  819.             'dateAdd' => 'desc'
  820.         ), 10);
  821.         $responses '';
  822.         foreach ($entries as $Entry) {
  823.             $responses .= '<tr>
  824.                 <td style="text-align:left;"><a href="' $this->generateUrl('admin_mod_blog_entry_edit', array('id' => $Entry->getId())) . '">' $Entry->getLabel() . '</a></td>
  825.                 <td style="text-align:center;">' $Entry->getReadCount() . '</td>
  826.             </tr>';
  827.         }
  828.         return array(
  829.             array(
  830.                 'title' => $this->trans('Laatste blog berichten''cms'),
  831.                 'class' => '',
  832.                 '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>' .
  833.                     ($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>' '')
  834.             ),
  835.         );
  836.     }
  837. }