comment.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 ORDER BY date DESC';
  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 count_comments($id, $type) {
  32. global $pdo;
  33. $sql = 'SELECT count(*) AS count FROM comments WHERE ' . get_field_name($type) . ' = :id';
  34. $values = array(
  35. ':id' => $id,
  36. );
  37. try {
  38. $res = $pdo->prepare($sql);
  39. $res->execute($values);
  40. } catch (PDOException $e) {
  41. echo 'Query error: ' . $e->getMessage();
  42. die();
  43. }
  44. $comments = [];
  45. if($row = $res->fetch(PDO::FETCH_ASSOC)) {
  46. return $row['count'];
  47. }
  48. return 0;
  49. }
  50. function db_store_comment($nick, $email, $type, $id, $content) {
  51. global $pdo;
  52. $sql = 'INSERT INTO comments (ip, ' . get_field_name($type) . ', email, author, date, content) VALUES (:ip, :id, :email, :nick, :date, :content)';
  53. $values = array (
  54. ':ip' => $_SERVER['REMOTE_ADDR'],
  55. ':id' => $id,
  56. ':email' => $email,
  57. ':nick' => $nick,
  58. ':date' => time(),
  59. ':content' => $content,
  60. );
  61. try {
  62. $res = $pdo->prepare($sql);
  63. $res->execute($values);
  64. } catch (PDOException $e) {
  65. echo 'Query error: ' . $e->getMessage();
  66. die();
  67. }
  68. }
  69. function generate_comment_field($comments, $type, $id) {
  70. $emptytexts = array(
  71. 'dir' => 'Brak komentarzy do tego katalogu, możesz być pierwszy!',
  72. 'image' => 'Brak komentarzy do tego obrazka, możesz być pierwszy!',
  73. );
  74. $headertexts = array(
  75. 'dir' => 'Komentarze do katalogu',
  76. 'image' => 'Komentarze do obrazka',
  77. );
  78. if (count($comments) == 0) {
  79. $output = '<h2>' . $emptytexts[$type] . '</h2>';
  80. } else {
  81. $output = '<h2>' . $headertexts[$type] . '</h2>';
  82. }
  83. foreach ($comments as $comment) {
  84. $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>';
  85. }
  86. $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>';
  87. $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>';
  88. $output .= '<tr><td colspan="2"><textarea id="comment-content" name="comment-content"></textarea></td></tr></table>';
  89. $output .= '<input type="submit" value="Wyślij">';
  90. $output .= '<input type="hidden" name="comment-type" id="comment-type" value="' . $type . '"><input type="hidden" name="comment-id" id="comment-id" value="' . $id . '"></form>';
  91. return $output;
  92. }
  93. ?>