ManageBlog.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * Copyright (C) 2022 Echedey López Romero <elr@disroot.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class ManageBlog {
  19. private static $Instance = null;
  20. private string $Title;
  21. private ManageUsers $ManageUsers;
  22. private ManageComments $ManageComments;
  23. private ManagePosts $ManagePosts;
  24. function __construct(string $Title, ManageUsers $ManageUsers, ManageComments $ManageComments, ManagePosts $ManagePosts) {
  25. $this->Title = $Title;
  26. $this->ManageUsers = $ManageUsers;
  27. $this->ManageComments = $ManageComments;
  28. $this->ManagePosts = $ManagePosts;
  29. }
  30. public static function GetInstance(string $Title, ManageUsers $ManageUsers, ManageComments $ManageComments, ManagePosts $ManagePosts) {
  31. if (self::$Instance === null) {
  32. self::$Instance = new ManageBlog($Title, $ManageUsers, $ManageComments, $ManagePosts);
  33. }
  34. return self::$Instance;
  35. }
  36. public function ShowPosts(): string {
  37. $HTML = '';
  38. $HTML .= '<h1>' . $this->Title . '</h1>' . PHP_EOL;
  39. $Posts = $this->ManagePosts->GetPosts();
  40. if (gettype($Posts) === 'array') {
  41. foreach ($Posts as $Post) {
  42. $HTML .= $Post->RenderCard();
  43. }
  44. } else {
  45. $HTML .= $Posts;
  46. }
  47. return $HTML;
  48. }
  49. public function ShowPost(string $Id): string {
  50. $HTML = '';
  51. $HTML .= '<h1>' . $this->Title . '</h1>' . PHP_EOL;
  52. $Post = $this->ManagePosts->GetPost($Id);
  53. if (gettype($Post) === 'object') {
  54. $UserName = $this->ManageUsers->GetUser($Post->GetUserId())->GetName();
  55. $CommentIdsList = $Post->GetComments();
  56. $CommentsList = [];
  57. $UserNamesList = [];
  58. foreach ($CommentIdsList as $CommentId) {
  59. $Comment = $this->ManageComments->GetComment($CommentId);
  60. $UserNameComment = $this->ManageUsers->GetUser($Comment->GetUserId())->GetName();
  61. array_push($CommentsList, $Comment);
  62. array_push($UserNamesList, $UserNameComment);
  63. }
  64. $HTML .= $Post->RenderComplete($UserName, $UserNamesList,
  65. $CommentsList);
  66. } else {
  67. $HTML .= $Post;
  68. }
  69. return $HTML;
  70. }
  71. public function ShowUser(string $Id): string {
  72. $HTML = '';
  73. $HTML .= '<h1>' . $this->Title . '</h1>' . PHP_EOL;
  74. $User = $this->ManageUsers->GetUser($Id);
  75. if (gettype($User) === 'object') {
  76. $HTML .= $User->Render();
  77. } else {
  78. $HTML .= $User;
  79. }
  80. return $HTML;
  81. }
  82. }