Files
sld-blocknotes/pages/view_blocknote.php
T
2026-08-19 17:26:06 +01:00

376 lines
18 KiB
PHP

<?php
require_login();
$user = current_user();
$blocknote_id = $_GET['id'] ?? '';
$bn = get_blocknote_by_id($blocknote_id);
// Check access permission
if (!$bn || !can_access_blocknote($bn, $user['username'], is_admin())) {
header("Location: index.php?page=home");
exit;
}
$msg_success = "";
$msg_error = "";
// Handle Form Submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
if ($action === 'add_note') {
$res = add_note_to_blocknote($blocknote_id, $_POST['title'] ?? '', $_POST['description'] ?? '', $user['username'], is_admin());
if ($res['success']) $msg_success = $res['message']; else $msg_error = $res['message'];
} elseif ($action === 'edit_note') {
$res = edit_note_in_blocknote($blocknote_id, $_POST['note_id'] ?? '', $_POST['title'] ?? '', $_POST['description'] ?? '', $user['username'], is_admin());
if ($res['success']) $msg_success = $res['message']; else $msg_error = $res['message'];
} elseif ($action === 'delete_note') {
$res = delete_note_from_blocknote($blocknote_id, $_POST['note_id'] ?? '', $user['username'], is_admin());
if ($res['success']) $msg_success = $res['message']; else $msg_error = $res['message'];
} elseif ($action === 'add_subnote') {
$res = add_subnote_to_note($blocknote_id, $_POST['note_id'] ?? '', $_POST['sub_title'] ?? '', $_POST['type'] ?? 'text', $_POST['content'] ?? '', $user['username'], is_admin());
if ($res['success']) $msg_success = $res['message']; else $msg_error = $res['message'];
} elseif ($action === 'edit_subnote') {
$res = edit_subnote_in_note($blocknote_id, $_POST['note_id'] ?? '', $_POST['sub_id'] ?? '', $_POST['sub_title'] ?? '', $_POST['type'] ?? 'text', $_POST['content'] ?? '', $user['username'], is_admin());
if ($res['success']) $msg_success = $res['message']; else $msg_error = $res['message'];
} elseif ($action === 'delete_subnote') {
$res = delete_subnote_from_note($blocknote_id, $_POST['note_id'] ?? '', $_POST['sub_id'] ?? '', $user['username'], is_admin());
if ($res['success']) $msg_success = $res['message']; else $msg_error = $res['message'];
}
// Refresh blocknote data
$bn = get_blocknote_by_id($blocknote_id);
}
$notes = $bn['notes'] ?? [];
?>
<main class="main-content">
<div style="margin-bottom: 1.5rem;">
<a href="index.php?page=home&view=all" class="btn btn-sm btn-dark"><?= __('btn_back_list') ?></a>
</div>
<div class="page-header" style="border-bottom: 1px solid rgba(255,255,255,0.08); padding-bottom: 1.5rem; margin-bottom: 2rem;">
<div class="page-title-group">
<h1>📓 <?= htmlspecialchars($bn['title']) ?></h1>
<p>
<?= __('label_owner_prefix') ?><strong style="color: #cbd5e1;"><?= htmlspecialchars($bn['username'] ?? '') ?></strong> &bull;
<?= __('label_updated_prefix') ?><?= date('d/m/Y H:i', strtotime($bn['updated_at'] ?? 'now')) ?>
</p>
</div>
<div style="display: flex; gap: 0.5rem; flex-wrap: wrap;">
<a href="index.php?page=export_blocknote&id=<?= urlencode($blocknote_id) ?>" class="btn btn-dark btn-sm" title="<?= __('btn_export_json') ?>">
<?= __('btn_export_json') ?>
</a>
<button class="btn btn-dark btn-sm" onclick="expandAllNotes()">
<?= __('btn_expand_all') ?>
</button>
<button class="btn btn-dark btn-sm" onclick="collapseAllNotes()">
<?= __('btn_collapse_all') ?>
</button>
<button class="btn btn-primary" data-modal-target="modal-add-note">
<?= __('btn_new_note') ?>
</button>
</div>
</div>
<?php if (!empty($msg_success)): ?>
<div class="alert alert-success">
✅ <?= htmlspecialchars($msg_success) ?>
</div>
<?php endif; ?>
<?php if (!empty($msg_error)): ?>
<div class="alert alert-danger">
⚠️ <?= htmlspecialchars($msg_error) ?>
</div>
<?php endif; ?>
<?php if (!empty($notes)): ?>
<!-- SEARCH TOOLBAR FOR CURRENT BLOCKNOTE -->
<div class="search-toolbar" style="margin-bottom: 1.5rem; display: flex; gap: 0.75rem; align-items: center;">
<div style="position: relative; flex: 1;">
<input type="text" id="note-search-input" class="form-control" placeholder="<?= __('search_placeholder') ?>" oninput="filterCurrentBlocknoteNotes(this.value)" style="padding-left: 2.5rem;">
<span style="position: absolute; left: 0.9rem; top: 50%; transform: translateY(-50%); font-size: 1rem; opacity: 0.5;">🔍</span>
</div>
<button class="btn btn-dark btn-sm" onclick="clearNoteSearch()" id="clear-search-btn" style="display: none; align-items: center; gap: 0.3rem;">
<?= __('btn_clear_search') ?>
</button>
</div>
<!-- SEARCH NO RESULTS STATE -->
<div id="no-search-results" class="empty-state" style="display: none; padding: 2.5rem 1rem;">
<div class="empty-state-icon">🔍</div>
<h3><?= __('search_no_results') ?></h3>
<p><?= __('search_no_results_desc') ?></p>
<button class="btn btn-dark btn-sm" onclick="clearNoteSearch()">
🔄 Reset
</button>
</div>
<?php endif; ?>
<?php if (empty($notes)): ?>
<div class="empty-state">
<div class="empty-state-icon">📝</div>
<h3><?= __('empty_blocknotes_title') ?></h3>
<p><?= __('empty_blocknotes_desc_user') ?></p>
<button class="btn btn-primary" data-modal-target="modal-add-note">
<?= __('btn_new_note') ?>
</button>
</div>
<?php else: ?>
<div class="notes-container">
<?php foreach ($notes as $note): ?>
<?php $subnotes = $note['subnotes'] ?? []; ?>
<div class="note-block collapsed" id="note-block-<?= htmlspecialchars($note['id']) ?>">
<div class="note-header" onclick="toggleNoteCollapse('<?= htmlspecialchars($note['id']) ?>')">
<div class="note-title-area">
<div class="note-title-wrapper">
<span class="collapse-icon">▶</span>
<h3>📌 <?= htmlspecialchars($note['title']) ?></h3>
<span class="subnote-count-badge">
<?= count($subnotes) ?> <?= (count($subnotes) === 1) ? __('subnote_single') : __('subnote_plural') ?>
</span>
</div>
</div>
<div class="note-actions" onclick="event.stopPropagation();">
<button class="btn btn-sm btn-secondary" onclick="openAddSubnoteModal('<?= htmlspecialchars($note['id']) ?>', '<?= htmlspecialchars(addslashes($note['title'])) ?>')">
<?= __('btn_add_subnote') ?>
</button>
<button class="btn btn-sm btn-dark" onclick="openEditNoteModal('<?= htmlspecialchars($note['id']) ?>', '<?= htmlspecialchars(addslashes($note['title'])) ?>', '<?= htmlspecialchars(addslashes($note['description'] ?? '')) ?>')">
✏️
</button>
<form action="index.php?page=view_blocknote&id=<?= urlencode($blocknote_id) ?>" method="POST" onsubmit="return confirm('<?= htmlspecialchars(addslashes(__('confirm_delete_note'))) ?>');" style="display:inline;">
<input type="hidden" name="action" value="delete_note">
<input type="hidden" name="note_id" value="<?= htmlspecialchars($note['id']) ?>">
<button type="submit" class="btn btn-sm btn-danger" title="<?= __('btn_delete') ?>">
🗑️
</button>
</form>
</div>
</div>
<div class="note-body">
<?php if (!empty($note['description'])): ?>
<div class="note-description-box">
<p><?= htmlspecialchars($note['description']) ?></p>
</div>
<?php endif; ?>
<div class="subnotes-list">
<?php if (empty($subnotes)): ?>
<div style="font-size: 0.88rem; color: #64748b; font-style: italic; text-align: center; padding: 1rem 0;">
<?= __('empty_blocknotes_desc_user') ?>
</div>
<?php else: ?>
<?php foreach ($subnotes as $sub): ?>
<div class="subnote-card">
<div class="subnote-header">
<div class="subnote-title-group">
<span class="subnote-title">📑 <?= htmlspecialchars($sub['title']) ?></span>
<span class="type-badge <?= htmlspecialchars($sub['type']) ?>">
<?= ($sub['type'] === 'command') ? __('badge_command') : __('badge_text') ?>
</span>
</div>
<div style="display: flex; gap: 0.4rem;">
<button class="btn btn-sm btn-dark" onclick="openEditSubnoteModal('<?= htmlspecialchars($note['id']) ?>', '<?= htmlspecialchars($sub['id']) ?>', '<?= htmlspecialchars(addslashes($sub['title'])) ?>', '<?= htmlspecialchars($sub['type']) ?>', '<?= htmlspecialchars(addslashes($sub['content'])) ?>')">
✏️
</button>
<form action="index.php?page=view_blocknote&id=<?= urlencode($blocknote_id) ?>" method="POST" onsubmit="return confirm('<?= htmlspecialchars(addslashes(__('confirm_delete_subnote'))) ?>');" style="display:inline;">
<input type="hidden" name="action" value="delete_subnote">
<input type="hidden" name="note_id" value="<?= htmlspecialchars($note['id']) ?>">
<input type="hidden" name="sub_id" value="<?= htmlspecialchars($sub['id']) ?>">
<button type="submit" class="btn btn-sm btn-danger" title="<?= __('btn_delete') ?>">
🗑️
</button>
</form>
</div>
</div>
<!-- Content rendering depending on type -->
<?php if ($sub['type'] === 'command'): ?>
<div class="command-block-wrapper">
<div class="command-block-header">
<span class="command-badge-title">⚡ <?= __('badge_command') ?></span>
<button class="copy-btn" onclick="copyToClipboard(this, <?= htmlspecialchars(json_encode($sub['content'])) ?>)" data-copy-text="<?= htmlspecialchars(__('btn_copy')) ?>" data-copied-text="<?= htmlspecialchars(__('btn_copied')) ?>">
<?= __('btn_copy') ?>
</button>
</div>
<pre class="command-content-code"><code><?= htmlspecialchars($sub['content']) ?></code></pre>
</div>
<?php else: ?>
<div class="subnote-content-text">
<?= nl2br(htmlspecialchars($sub['content'])) ?>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</main>
<!-- MODAL AGGIUNGI NOTA -->
<div class="modal-backdrop" id="modal-add-note">
<div class="modal-card">
<div class="modal-header">
<h3><?= __('modal_add_note') ?></h3>
<button class="modal-close-btn">&times;</button>
</div>
<form action="index.php?page=view_blocknote&id=<?= urlencode($blocknote_id) ?>" method="POST">
<input type="hidden" name="action" value="add_note">
<div class="form-group">
<label for="note_title"><?= __('label_title') ?></label>
<input type="text" name="title" id="note_title" class="form-control" required placeholder="<?= htmlspecialchars(__('placeholder_note_title')) ?>">
</div>
<div class="form-group">
<label for="note_description"><?= __('label_note_description') ?></label>
<textarea name="description" id="note_description" class="form-control" placeholder="<?= htmlspecialchars(__('placeholder_note_desc')) ?>"></textarea>
</div>
<div style="display: flex; gap: 0.75rem; justify-content: flex-end; margin-top: 1.5rem;">
<button type="button" class="btn btn-dark modal-close-trigger"><?= __('btn_cancel') ?></button>
<button type="submit" class="btn btn-primary"><?= __('btn_save') ?></button>
</div>
</form>
</div>
</div>
<!-- MODAL MODIFICA NOTA -->
<div class="modal-backdrop" id="modal-edit-note">
<div class="modal-card">
<div class="modal-header">
<h3><?= __('modal_edit_note') ?></h3>
<button class="modal-close-btn">&times;</button>
</div>
<form action="index.php?page=view_blocknote&id=<?= urlencode($blocknote_id) ?>" method="POST">
<input type="hidden" name="action" value="edit_note">
<input type="hidden" name="note_id" id="edit_note_id">
<div class="form-group">
<label for="edit_note_title"><?= __('label_title') ?></label>
<input type="text" name="title" id="edit_note_title" class="form-control" required>
</div>
<div class="form-group">
<label for="edit_note_description"><?= __('label_note_description') ?></label>
<textarea name="description" id="edit_note_description" class="form-control"></textarea>
</div>
<div style="display: flex; gap: 0.75rem; justify-content: flex-end; margin-top: 1.5rem;">
<button type="button" class="btn btn-dark modal-close-trigger"><?= __('btn_cancel') ?></button>
<button type="submit" class="btn btn-secondary"><?= __('btn_save') ?></button>
</div>
</form>
</div>
</div>
<!-- MODAL AGGIUNGI SOTTONOTA -->
<div class="modal-backdrop" id="modal-add-subnote">
<div class="modal-card">
<div class="modal-header">
<h3><?= __('modal_add_subnote') ?></h3>
<button class="modal-close-btn">&times;</button>
</div>
<form action="index.php?page=view_blocknote&id=<?= urlencode($blocknote_id) ?>" method="POST">
<input type="hidden" name="action" value="add_subnote">
<input type="hidden" name="note_id" id="add_subnote_parent_id">
<div class="form-group">
<label><?= __('note_single') ?></label>
<input type="text" id="add_subnote_parent_name" class="form-control" disabled style="opacity: 0.7;">
</div>
<div class="form-group">
<label for="sub_title"><?= __('label_subnote_title') ?></label>
<input type="text" name="sub_title" id="sub_title" class="form-control" required placeholder="<?= htmlspecialchars(__('placeholder_subnote_title')) ?>">
</div>
<div class="form-group">
<label for="sub_type"><?= __('label_subnote_type') ?></label>
<select name="type" id="sub_type" class="form-control">
<option value="text"><?= __('type_option_text') ?></option>
<option value="command"><?= __('type_option_command') ?></option>
</select>
</div>
<div class="form-group">
<label for="sub_content"><?= __('label_subnote_content') ?></label>
<textarea name="content" id="sub_content" class="form-control" style="min-height: 120px; font-family: monospace;" required placeholder="<?= htmlspecialchars(__('placeholder_subnote_content')) ?>"></textarea>
</div>
<div style="display: flex; gap: 0.75rem; justify-content: flex-end; margin-top: 1.5rem;">
<button type="button" class="btn btn-dark modal-close-trigger"><?= __('btn_cancel') ?></button>
<button type="submit" class="btn btn-primary"><?= __('btn_save') ?></button>
</div>
</form>
</div>
</div>
<!-- MODAL MODIFICA SOTTONOTA -->
<div class="modal-backdrop" id="modal-edit-subnote">
<div class="modal-card">
<div class="modal-header">
<h3><?= __('modal_edit_subnote') ?></h3>
<button class="modal-close-btn">&times;</button>
</div>
<form action="index.php?page=view_blocknote&id=<?= urlencode($blocknote_id) ?>" method="POST">
<input type="hidden" name="action" value="edit_subnote">
<input type="hidden" name="note_id" id="edit_subnote_parent_id">
<input type="hidden" name="sub_id" id="edit_subnote_id">
<div class="form-group">
<label for="edit_sub_title"><?= __('label_subnote_title') ?></label>
<input type="text" name="sub_title" id="edit_sub_title" class="form-control" required>
</div>
<div class="form-group">
<label for="edit_sub_type"><?= __('label_subnote_type') ?></label>
<select name="type" id="edit_sub_type" class="form-control">
<option value="text"><?= __('type_option_text') ?></option>
<option value="command"><?= __('type_option_command') ?></option>
</select>
</div>
<div class="form-group">
<label for="edit_sub_content"><?= __('label_subnote_content') ?></label>
<textarea name="content" id="edit_sub_content" class="form-control" style="min-height: 120px; font-family: monospace;" required></textarea>
</div>
<div style="display: flex; gap: 0.75rem; justify-content: flex-end; margin-top: 1.5rem;">
<button type="button" class="btn btn-dark modal-close-trigger"><?= __('btn_cancel') ?></button>
<button type="submit" class="btn btn-secondary"><?= __('btn_save') ?></button>
</div>
</form>
</div>
</div>
<script>
function openEditNoteModal(noteId, currentTitle, currentDesc) {
document.getElementById('edit_note_id').value = noteId;
document.getElementById('edit_note_title').value = currentTitle;
document.getElementById('edit_note_description').value = currentDesc;
document.getElementById('modal-edit-note').classList.add('active');
}
function openAddSubnoteModal(noteId, noteTitle) {
document.getElementById('add_subnote_parent_id').value = noteId;
document.getElementById('add_subnote_parent_name').value = noteTitle;
document.getElementById('modal-add-subnote').classList.add('active');
}
function openEditSubnoteModal(noteId, subId, title, type, content) {
document.getElementById('edit_subnote_parent_id').value = noteId;
document.getElementById('edit_subnote_id').value = subId;
document.getElementById('edit_sub_title').value = title;
document.getElementById('edit_sub_type').value = type;
document.getElementById('edit_sub_content').value = content;
document.getElementById('modal-edit-subnote').classList.add('active');
}
</script>