src/Web/Modules/Advertisement/Model/Advertisement.php line 45

Open in your IDE?
  1. <?php
  2. namespace Web\Modules\Advertisement\Model;
  3. use Application\Modules\Profiles\Model\Profiles;
  4. use Common\Mailer\Message;
  5. use Web\Core\Language\Language as FL;
  6. use Web\Core\Engine\Model as WebModel;
  7. use Web\Core\Engine\Navigation as WebNavigation;
  8. use Web\Core\Engine\Url as WebUrl;
  9. use Web\Modules\Profiles\Model\Profile;
  10. class Advertisement
  11. {
  12.     const QUERY_DATAGRID_BROWSE_PROFILE_GROUPS =
  13.         'SELECT gr.id, g.name AS group_name, UNIX_TIMESTAMP(gr.expires_on) AS expires_on,
  14.           IF(gr.expires_on IS NOT NULL AND gr.expires_on <= NOW(), 1, 0) AS hidden
  15.          FROM profiles_groups AS g
  16.          INNER JOIN profiles_groups_rights AS gr ON gr.group_id = g.id
  17.          WHERE gr.profile_id = ?';
  18.     public static function get(string $url): array
  19.     {
  20.         $advertisementPost = (array)WebModel::getContainer()->get('database')->getRecord(
  21.             'SELECT i.id, i.revision_id, i.language, i.title, i.introduction, i.text, i.category_id, 
  22.              c.title AS category_title, m2.url AS category_url, i.image,
  23.              UNIX_TIMESTAMP(i.publish_on) AS publish_on, i.user_id,
  24.              i.allow_comments, m.id AS meta_id, i.open_hours, i.special_open_hours, i.payments,i.facebook, i.twitter, i.instagram, p.email, p.id as profile_id, i.type,
  25.              m.keywords AS meta_keywords, m.keywords_overwrite AS meta_keywords_overwrite,
  26.              m.description AS meta_description, m.description_overwrite AS meta_description_overwrite,
  27.              m.title AS meta_title, m.title_overwrite AS meta_title_overwrite, m.custom AS meta_custom,
  28.              m.url, i.tags, i.youtube, l.lat, l.lng,
  29.              m.data AS meta_data, m.seo_follow AS meta_seo_follow, m.seo_index AS meta_seo_index, l.street as loc_street, l.number as loc_number, l.zip as loc_zip, l.city as loc_city, l.country as loc_country, l.phone_number as loc_phone_number, l.email as loc_email, l.website as loc_website
  30.              FROM advertisement_posts AS i
  31.              LEFT JOIN advertisement_categories AS c ON i.category_id = c.id
  32.              INNER JOIN meta AS m ON i.meta_id = m.id
  33.              LEFT JOIN meta AS m2 ON c.meta_id = m2.id
  34.              LEFT JOIN profile_advertisement AS pa ON pa.advertisement_id = i.id
  35.              LEFT JOIN profiles AS p on p.id = pa.profile_id
  36.              LEFT JOIN location AS l on i.id = l.advertisement_id
  37.              WHERE i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on <= ? AND m.url = ?
  38.              LIMIT 1',
  39.             ['active'LANGUAGEfalseWebModel::getUTCDate('Y-m-d H:i'), $url]
  40.         );
  41.         if ($advertisementPost['profile_id'])
  42.             $advertisementPost['profile'] = Profiles::getSettings($advertisementPost['profile_id']);
  43.         else {
  44.             $advertisementPost['profile'] = [];
  45.         }
  46.         $advertisementPost['open_hours'] = json_decode($advertisementPost['open_hours']);
  47.         $special_open_hours json_decode($advertisementPost['special_open_hours']);
  48.         $advertisementPost['special_open_hours'] = [];
  49.         if (!empty($special_open_hours)) {
  50.             foreach ($special_open_hours as $key => $special_open_hour) {
  51.                 if ($special_open_hour == 1)
  52.                     $advertisementPost['special_open_hours'][] = $key;
  53.             }
  54.         }
  55.         $payments json_decode($advertisementPost['payments']);
  56.         if (!empty($payments)) {
  57.             $advertisementPost['payments'] = [];
  58.             foreach ($payments as $key => $payment) {
  59.                 $advertisementPost['payments'][] = $key;
  60.             }
  61.         }
  62.         return self::completeAdvertisementPost($advertisementPost);
  63.     }
  64.     public static function getAllSearchPost($query)
  65.     {
  66.         return (array)WebModel::getContainer()->get('database')->getRecords(
  67.             "SELECT i.title
  68.              FROM advertisement_posts AS i
  69.              WHERE (i.title LIKE '%$query%' OR i.tags LIKE '%$query%') AND i.status LIKE 'active'
  70.              GROUP BY i.title
  71.              ORDER BY i.title ASC"
  72.         );
  73.     }
  74.     public static function getAllSearchCategories($query)
  75.     {
  76.         return (array)WebModel::getContainer()->get('database')->getRecords(
  77.             "SELECT i.title
  78.              FROM advertisement_categories AS i
  79.              WHERE (i.title LIKE '%$query%')
  80.              GROUP BY i.title
  81.              ORDER BY i.title ASC"
  82.         );
  83.     }
  84.     public static function getAllSearchTags($query)
  85.     {
  86.         return (array)WebModel::getContainer()->get('database')->getRecords(
  87.             "SELECT i.title
  88.              FROM advertisement_tags AS i
  89.              WHERE i.title LIKE '%$query%' AND i.header NOT LIKE 'zoekfilter'
  90.              GROUP BY i.title
  91.              ORDER BY i.title ASC"
  92.         );
  93.     }
  94.     public static function getAll($company false$city false$rubriek false$service false$state false$order false$page false): array
  95.     {
  96.         $queryCompany '';
  97.         $queryCity '';
  98.         $queryRubriek '';
  99.         $active '';
  100.         $queryService '';
  101.         $queryState '';
  102.         $queryOrder '';
  103.         $rand '';
  104.         if ($company) {
  105.             $company str_replace("'""\\'"strtolower($company));
  106.             $queryCompany " AND (i.title LIKE '%$company%' OR c.title LIKE '$company' OR i.tags LIKE '$company' OR at.title LIKE '$company')";
  107.         }
  108.         if ($city)
  109.             $queryCity " AND (l.city LIKE '$city' OR l.state LIKE '$city' OR l.street LIKE '$city' OR pr.name LIKE '$city')";
  110.         if ($rubriek) {
  111.             $r join("','"$rubriek);
  112.             $queryRubriek " AND c.title IN ('$r')";
  113.         }
  114.         if ($service) {
  115.             $s join("'|'"$service);
  116.             $queryService " AND i.tags REGEXP '$s'";
  117.         }
  118.         if ($state) {
  119.             $t join("','"$state);
  120.             $queryState " AND l.state IN ('$t')";
  121.         }
  122.         if($rubriek || $service || $state)
  123.             $active ' AND i.type = "adverteerder"';
  124.         if($order)
  125.             $queryOrder $order == ', i.title ASC' ', i.title DESC';
  126.         if($page)
  127.             $rand ', RAND()';
  128.         $items = (array)WebModel::getContainer()->get('database')->getRecords(
  129.             "SELECT i.id, i.revision_id, i.language, i.title, i.introduction, i.text, i.num_comments AS comments_count, i.category_id, i.profile_id,
  130.              c.title AS category_title, i.image, i.tags, i.user_id, i.allow_comments,
  131.              m.url, i.open_hours, l.street as loc_street, l.number as loc_number, l.zip as loc_zip, l.city as loc_city, l.country as loc_country, l.phone_number as loc_phone_number, l.email as loc_email, l.website as loc_website, l.lat, l.lng, i.type, l.state
  132.              FROM advertisement_posts AS i
  133.              LEFT JOIN advertisement_categories AS c ON i.category_id = c.id
  134.              LEFT JOIN meta AS m ON i.meta_id = m.id
  135.              LEFT JOIN location AS l on i.id = l.advertisement_id
  136.              LEFT JOIN provincies AS pr on l.state = pr.id
  137.              LEFT JOIN advertisement_tags AS at on at.category_id = c.id
  138.              WHERE i.status = ?
  139.              $queryCompany
  140.              $queryCity
  141.              $queryRubriek
  142.              $queryService
  143.              $queryState
  144.              $active
  145.              GROUP BY i.id
  146.              ORDER BY FIELD(i.type, 'adverteerder') DESC $queryOrder $rand",
  147.             ['active']
  148.         );
  149.         if (empty($items)) {
  150.             return [];
  151.         }
  152.         return $items;
  153.     }
  154.     public static function getAllCategories(): array
  155.     {
  156.         $return = (array)WebModel::getContainer()->get('database')->getRecords(
  157.             'SELECT c.id, c.title AS label, m.url, COUNT(c.id) AS total, m.data AS meta_data,
  158.                  m.seo_follow AS meta_seo_follow, m.seo_index AS meta_seo_index
  159.              FROM advertisement_categories AS c
  160.              INNER JOIN advertisement_posts AS i ON c.id = i.category_id AND c.language = i.language
  161.              INNER JOIN meta AS m ON c.meta_id = m.id
  162.              WHERE c.language = ? AND i.status = ? AND i.hidden = ? AND i.publish_on <= ?
  163.              GROUP BY c.id',
  164.             [LANGUAGE'active'falseWebModel::getUTCDate('Y-m-d H:i')],
  165.             'id'
  166.         );
  167.         foreach ($return as &$row) {
  168.             if (isset($row['meta_data'])) {
  169.                 $row['meta_data'] = @unserialize($row['meta_data']);
  170.             }
  171.         }
  172.         return $return;
  173.     }
  174.     public static function getAllRubrieken(): array
  175.     {
  176.         $return = (array)WebModel::getContainer()->get('database')->getRecords(
  177.             'SELECT c.id, c.title AS label, m.url, COUNT(c.id) AS total, m.data AS meta_data,
  178.                  m.seo_follow AS meta_seo_follow, m.seo_index AS meta_seo_index
  179.              FROM advertisement_categories AS c
  180.              INNER JOIN meta AS m ON c.meta_id = m.id
  181.              WHERE c.language = ?
  182.              GROUP BY c.id ORDER BY c.title',
  183.             ['nl'],
  184.             'id'
  185.         );
  186.         foreach ($return as &$row) {
  187.             if (isset($row['meta_data'])) {
  188.                 $row['meta_data'] = @unserialize($row['meta_data']);
  189.             }
  190.         }
  191.         return $return;
  192.     }
  193.     public static function getSearchTerms(): array
  194.     {
  195.         $return = (array)WebModel::getContainer()->get('database')->getRecords(
  196.             'SELECT *
  197.              FROM advertisement_search_result AS c 
  198.              WHERE MONTH(created_at) = MONTH(CURRENT_DATE())
  199.                 AND YEAR(created_at) = YEAR(CURRENT_DATE())
  200.                 AND term NOT LIKE ""
  201.              GROUP BY c.term, c.ip_address ORDER BY c.created_at DESC LIMIT 10'
  202.         );
  203.         return $return;
  204.     }
  205.     public static function getSearchTermsAmount(): array
  206.     {
  207.         $return = (array)WebModel::getContainer()->get('database')->getRecords(
  208.             'SELECT *
  209.              FROM advertisement_search_result AS c 
  210.              WHERE MONTH(created_at) = MONTH(CURRENT_DATE())
  211.                 AND YEAR(created_at) = YEAR(CURRENT_DATE())
  212.                 AND term NOT LIKE ""
  213.             ORDER BY c.created_at DESC'
  214.         );
  215.         return $return;
  216.     }
  217.     public static function getBannerStats(): array
  218.     {
  219.         $return = (array)WebModel::getContainer()->get('database')->getRecords(
  220.             'SELECT *
  221.              FROM mv_stats_banner AS c 
  222.              INNER JOIN advertisement_media as m ON m.title = banner_title WHERE m.type IN ("banner", "banner_big")'
  223.         );
  224.         return $return;
  225.     }
  226.     public static function getFormStats(): array
  227.     {
  228.         $return = (array)WebModel::getContainer()->get('database')->getRecords(
  229.             'SELECT *
  230.              FROM mv_stats_contactform AS c
  231.              ORDER BY c.date DESC'
  232.         );
  233.         return $return;
  234.     }
  235.     public static function getFormStatsClient($postId$type): array
  236.     {
  237.         $return = (array)WebModel::getContainer()->get('database')->getRecords(
  238.             'SELECT *
  239.              FROM advertisement_offerte AS c
  240.              INNER JOIN forms_data_fields as f on c.data_id = f.data_id
  241.              WHERE c.post_id = ? AND c.type = ?
  242.              ORDER BY c.created_at DESC',
  243.             [$postId$type]
  244.         );
  245.         return $return;
  246.     }
  247.     public static function getFormStatsAll($type): array
  248.     {
  249.         $return = (array)WebModel::getContainer()->get('database')->getRecords(
  250.             'SELECT *
  251.              FROM advertisement_offerte AS c
  252.              INNER JOIN forms_data_fields as f on c.data_id = f.data_id
  253.              LEFT JOIN advertisement_posts as a on c.post_id = a.id
  254.              WHERE c.type = ? and a.status = ?
  255.              ORDER BY c.created_at DESC',
  256.             [$type'active']
  257.         );
  258.         return $return;
  259.     }
  260.     public static function getCategory(string $slug): array
  261.     {
  262.         $category = (array)WebModel::getContainer()->get('database')->getRecord(
  263.             'SELECT c.id, c.title AS label, m.url, m.id AS meta_id, COUNT(c.id) AS total
  264.              FROM advertisement_categories AS c
  265.              INNER JOIN advertisement_posts AS i ON c.id = i.category_id AND c.language = i.language
  266.              INNER JOIN meta AS m ON c.meta_id = m.id AND m.url = ?
  267.              WHERE c.language = ? AND i.status = ? AND i.hidden = ? AND i.publish_on <= ?
  268.              GROUP BY c.id',
  269.             [$slugLANGUAGE'active'falseWebModel::getUTCDate('Y-m-d H:i')],
  270.             'id'
  271.         );
  272.         if (empty($category)) {
  273.             return [];
  274.         }
  275.         $category['meta'] = WebModel::get('icebird.repository.meta')->find($category['meta_id']);
  276.         return $category;
  277.     }
  278.     public static function getAllComments(int $limit 10int $offset 0): array
  279.     {
  280.         $comments = (array)WebModel::getContainer()->get('database')->getRecords(
  281.             'SELECT i.id, UNIX_TIMESTAMP(i.created_on) AS created_on, i.author, i.text,
  282.              p.id AS post_id, p.title AS post_title, m.url AS post_url, i.email
  283.              FROM advertisement_comments AS i
  284.              INNER JOIN advertisement_posts AS p ON i.post_id = p.id AND i.language = p.language
  285.              INNER JOIN meta AS m ON p.meta_id = m.id
  286.              WHERE i.status = ? AND i.language = ?
  287.              GROUP BY i.id
  288.              ORDER BY i.created_on DESC
  289.              LIMIT ?, ?',
  290.             ['published'LANGUAGE$offset$limit]
  291.         );
  292.         foreach ($comments as &$row) {
  293.             $row['author'] = htmlspecialchars($row['author']);
  294.             $row['text'] = htmlspecialchars($row['text']);
  295.             $row['gravatar_id'] = md5($row['email']);
  296.         }
  297.         return $comments;
  298.     }
  299.     public static function getAllCount($company false$city false): int
  300.     {
  301.         $queryCompany '';
  302.         $queryCity '';
  303.         if ($company) {
  304.             $company str_replace("'""\\'"$company);
  305.             $queryCompany " AND (i.title LIKE '%$company%' OR c.title LIKE '$company' OR at.title LIKE '$company' OR i.tags LIKE '%$company%')";
  306.         }
  307.         if ($city)
  308.             $queryCity " AND (l.city LIKE '$city' OR l.state LIKE '$city' OR l.street LIKE '$city')";
  309.         return (int)WebModel::getContainer()->get('database')->getNumRows(
  310.             "SELECT COUNT(i.id)
  311.              FROM advertisement_posts AS i
  312.              LEFT JOIN advertisement_categories AS c ON i.category_id = c.id
  313.              LEFT JOIN location AS l on i.id = l.advertisement_id
  314.              LEFT JOIN advertisement_tags as at on c.id = at.category_id
  315.              WHERE i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on <= ?
  316.              $queryCompany
  317.              $queryCity
  318.              GROUP BY i.id",
  319.             ['active',
  320.                 'nl',
  321.                 false,
  322.                 WebModel::getUTCDate('Y-m-d H:i'),
  323.             ]
  324.         );
  325.     }
  326.     public static function getAllForCategory(string $categoryUrlint $limit 10int $offset 0): array
  327.     {
  328.         $rand '';
  329.         if ($offset == 0)
  330.             $rand ', RAND() ';
  331.         $items = (array)WebModel::getContainer()->get('database')->getRecords(
  332.             'SELECT i.id, i.revision_id, i.language, i.title, i.introduction, i.text, i.num_comments AS comments_count, i.category_id, i.profile_id,
  333.              c.title AS category_title, i.image, m2.url AS category_url, i.tags,
  334.              UNIX_TIMESTAMP(i.publish_on) AS publish_on, i.user_id, i.allow_comments,
  335.              m.url, i.open_hours, l.street as loc_street, l.number as loc_number, l.zip as loc_zip, l.city as loc_city, l.country as loc_country, l.phone_number as loc_phone_number, l.email as loc_email, l.website as loc_website, l.lat, l.lng, i.type, l.state
  336.              FROM advertisement_posts AS i
  337.              INNER JOIN advertisement_categories AS c ON i.category_id = c.id
  338.              INNER JOIN meta AS m ON i.meta_id = m.id
  339.              INNER JOIN meta AS m2 ON c.meta_id = m2.id
  340.              LEFT JOIN location AS l on i.id = l.advertisement_id
  341.              LEFT JOIN advertisement_tags as at on c.id = at.category_id
  342.              WHERE i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on <= ? AND m2.url = ?
  343.              GROUP BY i.id
  344.              ORDER BY FIELD(i.type, "adverteerder") DESC ' $rand ' LIMIT ?, ?',
  345.             [
  346.                 'active',
  347.                 LANGUAGE,
  348.                 false,
  349.                 WebModel::getUTCDate('Y-m-d H:i'),
  350.                 $categoryUrl,
  351.                 $offset,
  352.                 $limit,
  353.             ],
  354.             'id'
  355.         );
  356.         if (empty($items)) {
  357.             return [];
  358.         }
  359.         $link WebNavigation::getUrlForBlock('Advertisement''Detail');
  360.         $categoryLink WebNavigation::getUrlForBlock('Advertisement''Category');
  361.         $folders WebModel::getThumbnailFolders(WEB_FILES_PATH '/Advertisement/images'true);
  362.         foreach ($items as $key => $row) {
  363.             $items[$key]['full_url'] = $link '/' $row['url'];
  364.             $items[$key]['category_full_url'] = $categoryLink '/' $row['category_url'];
  365.             if ($row['comments_count'] > 0) {
  366.                 $items[$key]['comments'] = true;
  367.             }
  368.             if ($row['comments_count'] > 1) {
  369.                 $items[$key]['comments_multiple'] = true;
  370.             }
  371.             $items[$key]['allow_comments'] = (bool)$row['allow_comments'];
  372.             if (!WebModel::get('icebird.settings')->get('Advertisement''allow_comments')) {
  373.                 $items[$key]['allow_comments'] = false;
  374.             }
  375.             if (isset($row['image'])) {
  376.                 foreach ($folders as $folder) {
  377.                     $items[$key]['image_' $folder['dirname']] = $folder['url'] . '/' $folder['dirname'] .
  378.                         '/' $row['image'];
  379.                 }
  380.             }
  381.         }
  382.         return $items;
  383.     }
  384.     public static function getAllForCategoryCount(string $url): int
  385.     {
  386.         return (int)WebModel::getContainer()->get('database')->getVar(
  387.             'SELECT COUNT(i.id) AS count
  388.              FROM advertisement_posts AS i
  389.              INNER JOIN advertisement_categories AS c ON i.category_id = c.id
  390.              INNER JOIN meta AS m ON c.meta_id = m.id
  391.              WHERE i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on <= ? AND m.url = ?',
  392.             ['active'LANGUAGEfalseWebModel::getUTCDate('Y-m-d H:i'), $url]
  393.         );
  394.     }
  395.     public static function getAllForDateRange(int $startint $endint $limit 10int $offset 0): array
  396.     {
  397.         $items = (array)WebModel::getContainer()->get('database')->getRecords(
  398.             'SELECT i.id, i.revision_id, i.language, i.title, i.introduction, i.text, i.num_comments AS comments_count,
  399.              c.title AS category_title, m2.url AS category_url, i.image,
  400.              UNIX_TIMESTAMP(i.publish_on) AS publish_on, i.user_id, i.allow_comments,
  401.              m.url
  402.              FROM advertisement_posts AS i
  403.              INNER JOIN advertisement_categories AS c ON i.category_id = c.id
  404.              INNER JOIN meta AS m ON i.meta_id = m.id
  405.              INNER JOIN meta AS m2 ON c.meta_id = m2.id
  406.              WHERE i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on BETWEEN ? AND ?
  407.              ORDER BY i.publish_on DESC
  408.              LIMIT ?, ?',
  409.             [
  410.                 'active',
  411.                 LANGUAGE,
  412.                 false,
  413.                 WebModel::getUTCDate('Y-m-d H:i'$start),
  414.                 WebModel::getUTCDate('Y-m-d H:i'$end),
  415.                 $offset,
  416.                 $limit,
  417.             ],
  418.             'id'
  419.         );
  420.         if (empty($items)) {
  421.             return [];
  422.         }
  423.         $link WebNavigation::getUrlForBlock('Advertisement''Detail');
  424.         $folders WebModel::getThumbnailFolders(WEB_FILES_PATH '/Advertisement/images'true);
  425.         foreach ($items as $key => $row) {
  426.             $items[$key]['full_url'] = $link '/' $row['url'];
  427.             if ($row['comments_count'] > 0) {
  428.                 $items[$key]['comments'] = true;
  429.             }
  430.             if ($row['comments_count'] > 1) {
  431.                 $items[$key]['comments_multiple'] = true;
  432.             }
  433.             $items[$key]['allow_comments'] = (bool)$row['allow_comments'];
  434.             if (!WebModel::get('icebird.settings')->get('Advertisement''allow_comments')) {
  435.                 $items[$key]['allow_comments'] = false;
  436.             }
  437.             if (isset($row['image'])) {
  438.                 foreach ($folders as $folder) {
  439.                     $items[$key]['image_' $folder['dirname']] = $folder['url'] . '/' $folder['dirname'] .
  440.                         '/' $row['image'];
  441.                 }
  442.             }
  443.         }
  444.         return $items;
  445.     }
  446.     public static function getAllForDateRangeCount(int $startint $end): int
  447.     {
  448.         return (int)WebModel::getContainer()->get('database')->getVar(
  449.             'SELECT COUNT(i.id)
  450.              FROM advertisement_posts AS i
  451.              WHERE i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on BETWEEN ? AND ?',
  452.             [
  453.                 'active',
  454.                 LANGUAGE,
  455.                 false,
  456.                 WebModel::getUTCDate('Y-m-d H:i:s'$start),
  457.                 WebModel::getUTCDate('Y-m-d H:i:s'$end),
  458.             ]
  459.         );
  460.     }
  461.     public static function getArchiveNumbers(): array
  462.     {
  463.         $numbers WebModel::getContainer()->get('database')->getPairs(
  464.             'SELECT DATE_FORMAT(i.publish_on, "%Y%m") AS month, COUNT(i.id)
  465.              FROM advertisement_posts AS i
  466.              INNER JOIN meta AS m ON i.meta_id = m.id
  467.              WHERE i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on <= ?
  468.              GROUP BY month',
  469.             ['active'LANGUAGEfalseWebModel::getUTCDate('Y-m-d H:i')]
  470.         );
  471.         $stats = [];
  472.         $link WebNavigation::getUrlForBlock('Advertisement''Archive');
  473.         $firstYear = (int)date('Y');
  474.         $lastYear 0;
  475.         foreach ($numbers as $key => $count) {
  476.             $year mb_substr($key04);
  477.             $month mb_substr($key42);
  478.             if ($year $firstYear) {
  479.                 $firstYear $year;
  480.             }
  481.             if ($year $lastYear) {
  482.                 $lastYear $year;
  483.             }
  484.             $timestamp gmmktime(000000$month01$year);
  485.             if (!isset($stats[$year])) {
  486.                 $stats[$year] = [
  487.                     'url' => $link '/' $year,
  488.                     'label' => $year,
  489.                     'total' => 0,
  490.                     'months' => null,
  491.                 ];
  492.             }
  493.             $stats[$year]['total'] += (int)$count;
  494.             $stats[$year]['months'][$key] = [
  495.                 'url' => $link '/' $year '/' $month,
  496.                 'label' => $timestamp,
  497.                 'total' => $count,
  498.             ];
  499.         }
  500.         for ($i $firstYear$i <= $lastYear; ++$i) {
  501.             if (!isset($stats[$i])) {
  502.                 $stats[$i] = ['url' => null'label' => $i'total' => 0'months' => null];
  503.             }
  504.         }
  505.         krsort($stats);
  506.         foreach ($stats as &$row) {
  507.             if ($row['total'] == 0) {
  508.                 $row['url'] = null;
  509.             }
  510.             if (!empty($row['months'])) {
  511.                 // sort months
  512.                 ksort($row['months']);
  513.             }
  514.         }
  515.         return $stats;
  516.     }
  517.     public static function getComments(int $advertisementPostId): array
  518.     {
  519.         $comments = (array)WebModel::getContainer()->get('database')->getRecords(
  520.             'SELECT c.id, UNIX_TIMESTAMP(c.created_on) AS created_on, c.text, c.data,
  521.              c.author, c.email, c.website, c.rating
  522.              FROM advertisement_comments AS c
  523.              WHERE c.post_id = ? AND c.status = ? AND c.language = ?
  524.              ORDER BY c.id ASC',
  525.             [$advertisementPostId'published'LANGUAGE]
  526.         );
  527.         foreach ($comments as &$row) {
  528.             $row['author'] = htmlspecialchars($row['author']);
  529.             $row['text'] = htmlspecialchars($row['text']);
  530.             $row['rating'] = htmlspecialchars($row['rating']);
  531.             $row['gravatar_id'] = md5($row['email']);
  532.         }
  533.         return $comments;
  534.     }
  535.     public static function getForTags(array $advertisementPostIds): array
  536.     {
  537.         $items = (array)WebModel::getContainer()->get('database')->getRecords(
  538.             'SELECT i.title, i.image, m.url
  539.              FROM advertisement_posts AS i
  540.              INNER JOIN meta AS m ON m.id = i.meta_id
  541.              WHERE i.status = ? AND i.hidden = ? AND i.id IN (' implode(','$advertisementPostIds) . ') AND i.publish_on <= ?
  542.              ORDER BY i.publish_on DESC',
  543.             ['active'falseWebModel::getUTCDate('Y-m-d H:i')]
  544.         );
  545.         if (!empty($items)) {
  546.             $link WebNavigation::getUrlForBlock('Advertisement''Detail');
  547.             $folders WebModel::getThumbnailFolders(WEB_FILES_PATH '/Advertisement/images'true);
  548.             foreach ($items as &$row) {
  549.                 $row['full_url'] = $link '/' $row['url'];
  550.                 if (isset($row['image'])) {
  551.                     foreach ($folders as $folder) {
  552.                         $row['image_' $folder['dirname']] = $folder['url'] . '/' $folder['dirname'] .
  553.                             '/' $row['image'];
  554.                     }
  555.                 }
  556.             }
  557.         }
  558.         return $items;
  559.     }
  560.     public static function getTagsByCategory ($categoryId) {
  561.         return (array)WebModel::getContainer()->get('database')->getRecords(
  562.             'SELECT i.title, i.header
  563.              FROM advertisement_tags AS i
  564.              WHERE i.category_id = ? AND i.header IN ("Faciliteiten", "Diensten", "Specialismen")
  565.              ORDER BY i.title ASC',
  566.             [$categoryId]
  567.         );
  568.     }
  569.     public static function getIdForTags(WebUrl $url): int
  570.     {
  571.         $itemUrl = (string)$url->getParameter(1);
  572.         return self::get($itemUrl)['id'] ?? 0;
  573.     }
  574.     public static function getNavigation(int $advertisementPostId): array
  575.     {
  576.         $database WebModel::getContainer()->get('database');
  577.         $date = (string)$database->getVar(
  578.             'SELECT i.publish_on
  579.              FROM advertisement_posts AS i
  580.              WHERE i.id = ? AND i.status = ?',
  581.             [$advertisementPostId'active']
  582.         );
  583.         if ($date === '') {
  584.             return [];
  585.         }
  586.         $navigation = [];
  587.         $detailLink WebNavigation::getUrlForBlock('Advertisement''Detail') . '/';
  588.         $navigation['previous'] = $database->getRecord(
  589.             'SELECT i.id, i.title, CONCAT(?, m.url) AS url
  590.              FROM advertisement_posts AS i
  591.              INNER JOIN meta AS m ON i.meta_id = m.id
  592.              WHERE i.id != ? AND i.status = ? AND i.hidden = ? AND i.language = ? AND
  593.                 ((i.publish_on = ? AND i.id < ?) OR i.publish_on < ?)
  594.              ORDER BY i.publish_on DESC, i.id DESC
  595.              LIMIT 1',
  596.             [$detailLink$advertisementPostId'active'falseLANGUAGE$date$advertisementPostId$date]
  597.         );
  598.         $navigation['next'] = $database->getRecord(
  599.             'SELECT i.id, i.title, CONCAT(?, m.url) AS url
  600.              FROM advertisement_posts AS i
  601.              INNER JOIN meta AS m ON i.meta_id = m.id
  602.              WHERE i.id != ? AND i.status = ? AND i.hidden = ? AND i.language = ? AND
  603.                 ((i.publish_on = ? AND i.id > ?) OR (i.publish_on > ? AND i.publish_on <= ?))
  604.              ORDER BY i.publish_on ASC, i.id ASC
  605.              LIMIT 1',
  606.             [
  607.                 $detailLink,
  608.                 $advertisementPostId,
  609.                 'active',
  610.                 false,
  611.                 LANGUAGE,
  612.                 $date,
  613.                 $advertisementPostId,
  614.                 $date,
  615.                 WebModel::getUTCDate('Y-m-d H:i'),
  616.             ]
  617.         );
  618.         if (empty($navigation['previous'])) {
  619.             unset($navigation['previous']);
  620.         }
  621.         if (empty($navigation['next'])) {
  622.             unset($navigation['next']);
  623.         }
  624.         return $navigation;
  625.     }
  626.     public static function getRecentComments(int $limit 5): array
  627.     {
  628.         $return = [];
  629.         $comments = (array)WebModel::getContainer()->get('database')->getRecords(
  630.             'SELECT c.id, c.author, c.website, c.email, UNIX_TIMESTAMP(c.created_on) AS created_on, c.text,
  631.              i.id AS post_id, i.title AS post_title,
  632.              m.url AS post_url
  633.              FROM advertisement_comments AS c
  634.              INNER JOIN advertisement_posts AS i ON c.post_id = i.id AND c.language = i.language
  635.              INNER JOIN meta AS m ON i.meta_id = m.id
  636.              WHERE c.status = ? AND i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on <= ?
  637.              ORDER BY c.id DESC
  638.              LIMIT ?',
  639.             ['published''active'LANGUAGEfalseWebModel::getUTCDate('Y-m-d H:i'), $limit]
  640.         );
  641.         if (empty($comments)) {
  642.             return $return;
  643.         }
  644.         $link WebNavigation::getUrlForBlock('Advertisement''Detail');
  645.         foreach ($comments as &$row) {
  646.             $row['post_full_url'] = $link '/' $row['post_url'];
  647.             $row['full_url'] = $link '/' $row['post_url'] . '#comment-' $row['id'];
  648.             $row['gravatar_id'] = md5($row['email']);
  649.         }
  650.         return $comments;
  651.     }
  652.     public static function getRelated(int $advertisementPostIdint $limit 5): array
  653.     {
  654.         $link WebNavigation::getUrlForBlock('Advertisement''Detail');
  655.         $items = (array)WebModel::getContainer()->get('database')->getRecords(
  656.             'SELECT i.id, i.title, m.url
  657.              FROM advertisement_posts AS i
  658.              INNER JOIN meta AS m ON i.meta_id = m.id
  659.              WHERE i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on <= ?
  660.              ORDER BY i.publish_on DESC, i.id DESC
  661.              LIMIT ?',
  662.             ['active'LANGUAGEfalseWebModel::getUTCDate('Y-m-d H:i'), $limit],
  663.             'id'
  664.         );
  665.         foreach ($items as &$row) {
  666.             $row['full_url'] = $link '/' $row['url'];
  667.         }
  668.         return $items;
  669.     }
  670.     public static function getRevision(string $urlint $revisionId): array
  671.     {
  672.         $advertisementPost = (array)WebModel::getContainer()->get('database')->getRecord(
  673.             'SELECT i.id, i.revision_id, i.language, i.title, i.introduction, i.text, i.image,
  674.              c.title AS category_title, m2.url AS category_url, m.id AS meta_id,
  675.              UNIX_TIMESTAMP(i.publish_on) AS publish_on, i.user_id,
  676.              i.allow_comments,
  677.              m.keywords AS meta_keywords, m.keywords_overwrite AS meta_keywords_overwrite,
  678.              m.description AS meta_description, m.description_overwrite AS meta_description_overwrite,
  679.              m.title AS meta_title, m.title_overwrite AS meta_title_overwrite, m.custom AS meta_custom,
  680.              m.url,
  681.              m.data AS meta_data, m.seo_follow AS meta_seo_follow, m.seo_index AS meta_seo_index
  682.              FROM advertisement_posts AS i
  683.              INNER JOIN advertisement_categories AS c ON i.category_id = c.id
  684.              INNER JOIN meta AS m ON i.meta_id = m.id
  685.              INNER JOIN meta AS m2 ON c.meta_id = m2.id
  686.              WHERE i.language = ? AND i.revision_id = ? AND m.url = ?
  687.              LIMIT 1',
  688.             [LANGUAGE$revisionId$url]
  689.         );
  690.         return self::completeAdvertisementPost($advertisementPost);
  691.     }
  692.     private static function completeAdvertisementPost(array $advertisementPost)
  693.     {
  694.         if (isset($advertisementPost['meta_id'])) {
  695.             $advertisementPost['meta'] = WebModel::get('icebird.repository.meta')->find($advertisementPost['meta_id']);
  696.         }
  697.         if (isset($advertisementPost['meta_data'])) {
  698.             $advertisementPost['meta_data'] = @unserialize($advertisementPost['meta_data'], ['allowed_classes' => false]);
  699.         }
  700.         if (isset($advertisementPost['image'])) {
  701.             $folders WebModel::getThumbnailFolders(WEB_FILES_PATH '/Advertisement/images'true);
  702.             foreach ($folders as $folder) {
  703.                 $advertisementPost['image_' $folder['dirname']] = $folder['url'] . '/' $folder['dirname'] . '/' $advertisementPost['image'];
  704.             }
  705.         }
  706.         return $advertisementPost;
  707.     }
  708.     public static function insertComment(array $comment): int
  709.     {
  710.         $database WebModel::getContainer()->get('database');
  711.         $comment['id'] = (int)$database->insert('advertisement_comments'$comment);
  712.         if ($comment['status'] == 'published') {
  713.             $numComments = (int)WebModel::getContainer()->get('database')->getVar(
  714.                 'SELECT COUNT(i.id) AS comment_count
  715.                  FROM advertisement_comments AS i
  716.                  INNER JOIN advertisement_posts AS p ON i.post_id = p.id AND i.language = p.language
  717.                  WHERE i.status = ? AND i.post_id = ? AND i.language = ? AND p.status = ?
  718.                  GROUP BY i.post_id',
  719.                 ['published'$comment['post_id'], 'nl''active']
  720.             );
  721.             $database->update('advertisement_posts', ['num_comments' => $numComments], 'id = ?'$comment['post_id']);
  722.         }
  723.         return $comment['id'];
  724.     }
  725.     public static function isModerated(string $authorstring $email): bool
  726.     {
  727.         return (bool)WebModel::getContainer()->get('database')->getVar(
  728.             'SELECT 1
  729.              FROM advertisement_comments AS c
  730.              WHERE c.status = ? AND c.author = ? AND c.email = ?
  731.              LIMIT 1',
  732.             ['published'$author$email]
  733.         );
  734.     }
  735.     public static function notifyAdmin(array $comment$emailClient false): void
  736.     {
  737.         if ($comment['status'] == 'spam') {
  738.             return;
  739.         }
  740.         $notifyByMailOnComment WebModel::get('icebird.settings')->get(
  741.             'Advertisement',
  742.             'notify_by_email_on_new_comment',
  743.             false
  744.         );
  745.         $notifyByMailOnCommentToModerate WebModel::get('icebird.settings')->get(
  746.             'Advertisement',
  747.             'notify_by_email_on_new_comment_to_moderate',
  748.             false
  749.         );
  750.         $url SITE_URL WebNavigation::getUrlForBlock('Advertisement''Detail') . '/' $comment['post_url'] . '#comment-' $comment['id'];
  751.         $ApplicationUrl SITE_URL WebNavigation::getApplicationUrlForBlock('comments''Advertisement') . '#tabModeration';
  752.         if($emailClient) {
  753.             $from WebModel::get('icebird.settings')->get('Core''mailer_from');
  754.             $replyTo WebModel::get('icebird.settings')->get('Core''mailer_reply_to');
  755.             $message Message::newInstance('Uw inloggegevens voor TurkseGids.nl')
  756.                 ->setFrom([$from['email'] => $from['name']])
  757.                 ->setTo([$comment['email'] => ''])
  758.                 ->setReplyTo([$replyTo['email'] => $replyTo['name']])
  759.                 ->parseHtml(
  760.                     '/Advertisement/Layout/Templates/Mails/Notification.html.twig',
  761.                     [
  762.                         'loginUrl' => 'https://www.turksegids.nl/aanmelden',
  763.                         'firstName' => $comment['first_name'],
  764.                         'lastName' => $comment['last_name'],
  765.                         'email' => $comment['email'],
  766.                         'password' => $comment['unencrypted_password'],
  767.                     ],
  768.                     true
  769.                 );
  770.             WebModel::get('mailer')->send($message);
  771.         }
  772.         if ($notifyByMailOnComment) {
  773.             $variables = [];
  774.             if ($comment['status'] == 'moderation') {
  775.                 $variables['message'] = vsprintf(
  776.                     FL::msg('AdvertisementEmailNotificationsNewCommentToModerate'),
  777.                     [$comment['author'], $url$comment['post_title'], $ApplicationUrl]
  778.                 );
  779.             } elseif ($comment['status'] == 'published') {
  780.                 $variables['message'] = vsprintf(
  781.                     FL::msg('AdvertisementEmailNotificationsNewComment'),
  782.                     [$comment['author'], $url$comment['post_title']]
  783.                 );
  784.             }
  785.             $to WebModel::get('icebird.settings')->get('Core''mailer_to');
  786.             $from WebModel::get('icebird.settings')->get('Core''mailer_from');
  787.             $replyTo WebModel::get('icebird.settings')->get('Core''mailer_reply_to');
  788.             $message Message::newInstance(FL::msg('NotificationSubject'))
  789.                 ->setFrom([$from['email'] => $from['name']])
  790.                 ->setTo([$to['email'] => $to['name']])
  791.                 ->setReplyTo([$replyTo['email'] => $replyTo['name']])
  792.                 ->parseHtml(
  793.                     '/Core/Layout/Templates/Mails/Notification.html.twig',
  794.                     $variables,
  795.                     true
  796.                 );
  797.             WebModel::get('mailer')->send($message);
  798.         } elseif ($notifyByMailOnCommentToModerate && $comment['status'] == 'moderation') {
  799.             $variables = [];
  800.             $variables['message'] = vsprintf(
  801.                 FL::msg('AdvertisementEmailNotificationsNewCommentToModerate'),
  802.                 [$comment['author'], $url$comment['post_title'], $ApplicationUrl]
  803.             );
  804.             $to WebModel::get('icebird.settings')->get('Core''mailer_to');
  805.             $from WebModel::get('icebird.settings')->get('Core''mailer_from');
  806.             $replyTo WebModel::get('icebird.settings')->get('Core''mailer_reply_to');
  807.             $message Message::newInstance(FL::msg('NotificationSubject'))
  808.                 ->setFrom([$from['email'] => $from['name']])
  809.                 ->setTo([$to['email'] => $to['name']])
  810.                 ->setReplyTo([$replyTo['email'] => $replyTo['name']])
  811.                 ->parseHtml(
  812.                     '/Core/Layout/Templates/Mails/Notification.html.twig',
  813.                     $variables,
  814.                     true
  815.                 );
  816.             WebModel::get('mailer')->send($message);
  817.         }
  818.     }
  819.     public static function search(array $ids): array
  820.     {
  821.         $items = (array)WebModel::getContainer()->get('database')->getRecords(
  822.             'SELECT i.id, i.title, i.introduction, i.text, m.url
  823.              FROM advertisement_posts AS i
  824.              INNER JOIN meta AS m ON i.meta_id = m.id
  825.              WHERE i.status = ? AND i.hidden = ? AND i.language = ? AND i.publish_on <= ? AND i.id IN (' .
  826.             implode(','$ids) . ')',
  827.             ['active'falseLANGUAGEdate('Y-m-d H:i')],
  828.             'id'
  829.         );
  830.         $detailUrl WebNavigation::getUrlForBlock('Advertisement''Detail');
  831.         foreach ($items as &$item) {
  832.             $item['full_url'] = $detailUrl '/' $item['url'];
  833.         }
  834.         return $items;
  835.     }
  836.     public static function getAdvertisementByProfile(int $profileId)
  837.     {
  838.         return (array)WebModel::getContainer()->get('database')->getRecord(
  839.             'SELECT *
  840.              FROM advertisement_posts AS a
  841.              WHERE a.profile_id = ?
  842.              AND a.language = ?
  843.              AND a.status = ?',
  844.             [$profileIdLANGUAGE'active']
  845.         );
  846.     }
  847.     public static function getAdvertisementMedia(int $postId$type 'image')
  848.     {
  849.         return (array)WebModel::getContainer()->get('database')->getRecords(
  850.             'SELECT m.id,m.filename,m.title
  851.              FROM advertisement_media AS m
  852.              WHERE m.post_id = ?
  853.              AND m.type = ?
  854.              ORDER BY m.sequence ASC',
  855.             [$postId$type]
  856.         );
  857.     }
  858.     public static function getInvoice()
  859.     {
  860.         return
  861.             'SELECT m.id,m.filename, m.created_at, m.profile_id
  862.              FROM advertisement_invoice AS m
  863.              WHERE m.profile_id = ?
  864.              AND m.type = ?';
  865.     }
  866.     public static function getInvoices($id$type 'invoice')
  867.     {
  868.         return (array)WebModel::getContainer()->get('database')->getRecords(
  869.             'SELECT m.id,m.filename, m.created_at
  870.              FROM advertisement_invoice AS m
  871.              WHERE m.profile_id = ?
  872.              AND m.type = ?',
  873.             [$id$type]
  874.         );
  875.     }
  876.     public static function updateAdvertisement(array $advertisementPost$advertisementId)
  877.     {
  878.         WebModel::getContainer()->get('database')->update('advertisement_posts'$advertisementPost'id = ?'$advertisementId);
  879.     }
  880.     public static function getZeroHits($subject null$location null)
  881.     {
  882.         return (array)WebModel::getContainer()->get('database')->getRecord(
  883.             'SELECT *
  884.              FROM search_results
  885.              WHERE subject = ?
  886.              AND location = ?',
  887.             [$subject$location]
  888.         );
  889.     }
  890.     public static function getAllZeroHits()
  891.     {
  892.         return (array)WebModel::getContainer()->get('database')->getRecords(
  893.             'SELECT *
  894.              FROM search_results'
  895.         );
  896.     }
  897.     public static function addZeroHists($item)
  898.     {
  899.         $database WebModel::getContainer()->get('database');
  900.         $database->insert('search_results'$item);
  901.     }
  902.     public static function addHist($item)
  903.     {
  904.         $database WebModel::getContainer()->get('database');
  905.         $database->insert('advertisement_search_result'$item);
  906.     }
  907.     public static function updateZeroHists($id$item)
  908.     {
  909.         $database WebModel::getContainer()->get('database');
  910.         $database->update('search_results'$item'id = ?'$id);
  911.     }
  912.     public static function getOldRatings()
  913.     {
  914.         return (array)WebModel::getContainer()->get('database')->getRecords(
  915.             'SELECT *
  916.              FROM waardering WHERE rating > 0'
  917.         );
  918.     }
  919.     public static function getOldTags($id)
  920.     {
  921.         return (array)WebModel::getContainer()->get('database')->getRecords(
  922.             'SELECT t.Trefwoord
  923.              FROM vermeldingtrefwoorden as vt
  924.              INNER JOIN trefwoorden as t ON t.TrefwoordID = vt.TrefwoordID
  925.              WHERE vt.VermeldingID = ?
  926.              GROUP BY t.trefwoord',
  927.             [$id]
  928.         );
  929.     }
  930.     public static function getTagByName($category$name)
  931.     {
  932.         return (array)WebModel::getContainer()->get('database')->getRecord(
  933.             'SELECT t.header, t.title
  934.              FROM advertisement_tags as t
  935.              WHERE t.title LIKE ? AND category_id = ?',
  936.             ["%$name%"$category]
  937.         );
  938.     }
  939.     public static function getTagByNameCat($category$name)
  940.     {
  941.         return (array)WebModel::getContainer()->get('database')->getRecord(
  942.             'SELECT t.header, t.title
  943.              FROM advertisement_tags as t
  944.              WHERE t.title LIKE ? AND category_id = ?',
  945.             ["$name"$category]
  946.         );
  947.     }
  948.     public static function getStats($id)
  949.     {
  950.         return (array)WebModel::getContainer()->get('database')->getRecords(
  951.             'SELECT * FROM advertisement_stats WHERE advertisement_id = ?',
  952.             [$id]
  953.         );
  954.     }
  955.     public static function insertOfferte($postId$dataId$type)
  956.     {
  957.         $database WebModel::getContainer()->get('database');
  958.         $database->insert('advertisement_offerte', ['post_id' => $postId'data_id' => $dataId'type' => $type]);
  959.     }
  960.     public static function addMedia($data)
  961.     {
  962.         $database WebModel::getContainer()->get('database');
  963.         $database->insert('advertisement_media'$data);
  964.     }
  965.     public function checkComment($ip$postId)
  966.     {
  967.         if (strpos($ip'2001:982:68dc:1') !== false) {
  968.             return false;
  969.         }else {
  970.             return (array)WebModel::getContainer()->get('database')->getRecords(
  971.                 'SELECT * FROM advertisement_comments WHERE post_id = ? AND ip_address LIKE ?',
  972.                 [$postId$ip]
  973.             );
  974.         }
  975.     }
  976. }