Файл: new_top/core/classes/Ads.php
Строк: 75
<?php
/**
* Advertising slots — display and helpers
*/
declare(strict_types=1);
class Ads
{
public static function positions(): array
{
return [
'home_top' => 'Главная — верх',
'home_bottom' => 'Главная — низ',
'cat_top' => 'Категории — верх',
'stats_top' => 'Статистика — верх',
'global_footer' => 'Подвал (все страницы)',
];
}
/** Active ads for a placement */
public static function forPosition(string $position, int $limit = 10): array
{
$now = time();
try {
return Database::fetchAll(
'SELECT * FROM ads
WHERE position = ? AND status = 1
AND (time_end = 0 OR time_end > ?)
ORDER BY id DESC
LIMIT ' . (int)$limit,
[$position, $now]
);
} catch (Throwable $e) {
return [];
}
}
public static function prices(): array
{
try {
return Database::fetchAll(
'SELECT * FROM ad_prices WHERE enabled = 1 ORDER BY sort, position'
);
} catch (Throwable $e) {
return [];
}
}
public static function price(string $position): ?array
{
try {
return Database::fetch(
'SELECT * FROM ad_prices WHERE position = ? AND enabled = 1',
[$position]
);
} catch (Throwable $e) {
return null;
}
}
/** HTML block for placement */
public static function render(string $position): string
{
$items = self::forPosition($position);
if (!$items) {
return '';
}
$html = '<div class="ad-slot ad-slot-' . e($position) . '">';
foreach ($items as $ad) {
$style = '';
if (!empty($ad['color'])) {
$style .= 'color:' . e($ad['color']) . ';';
}
if (!empty($ad['bold'])) {
$style .= 'font-weight:700;';
}
if (!empty($ad['italic'])) {
$style .= 'font-style:italic;';
}
$url = $ad['url'];
if (!preg_match('#^https?://#i', $url)) {
$url = 'http://' . $url;
}
$label = $ad['name'] !== '' ? $ad['name'] : $ad['url'];
$text = $ad['text'] !== '' ? $ad['text'] : '';
$html .= '<a class="ad-item" href="' . e($url) . '" target="_blank" rel="nofollow sponsored noopener" style="' . $style . '">';
$html .= '<span class="ad-name">' . e($label) . '</span>';
if ($text !== '') {
$html .= '<span class="ad-text">' . e($text) . '</span>';
}
$html .= '</a>';
}
$html .= '</div>';
return $html;
}
public static function statusLabel(int $status): string
{
switch ($status) {
case 0: return 'Ожидает';
case 1: return 'Активна';
case 2: return 'Отклонена';
case 3: return 'Истекла';
default: return (string)$status;
}
}
}