<?php


define('_IN_JOHNCMS', 1);
$headmod = 'awardpanel';
$textl = 'Sovrinlar';
require('../incfiles/core.php');
require('../incfiles/head.php');
require('../incfiles/lib/class.upload.php');
$error = '';
if (!$user_id)
    $error = $lng['access_guest_forbidden'];
if ($error) {
    require_once ("../incfiles/head.php");
    echo '<div class="rmenu"><p>' . $error . '</p></div>';
    require_once ("../incfiles/end.php");
    exit;
}
if ($rights < 9) header("location: index.php");

switch ($act) { 
    default:
		echo '<div class="phdr">Barcha sovrinlar</div>';
		echo '<div class="menu">';
		$id = isset($_GET['id']) ? abs(intval($_GET['id'])) : 0;
		$count = mysql_result(mysql_query("select count(*) from `awards`"),0);
			if	($count	> 0) {
						$req = mysql_query("select `awards`.*, `users`.`name` as `nick` from `awards` left join `users` on `awards`.`user_id` = `users`.`id` order by `awards`.`id` desc limit $start, $kmess");
						while ($res = mysql_fetch_array($req)) {

$name=html_entity_decode($res[name]);

$info = html_entity_decode($res[desc]);
							echo '<div class="menu">';
							echo '<img src="img/'.functions::checkout($res['img']). '.jpg" alt="Sovrin"/> <br />Nomi: <span style="font-size: 14px;">'.$name.'</span>';
							if ($res['desc'] != '')echo  '<br /> Ma\'lumot: <span style="font-size: 14px;">'.$info.'</span>';
							echo '<br /> Qo\'shdi: <span style="font-size: 14px;"><a href="../users/profile.php?user=' .abs(intval($res['user_id'])). '">' .functions::checkout($res['nick']). '</a></span> <br />' . functions::display_date(abs(intval($res['time'])));
							$idn = abs(intval($res['id']));
							if ($id) {
								$tot2 =  mysql_result(mysql_query("select count(*) from `award_users` where `id_user` = '".$id."' and `id_award` = '".$idn."'"),0);
								if (!$tot2) echo '<br /> <a href="panel.php?act=give&amp;user='.$id.'&amp;id='. $idn .'">Sovrin berish</a>';
								else echo '<br /> <a href="panel.php?act=take&amp;user='.$id.'&amp;id='. $idn.'">Sovrinni olish</a>';
							}
							$tot1 =  mysql_result(mysql_query("select count(*) from `award_users` where `id_award` = '". $idn."'"),0);
							echo '<br />Berildi: ' .($tot1 ? '<a href="panel.php?act=given&amp;id='. $idn .'">'.$tot1.'</a> marta' : '0 marta');
							if (!$id) echo '<br /> <a href="panel.php?act=del&amp;id=' . $idn . '">O\'chirish</a>';
							echo '</div>';
						}
				}
			else echo '<div class="menu">sovrinlar yo\'q </div>';	
			if ($count > $kmess) echo '<div class="topmenu">' . functions::display_pagination('panel.php?', $start, $count, $kmess) . '</div>';
			
			
		echo '</div><div class="gmenu"><a href="panel.php?act=new">Sovrin qo\'shish</a></div>';	
	
	break;
	
	case "new":
	echo '<div class="menu">';
	if (isset ($_POST['submit'])) {
			$name = !empty($_POST['name']) ? mb_substr(trim($_POST['name']), 0, 50) : 'Sovrin';
			$name = functions::check($name);
			$desc = !empty($_POST['desc']) ? mb_substr(trim($_POST['desc']), 0, 150) : '';
			$desc = functions::check($desc);
			$handle = new upload($_FILES['file']);
			if ($handle->uploaded) {
					$fname = time();
					$handle->file_new_name_body = $fname;
					$handle->allowed = array (
						'image/jpeg',
						'image/gif',
						'image/png'
					);
					$handle->file_max_size = 1024 * $set['flsz'];
					$handle->file_overwrite = true;
					$handle->image_convert = 'jpg';
					$handle->process('img/');
					if ($handle->processed) {
						mysql_query("INSERT INTO `awards` SET
							`name`='" . $name . "',
							`desc`='" . $desc . "',
							`user_id`='" . $user_id . "',
							`img`='" . $fname . "',
							`time`='" . time() . "'		
							");
					} else {
						echo functions::display_error($handle->error);
					}
					$handle->clean();
				}
				header("location: panel.php");
			}

			else {
				echo '<form action="panel.php?act=new"  method="post" enctype="multipart/form-data">';
				echo 'Nomi <small>(max. 50 belgi)</small>:<br/> <input name="name" type="text" /><br/>';
				echo 'Ma\'lumoti <small>(max. 150 belgi)</small>:<br/> <textarea name="desc" cols="25" rows="3" ></textarea><br/>';
				echo 'Rasmi:<br /><input type="file" name="file" /><br />';
				echo '<input type="submit" name = "submit" value="Qo\'shish" /></form>';
				
		}
		echo '</div>';
		echo '<div class="gmenu"><a href="panel.php">Orqaga</a></div>';
	break;
	
	case "give":
		echo '<div class="menu">';
		if (isset($_GET['yes'])) {
			$id = isset($_GET['id']) ? abs(intval($_GET['id'])) : 0;
			$user = isset($_GET['user']) ? abs(intval($_GET['user'])) : 0;
			$count1 = mysql_result(mysql_query("select count(*) from `users` where `id` = '".$user."'"),0);
			$count2 = mysql_result(mysql_query("select count(*) from `awards` where `id` = '".$id."'"),0);
			$count3 = mysql_result(mysql_query("select count(*) from `award_users` where `id_award` = '".$id."' and `id_user` = '".$user."'"),0);
			if ($count3 or !$count1 or !$count2) header("location: panel.php?id=$user");
			else {
				mysql_query("INSERT INTO `award_users` SET
								`id_award`='" . $id . "',
								`id_user`='" . $user . "',
								`id_who`='" . $user_id . "',
								`time`='" . time() . "'		
								");
				echo '<div class="menu">Foydalanuvchi mukofotlandi!</div>';
			}
		}
		else {
			 $id = isset($_GET['id']) ? abs(intval($_GET['id'])) : 0;
			 $user = isset($_GET['user']) ? abs(intval($_GET['user'])) : 0;
			 if ($user and $id) {
				 echo '<div class="menu">Siz rostdan ham ushbu foydalanuvchini mukofotlamoqchimisiz?<br/>';
				 echo '<a href="panel.php?act=give&amp;user='.$user.'&amp;id='.$id.'&amp;yes">Mukofotlash</a> | <a href="panel.php?id='.$user.'">Orqaga</a></div>';
			}
			else header("location: panel.php?id=$user");
		}	
		echo '</div>';
		echo '<div class="gmenu"><a href="panel.php?id='.$user.'">Orqaga</a></div>';
	break;
	
	case "take":
		echo '<div class="menu">';
		if (isset($_GET['yes'])) {
			$id = isset($_GET['id']) ? abs(intval($_GET['id'])) : 0;
			$user = isset($_GET['user']) ? abs(intval($_GET['user'])) : 0;
			$count = mysql_result(mysql_query("select count(*) from `award_users` where `id_award` = '".$id."' and `id_user` = '".$user."'"),0);
			if (!$count) header("location: panel.php?id=$user");
			else {
				mysql_unbuffered_query ( "delete from `award_users` where `id_award` = '".$id."' and `id_user` = '".$user."'");
				echo '<div class="menu">Siz ushbu foydalanuvchining sovrinini olib tashladingiz !</div>';
			}
		}
		else {
			 $id = isset($_GET['id']) ? abs(intval($_GET['id'])) : 0;
			 $user = isset($_GET['user']) ? abs(intval($_GET['user'])) : 0;
			 if ($user and $id) {
				 echo '<div class="menu">Siz ushbu foydalanuvchining sovrinini olib tashlamoqchimisiz?<br/>';
				 echo '<a href="panel.php?act=take&amp;user='.$user.'&amp;id='.$id.'&amp;yes">Olib tashlash</a> | <a href="panel.php?id='.$user.'">Orqaga</a></div>';
			}
			else header("location: panel.php?id=$user");
		}	
		echo '</div>';
		echo '<div class="gmenu"><a href="panel.php">Orqaga</a></div>';
	break;
	
	case "del":
		echo '<div class="menu">';
		if (isset($_GET['yes'])) {
			$id = isset($_GET['id']) ? abs(intval($_GET['id'])) : 0;
			$req = mysql_query("select `img` from `awards` where `id` = '".$id."'");
			$res = mysql_fetch_array($req);
			$count = mysql_num_rows($req);
			if ($count) {
				$file = 'img/'.abs(intval($res['img'])).'.jpg';
				unlink ($file);
				mysql_unbuffered_query ( "delete from `awards` where `id` = '".$id."'");
				mysql_unbuffered_query ( "delete from `award_users` where `id_award` = '".$id."'");
				echo '<div class="menu">Sovrin o\'chirildi!</div>';
			}
			else header("location: panel.php");
		}
		else {
			 $id = isset($_GET['id']) ? abs(intval($_GET['id'])) : 0;
			 echo '<div class="menu">Siz rostdan ham shu sovrinni o\'chirmoqchimisiz?<br/>';
			 echo '<a href="panel.php?act=del&amp;id='.$id.'&amp;yes">O\'chirish</a> | <a href="panel.php">Orqaga</a></div>';
		}
		echo '</div>';
		echo '<div class="gmenu"><a href="panel.php">Orqaga</a></div>';
	break;
	
	case "given":
		echo '<div class="menu">';
		$id = isset($_GET['id']) ? abs(intval($_GET['id'])) : 0;
		$count = mysql_result(mysql_query("select count(*) from `awards` where `id` = '".$id."'"),0);
		if ($count) {
			$req = mysql_query("select `award_users`.*, `users`.`name` as `nick` from `award_users` left join `users` on `award_users`.`id_user` = `users`.`id` where `id_award` = '".$id."'");
				while ($res = mysql_fetch_array($req)) {
					echo '<a href="../users/profile.php?user=' .abs(intval($res['id_user'])). '">' .functions::checkout($res['nick']). '</a>, ';
				}
		}
		else header("location: panel.php");
		echo '</div>';
		echo '<div class="tmn"><a href="panel.php">Orqaga</a></div>';
	break;
}

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