comment.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if($row = $res->fetch(PDO::FETCH_ASSOC)) {
  45. return $row['count'];
  46. }
  47. return 0;
  48. }
  49. function count_comments_all_dir_images($id) {
  50. global $pdo;
  51. $sql = 'SELECT count(*) AS count FROM comments WHERE image IN (SELECT id FROM images WHERE dir = :dir)';
  52. $values = array(
  53. ':dir' => $id,
  54. );
  55. try {
  56. $res = $pdo->prepare($sql);
  57. $res->execute($values);
  58. } catch (PDOException $e) {
  59. echo 'Query error: ' . $e->getMessage();
  60. die();
  61. }
  62. if($row = $res->fetch(PDO::FETCH_ASSOC)) {
  63. return $row['count'];
  64. }
  65. return 0;
  66. }
  67. function db_store_comment($nick, $email, $type, $id, $content) {
  68. global $pdo;
  69. $sql = 'INSERT INTO comments (ip, ' . get_field_name($type) . ', email, author, date, content) VALUES (:ip, :id, :email, :nick, :date, :content)';
  70. $values = array (
  71. ':ip' => $_SERVER['REMOTE_ADDR'],
  72. ':id' => $id,
  73. ':email' => $email,
  74. ':nick' => $nick,
  75. ':date' => time(),
  76. ':content' => $content,
  77. );
  78. try {
  79. $res = $pdo->prepare($sql);
  80. $res->execute($values);
  81. } catch (PDOException $e) {
  82. echo 'Query error: ' . $e->getMessage();
  83. die();
  84. }
  85. }
  86. function generate_comment_field($comments, $type, $id) {
  87. $emptytexts = array(
  88. 'dir' => 'Brak komentarzy do tego katalogu, możesz być pierwszy!',
  89. 'image' => 'Brak komentarzy do tego obrazka, możesz być pierwszy!',
  90. );
  91. $headertexts = array(
  92. 'dir' => 'Komentarze do katalogu',
  93. 'image' => 'Komentarze do obrazka',
  94. );
  95. if (count($comments) == 0) {
  96. $output = '<h2>' . $emptytexts[$type] . '</h2>';
  97. } else {
  98. $output = '<h2>' . $headertexts[$type] . '</h2>';
  99. }
  100. foreach ($comments as $comment) {
  101. $output .= '<div class="comment-block"><div class="comment-header">Autor: <span class="comment-author">' . htmlspecialchars($comment['author']) . '</span>, data: <span class="comment-date">' . date('j.m.Y G:i:s', $comment['date']) . '</span></div><div class="comment-text">' . htmlspecialchars($comment['content']) . '</div></div>';
  102. }
  103. $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>';
  104. $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>';
  105. $output .= '<tr><td colspan="2"><textarea id="comment-content" name="comment-content"></textarea></td></tr></table>';
  106. $output .= '<input type="submit" value="Wyślij">';
  107. $output .= '<input type="hidden" name="comment-type" id="comment-type" value="' . $type . '"><input type="hidden" name="comment-id" id="comment-id" value="' . $id . '"></form>';
  108. return $output;
  109. }
  110. ?>