refactor actions

fix index upon delete and rename
This commit is contained in:
quenousimporte 2024-03-07 12:48:31 +01:00
parent c13d688e90
commit bf1c9cc9d9
1 changed files with 35 additions and 67 deletions

View File

@ -20,66 +20,33 @@
session_start(); session_start();
function removefromindex($title)
{
global $dir;
$key = array_search($dir . '/' . $title, $_SESSION['index']);
if ($key !== FALSE)
{
array_splice($_SESSION['index'], $key, 1);
}
}
function savenote($title, $content) function savenote($title, $content)
{ {
global $dir; global $dir;
file_put_contents($dir . '/' . $title, $content); file_put_contents($dir . '/' . $title, $content);
$key = array_search($dir . '/' . $title, $_SESSION['index']); removefromindex($title);
if ($key)
{
// handle the rename case
array_splice($_SESSION['index'], $key, 1);
array_unshift($_SESSION['index'], $dir . '/' . $title); array_unshift($_SESSION['index'], $dir . '/' . $title);
} }
}
$action = ''; $nextpage = 'home';
if (isset($_GET['open'])) if (isset($_GET['clip']) && $_GET['param'])
{
$action = 'open';
}
else if (isset($_GET['search']))
{
$action = 'search';
}
else if (isset($_GET['tags']))
{
$action = 'tags';
}
else if (isset($_GET['clip']))
{
$action = 'clip';
}
else if (isset($_POST['save']))
{
$action = 'save';
}
else if (isset($_POST['delete']))
{
$action = 'delete';
}
else if (isset($_POST['home']))
{
$action = 'save';
}
else if(isset($_GET['home']))
{
$action = '';
}
else if (isset($_POST['preview']))
{
$action = 'preview';
}
$content = '';
if ($action == 'clip' && $_GET['param'])
{ {
$content = $_GET['param'] . "\r\n" . file_get_contents($dir . '/todo'); $content = $_GET['param'] . "\r\n" . file_get_contents($dir . '/todo');
savenote('todo', $content); savenote('todo', $content);
$_GET['param'] = ''; $_GET['param'] = '';
} }
if ($action == 'save') else if (isset($_POST['save']) || isset($_POST['home']))
{ {
$title = $_POST['title']; $title = $_POST['title'];
$content = $_POST['content']; $content = $_POST['content'];
@ -89,26 +56,21 @@
if ($title != $previoustitle) if ($title != $previoustitle)
{ {
rename($dir . '/' . $previoustitle, $dir . '/' . $previoustitle . '.del'); rename($dir . '/' . $previoustitle, $dir . '/' . $previoustitle . '.del');
removefromindex($previoustitle);
} }
if (isset($_POST['home'])) if (!isset($_POST['home']))
{ {
$action = ''; $nextpage = 'note';
}
else
{
$action = 'open';
$_GET['param'] = $title;
} }
} }
else if ($action == 'delete') else if (isset($_POST['delete']))
{ {
$title = $_POST['title']; $title = $_POST['title'];
rename($dir . '/' . $title, $dir . '/' . $title . '.del'); rename($dir . '/' . $title, $dir . '/' . $title . '.del');
// reindex! removefromindex($title);
} }
else if (isset($_POST['preview']))
if ($action == 'preview')
{ {
require 'libs/Parsedown.php'; require 'libs/Parsedown.php';
$title = $_POST['title']; $title = $_POST['title'];
@ -121,10 +83,16 @@
$Parsedown = new Parsedown(); $Parsedown = new Parsedown();
$Parsedown->setBreaksEnabled(true); $Parsedown->setBreaksEnabled(true);
echo $Parsedown->text('# ' . $title . "\r\n" . substr($content, $pos + 3)); echo $Parsedown->text('# ' . $title . "\r\n" . substr($content, $pos + 3));
$nextpage = '';
} }
else if ($action == 'open') else if (isset($_GET['open']))
{ {
$nextpage = 'note';
$title = $_GET['param']; $title = $_GET['param'];
}
if ($nextpage == 'note')
{
$now = date("Y-m-d H.i.s", time()); $now = date("Y-m-d H.i.s", time());
if (!$title) if (!$title)
{ {
@ -175,7 +143,7 @@
echo '<span class="grey">' . $title . ' - ' . $now . ' - ' . $length . 'c - ' . $words . 'w - ' . $lines . 'l </span>'; echo '<span class="grey">' . $title . ' - ' . $now . ' - ' . $length . 'c - ' . $words . 'w - ' . $lines . 'l </span>';
} }
else else if ($nextpage == 'home')
{ {
if (isset($_GET['param'])) if (isset($_GET['param']))
{ {
@ -201,18 +169,18 @@
$name = basename($path); $name = basename($path);
if (!str_ends_with($name, '.del')) if (!str_ends_with($name, '.del'))
{ {
if ($action == 'search' || $action == 'tags') if (isset($_GET['search']) || isset($_GET['tags']))
{ {
$content = file_get_contents($path); $content = file_get_contents($path);
if (($action == 'search' && !str_contains(strtolower($content), strtolower($param)) && !str_contains(strtolower($name), strtolower($param))) if ((isset($_GET['search']) && !str_contains(strtolower($content), strtolower($param)) && !str_contains(strtolower($name), strtolower($param)))
|| ($action == 'tags' && !preg_match('/tags:.*' . $param . '/i', $content))) || (isset($_GET['tags']) && !preg_match('/tags:.*' . $param . '/i', $content)))
{ {
continue; continue;
} }
} }
echo '<div><a href=index.php?open=true&param=' . urlencode($name) . '>' . $name .'</a>'; echo '<div><a href=index.php?open=true&param=' . urlencode($name) . '>' . $name .'</a>';
if ($action == 'tags') if (isset($_GET['tags']))
{ {
$tags = array(); $tags = array();
$hastags = preg_match_all('/tags:(.*)/i', $content, $tags, PREG_SET_ORDER); $hastags = preg_match_all('/tags:(.*)/i', $content, $tags, PREG_SET_ORDER);
@ -228,7 +196,7 @@
echo '</span>'; echo '</span>';
} }
} }
else if ($action == 'search' && str_contains(strtolower($content), strtolower($param))) else if (isset($_GET['search']) && str_contains(strtolower($content), strtolower($param)))
{ {
$pos = strpos(strtolower($content), strtolower($param)); $pos = strpos(strtolower($content), strtolower($param));
echo '<span class="grey"> ' . $param . substr($content, $pos + strlen($param), 42) . '</span>'; echo '<span class="grey"> ' . $param . substr($content, $pos + strlen($param), 42) . '</span>';