<?php

/**
Jane, icq: 355-350-450, http://4erti.ru
 */
define('_IN_JOHNCMS', 1);
$headmod = 'lider';
$textl = 'Kun lideri';
require('../incfiles/core.php');

require("../incfiles/head.php");
if (!$user_id){
    require_once ("../incfiles/head.php");
echo functions::display_error($lng['access_guest_forbidden']);
    require_once ("../incfiles/end.php");
    exit;
}
switch ($act) {
    default:
if (empty($id)) {
    $textl = 'lider';
} else {
    $req = mysql_query("SELECT `text` FROM `lider` WHERE `id`= '" . $id . "'");
    $res = mysql_fetch_assoc($req);
    $hdr = strtr($res['text'], array(
        '&laquo;' => '',
        '&raquo;' => '',
        '&quot;'  => '',
        '&amp;'   => '',
        '&lt;'    => '',
        '&gt;'    => '',
        '&#039;'  => ''
    ));
    $hdr = mb_substr($hdr, 0, 30);
    $hdr = functions::checkout($hdr);
    $textl = mb_strlen($res['text']) > 30 ? $hdr . '...' : $hdr;
}
if (empty($id)) {
    $textl = 'lider';
} else {
    $req = mysql_query("SELECT `text` FROM `lider` WHERE `id`= '" . $id . "'");
    $res = mysql_fetch_assoc($req);
    $hdr = strtr($res['text'], array(
        '&laquo;' => '',
        '&raquo;' => '',
        '&quot;'  => '',
        '&amp;'   => '',
        '&lt;'    => '',
        '&gt;'    => '',
        '&#039;'  => ''
    ));
    $hdr = mb_substr($hdr, 0, 30);
    $hdr = functions::checkout($hdr);
    $textl = mb_strlen($res['text']) > 30 ? $hdr . '...' : $hdr;
}
		$count_us = mysql_result(mysql_query("select count(`id`) from `lider` where `time` >= '".time()."' and `user_id` = '".$user_id."'"),0);
echo '<div class="phdr" align="center"><b> Kun anketasi ' .($count_us ? '' : '| <a href="index.html?act=get"><small>Kun anketasi bo\'lish</small></a>' ) . '</b></div>';
		echo '<div class="menu">';
		$count = mysql_result(mysql_query("select count(*) from `lider` where `time` >= '".time()."'"),0);
			
		if	($count	> 0) {
			$sets1 = explode('|', file_get_contents('sets.dat'));
			$req = mysql_query("select * from `lider` where `time` >= '".time()."'");
			while ($res = mysql_fetch_array($req)) {
				$us = mysql_fetch_array(mysql_query("SELECT `name`, `yearofbirth`, `dayb`, `monthb` FROM `users` WHERE `id` = '" . $res['user_id'] . "' LIMIT 1"));
				echo '<table><tr>';
				
				if ($sets1[1]) {
					if (file_exists('../files/users/photo/' . $res['user_id'] . '_small.jpg')) 
echo '<td><img src="../files/users/photo/' . $res['user_id'] . '_small.jpg" border="0" width="60" /></td>';
					else 
						echo '<td><img src="no_photo.jpg" border="0" /></td>';
					
				}
echo '<td><img src="../lider/users.png"> Nik:<a href ="../users/profile.html?user='.$res['user_id'].'"> <b>' . $us['name'] . '</b></a><br />';
				if ($sets1[2]) {
						if (!$us['yearofbirth'] or !$us['monthb'] or !$us['dayb']) 
							$a = 0;
						else {
							$r = mktime(0, 0, 0, $us['monthb'], $us['dayb'], $us['yearofbirth']);  
							$age = (time()-$r)/31536000; 
							list($a) = explode(".",$age);   
						}
echo '<img src="../lider/age.png"> Yoshi: '.$a;
				}
echo '<br /><img src="../lider/text.png"> Matn: <i><b>' . $res['text'] .'</b></i>' ;
				if ($rights >=7) echo '<br /><a href="index.html?act=del&amp;id='.$res['id'].'">[удалить]</a>';
echo '</td>';
if($user_id){
echo '<td><a href="/mail/index.html?act=write&amp;id='.$res['user_id'].'"><img src="../lider/sms.png"/></a>';
}
echo '</td></tr></table>';
			}
		}
else echo 'Kun anketasi yo\'q';
				
		echo '</div>';
if ($rights == 9) echo '<div class="tmn"><a href = "index.html?act=panel">Sozlash</a></div>';
	break;
	
	case "get":
echo '<div class="phdr">Kun anketasi bo\'lish</div>';
		echo '<div class="menu">';
		$count = mysql_result(mysql_query("select count(*) from `lider` where `time` >= '".time()."' and `user_id` = '".$user_id."'"),0);
		if ($count)	{
			echo '<div class="rmenu">Услуга уже подключена</div>';
		}
		else {
			$sets1 = explode('|', file_get_contents('sets.dat'));
echo 'Sizning balans - <b>'.$datauser['balans'].'</b> ball. <br />Bir kunlik narxi <b>'.$sets1[0].'</b> ball. <br /> necha kun, kun anketasi bo`lishni xoxlaysiz (max 30):<br />';
			if ($_POST['submit']) {
				$days = abs(intval($_POST['days']));
				if ($days < 1 or $days  > 30) $days = 1;
$text = htmlentities($text,ENT_QUOTES, 'UTF-8');
				$text = functions::check($_POST['text']);
				$cost = $days*$sets1[0];
if ($cost > $datauser['balans']) echo '<div class="rmenu">Sizda yetarlicha ball yo\'q</div>';
				else {
					$t = time() + $days * 24 * 3600;
					mysql_query("update `users` SET `balans`=`balans` - $cost where `id` = '".$user_id."'");
					mysql_query("INSERT INTO `lider` SET
									`user_id`='" . $user_id . "',
									`text` ='".$text."',
									`time`='" . $t . "'		
									");
echo '<div class="rmenu">Tabriklaymiz siz kun anketasiga ega bo`ldingiz  '.$days.' kun.</div> ';
				}
			} 
		
			else {
					echo '<form action="" method="post">';
					echo '<input type="text" name="days" size="2"/>';
echo '<br />Matn kiriting (max 255симв.)<textarea cols="30" rows="2" name="text" ></textarea>';
					echo '<input type="submit" name="submit" value="OK" />';
					echo '</form>';
				}
		}
		echo '</div>';
echo '<div class="tmn"><a href = "index.html">Kun anketasi</a></div>';
	break;
	
	case "panel":
		if ($rights < 9) {
			header("location: index.html");
			exit;
		}
		if ($_POST['submit']) {

			$sets2 = array();
			if (($_POST['sets'][0]) == '') $sets2[0] = 0; else $sets2[0] = abs(intval($_POST['sets'][0]));
			if (!isset($_POST['sets'][1])) $sets2[1] = 0; else $sets2[1] = 1;
			if (!isset($_POST['sets'][2])) $sets2[2] = 0; else $sets2[2] = 1;
			if (file_put_contents('sets.dat', implode('|', $sets2))) echo '<div class="rmenu">Данные записаны</div>';
			
			} 
			
				$sets1 = explode('|', file_get_contents('sets.dat'));
				echo '<div class="phdr">Настройки Лидера Дня</div><div class="menu"><form action="" method="post">';
				echo 'Цена 1 дня показа<br/><input type="text" name="sets[0]" value="'.$sets1[0].'" size="5"/> баллов<br/>';
				echo '<br/><input type="checkbox" name="sets[1]" value="1" ' . ($sets1[1] ? 'checked="checked"' : '') . '/>Показывать фото<br/>';
				echo '<br/><input type="checkbox" name="sets[2]" value="1" ' . ($sets1[2] ? 'checked="checked"' : '') . ' />Показывать возраст<br/>';
				echo '<input type="submit" name="submit" value="Обновить" />';
				echo '</form></div>';
			echo '<div class="tmn"><a href = "index.html">Лидеры Дня</a></div>';
			
	break;
	
	case "del":
		if ($rights < 7) {
			header("location: index.html");
			exit;
		}
		if (isset($_GET['yes'])) {
				mysql_query("DELETE from `lider` where `id` = '".$id."'");
				header("location: index.html");
			}
			else {
			   echo '<p>Вы действительно хотите удалить пользователя из списка лидеров дня?<br/>';
			   echo '<a href="index.html?act=del&amp;id=' . $id . '&amp;yes">Удалить</a> | <a href="index.html">Отмена</a></p>';
			}
		
	break;
	
}

require('../incfiles/end.php');
?>