comment.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. function get_field_name($type) {
  3. switch ($type) {
  4. case 'dir':
  5. return 'dir';
  6. case 'image':
  7. return 'image';
  8. default:
  9. throw new Exception('Invalid $type');
  10. }
  11. }
  12. function get_comments($id, $type) {
  13. global $pdo;
  14. $sql = 'SELECT * FROM comments WHERE ' . get_field_name($type) . ' = :id';
  15. $values = array(
  16. ':id' => $id,
  17. );
  18. try {
  19. $res = $pdo->prepare($sql);
  20. $res->execute($values);
  21. } catch (PDOException $e) {
  22. echo 'Query error: ' . $e->getMessage();
  23. die();
  24. }
  25. $comments = [];
  26. while($row = $res->fetch(PDO::FETCH_ASSOC)) {
  27. $comments[] = $row;
  28. }
  29. return $comments;
  30. }
  31. function db_store_comment($nick, $email, $type, $id, $content) {
  32. global $pdo;
  33. $sql = 'INSERT INTO comments (ip, ' . get_field_name($type) . ', email, author, date, content) VALUES (:ip, :id, :email, :nick, :date, :content)';
  34. $values = array (
  35. ':ip' => $_SERVER['REMOTE_ADDR'],
  36. ':id' => $id,
  37. ':email' => $email,
  38. ':nick' => $nick,
  39. ':date' => time(),
  40. ':content' => $content,
  41. );
  42. try {
  43. $res = $pdo->prepare($sql);
  44. $res->execute($values);
  45. } catch (PDOException $e) {
  46. echo 'Query error: ' . $e->getMessage();
  47. die();
  48. }
  49. }
  50. function generate_comment_field($comments, $type, $id) {
  51. $emptytexts = array(
  52. 'dir' => 'Brak komentarzy do tego katalogu, możesz być pierwszy!',
  53. 'image' => 'Brak komentarzy do tego obrazka, możesz być pierwszy!',
  54. );
  55. $headertexts = array(
  56. 'dir' => 'Komentarze do katalogu',
  57. 'image' => 'Komentarze do obrazka',
  58. );
  59. if (count($comments) == 0) {
  60. $output = '<h2>' . $emptytexts[$type] . '</h2>';
  61. } else {
  62. $output = '<h2>' . $headertexts[$type] . '</h2>';
  63. }
  64. foreach ($comments as $comment) {
  65. $output .= '<div class="comment-block"><div class="comment-author">Autor: ' . htmlspecialchars($comment['author']) . ', data: ' . date('j.m.Y G:i:s', $comment['date']) . '</div><div class="comment-text">' . htmlspecialchars($comment['content']) . '</div></div>';
  66. }
  67. $output .= '<h2>Dodaj nowy komentarz</h2><form id="comment-form" method="post" action="sendcomment.php"><table id="comment-form-table"><tr><td class="form-caption">Autor:</td><td><input type="text" name="comment-nick" id="comment-nick"></td></tr>';
  68. $output .= '<tr><td class="form-caption">E-mail (nie pokażę go nikomu):</td><td><input type="text" name="comment-email" id="comment-email"></td></tr>';
  69. $output .= '<tr><td colspan="2"><textarea id="comment-content" name="comment-content"></textarea></td></tr></table>';
  70. $output .= '<input type="submit" value="Wyślij">';
  71. $output .= '<input type="hidden" name="comment-type" id="comment-type" value="' . $type . '"><input type="hidden" name="comment-id" id="comment-id" value="' . $id . '"></form>';
  72. return $output;
  73. }
  74. ?>