SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/SearchController.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Controllers;
  15.  
  16. use Exception;
  17.  
  18. use Kohana;
  19.  
  20. use Sprout\Helpers\FrontEndSearch;
  21. use Sprout\Helpers\Navigation;
  22. use Sprout\Helpers\Register;
  23. use Sprout\Helpers\Search;
  24. use Sprout\Helpers\SearchHandler;
  25. use Sprout\Helpers\View;
  26.  
  27.  
  28. /**
  29.  * Performs site-wide searches
  30.  */
  31. class SearchController extends Controller
  32. {
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. }
  37.  
  38.  
  39. /**
  40.   * Performs a site-wide search for the 'q' term specified via the query string.
  41.   * Basically a wrapper for the Search::query() method
  42.   * @get string q The search query
  43.   * @get int page Page number, for results pagination
  44.   * @return void Outputs HTML directly, containing the search results
  45.   */
  46. public function index()
  47. {
  48. $config_handlers = Register::getSearchHandlers();
  49.  
  50. if (! isset($_GET['page'])) $_GET['page'] = 1;
  51. $_GET['page'] = (int) $_GET['page'];
  52.  
  53. // Nonsensical page numbers -> force to 1
  54. if ($_GET['page'] < 1) $_GET['page'] = 1;
  55.  
  56. $search_handlers = array();
  57. foreach ($config_handlers as $ch) {
  58. if (is_array($ch)) {
  59. $search_handlers[] = new SearchHandler($ch[0], $ch[1]);
  60. } else if ($ch instanceof SearchHandler) {
  61. $search_handlers[] = $ch;
  62. } else {
  63. throw new Exception("Invalid SearchHandler registered");
  64. }
  65. }
  66.  
  67. $_GET['q'] = trim(@$_GET['q']);
  68.  
  69. if ($_GET['q']) {
  70. $search_result = Search::query($_GET['q'], $search_handlers, $_GET['page'] - 1);
  71. } else {
  72. $search_result = null;
  73. }
  74.  
  75. if (! $search_result) {
  76. // No valid keywords specified
  77. $page_view = new View('skin/inner');
  78. $page_view->page_title = 'Search';
  79. $page_view->main_content = '<div class="site-search-form">' . new View('sprout/search_form') . '</div>';
  80.  
  81. echo $page_view->render();
  82. return;
  83. }
  84.  
  85. list ($res, $keywords, $num_results, $num_pages) = $search_result;
  86.  
  87. if ($res->rowCount() == 0) {
  88. $out = '<p>No results were found which match your search terms.</p>';
  89.  
  90. } else {
  91. // Instantiate search handlers
  92. $handler_inst = array();
  93. foreach ($search_handlers as $handler) {
  94. $class = $handler->getCtlrName();
  95.  
  96. $ctlr = new $class;
  97. if (! $ctlr instanceof FrontEndSearch) throw new Exception("Search handler {$class} does not implement FrontEndSearch");
  98. $handler_inst[$class] = $ctlr;
  99. }
  100.  
  101. // Iterate through results, passing the rendering task off to the appropriate controller
  102. $out = '';
  103. $classes = array();
  104. foreach ($res as $row) {
  105. $ctlr = $handler_inst[$row['controller_class']];
  106.  
  107. $resp = $ctlr->frontEndSearch($row['record_id'], $row['relevancy'], $keywords);
  108. if (! $resp) continue;
  109.  
  110. $out .= '<div class="search-result">' . $resp . '</div>';
  111. }
  112. }
  113. $res->closeCursor();
  114.  
  115. $out .= Search::paginate($_GET['page'], $num_pages, 'search-paginate');
  116.  
  117. $page_view = new View('skin/inner');
  118. $page_view->page_title = 'Search';
  119. $page_view->browser_title = Navigation::buildBrowserTitle('Search');
  120. $page_view->main_content = '<div class="site-search-form">' . new View('sprout/search_form') . '</div>' . $out;
  121. $page_view->controller_name = $this->getCssClassName();
  122.  
  123. echo $page_view->render();
  124. }
  125.  
  126. }
  127.