src/Web/Modules/Advertisement/Controller/Detail.php line 58

Open in your IDE?
  1. <?php
  2. namespace Web\Modules\Advertisement\Controller;
  3. use Application\Core\Engine\Model as ApplicationModel;
  4. use Application\Modules\Advertisement\Model\Advertisement;
  5. use Application\Modules\Profiles\Model\Profiles;
  6. use Common\Doctrine\Entity\Meta;
  7. use Web\Core\Engine\Base\Block as WebBaseBlock;
  8. use Web\Core\Engine\Form as WebForm;
  9. use Web\Core\Header\MetaLink;
  10. use Web\Core\Language\Language as FL;
  11. use Web\Core\Engine\Model as WebModel;
  12. use Web\Core\Engine\Navigation as WebNavigation;
  13. use Web\Modules\Advertisement\Model\Advertisement as WebAdvertisementModel;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. class Detail extends WebBaseBlock
  16. {
  17.     private $comments;
  18.     private $form;
  19.     private $blogPost;
  20.     private $settings;
  21.     private $media;
  22.     private $logo;
  23.     private $sales;
  24.     private $news;
  25.     private $tags;
  26.     public function execute(): void
  27.     {
  28.         parent::execute();
  29.         $this->loadTemplate();
  30.         $this->getData();
  31.         $this->buildForm();
  32.         $this->handleForm();
  33.         $this->parse();
  34.     }
  35.     protected function setMeta(Meta $meta): void
  36.     {
  37.         $apikey $this->get('icebird.settings')->get('Core''google_maps_key');
  38.         $this->header->addJS('https://maps.googleapis.com/maps/api/js?key='.$apikey.'&callback=initMap');
  39.         parent::setMeta($meta);
  40.         if ($this->url->getParameter('revision''int') !== 0) {
  41.             $this->header->addMetaData(['name' => 'robots''content' => 'noindex, nofollow'], true);
  42.         }
  43.     }
  44.     private function getAdvertisementPost(): array
  45.     {
  46.         if ($this->url->getParameter(1) === null) {
  47.             throw new NotFoundHttpException();
  48.         }
  49.         if ($this->url->getParameter('revision''int') === null) {
  50.             return $this->completeAdvertisementPost(WebAdvertisementModel::get($this->url->getParameter(1)));
  51.         }
  52.         return $this->completeAdvertisementPost(
  53.             WebAdvertisementModel::getRevision(
  54.                 $this->url->getParameter(1),
  55.                 $this->url->getParameter('revision''int')
  56.             )
  57.         );
  58.     }
  59.     private function completeAdvertisementPost(array $blogPost): array
  60.     {
  61.         if (empty($blogPost)) {
  62.             throw new NotFoundHttpException();
  63.         }
  64.         $baseCategoryUrl WebNavigation::getUrlForBlock($this->getModule(), 'Category');
  65.         $blogPost['category_full_url'] = $baseCategoryUrl '/' $blogPost['category_url'];
  66.         $baseDetailUrl WebNavigation::getUrlForBlock($this->getModule(), 'Detail');
  67.         $blogPost['full_url'] = $baseDetailUrl '/' $blogPost['url'];
  68.         return $blogPost;
  69.     }
  70.     private function getAdvertisementPostComments(): array
  71.     {
  72.         $comments WebAdvertisementModel::getComments($this->blogPost['id']);
  73.         $this->blogPost['comments_count'] = count($comments);
  74.         $rating 0;
  75.         $totalRating 0;
  76.         if($comments)
  77.         {
  78.             foreach($comments as $comment)
  79.             {
  80.                 $rating += $comment['rating'];
  81.             }
  82.             $totalRating $rating count($comments);
  83.         }
  84.         $this->blogPost['rating'] = round($totalRating);
  85.         return $comments;
  86.     }
  87.     private function getModuleSettings(): array
  88.     {
  89.         $moduleSettings $this->get('icebird.settings')->getForModule($this->getModule());
  90.         if (!$moduleSettings['allow_comments']) {
  91.             $this->blogPost['allow_comments'] = false;
  92.         }
  93.         return $moduleSettings;
  94.     }
  95.     private function getData(): void
  96.     {
  97.         $this->blogPost $this->getAdvertisementPost();
  98.         $this->comments $this->getAdvertisementPostComments();
  99.         $this->settings $this->getModuleSettings();
  100.         $this->media WebAdvertisementModel::getAdvertisementMedia($this->blogPost['id']);
  101.         $this->logo WebAdvertisementModel::getAdvertisementMedia($this->blogPost['id'], 'logo');
  102.         $this->sales WebAdvertisementModel::getAdvertisementMedia($this->blogPost['id'], 'sales');
  103.         $this->news WebAdvertisementModel::getAdvertisementMedia($this->blogPost['id'], 'news');
  104.         Advertisement::addStats('detailpagina'$this->blogPost['id']);
  105.     }
  106.     private function addLinkToRssFeeds(): void
  107.     {
  108.         $this->header->addRssLink(
  109.             $this->get('icebird.settings')->get($this->getModule(), 'rss_title_' LANGUAGESITE_DEFAULT_TITLE),
  110.             WebNavigation::getUrlForBlock($this->getModule(), 'Rss')
  111.         );
  112.         $this->header->addRssLink(
  113.             vsprintf(FL::msg('CommentsOn'), [$this->blogPost['title']]),
  114.             WebNavigation::getUrlForBlock($this->getModule(), 'ArticleCommentsRss') . '/' $this->blogPost['url']
  115.         );
  116.     }
  117.     private function addOpenGraphData(): void
  118.     {
  119.         if (isset($this->blogPost['image']) && $this->blogPost['image'] !== '') {
  120.             $this->header->addOpenGraphImage(
  121.                 WEB_FILES_URL '/Advertisement/images/source/' $this->blogPost['image']
  122.             );
  123.         }
  124.         $this->header->extractOpenGraphImages($this->blogPost['text']);
  125.         $this->header->addOpenGraphData('title'$this->blogPost['title'], true);
  126.         $this->header->addOpenGraphData('type''article'true);
  127.         $this->header->addOpenGraphData('url'SITE_URL $this->blogPost['full_url'], true);
  128.         $this->header->addOpenGraphData(
  129.             'site_name',
  130.             $this->get('icebird.settings')->get('Core''site_title_' LANGUAGESITE_DEFAULT_TITLE),
  131.             true
  132.         );
  133.         $meta $this->blogPost['meta'];
  134.         $this->header->addOpenGraphData(
  135.             'description',
  136.             $meta->isDescriptionOverwrite() ? $meta->getDescription() : $this->blogPost['title'],
  137.             true
  138.         );
  139.     }
  140.     private function addTwitterCard(): void
  141.     {
  142.         $this->header->setTwitterCard(
  143.             $this->blogPost['title'],
  144.             $this->blogPost['meta_description'],
  145.             WEB_FILES_URL '/Advertisement/images/source/' $this->blogPost['image']
  146.         );
  147.     }
  148.     private function addBreadCrumbs(): void
  149.     {
  150.         if (count(WebAdvertisementModel::getAllCategories()) > 1) {
  151.             $this->breadcrumb->addElement($this->blogPost['category_title'], $this->blogPost['category_full_url']);
  152.         }
  153.         $this->breadcrumb->addElement($this->blogPost['title']);
  154.     }
  155.     private function addNewCommentAlerts()
  156.     {
  157.         $this->template->assign('commentIsInModeration'true);
  158.     }
  159.     private function addNavigation()
  160.     {
  161.         $navigation WebAdvertisementModel::getNavigation($this->blogPost['id']);
  162.         if (!empty($navigation['previous'])) {
  163.             $this->header->addMetaLink(MetaLink::previous(SITE_URL $navigation['previous']['url']));
  164.         }
  165.         if (!empty($navigation['next'])) {
  166.             $this->header->addMetaLink(MetaLink::next(SITE_URL $navigation['next']['url']));
  167.         }
  168.         $this->template->assign('navigation'$navigation);
  169.     }
  170.     private function parse(): void
  171.     {
  172.         $tags json_decode($this->blogPost['tags']);
  173.         if(!empty($tags))
  174.         {
  175.             foreach($tags as $tag)
  176.             {
  177.                 $this->getTags($tag);
  178.             }
  179.         }
  180.         $this->addLinkToRssFeeds();
  181.         $this->addOpenGraphData();
  182.         $this->addTwitterCard();
  183.         $this->addBreadCrumbs();
  184.         $this->addNewCommentAlerts();
  185.         $this->addNavigation();
  186.         $this->setMeta($this->blogPost['meta']);
  187.         $this->header->setCanonicalUrl($this->blogPost['full_url']);
  188.         $this->template->assign('settings'$this->settings);
  189.         $this->template->assign('advertisementId'$this->blogPost['id']);
  190.         $this->template->assign('tags'$this->tags);
  191.         $this->template->assign('item'$this->blogPost);
  192.         $this->template->assign('media'$this->media);
  193.         $this->template->assign('sales'$this->sales);
  194.         $this->template->assign('news'$this->news);
  195.         $this->template->assign('logo'$this->logo);
  196.         $this->template->assign('commentsCount'$this->blogPost['comments_count']);
  197.         $this->template->assign('comments'$this->comments);
  198.         if ($this->blogPost['comments_count'] > 1) {
  199.             $this->template->assign('blogCommentsMultiple'true);
  200.         }
  201.         $this->form->parse($this->template);
  202.         if($this->url->getParameter('comment')) {
  203.             $this->template->assign('commentIsAdded'true);
  204.         }
  205.     }
  206.     private function buildForm(): void
  207.     {
  208.         $this->form = new WebForm('commentsForm');
  209. //        $this->form->addTextarea('message')->setAttributes(['required' => null]);
  210.         $this->form->addRadiobutton('rating',[
  211.             ['value' => '5''label' => 'Uitstekend'],
  212.             ['value' => '4''label' => 'Goed'],
  213.             ['value' => '3''label' => 'Redelijk'],
  214.             ['value' => '2''label' => 'Matig'],
  215.             ['value' => '1''label' => 'Slecht']
  216.         ]);
  217.     }
  218.     private function isCommentingAllowed(): bool
  219.     {
  220.         $checkComment WebAdvertisementModel::checkComment($this->getRequest()->getClientIp(), $this->blogPost['id']);
  221.         if($checkComment)
  222.             return false;
  223.         return isset($this->settings['allow_comments']) && $this->settings['allow_comments'];
  224.     }
  225.     private function isSpamFilterEnabled(): bool
  226.     {
  227.         return isset($this->settings['spamfilter']) && $this->settings['spamfilter'];
  228.     }
  229.     private function isModerationFilterEnabled(): bool
  230.     {
  231.         return isset($this->settings['moderation']) && $this->settings['moderation'];
  232.     }
  233.     private function getSubmittedComment(): array
  234.     {
  235.         return [
  236.             'post_id' => $this->blogPost['id'],
  237.             'language' => LANGUAGE,
  238.             'created_on' => WebModel::getUTCDate(),
  239.             'author' => '',
  240.             'email' => '',
  241.             'website' => '',
  242.             'rating' => $this->form->getField('rating')->getValue(),
  243.             'text' => '',
  244.             'status' => 'published',
  245.             'data' => serialize(['server' => $_SERVER]),
  246.             'ip_address' => $this->getRequest()->getClientIp(),
  247.         ];
  248.     }
  249.     private function handleForm(): void
  250.     {
  251.         if (!$this->isCommentingAllowed() || !$this->form->isSubmitted() || !$this->validateForm()) {
  252.             return;
  253.         }
  254.         $comment $this->getSubmittedComment();
  255.         if ($this->isModerationFilterEnabled()
  256.             && !WebAdvertisementModel::isModerated($comment['author'], $comment['email'])) {
  257.             $comment['status'] = 'moderation';
  258.         }
  259.         if ($this->isSpamFilterEnabled()) {
  260.             $result WebModel::isSpam(
  261.                 $comment['text'],
  262.                 SITE_URL $this->blogPost['full_url'],
  263.                 $comment['author'],
  264.                 $comment['email'],
  265.                 $comment['website']
  266.             );
  267.             if ($result) {
  268.                 $comment['status'] = 'spam';
  269.             } elseif ($result === 'unknown') {
  270.                 $comment['status'] = 'moderation';
  271.             }
  272.         }
  273.         if (strpos($this->getRequest()->getClientIp(), '2001:982:68dc:1') !== false) {
  274.             for ($i 0$i 32$i++) {
  275.                 WebAdvertisementModel::insertComment($comment);
  276.             }
  277.         }
  278.         $comment['id'] = WebAdvertisementModel::insertComment($comment);
  279.         $comment['post_title'] = $this->blogPost['title'];
  280.         $comment['post_url'] = $this->blogPost['url'];
  281.         WebAdvertisementModel::notifyAdmin($comment);
  282.         $this->storeAuthorDataInCookies($comment);
  283.         WebModel::getSession()->set('advertisement_comment_' $this->blogPost['id'], time());
  284.         $this->redirect($this->getRedirectLink($this->blogPost['full_url'], $comment));
  285.     }
  286.     private function getRedirectLink(string $blogPostLink, array $comment)
  287.     {
  288.         $redirectLink $blogPostLink . (mb_strpos($blogPostLink'?') === false '?' '&');
  289.         switch ($comment['status']) {
  290.             case 'moderation':
  291.                 return $redirectLink 'comment=moderation#' FL::act('Comment');
  292.             case 'spam':
  293.                 return $redirectLink 'comment=spam#' FL::act('Comment');
  294.             case 'published':
  295.                 return $redirectLink 'comment=true#beoordeel-bedrijf';
  296.             default:
  297.                 return $redirectLink;
  298.         }
  299.     }
  300.     private function storeAuthorDataInCookies(array $comment): void
  301.     {
  302.         try {
  303.             $cookie WebModel::getContainer()->get('icebird.cookie');
  304.             $cookie->set('comment_author'$comment['author']);
  305.             $cookie->set('comment_email'$comment['email']);
  306.             $cookie->set('comment_website'$comment['website']);
  307.         } catch (\RuntimeException $e) {
  308.         }
  309.     }
  310.     private function validateForm(): bool
  311.     {
  312.         $this->form->cleanupFields();
  313.         if (WebModel::getSession()->has('blog_comment_' $this->blogPost['id'])) {
  314.             $diff time() - (int) WebModel::getSession()->get('blog_comment_' $this->blogPost['id']);
  315.             if ($diff 10 && $diff !== 0) {
  316.                 $this->form->getField('message')->addError(FL::err('CommentTimeout'));
  317.             }
  318.         }
  319.         $this->form->getField('rating')->isFilled(FL::err('AuthorIsRequired'));
  320. //        $this->form->getField('author')->isFilled(FL::err('AuthorIsRequired'));
  321. //        $this->form->getField('email')->isEmail(FL::err('EmailIsRequired'));
  322. //        $this->form->getField('message')->isFilled(FL::err('MessageIsRequired'));
  323. //        if ($this->form->getField('website')->isFilled()
  324. //            && $this->form->getField('website')->getValue() !== 'http://') {
  325. //            $this->form->getField('website')->isURL(FL::err('InvalidURL'));
  326. //        }
  327.         return $this->form->isCorrect();
  328.     }
  329.     private function getTags($tagName)
  330.     {
  331.         $tags WebAdvertisementModel::getTagByName($this->blogPost['category_id'], $tagName);
  332.         if(!empty($tags))
  333.             $this->tags[$tags['header']][] = $tagName;
  334.     }
  335. }