TopicModel.inc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. class TopicModel {
  3. public function getAllTopicNames() {
  4. $topicNames = array();
  5. $sql = sprintf("SELECT topic_id,display_name,url_name FROM topics");
  6. $database = new Database();
  7. $topicNamesRaw = $database->query($sql);
  8. foreach($topicNamesRaw as $topicData) {
  9. $topicNames[$topicData['topic_id']] = strtolower($topicData['display_name']);
  10. }
  11. return $topicNames;
  12. }
  13. public function getDefaultTopics() {
  14. $sql = "SELECT * FROM topics WHERE topic_id IN (SELECT topic_id FROM default_topics ORDER BY sort_order ASC)";
  15. $database = new Database();
  16. return $database->query($sql);
  17. }
  18. public function getUserTopics() {
  19. $session = new Session();
  20. $userId = $session->getValue('logged_in');
  21. if($userId) {
  22. $sql = sprintf("SELECT *
  23. FROM topics
  24. WHERE topic_id
  25. IN (SELECT topic_id FROM user_topic_subscription WHERE user_id = '%s')", $userId);
  26. $database = new Database();
  27. return $database->query($sql);
  28. }
  29. return $this->getDefaultTopics();
  30. }
  31. public function getCurrentTopic($topicId) {
  32. $topicData = array();
  33. $sql = sprintf("SELECT * FROM topics WHERE topic_id = '%s'", $topicId);
  34. $database = new Database();
  35. $topicData = $database->query($sql);
  36. if(isset($topicData[0])) {
  37. return $topicData[0];
  38. }
  39. return array();
  40. }
  41. public function getIdForTopicName($topicName) {
  42. $topicId = 0;
  43. $sql = "SELECT topic_id FROM topics WHERE display_name LIKE '%". $topicName . "%'";
  44. $database = new Database();
  45. $result = $database->query($sql);
  46. if(count($result) > 0) {
  47. $topicId = $result[0]['topic_id'];
  48. }
  49. return $topicId;
  50. }
  51. }