Search_Page.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Search results page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. * @todo Implement
  11. */
  12. require_once(PATH::PAGE . "Page.php");
  13. /**
  14. * Main reports page model.
  15. *
  16. * @category Page
  17. */
  18. class Search_Page extends Page{
  19. /**
  20. * @var string Searched query.
  21. *
  22. * Will be processed do standarize it.
  23. */
  24. private $query = "";
  25. /**
  26. * @var mixed[] Search results.
  27. */
  28. private $results = [
  29. "monster" => [],
  30. "dungeon" => [],
  31. "building" => [],
  32. "report" => [],
  33. "player" => [],
  34. "unit" => [],
  35. "team" => [],
  36. ];
  37. /**
  38. * @var mixed[] Default filter values.
  39. */
  40. private $filters = [
  41. "MONSTER" => true,
  42. "DUNGEON" => true,
  43. "BUILDING" => true,
  44. "REPORT" => true,
  45. "PLAYER" => true,
  46. "UNIT" => true,
  47. "TEAM" => true
  48. ];
  49. /**
  50. * Constructor.
  51. *
  52. * Retrieves the data and initializes the variables.
  53. *
  54. */
  55. public function __construct($query = ""){
  56. parent::__construct();
  57. $this->set_public(true);
  58. $this->set_view("search.php");
  59. $this->set_title("Search results");
  60. $this->set_description("Search results: " . htmlentities($query));
  61. $this->set_canonical("search/" . $query);
  62. $this->add_css("search.css");
  63. $this->parse_filters();
  64. error_log("QUERY: " . $query);
  65. if ($query != null && $query != ""){
  66. $q = $query;
  67. $q = preg_replace('/\s+/', ' ', $q); // Remove multiple whitespace
  68. $q = strtolower($q); // Lowercase
  69. $this->query = $q;
  70. if ($this->filters["MONSTER"] == true){
  71. $this->search_monster();
  72. }
  73. if ($this->filters["DUNGEON"] == true){
  74. $this->search_dungeon();
  75. }
  76. if ($this->filters["BUILDING"] == true){
  77. $this->search_building();
  78. }
  79. /*if ($this->filters["REPORT"] == true){
  80. $this->search_report();
  81. }
  82. if ($this->filters["PLAYER"] == true){
  83. $this->search_player();
  84. }
  85. if ($this->filters["UNIT"] == true){
  86. $this->search_unit();
  87. }
  88. if ($this->filters["TEAM"] == true){
  89. $this->search_team();
  90. }*/
  91. }
  92. $this->set_code(200);
  93. $this->set_message("OK");
  94. }
  95. /**
  96. * Parses the request looking for the selected filters, validates them
  97. * and adds them to the $filters array.
  98. */
  99. private function parse_filters(){
  100. // filters["MONSTER"]
  101. if (isset($_GET["monster"]) && $_GET["monster"] != "on"){
  102. $this->filters["MONSTER"] = false;
  103. }
  104. else{
  105. $this->filters["MONSTER"] = true;
  106. }
  107. // filters["DUNGEON"]
  108. if (isset($_GET["dungeon"]) && $_GET["dungeon"] != "on"){
  109. $this->filters["DUNGEON"] = false;
  110. }
  111. else{
  112. $this->filters["DUNGEON"] = true;
  113. }
  114. // filters["BUILDING"]
  115. if (isset($_GET["building"]) && $_GET["building"] != "on"){
  116. $this->filters["BUILDING"] = false;
  117. }
  118. else{
  119. $this->filters["BUILDING"] = true;
  120. }
  121. // filters["REPORT"]
  122. if (isset($_GET["report"]) && $_GET["report"] != "on"){
  123. $this->filters["REPORT"] = false;
  124. }
  125. else{
  126. $this->filters["DUNGEON"] = true;
  127. }
  128. // filters["PLAYER"]
  129. if (isset($_GET["player"]) && $_GET["player"] != "on"){
  130. $this->filters["PLAYER"] = false;
  131. }
  132. else{
  133. $this->filters["PLAYER"] = true;
  134. }
  135. // filters["UNIT"]
  136. if ((isset($_GET["unit"]) && $_GET["unit"] != "on") || get_context()->get_user() == null){
  137. $this->filters["UNIT"] = false;
  138. }
  139. else{
  140. $this->filters["UNIT"] = true;
  141. }
  142. // filters["TEAM"]
  143. if ((isset($_GET["team"]) && $_GET["team"] != "on") || get_context()->get_user() == null){
  144. $this->filters["TEAM"] = false;
  145. }
  146. else{
  147. $this->filters["TEAM"] = true;
  148. }
  149. return;
  150. }
  151. /**
  152. * Searchs monsters matching the query and adds them to the result array.
  153. */
  154. private function search_monster(){
  155. $statement = get_context()->get_db()->prepare("
  156. SELECT id
  157. FROM monster
  158. WHERE obtainable = 1 AND
  159. (
  160. lower(name) LIKE :query OR
  161. id LIKE :query
  162. );
  163. ");
  164. $statement->bindValue(":query", "%" . $this->query . "%", SQLITE3_TEXT);
  165. error_log($statement->getSQL(true));
  166. $result_set = $statement->execute();
  167. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  168. array_push($this->results["monster"], new Monster($result["id"]));
  169. }
  170. return;
  171. }
  172. /**
  173. * Searchs monsters matching the query and adds them to the result array.
  174. * @todo: implement when dungeons area thing.
  175. */
  176. private function search_dungeon(){
  177. return;
  178. }
  179. /**
  180. * Searchs monsters matching the query and adds them to the result array.
  181. */
  182. private function search_building(){
  183. $statement = get_context()->get_db()->prepare("
  184. SELECT id, type
  185. FROM (
  186. SELECT id, name, description, 'buildable' AS type FROM buildable
  187. UNION
  188. SELECT id, name, description, 'decorative' AS type FROM decorative
  189. )
  190. WHERE
  191. lower(name) LIKE :query OR
  192. lower(description) LIKE :query OR
  193. id LIKE :query;
  194. ");
  195. $statement->bindValue(":query", "%" . $this->query . "%", SQLITE3_TEXT);
  196. error_log($statement->getSQL(true));
  197. $result_set = $statement->execute();
  198. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  199. if ("buildable" == $result["type"]){
  200. array_push($this->results["building"], new Buildable($result["id"]));
  201. }
  202. elseif ("decorative" == $result["type"]){
  203. array_push($this->results["building"], new Decorative($result["id"]));
  204. }
  205. }
  206. }
  207. /**
  208. * Retrieves the page filters.
  209. *
  210. * @return mixed[] Page filtes
  211. */
  212. public function get_filters(){
  213. return $this->filters;
  214. }
  215. /**
  216. * Retrieves one filter.
  217. *
  218. * @param string $key
  219. * @return mixed Filter value, null if if doesn't exist.
  220. */
  221. public function get_filter($key){
  222. if (array_key_exists($key, $this->filters)){
  223. return $this->filters[$key];
  224. }
  225. else{
  226. return null;
  227. }
  228. }
  229. /**
  230. * Retrieves the results.
  231. *
  232. * If no key is specified, an array with the following keys will be returned:
  233. * "monster", "dungeon", "building", "player", "report". Each of these keys is an array itself
  234. * with instances of the found elements. If a key is specified, only it's array will be
  235. * returned.
  236. *
  237. *
  238. * @param string $key Result group name.
  239. * @return mixed[] Result list.
  240. */
  241. public function get_results($key = null){
  242. if ($key == null){
  243. error_log("RET NULL");
  244. return $this->results;
  245. }
  246. else{
  247. error_log("RET KEY : $key");
  248. if (isset($this->results[$key])){
  249. error_log("RET KEY OK");
  250. return $this->results[$key];
  251. }
  252. else{
  253. error_log("RET NULL NULL");
  254. return null;
  255. }
  256. }
  257. }
  258. }