<?php
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');
$title= new Body;
$title->SetTitle(''.homeurl.' - Личный блокнот');

///Подключаем шапку
$title->GetHeader();
if ($b == false) {
header('location: /');
die();
}
echo '<div class="container-fluid page_header"><div class="container"><div class="row"><div class="col-xs-12 col-sm-8 col-md-9 col-lg-9"><h1>Личный блокнот</h1><ol class="breadcrumb" xmlns:v="http://'.homeurl.'/#"><li typeof="v:Breadcrumb"><a href="/" rel="v:url" property="v:title">'.homeurl.'</a></li><li typeof="v:Breadcrumb"><a href="/office" rel="v:url" property="v:title">Кабинет</a></li><li typeof="v:Breadcrumb"><a href="/notebook" rel="v:url" property="v:title">Блокнот</a></li><li class="active" typeof="v:Breadcrumb"><span property="v:title">Добавить запись</span></li></ol></div></div></div></div>';
echo'<div class="container page_body"><div class="row"><div class="col-xs-12 page_content col-lg-9"><div class="navbar page_content_navbar collapse hidden-lg"><div class="container-fluid"><div class="navbar-collapse"></div></div></div><div class="text_box hidden-lg pr_block_small hidden"></div><div class="container-fluid cols_table">';
echo '<div class="catSides"><a href="/"><i class="md-format-list-bulleted"></i> Главная </a> -<a href="/office"> Кабинет</a> - <a href="/notebook">Личный блокнот</a> - Добавить запись</div>';
// Добавление записи
if (isset($_GET['add']))
{
$name = check($_POST['name']);
$msg = check($_POST['msg']);
if (empty($name) OR empty($msg)) {
err('Заполните все поля.');
} else {
$key = pass(5);
DB::$dbs->query("INSERT INTO `notebook` (`name`, `msg`, `key`, `time`, `id_user`) VALUES (?,?,?,?,?)", [$name,$msg,$key,time(),$b['id']]);
header('Location: note.php?key=' . $key);
exit;
}
}


// Редактирование
if (isset($_GET['edit']))
{
$notebook = DB::$dbs->queryFetch("SELECT * FROM `notebook` WHERE `id` = ? LIMIT 1", array(intval($_GET['edit'])));
if ($notebook['id_user'] != $b['id'])
{
header('Location: /');
exit;
}
if (isset($_POST['edit']))
{
$name = check($_POST['name']);
$msg = check($_POST['msg']);
if (empty($name) OR empty($msg)) {
err('Заполните все поля.');
} else {
DB::$dbs->query("UPDATE `notebook` SET `name` = ?, `msg` = ? WHERE `id` = ? LIMIT 1", [$name,$msg,$notebook['id']]);
header('Location: note.php?key=' . $notebook['key']);
exit;
}
}
echo '<div class="row text_box"><form method="post" action="?edit=' . $notebook['id'] . '">Название:<br /><input name="name" class="form-control" value="' . $notebook['name'] . '" type="text" /><br />Текст:<br /><textarea name="msg" class="form-control" rows="3"/>' .$notebook['msg'] . '</textarea><br /><input value="Cохранить" class="btn btn-primary" name="edit" type="submit" /></form></div>';

///Подключаем меню
$title->GetMenu();

///Подключаем футер
$title->GetFooter();
exit;
}

echo '<div class="row text_box"><form method="post" name="message" action="?add">Название:<br /><input name="name" class="form-control" value="" type="text" placeholder="Название записи.."  /><br />Текст:<br /><textarea name="msg" class="form-control" rows="3"/></textarea><br /><input value="Записать" type="submit" class="btn btn-primary"/></form></div>';

///Подключаем меню
$title->GetMenu();

///Подключаем футер
$title->GetFooter();
?>