<?php

/**
 * @package     JohnCMS
 * @link        http://johncms.com
 * @copyright   Copyright (C) 2008-2011 JohnCMS Community
 * @license     LICENSE.txt (see attached file)
 * @version     VERSION.txt (see attached file)
 * @author      http://johncms.com/about
 */

echo '<title>Покупка рекламы</title>';
define('_IN_JOHNCMS', 1);
require('incfiles/core.php');
$lng = array_merge($lng, core::load_lng('admin'));
/*
-----------------------------------------------------------------
Получаем данные пользователя
-----------------------------------------------------------------
*/
$user = functions::get_user($user);
if (!$user) {
    require('incfiles/head.php');
    echo 'Для покупки рекламы нужно авторизоваться!';
    require('incfiles/end.php');
    exit;
}
switch ($mod) {
    case 'edit':
	require('incfiles/head.php');
        /*
        -----------------------------------------------------------------
        Добавляем / редактируем ссылку
        -----------------------------------------------------------------
        */
        echo '<div class="phdr"><a href="index.php?act=ads"><b>' . $lng['advertisement'] . '</b></a> | ' . ($id ? $lng['link_edit'] : $lng['link_add']) . '</div>';
        if ($id) {
            // Если ссылка редактироется, запрашиваем ее данные в базе
            $req = mysql_query("SELECT * FROM `cms_ads` WHERE `id` = '$id'");
            if (mysql_num_rows($req)) {
                $res = mysql_fetch_assoc($req);
            } else {
                echo functions::display_error($lng['error_wrong_data'], '<a href="index.php?act=ads">' . $lng['back'] . '</a>');
                require('../incfiles/end.php');
                exit;
            }
        } else {
            $res = array('link' => 'http://');
        }
        if (isset($_POST['submit'])) {
            $link = isset($_POST['link']) ? mysql_real_escape_string(trim($_POST['link'])) : '';
            $name = isset($_POST['name']) ? mysql_real_escape_string(trim($_POST['name'])) : '';
            $bold = isset($_POST['bold']);
            $italic = isset($_POST['italic']);
            $underline = isset($_POST['underline']);
            $show = isset($_POST['show']);
            $font = $font_1 + $font_2 + $font_3;
            $view = isset($_POST['view']) ? abs(intval($_POST['view'])) : 0;
            $day = isset($_POST['day']) ? abs(intval($_POST['day'])) : 0;
            $dayp = isset($_POST['dayp']) ? abs(intval($_POST['dayp'])) : 0;
            $count = isset($_POST['count']) ? abs(intval($_POST['count'])) : 0;
            $day = isset($_POST['day']) ? abs(intval($_POST['day'])) : 0;
            $layout = isset($_POST['layout']) ? abs(intval($_POST['layout'])) : 0;
            $type = isset($_POST['type']) ? intval($_POST['type']) : 1;
            $mesto = isset($_POST['mesto']) ? abs(intval($_POST['mesto'])) : 0;
            $color = isset($_POST['color']) ? mb_substr(trim($_POST['color']), 0, 6) : '';
            
			$cdey = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rday' LIMIT 1"));
$aday = $cdey['val'] * $day;

if (empty($color)) {
	$a1 = 0; }else{ 
$rcolor = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rcolor' LIMIT 1"));
$a1 = $rcolor['val']; }

if ($type==1) {
	$rp1 = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rp1' LIMIT 1"));
$a2 = $rp1['val']; } 
	elseif($type==2){ 
$rp2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rp2' LIMIT 1"));
$a2 = $rp2['val']; }

if ($layout==0)  {
	$rall = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rall' LIMIT 1"));
$a3 = $rall['val'];} else {
	$rindex = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rindex' LIMIT 1"));
$a3 = $rindex['val'];
	}
	
if ($bold==0)  {
	$a4=0; } else {
	$rb = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rb' LIMIT 1"));
$a4 = $rb['val'];
	}
	
	if ($italic==0)  {
	$a5=0; } else {
	$ri = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'ri' LIMIT 1"));
$a5 = $ri['val'];
	}
	
	if ($underline==0)  {
	$a6=0; } else {
	$ru = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'ru' LIMIT 1"));
$a6 = $ru['val'];
	}

$all = $aday + $a1 + $a2 + $a3 + $a4 + $a5 + $a6;


	 $yyu = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_ads` WHERE `id` = '$id' LIMIT 1"));

$idey = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rday' LIMIT 1"));
$uday = $idey['val'] * $dayp;

	
if ($yyu['color'] == $color) {
	$b1 = 0; }else{ 
$rcolor = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rcolor' LIMIT 1"));
$b1 = $rcolor['val']; }

	if ($yyu['type'] == $type) {$b2=0;} else {	
if ($type==1) {
	$rp1 = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rp1' LIMIT 1"));
$b2 = $rp1['val']; } 
	elseif($type==2){ 
$rp2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rp2' LIMIT 1"));
$b2 = $rp2['val']; }}
if ($yyu['layout'] == $layout) {$b3=0;} else {
if ($layout==0)  {
	$rall = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rall' LIMIT 1"));
$b3 = $rall['val'];} else {
	$rindex = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rindex' LIMIT 1"));
$b3 = $rindex['val'];
	}}
if ($yyu['bold'] == $bold)  {
	$b4 = 0; }else {	
if ($bold==0)  {
	$a4=0; } else {
	$rb = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rb' LIMIT 1"));
$b4 = $rb['val'];
	}
}
if ($yyu['italic'] == $italic) {
	$b5=0; } else {
	if ($italic==0)  {
	$b5=0; } else {
	$ri = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'ri' LIMIT 1"));
$b5 = $ri['val'];
	}}
	
	if ($yyu['underline'] == $underline) {
		$b6=0; }else {
		if ($underline==0)  {
	$b6=0; } else {
	$ru = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'ru' LIMIT 1"));
$b6 = $ru['val'];
	}}

$all2 = $uday + $b1 + $b2 + $b3 + $b4 + $b5 + $b6;
			
						
				$alkday=$yyu['day']+$dayp;
				
				
			$error = array();
            if (!$link || !$name)
                $error[] = $lng['error_empty_fields'];
            if ($type > 3 || $type < 0)
                $type = 0;
            if (!$mesto) {
                $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_ads` WHERE `mesto` = '" . $mesto . "' AND `type` = '" . $type . "'"), 0);
                if ($total != 0)
                    $error[] = $lng['links_place_occupied'];
            }
			 if (!$id) {
				 if ($day==0) {
				$error[] = 'Заполните количество дней';
				}
				
				}
				
			 if ($id) {	if ($user['id']!= $yyu['user_id']){
			$error[] = 'Нельзя редактировать чужие ссылки';
			} }
            if ($color) {
                if (preg_match("/[^\da-fA-F_]+/", $color))
                    $error[] = $lng['error_wrong_symbols'];
                if (strlen($color) < 6)
                    $error[] = $lng['error_color'];
            }
		if (($user['balans']<$all) || ($user['balans']<$all2)){
			$error[] = 'Не хватает денег';
			}
			
			
			
			
            if ($error) {
                echo functions::display_error($error, '<a href="/ads.php?from=addlink">' . $lng['back'] . '</a>');
                require('../incfiles/end.php');
                exit;
            }
            if ($id) {

                // Обновляем ссылку после редактирования
                mysql_query("UPDATE `cms_ads` SET
                    `type` = '$type',
                    `view` = '$view',
                    `link` = '$link',
                    `name` = '$name',
                    `color` = '$color',
                    `count_link` = '$count',
                    `day` = '$alkday',
                    `layout` = '$layout',
                    `bold` = '$bold',
                    `show` = '0',
                    `italic` = '$italic',
					`user_id` = '$user[id]',
                    `underline` = '$underline'
				     WHERE `id` = '$id'
                ");

				 mysql_query("UPDATE `users` SET `balans` = '".($user['balans']-$all2)."' WHERE `id` = '$user[id]' LIMIT 1");



            } else {
			   

                $req = mysql_query("SELECT `mesto` FROM `cms_ads` ORDER BY `mesto` DESC LIMIT 1");
                if (mysql_num_rows($req) > 0) {
                    $res = mysql_fetch_array($req);
                    $mesto = $res['mesto'] + 1;
                } else {
                    $mesto = 1;
                }
				 mysql_query("UPDATE `users` SET `balans` = '".($user['balans']-$all)."' WHERE `id` = '$user[id]' LIMIT 1");

 
                mysql_query("INSERT INTO `cms_ads` SET
                    `type` = '$type',
                    `view` = '$view',
                    `mesto` = '$mesto',
                    `link` = '$link',
                    `name` = '$name',
                    `color` = '$color',
                    `count_link` = '$count',
                    `day` = '$day',
                    `layout` = '$layout',
                    `to` = '0',
                    `show` = '0',
                    `time` = '" . time() . "',
                    `bold` = '$bold',
                    `italic` = '$italic',
					`user_id` = '$user[id]',
                    `underline` = '$underline'
					
                ") or die (mysql_error());
				
           }
            mysql_query("UPDATE `users` SET `lastpost` = '" . time() . "' WHERE `id` = '$user_id'");
            echo '<div class="menu"><p>' . ($id ? $lng['link_edit_ok'] : $lng['link_add_ok']) . '<br />' .
                 '<a href="/ads.php?sort=' . $type . '">' . $lng['continue'] . '</a></p></div>';
			} else {
          
		  
    $cdey = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rday' LIMIT 1"));

    $rcolor = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rcolor' LIMIT 1"));

	$rp1 = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rp1' LIMIT 1"));

    $rp2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rp2' LIMIT 1"));

	$rall = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rall' LIMIT 1"));

	$rindex = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rindex' LIMIT 1"));

	$rb = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'rb' LIMIT 1"));

	$ri = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'ri' LIMIT 1"));

	$ru = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'ru' LIMIT 1"));

 
            echo '<form action="/ads.php?mod=edit' . ($id ? '&amp;id=' . $id : '') . '" method="post">' .
                 '<div class="menu"><p><h3>' . $lng['link'] . '</h3>' .
                 '<input type="text" name="link" value="' . htmlentities($res['link'], ENT_QUOTES, 'UTF-8') . '"/><br />' .
                 '<input type="checkbox" name="show" ' . ($res['show'] ? 'checked="checked"' : '') . '/>&nbsp;' . $lng['link_direct'] . '<br />' .
                 '<small>' . $lng['link_direct_help'] . '</small></p>' .
                 '<p><h3>' . $lng['title'] . '</h3>' .
                 '<input type="text" name="name" value="' . htmlentities($res['name'], ENT_QUOTES, 'UTF-8') . '"/><br />' .
                 '<small>' . $lng['link_add_name_help'] . '</small></p>' .
                 '<p><h3>' . $lng['color'] . '</h3>' .
                 '<input type="text" name="color" size="6" value="' . $res['color'] . '"/><br />			 ' .
                 '<small>' . $lng['link_add_color_help'] . '</small></p>
				  <p style="color:#009966; font-weight:bold;">Стоимость: '. $rcolor['val'] .' руб </p>';
				 if (!$id) { echo '<p><h3>' . $lng['days'] . '</h3>' .
                 '<input type="text" name="day" size="6" value="' . $res['day'] . '"/><br />' .
                 '<small>Количество дней с момента установки ссылки, по истечению которого ссылка автоматически уберется</small></p>
				  <p style="color:#009966; font-weight:bold;">Стоимость одного дня: '. $cdey['val'] .' руб </p>' .
                 '';}
				 else {echo '<p><h3>Продлить на (дней):</h3>' .
                 '<input type="text" name="dayp" size="6"/><br />' .
                 '<small>Количество дней с момента установки ссылки, по истечению которого ссылка автоматически уберется</small></p>
				  <p style="color:#009966; font-weight:bold;">Стоимость одного дня: '. $cdey['val'] .' руб </p>' .
                 '';
					 }
				 echo '</div><div class="gmenu">' .
                   '<p><h3>' . $lng['arrangement'] . '</h3>' .
                 '<input type="radio" name="type" value="1" ' . (!$res['type'] == 1 ? 'checked="checked"' : '') . '/>&nbsp;' . $lng['links_armt_under_usermenu'] . ' <font style="color:#009966; font-weight:bold;"> '. $rp1['val'] .' руб </font><br />' .
                 '<input type="radio" name="type" value="2" ' . ($res['type'] == 2 ? 'checked="checked"' : '') . '/>&nbsp;' . $lng['links_armt_over_counters'] . '<font style="color:#009966; font-weight:bold;"> '. $rp2['val'] .' руб </font><br />' .
                 '
				 <p><h3>' . $lng['placing'] . '</h3>' .
                 '<input type="radio" name="layout" value="0" ' . (!$res['layout'] ? 'checked="checked"' : '') . '/>&nbsp;' . $lng['link_add_placing_all'] . ' <font style="color:#009966; font-weight:bold;"> '. $rall['val'] .' руб </font><br />' .
                 '<input type="radio" name="layout" value="1" ' . ($res['layout'] == 1 ? 'checked="checked"' : '') . '/>&nbsp;' . $lng['link_add_placing_front'] . ' <font style="color:#009966; font-weight:bold;"> '. $rindex['val'] .' руб </font><br />' .
                 '<p><h3>' . $lng['links_allocation'] . '</h3>' .
                 '<input type="checkbox" name="bold" ' . ($res['bold'] ? 'checked="checked"' : '') . '/>&nbsp;<b>' . $lng['font_bold'] . '</b>  <font style="color:#009966; font-weight:bold;">+ '. $rb['val'] .' руб </font><br />' .
                 '<input type="checkbox" name="italic" ' . ($res['italic'] ? 'checked="checked"' : '') . '/>&nbsp;<i>' . $lng['font_italic'] . '</i> <font style="color:#009966; font-weight:bold;">+ '. $ri['val'] .' руб </font><br />' .
                 '<input type="checkbox" name="underline" ' . ($res['underline'] ? 'checked="checked"' : '') . '/>&nbsp;<u>' . $lng['font_underline'] . '</u> <font style="color:#009966; font-weight:bold;">+ '. $ru['val'] .' руб </font></p></div>' .
                 '<div class="phdr"><input type="submit" class = "btn btn-default" name="submit" value="' . ($id ? $lng['edit'] : $lng['add']) . '" /></div></form>' .
                 '<p><a href="index.php?act=ads">' . $lng['advertisement'] . '</a><br />' .
                 '<a href="index.php">' . $lng['admin_panel'] . '</a></p>';
        } 
        break;

    case 'down':
		 require('incfiles/head.php');
        /*
        -----------------------------------------------------------------
        Перемещаем на позицию вниз
        -----------------------------------------------------------------
        */
		$yyu = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_ads` WHERE `id` = '$id' LIMIT 1"));

        if (($id) && ($user['id'] == $yyu['user_id'])) {
            $req = mysql_query("SELECT `mesto`, `type` FROM `cms_ads` WHERE `id` = '$id'");
            if (mysql_num_rows($req) > 0) {
                $res = mysql_fetch_array($req);
                $mesto = $res['mesto'];
                $req = mysql_query("SELECT * FROM `cms_ads` WHERE `mesto` > '$mesto' AND `type` = '" . $res['type'] . "' ORDER BY `mesto` ASC");
                if (mysql_num_rows($req) > 0) {
                    $res = mysql_fetch_array($req);
                    $id2 = $res['id'];
                    $mesto2 = $res['mesto'];
                    mysql_query("UPDATE `cms_ads` SET `mesto` = '$mesto2' WHERE `id` = '$id'");
                    mysql_query("UPDATE `cms_ads` SET `mesto` = '$mesto' WHERE `id` = '$id2'");
                }
            }
        }
        header('Location: ' . getenv("HTTP_REFERER"));
        break;

    case 'up':
        /*
        -----------------------------------------------------------------
        Перемещаем на позицию вверх
        -----------------------------------------------------------------
        */	 require('incfiles/head.php');
		$yyu = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_ads` WHERE `id` = '$id' LIMIT 1"));

         if (($id) && ($user['id'] == $yyu['user_id'])){
			 
            $req = mysql_query("SELECT `mesto`, `type` FROM `cms_ads` WHERE `id` = '$id'");
            if (mysql_num_rows($req) > 0) {
                $res = mysql_fetch_array($req);
                $mesto = $res['mesto'];
                $req = mysql_query("SELECT * FROM `cms_ads` WHERE `mesto` < '$mesto' AND `type` = '" . $res['type'] . "' ORDER BY `mesto` DESC");
                if (mysql_num_rows($req) > 0) {
                    $res = mysql_fetch_array($req);
                    $id2 = $res['id'];
                    $mesto2 = $res['mesto'];
                    mysql_query("UPDATE `cms_ads` SET `mesto` = '$mesto2' WHERE `id` = '$id'");
                    mysql_query("UPDATE `cms_ads` SET `mesto` = '$mesto' WHERE `id` = '$id2'");
                }
            }
        }
        header('Location: ' . getenv("HTTP_REFERER") . '');
        break;

    case 'del':
        /*
        -----------------------------------------------------------------
        Удаляем ссылку
        -----------------------------------------------------------------
        */
			 require('incfiles/head.php');
			 $yyu = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_ads` WHERE `id` = '$id' LIMIT 1"));

         if (($id) && ($user['id'] == $yyu['user_id'])) {
		
            if (isset($_POST['submit'])) {
                mysql_query("DELETE FROM `cms_ads` WHERE `id` = '$id'");
                header('Location: ' . $_POST['ref']);
            } else {
                echo '<div class="phdr"><a href="index.php?act=ads"><b>' . $lng['advertisement'] . '</b></a> | ' . $lng['delete'] . '</div>' .
                     '<div class="rmenu"><form action="/ads.php?mod=del&amp;id=' . $id . '" method="post">' .
                     '<p>' . $lng['link_deletion_warning'] . '</p>' .
                     '<p><input type="submit" name="submit" value="' . $lng['delete'] . '" /></p>' .
                     '<input type="hidden" name="ref" value="' . htmlspecialchars($_SERVER['HTTP_REFERER']) . '" />' .
                     '</form></div>' .
                     '<div class="phdr"><a href="' . htmlspecialchars($_SERVER['HTTP_REFERER']) . '">' . $lng['cancel'] . '</a></div>';
            }
        }
        break;

    case 'clear':
        /*
        -----------------------------------------------------------------
        Очистка базы от неактивных ссылок
        -----------------------------------------------------------------
        */
       /* if (isset($_POST['submit'])) {
            mysql_query("DELETE FROM `cms_ads` WHERE `to` = '1'");
            mysql_query("OPTIMIZE TABLE `cms_ads`");
            header('location: index.php?act=ads');
        } else {
            echo '<div class="phdr"><a href="index.php?act=ads"><b>' . $lng['advertisement'] . '</b></a> | ' . $lng['links_delete_hidden'] . '</div>' .
                 '<div class="menu"><form method="post" action="/ads.php?mod=clear">' .
                 '<p>' . $lng['link_clear_warning'] . '</p>' .
                 '<p><input type="submit" name="submit" value="' . $lng['delete'] . '" />' .
                 '</p></form></div>' .
                 '<div class="phdr"><a href="index.php?act=ads">' . $lng['cancel'] . '</a></div>';
        }*/
        break;

    case 'show':
        /*
        -----------------------------------------------------------------
        Восстанавливаем / скрываем ссылку
        -----------------------------------------------------------------
        */
		require('incfiles/head.php');
        if ($id) {
            $req = mysql_query("SELECT * FROM `cms_ads` WHERE `id` = '$id'");
            if (mysql_num_rows($req)) {
                $res = mysql_fetch_assoc($req);
                mysql_query("UPDATE `cms_ads` SET `to`='" . ($res['to'] ? 0 : 1) . "' WHERE `id` = '$id'");
            }
        }
        header('Location: ' . $_SERVER['HTTP_REFERER']);
        break;

    default:
	 require('incfiles/head.php');
        /*
        -----------------------------------------------------------------
        Главное меню модуля управления рекламой
        -----------------------------------------------------------------
        */
        echo '<div class="phdr">Покупка рекламы</div>';
        $array_type = array(
            $lng['links_armt_over_logo'],
            $lng['links_armt_under_usermenu'],
            $lng['links_armt_over_counters'],
            $lng['links_armt_under_counters']
        );
        $array_placing = array(
            $lng['link_add_placing_all'],
            $lng['link_add_placing_front'],
            $lng['link_add_placing_child']
        );
        $array_show = array(
            $lng['to_all'],
            $lng['to_guest'],
            $lng['to_users']
        );
        $type = isset($_GET['type']) ? intval($_GET['type']) : 1;
        $array_menu = array(
            (!$type ? $lng['links_armt_over_logo'] : ''),
            ($type == 1 ? $lng['links_armt_under_usermenu'] : '<a href="/ads.php?&amp;type=1">' . $lng['links_armt_under_usermenu'] . '</a>'),
            ($type == 2 ? $lng['links_armt_over_counters'] : '<a href="/ads.php?type=2">' . $lng['links_armt_over_counters'] . '</a>'),
            ($type == 3 ? $lng['links_armt_under_counters'] : '')
        );
        echo '<div class="topmenu">' . functions::display_menu($array_menu) . '</div>';
        $total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_ads` WHERE `type` = '$type' and user_id = '$user[id]'"), 0);
        if ($total) {
            $req = mysql_query("SELECT * FROM `cms_ads` WHERE `type` = '$type' and user_id = '$user[id]' ORDER BY `mesto` ASC LIMIT $start,$kmess");
            $i = 0;
            while ($res = mysql_fetch_assoc($req)) {
                echo $i % 2 ? '<div class="list2">' : '<div class="list1">';
                $name = str_replace('|', '; ', $res['name']);
                $name = htmlentities($name, ENT_QUOTES, 'UTF-8');
                // Если был задан цвет, то применяем
                if (!empty($res['color']))
                    $name = '<span style="color:#' . $res['color'] . '">' . $name . '</span>';
                // Если было задано начертание шрифта, то применяем
                $font = $res['bold'] ? 'font-weight: bold;' : false;
                $font .= $res['italic'] ? ' font-style:italic;' : false;
                $font .= $res['underline'] ? ' text-decoration:underline;' : false;
                if ($font)
                    $name = '<span style="' . $font . '">' . $name . '</span>';
                ////////////////////////////////////////////////////////////
                // Выводим рекламмную ссылку с атрибутами                 //
                ////////////////////////////////////////////////////////////
                echo '<p><img src="../images/' . ($res['to'] ? 'red' : 'green') . '.gif" width="16" height="16" class="left"/>&#160;' .
                     '<a href="' . htmlspecialchars($res['link']) . '">' . htmlspecialchars($res['link']) . '</a>&nbsp;[' . $res['count'] . ']<br />' . $name . '</p>';
                $menu = array(
                    '<a href="/ads.php?mod=edit&amp;id=' . $res['id'] . '">' . $lng['edit'] . '</a>',
                    '<a href="/ads.php?mod=del&amp;id=' . $res['id'] . '">' . $lng['delete'] . '</a>'
                   );
                echo '<div class="sub">' .
                     '<div>' . functions::display_menu($menu) . '</div>' .
                     '<p><span class="gray">' . $lng['installation_date'] . ':</span> ' . functions::display_date($res['time']) . '<br />' .
                     '<span class="gray">' . $lng['placing'] . ':</span>&nbsp;' . $array_placing[$res['layout']] . '<br />' .
                     '<span class="gray">' . $lng['to_show'] . ':</span>&nbsp;' . $array_show[$res['view']];
                // Вычисляем условия договора на рекламу
                $agreement = array();
                $remains = array();
                if (!empty($res['count_link'])) {
                    $agreement[] = $res['count_link'] . ' ' . $lng['transitions_n'];
                    $remains_count = $res['count_link'] - $res['count'];
                    if ($remains_count > 0)
                        $remains[] = $remains_count . ' ' . $lng['transitions_n'];
                }
                if (!empty($res['day'])) {
                    $agreement[] = functions::timecount($res['day'] * 86400);
                    $remains_count = $res['day'] * 86400 - (time() - $res['time']);
                    if ($remains_count > 0)
                        $remains[] = functions::timecount($remains_count);
                }
                // Если был договор, то выводим описание
                if ($agreement) {
                    echo '<br /><span class="gray">' . $lng['agreement'] . ':</span>&nbsp;' . implode($agreement, ', ');
                    if ($remains)
                        echo '<br /><span class="gray">' . $lng['remains'] . ':</span> ' . implode($remains, ', ');
                }
                echo ($res['show'] ? '<br /><span class="red"><b>' . $lng['link_direct'] . '</b></span>' : '') . '</p></div></div>';
                ++$i;
            }
        } else {
            echo '<div class="menu"><p>' . $lng['list_empty'] . '</p></div>';
        }
        echo '<div class="phdr">' . $lng['total'] . ': ' . $total . '</div>';
        if ($total > $kmess) {
            echo '<div class="topmenu">' . functions::display_pagination('/ads.php?type=' . $type . '&amp;', $start, $total, $kmess) . '</div>' .
                 '<p><form action="/ads.php?type=' . $type . '" method="post">' .
                 '<input type="text" name="page" size="2"/>' .
                 '<input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/></form></p>';
        }
        echo '<p><a href="/ads.php?mod=edit">' . $lng['link_add'] . '</a><br />';
}
 require('incfiles/end.php');
?>
