home » zplus/freepost.git
Author zPlus <--> 2016-10-15 08:38:49
Committer zPlus <--> 2016-10-15 08:38:49
Commit 3ff99d5 (patch)
Tree e6300a0
Parent(s)

Fix #33: Allow editing title and URL of post


commits diff: 0ec4a7b..3ff99d5
5 files changed, 113 insertions, 43 deletionsdownload


Diffstat
-rw-r--r-- database.php 9
-rw-r--r-- edit.php 95
-rw-r--r-- submit.php 11
-rw-r--r-- template/edit_comment.twig 9
-rw-r--r-- template/edit_post.twig 32

Diff options
View
Side
Whitespace
Context lines
Inter-hunk lines
+6/-3 M   database.php
index 22911c2..acba971
old size: 40K - new size: 40K
@@ -832,14 +832,17 @@ class Database
832 832 /**
833 833 * Update a post text
834 834 */
835 - function edit_post ($text, $post_hash_id, $user_id)
835 + function edit_post ($title, $link, $text, $post_hash_id, $user_id)
836 836 {
837 837 $query = $this->database->prepare (
838 838 'UPDATE `post`' .
839 - 'SET `text` = ? ' .
839 + 'SET ' .
840 + '`title` = ?, ' .
841 + '`link` = ?, ' .
842 + '`text` = ? ' .
840 843 'WHERE `hashId` = ? AND `userId` = ?');
841 844
842 - $query->execute (array ($text, $post_hash_id, $user_id));
845 + $query->execute ([$title, $link, $text, $post_hash_id, $user_id]);
843 846
844 847 $affected_rows = $query->rowCount();
845 848

+70/-25 M   edit.php
index 0b5da3f..65fbf4d
old size: 2K - new size: 3K
@@ -17,13 +17,35 @@ if (!Session::is_valid ())
17 17 exit ();
18 18 }
19 19
20 - // POST: save changes
20 +
21 + // POST: save changes =======================================================
22 +
23 +
21 24 if ($_SERVER['REQUEST_METHOD'] === 'POST')
22 25 {
23 - // Make sure we have a text
24 - if (!isset ($_POST['text']))
26 + // Edit a comment
27 + if (isset ($_POST['comment']))
25 28 {
26 - header ('Location: ./');
29 + $comment = $db->get_comment ($_POST['comment']);
30 +
31 + // Make sure user has the right to edit this comment
32 + if ($comment['userId'] != Session::get_userid ())
33 + {
34 + header ('Location: ./');
35 + exit ();
36 + }
37 +
38 + $new_comment_data =
39 + [
40 + 'text' => isset ($_POST['text']) ? trim ($_POST['text']) : ''
41 + ];
42 +
43 + $db->edit_comment (
44 + $new_comment_data['text'],
45 + $comment['hashId'],
46 + Session::get_userid ());
47 +
48 + header ('Location: ./post/' . $comment['postHashId'] . '#comment-' . $comment['hashId']);
27 49 exit ();
28 50 }
29 51
@@ -39,36 +61,42 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
39 61 exit ();
40 62 }
41 63
42 - $db->edit_post ($_POST['text'], $post['hashId'], Session::get_userid ());
64 + // New title/link/text to update the post with
65 + $new_post_data =
66 + [
67 + 'title' => isset ($_POST['title']) ? trim ($_POST['title']) : '',
68 + 'link' => isset ($_POST['link']) ? trim ($_POST['link']) : '',
69 + 'text' => isset ($_POST['text']) ? trim ($_POST['text']) : ''
70 + ];
43 71
44 - header ('Location: ./post/' . $post['hashId']);
45 - exit ();
46 - }
47 -
48 - // Edit a comment
49 - if (isset ($_POST['comment']))
50 - {
51 - $comment = $db->get_comment ($_POST['comment']);
72 + // MUST have a title
73 + if (strlen ($new_post_data['title']) == 0)
74 + $new_post_data['title'] = $post['title'];
52 75
53 - // Make sure user has the right to edit this comment
54 - if ($comment['userId'] != Session::get_userid ())
55 - {
56 - header ('Location: ./');
57 - exit ();
58 - }
76 + // Add "http://" if URL scheme is missing
77 + $link_components = parse_url ($new_post_data['link']);
78 + if (!isset ($link_components['scheme']))
79 + $new_post_data['link'] = 'http://' . $new_post_data['link'];
59 80
60 - $db->edit_comment ($_POST['text'], $comment['hashId'], Session::get_userid ());
81 + $db->edit_post (
82 + $new_post_data['title'],
83 + $new_post_data['link'],
84 + $new_post_data['text'],
85 + $post['hashId'],
86 + Session::get_userid ());
61 87
62 - header ('Location: ./post/' . $comment['postHashId'] . '#comment-' . $comment['hashId']);
88 + header ('Location: ./post/' . $post['hashId']);
63 89 exit ();
64 90 }
65 91
92 +
93 +
66 94 header ('Location: ./');
67 95 exit ();
68 96 }
69 97
70 98
71 - // GET: show reply page
99 + // GET: show reply page =====================================================
72 100
73 101
74 102 // Must have a comment id (to reply to)
@@ -94,8 +122,25 @@ if ($item['data']['userId'] != Session::get_userid ())
94 122 header ('Location: ./');
95 123 exit ();
96 124 }
97 -
125 +
98 126 // Render template
127 + switch ($item['type'])
128 + {
129 + case 'comment':
130 + $template = 'edit_comment.twig';
131 + break;
132 +
133 + case 'post':
134 + $template = 'edit_post.twig';
135 + break;
136 + }
137 +
99 138 echo $twig->render (
100 - 'edit.twig',
101 - array ('item' => $item));
101 > \ No newline at end of file
139 + $template,
140 + array ('item' => $item));
141 +
142 +
143 +
144 +
145 +
146 +
146 < \ No newline at end of file

