<?php


define('_IN_JOHNCMS', 1);
$headmod = 'notebook';
$rootpath = '../';
$textl = 'Bloknot';
require_once ("../incfiles/core.php");
require_once ("../incfiles/head.php");



// Закрываем доступ для гостей
if (!$user_id) {
    echo functions::display_error($lng['access_guest_forbidden']);
    require_once('../incfiles/end.php');
    exit;
}


switch ($act) {


    case "add":

        if (isset($_POST['submit'])) {
            $error = array ();
            $pr = isset($_POST['pr']) ? intval($_POST['pr']) : '';
            $name = isset($_POST['name']) ? mb_substr(functions::checkin($_POST['name']), 0, 100) : '';
            $text = isset($_POST['text']) ? functions::checkin($_POST['text']) : '';
            if (($_POST['submit']) && empty($_POST['name']))
                $error[] = 'Siz yozuv nomini kiritmadingiz';
            if (($_POST['submit']) && empty($_POST['text']))
                $error[] = 'Siz yozuv matnini kiritmadingiz';
            $count1 = mysql_result(mysql_query("SELECT COUNT(*) FROM `notebook` WHERE `user_id`= '".$user_id."' AND `name` = '".$name."'"), 0);
            $count2 = mysql_result(mysql_query("SELECT COUNT(*) FROM `notebook` WHERE `user_id`= '".$user_id."' AND `text` = '".$text."'"), 0);
            if (($_POST['submit']) && $count1 > 0)
                $error[] = 'Bunday nomli xabar bloknotingizda mavjud';
            if (($_POST['submit']) && $count2 > 0)
                $error[] = 'Bunday xabar bor';
            if (!$error) {
                $sql = "INSERT INTO `notebook` SET
	        `time` = '" . time() . "',
	        `user_id` = '" . $user_id . "',
	        `priority` = '" . $pr . "',
	        `name` = '" . mysql_real_escape_string($name) . "',
			`text` = '" . mysql_real_escape_string($text) . "'";
                if (mysql_query($sql))
                    echo '<div class="gmenu"><p>Yozuvingiz muvoffaqiyatli saqlandi<br /><a href="notebook.php">Davom etish</a></p></div>';
                else
                    $error[] = 'Bazaga yozishda xatolik';
            } else {
                echo functions::display_error($error, '<a href="notebook.php?act=add">Qayta urinish</a>');
            }
        } else {
            echo '<div class="phdr"><b>Bloknot:</b> yozuv qo\'shish</div>';
            echo '<form action="notebook.php?act=add" method="post" name="form">';
            echo '<div class="list1"><p><h3>Darajasi</h3><select name="pr">';
            echo '<option value="1">Yuqori</option>';
            echo '<option value="2" selected="selected">O\'rta</option>';
            echo '<option value="3">Past</option>';
            echo '</select></p></div>';
            echo '<div class="list2"><p><h3>Nomi</h3><input type="text" value="" name="name" maxlength="100"/><br /><small>Max. 100 belgi</small>';
			echo '</p><p><h3>Matni</h3>' . bbcode::auto_bb('form', 'text') .'<textarea rows="' . $set_user['field_h'] . '" name="text"></textarea></p>';
			echo '<p><input type="submit" title="Yuborish uchun bosing" name="submit" value="Saqlash" /></p></div></form>';
			echo '<div class="menu"><a href="notebook.php">Orqaga</a></div>';
        }
        break;

    case "edit":
        if (!$id) {
            header('Location: notebook.php');
            exit;
        }
        $req = mysql_query("SELECT `name` FROM `notebook` WHERE `id` = '" . $id . "' LIMIT 1");

        if (mysql_num_rows($req) != 1) {
            echo functions::display_error('Yozuv tanlanmadi');
            require_once ('../incfiles/end.php');
            exit;
        }
        $req = mysql_query("SELECT * FROM `notebook` WHERE `id`='" . $id . "';");
        $ms = mysql_fetch_assoc($req);
        if ($ms['user_id'] != $user_id) {
            echo functions::display_error('Siz boshqa foydalanuvchi yozuvini o\'chira olmaysiz');
            require_once ('../incfiles/end.php');
            exit;
        }
        if (isset($_POST['submit'])) {
            $error = array ();
            $pr = isset($_POST['pr']) ? intval($_POST['pr']) : '';
            $name = isset($_POST['name']) ? functions::checkin(mb_substr(trim($_POST['name']), 0, 50)) : '';
			$text = isset($_POST['text']) ? functions::checkin(mb_substr(trim($_POST['text']), 0, 10000)) : '';
            if (($_POST['submit']) && empty($_POST['name']))
                $error[] = 'Вы не ввели название';
            if (($_POST['submit']) && empty($_POST['text']))
                $error[] = 'Вы не ввели текст';
            $count1 = mysql_result(mysql_query("SELECT COUNT(*) FROM `notebook` WHERE `user_id`= '".$user_id."' AND `name` = '".$name."' AND `id` != '".$id."'"), 0);
            $count2 = mysql_result(mysql_query("SELECT COUNT(*) FROM `notebook` WHERE `user_id`= '".$user_id."' AND `text` = '".$text."' AND `id` != '".$id."'"), 0);
            if (($_POST['submit']) && $count1 > 0)
                $error[] = 'Bunday nomli yozuv mavjud';
            if (($_POST['submit']) && $count2 > 0)
                $error[] = 'Bunday yozuv mavjud';
            if (!$error) {
            mysql_query("UPDATE `notebook` SET
	        `priority` = '" . $pr . "',
			`name` = '" . mysql_real_escape_string($name) . "',
			`text` = '" . mysql_real_escape_string($text) . "'
		     where `id` = '" . $id . "';");
                header("location: " . $_SESSION['nbk'] . "");
            } else {
                echo functions::display_error($error, '<a href="notebook.php?act=edit&amp;id=' . $id . '">Qayta urinish</a>');
            }
        } else {
            echo "<div class='phdr'><b>Bloknot:</b> yozuvni o\'zgartirish</div>";
            echo '<form action="notebook.php?act=edit&amp;id=' . $id . '" method="post"  name="form">';
            if ($ms['priority'] == 2)
                $div = 'list1';
            elseif ($ms['priority'] == 1)
                $div = 'gmenu';
            else
                $div = 'rmenu';
            echo '<div class="' . $div . '"><p><h3>Darajasi</h3><select name="pr">';
            echo '<option value="1"' . ($ms['priority'] == 1 ? ' selected="selected">' : '>') . 'Yuqori</option>';
            echo '<option value="2"' . ($ms['priority'] == 2 ? ' selected="selected">' : '>') . 'O\'rtacha</option>';
            echo '<option value="3"' . ($ms['priority'] == 3 ? ' selected="selected">' : '>') . 'Past</option>';
            echo '</select></p></div>';
            echo '<div class="list2"><p><h3>Nomi</h3><input type="text" value="' . $ms['name'] . '" name="name" maxlength="100"/><br /><small>Max. 1000 belgi</small>';
			echo '</p><p><h3>Matni</h3>' . bbcode::auto_bb('form', 'text') .'<textarea rows="' . $set_user['field_h'] . '" name="text">'.$ms['text'].'</textarea></p>';
            echo '<p><input type="submit" name="submit" value="Saqlash"/></p></div></form>';
            echo '<div class="menu"><a href="' . $_SESSION['nbk'] . '">Orqaga</a></div>';
        }
        break;

    case "delete":
        if (!$id) {
            header('Location: notebook.php');
            exit;
        }

        $req = mysql_query("SELECT `name` FROM `notebook` WHERE `id` = '" . $id . "' LIMIT 1");
        if (mysql_num_rows($req) != 1) {
            echo functions::display_error('Yozuv tanlanmadi');
            require_once ('../incfiles/end.php');
            exit;
        }
        $req = mysql_query("SELECT * FROM `notebook` WHERE `id`='" . $id . "';");
        $ms = mysql_fetch_assoc($req);
        if ($ms['user_id'] != $user_id) {
            echo functions::display_error('Siz boshqa foydalanuvchi yozuvini o\'chira olmaysiz');
            require_once ('../incfiles/end.php');
            exit;
        }
        if (isset ($_GET['yes'])) {
            mysql_query("DELETE FROM `notebook` WHERE `id`='" . $id . "' LIMIT 1;");
            echo '<div class="gmenu"><p>Yozuv muvoffaqiyatli o\'chirildi<br/><a href="' . $_SESSION['nbk'] . '">Davom etish</a></p></div>';
        } else {
            echo '<div class="phdr"><b>Bloknot:</b> yozuvni o\'chirish</div>';
            echo '<div class="rmenu"><p>Siz rostdan ham yozuvni o\'chirmoqchimisiz?<br/><a href="notebook.php?act=delete&amp;id=' . $id . '&amp;yes">O\'chirish</a> | <a href="' . $_SESSION['nbk'] . '">Bekor qilish</a></p></div>';
            echo '<div class="phdr">&nbsp;</div>';
        }
        break;

    case 'clean' :
        echo '<div class="phdr"><a href="notebook.php"><b>Bloknot</b></a> | Ro\'yxatni tozalash</div>';
        if (isset ($_POST['submit'])) {
            $cl = isset ($_POST['cl']) ? intval($_POST['cl']) : '';
            switch ($cl) {
                case '1' :

                    mysql_query("DELETE FROM `notebook` WHERE `star`='0' AND `user_id`='".$user_id."';");
                    mysql_query("OPTIMIZE TABLE `notebook`;");
                    echo '<div class="rmenu">Zakladkadagi yozuvlardan boshqalari o\'chirildi</div>';
                    break;

                case '2' :
                    mysql_query("DELETE FROM `notebook` WHERE `user_id`='".$user_id."';");
                    mysql_query("OPTIMIZE TABLE `notebook`;");
                    echo '<div class="rmenu">Bloknot to\'liq tozalandi</div>';
                    break;

                default :
                    mysql_query("DELETE FROM `notebook` WHERE `star`='1' AND `user_id`='".$user_id."';");
                    mysql_query("OPTIMIZE TABLE `notebook`;");
                    echo '<div class="rmenu">Zakladkadagi yozuvlar o\'chirildi</div>';
            }
        }
        echo '<form id="clean" method="post" action="notebook.php?act=clean">';
        echo '<div class="menu"><p><h3>Nimani tozalaymiz?</h3>';
        echo '&nbsp;<input type="radio" name="cl" value="0" checked="checked" />&nbsp;Zakladkadagilar<br />';
        echo '&nbsp;<input type="radio" name="cl" value="1"' . ($cl == 1 ? ' checked="checked"' : '') . '/>&nbsp;Zakladkaga kiritilmaganlar<br />';
        echo '&nbsp;<input type="radio" name="cl" value="2"' . ($cl == 2 ? ' checked="checked"' : '') . '/>&nbsp;Barchasi</p>';
        echo '<p><input type="submit" name="submit" value="Tozalash" /></p>';
        echo '</div></form>';
        $count = mysql_result(mysql_query("SELECT COUNT(*) FROM `notebook`  WHERE `user_id` = '".$user_id."'"), 0);
        echo '<div class="phdr">Barcha yozuvlar: ' . $count . '</div>';
        echo '<p><a href="notebook.php">Bloknotga</a></p>';
        break;

    case "record":

        if (!$id) {
            header('Location: notebook.php');
            exit;
        }
        $req = mysql_query("SELECT `name` FROM `notebook` WHERE `id` = '" . $id . "' LIMIT 1");
        if (mysql_num_rows($req) != 1) {
            echo functions::display_error('Yozuv tanlanmadi');
            require_once ('../incfiles/end.php');
            exit;
        }
        $req = mysql_query("SELECT * FROM `notebook` WHERE `id`='" . $id . "';");
        $ms = mysql_fetch_assoc($req);
        if ($ms['user_id'] != $user_id) {
            echo functions::display_error('Siz boshqa foydalanuvchi yozuvini o\'qiy olmaysiz');
            require_once ('../incfiles/end.php');
            exit;
        }
        echo '<div class="phdr"><b>Yozuv:</b> ' . $ms['name'] . '</div>';
        $text = $ms['text'];

           
            
            
            
       echo'<div class="menu">'.functions::smileys(functions::checkout($text, 1, 1), 1).'</div>';
        echo '<div class="phdr"><a href="' . $_SESSION['nbk'] . '">Orqaga</a></div>';
        break;

    case "star":
        if (isset($_GET['yes'])) {
            $dc = $_SESSION['dc'];
            foreach ($dc as $starid) {
                mysql_query("UPDATE `notebook` SET  `star` = '1' WHERE `id`='" . intval($starid) . "';");
            }
            echo "<div class='menu'><p>Belgilangan yozuvlar zakladkaga qo\'shildi<br/><a href='notebook.php'>Orqaga</a></p></div>";
        } else {
            if (empty($_POST['starch'])) {
                echo '<div class="rmenu"><p>Siz hechnarsani belgilamadingiz<br/><a href="notebook.php">Orqaga</a></p></div>';
                require_once ('../incfiles/end.php');
                exit;
            }
            foreach ($_POST['starch'] as $v) {
                $dc[] = intval($v);
            }
            $_SESSION['dc'] = $dc;
            echo '<div class="menu"><p>Siz rostdan ham belgilangan xabarlarni zakladkaga qo\'shmoqchimisiz<br/><a href="notebook.php?act=star&amp;yes">Ha</a> | <a href="notebook.php">Bekor qilish</a></p></div>';
        }
        break;

    case "reset":
        if (isset($_GET['yes'])) {
            $dc = $_SESSION['dc'];
            foreach ($dc as $resetid) {
                mysql_query("UPDATE `notebook` SET  `star` = '0' WHERE `id`='" . intval($resetid) . "';");
            }
            echo "<div class='menu'><p>Belgilangan yozuvlar zakladkadan olindi<br/><a href='notebook.php?act=starred'>Orqaga</a></p></div>";
        } else {
            if (empty($_POST['resetch'])) {
                echo '<div class="rmenu"><p>Siz hechnarsani belgilamadingiz<br/><a href="notebook.php?act=starred">Orqaga</a></p></div>';
                require_once("../incfiles/end.php");
                exit;
            }
            foreach ($_POST['resetch'] as $v) {
                $dc[] = intval($v);
            }
            $_SESSION['dc'] = $dc;
            echo '<div class="menu"><p>Siz rostdan ham belgilangan xabarlarni zakladkadan olmoqchimisiz?<br/><a href="notebook.php?act=reset&amp;yes">Olish</a> | <a href="notebook.php?act=starred">Bekor qilish</a></p></div>';
        }
        break;

    case "starred":
 
        echo '<div class="phdr"><a href="notebook.php"><b>Bloknot</b></a> | Zakladka</div>';
        $sort = isset ($_GET['sort']) ? trim($_GET['sort']) : '';
        echo '<div class="gmenu"><p><span class="gray">Taxlash:</span> ';
        switch ($sort) {
            case 'priority' :
                $sort = 'priority';
                echo '<a href="notebook.php?act=starred&amp;sort=date">Sana</a> | Daraja | <a href="notebook.php?act=starred&amp;sort=name">Nomi</a></p></div>';
                $order = '`priority` ASC';
                break;
            case 'name' :
                $sort = 'name';
                echo '<a href="notebook.php?act=starred&amp;sort=date">Sana</a> | <a href="notebook.php?act=starred&amp;sort=priority">Daraja</a> | Nomi</p></div>';
                $order = '`name` ASC';
                break;
            default :
                $sort = 'date';
                echo 'Sana | <a href="notebook.php?act=starred&amp;sort=priority">Daraja</a> | <a href="notebook.php?act=starred&amp;sort=name">Nomi</a></p></div>';
                $order = '`time` ASC';
        }
        $req = mysql_query("SELECT COUNT(*) FROM `notebook` WHERE `user_id` = '".$user_id."' AND `star` = '1'");
        $total = mysql_result($req, 0);
        if ($total) {
            $req = mysql_query("SELECT * FROM `notebook` WHERE `user_id` = '".$user_id."' AND `star` = '1' ORDER BY ".$order." LIMIT " . $start . "," . $kmess . ";");
            echo '<form action="notebook.php?act=reset" method="post">';
            while ($res = mysql_fetch_assoc($req)) {
                echo ceil(ceil($i / 2) - ($i / 2)) == 0 ? '<div class="list1">' : '<div class="list2">';
				$shift = ($system_set['timeshift'] + $user_set['timeshift']) * 3600;
                $vrp = $res['time'] + $shift;
                $vr1 = date("d.m.Y", $vrp);
                $vr2 = date("H:i", $vrp);
                echo '<p><h3>' . $res['name'] . '</h3>';
                echo '<div class="gray"><small>Qo\'shildi: ' . $vr1 . '&nbsp;[' . $vr2 . '] '.$shift.'</small></div></p>';
                $text = $res['text'];
                echo '<div class="menu">'.functions::smileys(functions::checkout(mb_substr($text, 0, 70), 1, 1), 1).'';
                if (mb_strlen(bbcode::notags($text)) > 70){
echo '......<br /><a href="notebook.php?act=record&amp;id=' . $res['key'] . '">davomini o\'qish &gt;&gt;</a>';
}
                echo '<div class="sub"><input type="checkbox" name="resetch[]" value="' . $res['id'] . '"/>&nbsp;<a href="notebook.php?act=edit&amp;id=' . $res['id'] . '">O\'zgartirish</a> | <a href="notebook.php?act=delete&amp;id=' . $res['id']
                 . '">O\'chirish</a>';
                echo '</div></div>';
                ++$i;
            }
            echo '<div class="rmenu"><input type="submit" class="write" value=" Zakladkadan olish "/></div>';
            echo '</form>';
        } else {
            echo '<div class="menu"><p>Zakladkada yozuvlar yo\'q</p></div>';
        }
        echo '<div class="phdr">Hammasi:&nbsp;' . $total . '</div>';
        if ($total > $kmess) {
            echo '<p>' . functions::display_pagination('notebook.php?act=starred&amp;sort=' . $sort . '&amp;', $start, $total, $kmess) . '</p>';
            echo '<p><form action="notebook.php?act=starred&amp;sort=' . $sort . '" method="post"><input type="text" name="page" size="2"/><input type="submit" class="write" value="sahifaga &gt;&gt;"/></form></p>';
        }
        echo '<p><a href="notebook.php">Bloknotga</a></p>';
        $_SESSION['nbk'] = 'notebook.php?act=starred';
        break;

    default:
        echo '<div class="phdr"><b>Bloknot</b></div>';
        $st = mysql_result(mysql_query("SELECT COUNT(*) FROM `notebook`  WHERE `star`= '1' AND `user_id` = '".$user_id."'"), 0);
        echo '<div class="rmenu"><a href="notebook.php?act=starred">Zakladkadagi yozuvlar</a> (' . $st . ')</div>';
        $sort = isset ($_GET['sort']) ? htmlspecialchars(trim($_GET['sort'])) : '';
        echo '<div class="gmenu"><p><span class="gray">Taxlash:</span> ';
        switch ($sort) {
            case 'priority' :
                $sort = 'priority';
                echo '<a href="notebook.php?sort=date">Sana</a> | Daraja | <a href="notebook.php?sort=name">Nomi</a></p></div>';
                $order = '`priority` ASC';
                break;
            case 'name' :
                $sort = 'name';
                echo '<a href="notebook.php?sort=date">Sana</a> | <a href="notebook.php?sort=priority">Daraja</a> | Nomi</p></div>';
                $order = '`name` ASC';
                break;
            default :
                $sort = 'date';
                echo 'Sana | <a href="notebook.php?sort=priority">Daraja</a> | <a href="notebook.php?sort=name">Nomi</a></p></div>';
                $order = '`time` ASC';
        }
        $req = mysql_query("SELECT COUNT(*) FROM `notebook` WHERE `user_id` = '".$user_id."'");
        $total = mysql_result($req, 0);
        if ($total) {
            $req = mysql_query("SELECT * FROM `notebook` WHERE `user_id` = '".$user_id."' ORDER BY ".$order." LIMIT " . $start . "," . $kmess . ";");
            echo '<form action="notebook.php?act=star" method="post">';
            while ($res = mysql_fetch_assoc($req)) {
                echo ceil(ceil($i / 2) - ($i / 2)) == 0 ? '<div class="list1">' : '<div class="list2">';
                $shift = (core::$system_set['timeshift'] + core::$user_set['timeshift']) * 3600;
                $vrp = $res['time'] + $shift;
                $vr1 = date("d.m.Y", $vrp);
                $vr2 = date("H:i", $vrp);
                echo '<p><h3>' . $res['name'] . '</h3>';
                echo '<div class="gray"><small>Qo\'shildi: ' . $vr1 . '&nbsp;[' . $vr2 . '] '.$shift.'</small></div></p>';
                $text = $res['text'];
               echo '<div class="menu">'.functions::smileys(functions::checkout(mb_substr($text, 0, 70), 1, 1), 1).'';
                if (mb_strlen(bbcode::notags($text)) > 70){
echo '......<br /><a href="notebook.php?act=record&amp;id=' . $res['id'] . '">davomini o\'qish &gt;&gt;</a>';
}
                echo '<div class="sub"><input type="checkbox" name="starch[]" value="' . $res['id'] . '"/>&nbsp;<a href="notebook.php?act=edit&amp;id=' . $res['id'] . '">O\'zgartirish</a> | <a href="notebook.php?act=delete&amp;id=' . $res['id']
                 . '">O\'chirish</a>';
                echo '</div></div>';
                ++$i;
            }
            echo '<div class="gmenu"><input type="submit" value=" Zakladkaga qo\'shish "/></div>';
            echo '</form>';
        } else {
            echo '<div class="menu"><p>Yozuvlar yo\'q</p></div>';
        }
        echo '<div class="phdr">Hammasi:&nbsp;' . $total . '</div>';
        if ($total > $kmess) {
            echo '<p>' .functions::display_pagination('notebook.php?sort=' . $sort . '&amp;', $start, $total, $kmess) . '</p>';
            echo '<p><form action="notebook.php?sort=' . $sort . '" method="post"><input type="text" name="page" size="2"/><input type="submit" class="write" value="Sahifaga &gt;&gt;"/></form></p>';
        }
        echo '<div class="func"><p><a href="notebook.php?act=add">Yozuv qo\'shish</a><br />';
        echo '<a href="notebook.php?act=clean">Ro\'yxatni tozalash</a></p></div>';
        echo '<div class="menu"><a href="../users/profile.php?act=office">Kabinetga</a></div>';
        $_SESSION['nbk'] = 'notebook.php';
        break;
}
require_once ("../incfiles/end.php");

?>