SubscribeController.inc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. class SubscribeController implements IController {
  3. public function execute() {
  4. $session = new Session();
  5. $view = new View();
  6. $database = new Database();
  7. $headRenderer = new HeadRenderer();
  8. $userId = $session->getValue("logged_in");
  9. // get list of all topics (paginated)
  10. $sql = "SELECT * FROM topics";
  11. $topics = $database->query($sql);
  12. $viewData = array(
  13. "header" => $headRenderer->render(),
  14. "topics" => $topics
  15. );
  16. if ($userId) {
  17. // get list of all user-subscriptions
  18. $sql = sprintf("SELECT topic_id FROM user_topic_subscription WHERE user_id = '%s'", $userId);
  19. $subscribedTopicsRaw = $database->query($sql);
  20. $subscribedTopics = array();
  21. foreach ($subscribedTopicsRaw as $topicData) {
  22. $subscribedTopics[] = $topicData['topic_id'];
  23. }
  24. // add "subscribed" flag to topics user is subscribed to
  25. foreach ($topics as $index => $topic) {
  26. if (in_array($topic['topic_id'], $subscribedTopics)) {
  27. $topics[$index]['subscribed'] = true;
  28. } else {
  29. $topics[$index]['subscribed'] = false;
  30. }
  31. }
  32. // return view for all topics
  33. $viewData["topics"] = $topics;
  34. }
  35. return $view->render("subscriptionView.inc", $viewData);
  36. }
  37. }