<?
//Автор: DjBoBaH 
//Сайт: http://my-perm.net 
include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/user.php';

if(isset($user) && isset($_GET['new']))
{
$set['title']='Дневники  - Новый'; // заголовок страницы
include_once '../sys/inc/thead.php';
title();
aut();
header("Content-type: text/html");
echo '<?xml version="2.0" encoding="utf-8"?>';
include_once 'inc/new_act.php';
}
else
{
$set['title']='Дневники '; // заголовок страницы
include_once '../sys/inc/thead.php';
title();
aut();
if(isset($user) && $user['level']>2 && isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `diary` WHERE `id` = '".intval($_GET['del'])."' LIMIT 1"),0)!=0)
{
$del=mysql_fetch_assoc(mysql_query("SELECT * FROM `diary` WHERE `id`='".intval($_GET['del'])."' LIMIT 1"));
$avtor=get_user($del['id_user']);
if($user['id']==$avtor['id'] || $user['level']>$avtor['level'])
{
$images=mysql_query("SELECT * FROM `diary_images` WHERE `id_diary`='$del[id]'");
while ($delete = mysql_fetch_assoc($images))
{
unlink(H.'diary/images/48/'.$delete['id'].'.'.$delete['ras'].'');
unlink(H.'diary/images/128/'.$delete['id'].'.'.$delete['ras'].'');
unlink(H.'diary/images/640/'.$delete['id'].'.'.$delete['ras'].'');
unlink(H.'diary/images/'.$delete['id'].'.'.$delete['ras'].'');
}
mysql_query("DELETE FROM `diary_rating` WHERE `id_diary`='$del[id]'");
mysql_query("DELETE FROM `diary_images` WHERE `id_diary`='$del[id]'");
mysql_query("DELETE FROM `diary_komm` WHERE `id_diary`='$del[id]'");
mysql_query("DELETE FROM `diary` WHERE `id`='$del[id]'");
msg('Дневник успешно удален');
}
else $err[]='Не хватает прав для удаления дневника';
}
if(isset($_GET['sort']))
{
if($_GET['sort']=='viewings'){$sort='viewings';}
elseif($_GET['sort']=='rating'){$sort='rating';}
else{$sort='time';}
}
else
{
$sort='time';
}

$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `diary`"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];

echo'<div class="foot">';
echo'Сортировать:<br/> <a href="?sort=rating&page='.$page.'">Рейтинг</a> <a href="?&sort=time&page='.$page.'">Новые</a> <a href="?&sort=viewings&page='.$page.'">Просмотры</a></div>';

echo '<table class="post">';
if ($k_post==0)
{
echo '<tr>';
echo '<td class="p_t">';
echo 'Нет дневников в данной категории';
echo '</td>';
echo '</tr>';
}
$q=mysql_query("SELECT * FROM `diary` ORDER BY $sort DESC LIMIT $start, $set[p_str]");
while ($diary = mysql_fetch_assoc($q))
{
$us=get_user($diary['id_user']);
echo '<tr>';
echo '<td class="icon14">';
echo '<img src="img/diary.png" alt=""/>';
echo '</td>';
echo '<td class="p_t">';
echo '<a href="/diary/'.$diary['name'].'/">'.$diary['name'].'</a>';
echo' ('.vremja($diary['time']).')';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="p_m" colspan="2">';
echo '<span class="ank_n">Просмотров:</span> <span class="ank_d">'.$diary['viewings'].'</span> | ';
echo '<span class="ank_n">Рейтинг:</span> <span class="ank_d">'.$diary['rating'].'</span><br/>';
echo '<span class="ank_n">Автор:</span> <a href="/info.php?id='.$us['id'].'" title="Анкета '.$us['nick'].'"><span style="color:'.$us['ncolor'].'">'.$us['nick'].'</span></a>';
if(isset($user) && $user['level']>2 && ($user['id']==$us['id'] || $user['level']>$us['level']))
{
   
echo'<br/><a href="?r='.$r.'&sort='.$sort.'&page='.$page.'&del='.$diary['id'].'" title="Удалить дневник"><span style="color:red">Удалить дневник</span></a><br/>';
}
echo '</td>';
echo '</tr>';
}
echo '</table>';
if ($k_page>1)str("?sort=$sort&",$k_page,$page); // Вывод страниц
if(isset($user))echo'<img src="img/add.png" alt=""/> <a href="?new">Создать дневник</a><br/>';
}
header("Content-type: text/html");
echo '<?xml version="2.0" encoding="utf-8"?>';
echo'<img src="img/back.png" alt=""/> <a href="/diary/">Дневники</a><br/>';


echo '<div class="p_m">';
echo '<img src="img/top.png" alt=""/> <a href="top.php" title="Популярные дневники">Топ дневников</a><br/>';
echo '<img src="img/search.png" alt=""/> <a href="search.php" title="Искать дневники">Поиск дневников</a>/<a href="tags.php" title="Поиск дневников по меткам">Метки</a></div><table class="post">';
$q2=mysql_query("SELECT * FROM `diary` ORDER BY `name` ASC");
while ($cat = mysql_fetch_assoc($q2))
{
$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `diary`"),0);
echo '<tr><td class="icon14"><img src="img/cat.png" alt=""/></td><td class="p_t"><a href="?r='.$cat['id'].'">'.$cat['name'].'</a> ('.$count.')';
if(isset($user) && $user['level']>2)
{
echo' [<a href="?edit='.$cat['id'].'">*</a>][<a href="?del='.$cat['id'].'">x</a>]';
}
echo '</td></tr>';
if ($cat['desc']!=NULL)
{
echo '<tr>
<td class="p_m" colspan="2">'
.output_text($cat['desc'])
.'</td></tr>';
}
}
echo '</table>';

include_once '../sys/inc/tfoot.php';