ResultsProvider.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. class ResultsProvider {
  3. private $db;
  4. public function __construct($db){
  5. $this->db = $db;
  6. }
  7. public function get_total_result($type,$term){
  8. if($type == 'sites'){
  9. return $this->total_sites_result($term);
  10. }
  11. if($type == 'images'){
  12. return $this->total_images_result($term);
  13. }
  14. }
  15. public function site_result($page,$pageSize,$term){
  16. $fromLimit = ($page - 1) * $pageSize;
  17. $query = $this->db->prepare("SELECT *
  18. FROM sites WHERE title LIKE :term
  19. OR url LIKE :term
  20. OR keywords LIKE :term
  21. OR description LIKE :term
  22. ORDER BY clicks DESC
  23. LIMIT :fromLimit, :pageSize");
  24. $searchTerm = "%". $term . "%";
  25. $query->bindParam(":term", $searchTerm);
  26. $query->bindParam(":fromLimit", $fromLimit, PDO::PARAM_INT);
  27. $query->bindParam(":pageSize", $pageSize, PDO::PARAM_INT);
  28. $query->execute();
  29. return $query->fetch(PDO::FETCH_ASSOC);
  30. }
  31. public function total_sites_result($term){
  32. $query = $this->db->prepare("SELECT COUNT(*) as total
  33. FROM sites WHERE title LIKE :term
  34. OR url LIKE :term
  35. OR keywords LIKE :term
  36. OR description LIKE :term");
  37. $searchTerm = "%". $term . "%";
  38. $query->bindParam(":term", $searchTerm);
  39. $query->execute();
  40. $row = $query->fetch(PDO::FETCH_ASSOC);
  41. return $row["total"];
  42. }
  43. public function site_html_result($page, $pageSize, $term){
  44. $fromLimit = ($page - 1) * $pageSize;
  45. $query = $this->db->prepare("SELECT *
  46. FROM sites WHERE title LIKE :term
  47. OR url LIKE :term
  48. OR keywords LIKE :term
  49. OR description LIKE :term
  50. ORDER BY clicks DESC
  51. LIMIT :fromLimit, :pageSize");
  52. $searchTerm = "%". $term . "%";
  53. $query->bindParam(":term", $searchTerm);
  54. $query->bindParam(":fromLimit", $fromLimit, PDO::PARAM_INT);
  55. $query->bindParam(":pageSize", $pageSize, PDO::PARAM_INT);
  56. $query->execute();
  57. $resultsHtml = "<div class='siteResults'>";
  58. while($row = $query->fetch(PDO::FETCH_ASSOC)) {
  59. $id = $row["id"];
  60. $url = $row["url"];
  61. $title = $row["title"];
  62. $description = $row["description"];
  63. $title = $this->trimField($title, 55);
  64. $description = $this->trimField($description, 230);
  65. $resultsHtml .= "<div class='resultContainer'>
  66. <h3 class='title'>
  67. <a class='result' href='$url' data-linkId='$id'>
  68. $title
  69. </a>
  70. </h3>
  71. <span class='url'>$url</span>
  72. <span class='description'>$description</span>
  73. </div>";
  74. }
  75. $resultsHtml .= "</div>";
  76. return $resultsHtml;
  77. }
  78. public function total_images_result($term){
  79. $query = $this->db->prepare("SELECT COUNT(*) as total
  80. FROM images
  81. WHERE (title LIKE :term
  82. OR alt LIKE :term)
  83. AND broken=0");
  84. $searchTerm = "%". $term . "%";
  85. $query->bindParam(":term", $searchTerm);
  86. $query->execute();
  87. $row = $query->fetch(PDO::FETCH_ASSOC);
  88. return $row["total"];
  89. }
  90. public function images_html_result($page, $pageSize, $term){
  91. $fromLimit = ($page - 1) * $pageSize;
  92. $query = $this->db->prepare("SELECT *
  93. FROM images
  94. WHERE (title LIKE :term
  95. OR alt LIKE :term)
  96. AND broken=0
  97. ORDER BY clicks DESC
  98. LIMIT :fromLimit, :pageSize");
  99. $searchTerm = "%". $term . "%";
  100. $query->bindParam(":term", $searchTerm);
  101. $query->bindParam(":fromLimit", $fromLimit, PDO::PARAM_INT);
  102. $query->bindParam(":pageSize", $pageSize, PDO::PARAM_INT);
  103. $query->execute();
  104. $resultsHtml = "<div class='imageResults'>";
  105. $count = 0;
  106. while($row = $query->fetch(PDO::FETCH_ASSOC)) {
  107. $count++;
  108. $id = $row["id"];
  109. $imageUrl = $row["imageUrl"];
  110. $siteUrl = $row["siteUrl"];
  111. $title = $row["title"];
  112. $alt = $row["alt"];
  113. if($title) {
  114. $displayText = $title;
  115. }
  116. else if($alt) {
  117. $displayText = $alt;
  118. }
  119. else {
  120. $displayText = $imageUrl;
  121. }
  122. $resultsHtml .= "<div class='gridItem image$count'>
  123. <a href='$imageUrl' data-fancybox data-caption='$displayText'
  124. data-siteurl='$siteUrl'>
  125. <script>
  126. $(document).ready(function() {
  127. loadImage(\"$imageUrl\", \"image$count\");
  128. });
  129. </script>
  130. <span class='details'>$displayText</span>
  131. </a>
  132. </div>";
  133. }
  134. $resultsHtml .= "</div>";
  135. return $resultsHtml;
  136. }
  137. private function trimField($string, $characterLimit) {
  138. $dots = strlen($string) > $characterLimit ? "..." : "";
  139. return substr($string, 0, $characterLimit) . $dots;
  140. }
  141. }