| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- /**
- * Search results page file.
- *
- * Provides a class with all the properties and methods to display the page.
- *
- * @author Iñigo Valentin <i@inigovalentin.com>
- * @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;
- }
- }
- }
-
- }
|