Post.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. require_once($path["entity"] . "Entity.php");
  3. require_once($path["entity"] . "Post_Comment.php");
  4. require_once($path["entity"] . "Post_Image.php");
  5. /**
  6. * Post.
  7. *
  8. * Represents an object from the table 'post'.
  9. */
  10. class Post extends Entity{
  11. /**
  12. * Identifier.
  13. */
  14. public $id;
  15. /**
  16. * Permalink for linking the entity (relative).
  17. */
  18. public $permalink;
  19. /**
  20. * Title in the selected language.
  21. */
  22. public $title;
  23. /**
  24. * The content in the defined language.
  25. */
  26. public $text;
  27. /**
  28. * Posting datetime.
  29. */
  30. public $dtime;
  31. /**
  32. * Array with the {@see Post_Image}.
  33. */
  34. public $image = [];
  35. /**
  36. * Array with the tags.
  37. */
  38. public $tag = [];
  39. /**
  40. * Array with the {@see Post_Comment}.
  41. */
  42. public $comment = [];
  43. /**
  44. * Constructor.
  45. *
  46. * Searches the database and retrieves the information about the
  47. * entity, populating it and it's items.
  48. *
  49. * @param db Connection to the database.
  50. * @param lang Lowercase, two-letter language code.
  51. * @param id Identifier or permalink.
  52. */
  53. public function __construct($db, $lang, $id){
  54. parent::__construct($db, $lang);
  55. $s =
  56. "SELECT " .
  57. " id, " .
  58. " permalink, " .
  59. " title_" . $this->lang . " AS title, " .
  60. " text_" . $this->lang . " AS text, " .
  61. " dtime " .
  62. "FROM post " .
  63. "WHERE " .
  64. " visible = 1 AND " .
  65. " ( " .
  66. " id = '$id' OR " .
  67. " permalink = '$id' " .
  68. " );";
  69. $q = mysqli_query($this->db, $s);
  70. if (mysqli_num_rows($q) > 0){
  71. $r = mysqli_fetch_array($q);
  72. $this->id = $r["id"];
  73. $this->permalink = $r["permalink"];
  74. $this->title = $r["title"];
  75. $this->text = $r["text"];
  76. $this->dtime = $r["dtime"];
  77. }
  78. $s_image =
  79. "SELECT id " .
  80. "FROM post_image " .
  81. "WHERE post = " . $this->id . " " .
  82. "ORDER BY idx; ";
  83. $q_image = mysqli_query($this->db, $s_image);
  84. while($r_image = mysqli_fetch_array($q_image)){
  85. array_push($this->image, new Post_Image($this->db, $r_image["id"]));
  86. }
  87. $s_tag =
  88. "SELECT tag " .
  89. "FROM post_tag " .
  90. "WHERE post = " . $this->id . ";";
  91. $q_tag = mysqli_query($this->db, $s_tag);
  92. while($r_tag = mysqli_fetch_array($q_tag)){
  93. array_push($this->tag, $r_tag["tag"]);
  94. }
  95. $s_comment =
  96. "SELECT id " .
  97. "FROM post_comment " .
  98. "WHERE " .
  99. " post = " . $this->id . " AND " .
  100. " approved = 1;";
  101. $q_comment = mysqli_query($this->db, $s_comment);
  102. while($r_comment = mysqli_fetch_array($q_comment)){
  103. array_push($this->comment, new Post_Comment($this->db, $r_comment["id"]));
  104. }
  105. }
  106. }
  107. ?>