diff --git a/database.php b/database.php index 22911c2..acba971 100644 --- a/database.php +++ b/database.php @@ -832,14 +832,17 @@ class Database /** * Update a post text */ - function edit_post ($text, $post_hash_id, $user_id) + function edit_post ($title, $link, $text, $post_hash_id, $user_id) { $query = $this->database->prepare ( 'UPDATE `post`' . - 'SET `text` = ? ' . + 'SET ' . + '`title` = ?, ' . + '`link` = ?, ' . + '`text` = ? ' . 'WHERE `hashId` = ? AND `userId` = ?'); - $query->execute (array ($text, $post_hash_id, $user_id)); + $query->execute ([$title, $link, $text, $post_hash_id, $user_id]); $affected_rows = $query->rowCount(); diff --git a/edit.php b/edit.php index 0b5da3f..65fbf4d 100644 --- a/edit.php +++ b/edit.php @@ -17,13 +17,35 @@ if (!Session::is_valid ()) exit (); } -// POST: save changes + +// POST: save changes ======================================================= + + if ($_SERVER['REQUEST_METHOD'] === 'POST') { - // Make sure we have a text - if (!isset ($_POST['text'])) + // Edit a comment + if (isset ($_POST['comment'])) { - header ('Location: ./'); + $comment = $db->get_comment ($_POST['comment']); + + // Make sure user has the right to edit this comment + if ($comment['userId'] != Session::get_userid ()) + { + header ('Location: ./'); + exit (); + } + + $new_comment_data = + [ + 'text' => isset ($_POST['text']) ? trim ($_POST['text']) : '' + ]; + + $db->edit_comment ( + $new_comment_data['text'], + $comment['hashId'], + Session::get_userid ()); + + header ('Location: ./post/' . $comment['postHashId'] . '#comment-' . $comment['hashId']); exit (); } @@ -39,36 +61,42 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') exit (); } - $db->edit_post ($_POST['text'], $post['hashId'], Session::get_userid ()); + // New title/link/text to update the post with + $new_post_data = + [ + 'title' => isset ($_POST['title']) ? trim ($_POST['title']) : '', + 'link' => isset ($_POST['link']) ? trim ($_POST['link']) : '', + 'text' => isset ($_POST['text']) ? trim ($_POST['text']) : '' + ]; - header ('Location: ./post/' . $post['hashId']); - exit (); - } - - // Edit a comment - if (isset ($_POST['comment'])) - { - $comment = $db->get_comment ($_POST['comment']); + // MUST have a title + if (strlen ($new_post_data['title']) == 0) + $new_post_data['title'] = $post['title']; - // Make sure user has the right to edit this comment - if ($comment['userId'] != Session::get_userid ()) - { - header ('Location: ./'); - exit (); - } + // Add "http://" if URL scheme is missing + $link_components = parse_url ($new_post_data['link']); + if (!isset ($link_components['scheme'])) + $new_post_data['link'] = 'http://' . $new_post_data['link']; - $db->edit_comment ($_POST['text'], $comment['hashId'], Session::get_userid ()); + $db->edit_post ( + $new_post_data['title'], + $new_post_data['link'], + $new_post_data['text'], + $post['hashId'], + Session::get_userid ()); - header ('Location: ./post/' . $comment['postHashId'] . '#comment-' . $comment['hashId']); + header ('Location: ./post/' . $post['hashId']); exit (); } + + header ('Location: ./'); exit (); } -// GET: show reply page +// GET: show reply page ===================================================== // Must have a comment id (to reply to) @@ -94,8 +122,25 @@ if ($item['data']['userId'] != Session::get_userid ()) header ('Location: ./'); exit (); } - + // Render template +switch ($item['type']) +{ + case 'comment': + $template = 'edit_comment.twig'; + break; + + case 'post': + $template = 'edit_post.twig'; + break; +} + echo $twig->render ( - 'edit.twig', - array ('item' => $item)); \ No newline at end of file + $template, + array ('item' => $item)); + + + + + + \ No newline at end of file diff --git a/submit.php b/submit.php index 35d2a7d..58ccda9 100644 --- a/submit.php +++ b/submit.php @@ -35,14 +35,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') // Normalize Link $link = trim ($_POST['link']); - - if (strlen ($link) > 0) - { - $link_components = parse_url ($link); - - if (!isset ($link_components['scheme'])) - $link = 'http://' . $link; - } + $link_components = parse_url ($link); + if (!isset ($link_components['scheme'])) + $link = 'http://' . $link; // Add the new post $post_hash_id = $db->new_post ($title, $link, $_POST['text'], Session::get_userid()); diff --git a/template/edit.twig b/template/edit_comment.twig similarity index 83% rename from template/edit.twig rename to template/edit_comment.twig index cbfe8c2..ba61dfb 100644 --- a/template/edit.twig +++ b/template/edit_comment.twig @@ -2,12 +2,7 @@