+3/-8 M   submit.php
index 35d2a7d..58ccda9
old size: 1K - new size: 1K
@@ -35,14 +35,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST')
35 35
36 36 // Normalize Link
37 37 $link = trim ($_POST['link']);
38 -
39 - if (strlen ($link) > 0)
40 - {
41 - $link_components = parse_url ($link);
42 -
43 - if (!isset ($link_components['scheme']))
44 - $link = 'http://' . $link;
45 - }
38 + $link_components = parse_url ($link);
39 + if (!isset ($link_components['scheme']))
40 + $link = 'http://' . $link;
46 41
47 42 // Add the new post
48 43 $post_hash_id = $db->new_post ($title, $link, $_POST['text'], Session::get_userid());

+2/-7 R   template/edit.twig -> template/edit_comment.twig
index cbfe8c2..ba61dfb
old size: 875B - new size: 745B
@@ -2,12 +2,7 @@
2 2
3 3 <div class="edit">
4 4 <h3>
5 - Edit:
6 - {% if item.type == 'post' %}
7 - {{ item.data.title }}
8 - {% else %}
9 - {{ item.data.postTitle }}
10 - {% endif %}
5 + Edit: {{ item.data.postTitle }}
11 6 </h3>
12 7
13 8 <div class="info">
@@ -20,7 +15,7 @@
20 15 </div>
21 16
22 17 <form action="" method="post">
23 - <input type="hidden" name="{{ item.type }}" value="{{ item.data.hashId }}" />
18 + <input type="hidden" name="comment" value="{{ item.data.hashId }}" />
24 19
25 20 <div style="margin: 2em 0;">
26 21 <textarea name="text" rows=10 class="form-control">{{ item.data.text }}</textarea>

+32/-0 A   template/edit_post.twig
index 0000000..279d5da
old size: 0B - new size: 966B
new file mode: -rw-r--r--
@@ -0,0 +1,32 @@
1 + {% include 'header.twig' %}
2 +
3 + <div class="edit">
4 + <form action="" method="post" class="submit">
5 + <input type="hidden" name="post" value="{{ item.data.hashId }}" />
6 +
7 + <h3>Title <em>(required)</em></h3>
8 + <div>
9 + <input type="text" name="title" class="form-control" value="{{ item.data.title }}" />
10 + </div>
11 + <div class="info">
12 + by {{ item.data.username }}
13 + <em>{{ item.data.created|ago }}</em>
14 + </div>
15 +
16 + <h3>Link</h3>
17 + <div>
18 + <input type="text" name="link" class="form-control" value="{{ item.data.link }}" />
19 + </div>
20 +
21 + <h3>Text</h3>
22 + <div>
23 + <textarea name="text" rows=10 class="form-control">{{ item.data.text }}</textarea>
24 + </div>
25 +
26 + <div style="margin: 1em 0 0 0;">
27 + <input type="submit" class="button button_info" value="Save changes" />
28 + </div>
29 + </form>
30 + </div>
31 +
32 + {% include 'footer.twig' %}
32 < \ No newline at end of file