* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3 * @package SWDB * @todo Implement */ require_once(PATH::PAGE . "Page.php"); /** * Main reports page model. * * @category Page */ class Search_Page extends Page{ /** * @var string Searched query. * * Will be processed do standarize it. */ private $query = ""; /** * @var mixed[] Search results. */ private $results = [ "monster" => [], "dungeon" => [], "building" => [], "report" => [], "player" => [], "unit" => [], "team" => [], ]; /** * @var mixed[] Default filter values. */ private $filters = [ "MONSTER" => true, "DUNGEON" => true, "BUILDING" => true, "REPORT" => true, "PLAYER" => true, "UNIT" => true, "TEAM" => true ]; /** * Constructor. * * Retrieves the data and initializes the variables. * */ public function __construct($query = ""){ parent::__construct(); $this->set_public(true); $this->set_view("search.php"); $this->set_title("Search results"); $this->set_description("Search results: " . htmlentities($query)); $this->set_canonical("search/" . $query); $this->add_css("search.css"); $this->parse_filters(); error_log("QUERY: " . $query); if ($query != null && $query != ""){ $q = $query; $q = preg_replace('/\s+/', ' ', $q); // Remove multiple whitespace $q = strtolower($q); // Lowercase $this->query = $q; if ($this->filters["MONSTER"] == true){ $this->search_monster(); } if ($this->filters["DUNGEON"] == true){ $this->search_dungeon(); } if ($this->filters["BUILDING"] == true){ $this->search_building(); } /*if ($this->filters["REPORT"] == true){ $this->search_report(); } if ($this->filters["PLAYER"] == true){ $this->search_player(); } if ($this->filters["UNIT"] == true){ $this->search_unit(); } if ($this->filters["TEAM"] == true){ $this->search_team(); }*/ } $this->set_code(200); $this->set_message("OK"); } /** * Parses the request looking for the selected filters, validates them * and adds them to the $filters array. */ private function parse_filters(){ // filters["MONSTER"] if (isset($_GET["monster"]) && $_GET["monster"] != "on"){ $this->filters["MONSTER"] = false; } else{ $this->filters["MONSTER"] = true; } // filters["DUNGEON"] if (isset($_GET["dungeon"]) && $_GET["dungeon"] != "on"){ $this->filters["DUNGEON"] = false; } else{ $this->filters["DUNGEON"] = true; } // filters["BUILDING"] if (isset($_GET["building"]) && $_GET["building"] != "on"){ $this->filters["BUILDING"] = false; } else{ $this->filters["BUILDING"] = true; } // filters["REPORT"] if (isset($_GET["report"]) && $_GET["report"] != "on"){ $this->filters["REPORT"] = false; } else{ $this->filters["DUNGEON"] = true; } // filters["PLAYER"] if (isset($_GET["player"]) && $_GET["player"] != "on"){ $this->filters["PLAYER"] = false; } else{ $this->filters["PLAYER"] = true; } // filters["UNIT"] if ((isset($_GET["unit"]) && $_GET["unit"] != "on") || get_context()->get_user() == null){ $this->filters["UNIT"] = false; } else{ $this->filters["UNIT"] = true; } // filters["TEAM"] if ((isset($_GET["team"]) && $_GET["team"] != "on") || get_context()->get_user() == null){ $this->filters["TEAM"] = false; } else{ $this->filters["TEAM"] = true; } return; } /** * Searchs monsters matching the query and adds them to the result array. */ private function search_monster(){ $statement = get_context()->get_db()->prepare(" SELECT id FROM monster WHERE obtainable = 1 AND ( lower(name) LIKE :query OR id LIKE :query ); "); $statement->bindValue(":query", "%" . $this->query . "%", SQLITE3_TEXT); error_log($statement->getSQL(true)); $result_set = $statement->execute(); while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){ array_push($this->results["monster"], new Monster($result["id"])); } return; } /** * Searchs monsters matching the query and adds them to the result array. * @todo: implement when dungeons area thing. */ private function search_dungeon(){ return; } /** * Searchs monsters matching the query and adds them to the result array. */ private function search_building(){ $statement = get_context()->get_db()->prepare(" SELECT id, type FROM ( SELECT id, name, description, 'buildable' AS type FROM buildable UNION SELECT id, name, description, 'decorative' AS type FROM decorative ) WHERE lower(name) LIKE :query OR lower(description) LIKE :query OR id LIKE :query; "); $statement->bindValue(":query", "%" . $this->query . "%", SQLITE3_TEXT); error_log($statement->getSQL(true)); $result_set = $statement->execute(); while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){ if ("buildable" == $result["type"]){ array_push($this->results["building"], new Buildable($result["id"])); } elseif ("decorative" == $result["type"]){ array_push($this->results["building"], new Decorative($result["id"])); } } } /** * Retrieves the page filters. * * @return mixed[] Page filtes */ public function get_filters(){ return $this->filters; } /** * Retrieves one filter. * * @param string $key * @return mixed Filter value, null if if doesn't exist. */ public function get_filter($key){ if (array_key_exists($key, $this->filters)){ return $this->filters[$key]; } else{ return null; } } /** * Retrieves the results. * * If no key is specified, an array with the following keys will be returned: * "monster", "dungeon", "building", "player", "report". Each of these keys is an array itself * with instances of the found elements. If a key is specified, only it's array will be * returned. * * * @param string $key Result group name. * @return mixed[] Result list. */ public function get_results($key = null){ if ($key == null){ error_log("RET NULL"); return $this->results; } else{ error_log("RET KEY : $key"); if (isset($this->results[$key])){ error_log("RET KEY OK"); return $this->results[$key]; } else{ error_log("RET NULL NULL"); return null; } } } }