Эх сурвалжийг харах

Merge branch 'Custom_Filters'

Iñigo Valentin 6 жил өмнө
parent
commit
7ee4a22e42

+ 75 - 0
application/entity/Custom_Filter.php

@@ -0,0 +1,75 @@
+<?php
+
+    require_once($path["entity"] . "Entity.php");
+
+    /**
+     * A custom_Filter.
+     *
+     * Represents an object from the table 'filter'.
+     */
+    class Custom_Filter extends Entity{
+
+        /**
+         * Player identifier.
+         */
+        public $uid;
+
+        /**
+         * Filter identifier.
+         */
+        public $id;
+
+        /**
+         * Name of the pge the filter is for.
+         */
+        public $page;
+
+        /**
+         * Filter name.
+         */
+        public $name;
+
+        /**
+         * Filter description.
+         */
+        public $description;
+
+        /**
+         * Filter condition.
+         */
+        public $condition;
+
+        /**
+         * Constructor.
+         *
+         * Searches the database and retrieves the information about the
+         * run, populating it and it's items.
+         *
+         * @param SQLite3 $db Connection to the database.
+         * @param int $id Filter identifier.
+         */
+        public function __construct($db, $id){
+            parent::__construct($db);
+            $s = "
+                SELECT
+                  uid,
+                  id,
+                  page,
+                  name,
+                  description,
+                  condition
+                FROM filter
+                WHERE id = $id;
+            ";
+            $q = $this->db->query($s);
+            $r = $q->fetchArray(SQLITE3_ASSOC);
+            $this->id = $r["id"];
+            $this->uid = $r["uid"];
+            $this->page = $r["page"];
+            $this->name = $r["name"];
+            $this->description = $r["description"];
+            $this->condition = $r["condition"];
+        }
+
+    }
+?>

+ 58 - 2
application/page/Runes_Page.php

@@ -1,6 +1,7 @@
  <?php
 
     require_once($path["page"] . "Page.php");
+    require_once($path["entity"] . "Custom_Filter.php");
     require_once($path["entity"] . "Rune.php");
     require_once($path["entity"] . "Unit.php");
 
@@ -16,6 +17,11 @@
          */
         public $runes = [];
 
+        /**
+         * Custom filters for this page.
+         */
+        public $custom_filters = [];
+
         /**
          * The filters the page can handle.
          */
@@ -50,10 +56,17 @@
         public function __construct($db){
             global $path;
             global $root;
+            global $UID;
             parent::__construct($db);
             $this->view = $path["view"] . "runes.php";
             $this->parse_filters();
-            $s = $this->build_query();
+            if (!isset($_GET["custom_filter"])){
+                $s = $this->build_query();
+                $this->filters["group"] = 1;
+            }
+            else{
+                $s = $this->build_custom_query($_GET["custom_filter"]);
+            }
             $q = $this->db->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 $rune = new Rune($db, $r["id"]);
@@ -61,7 +74,18 @@
                     $rune->assigned_to = new Unit($this->db, $rune->assigned_to, false);
                 }
                 array_push($this->runes, $rune);
-        }
+            }
+            $s = "
+              SELECT id
+              FROM filter
+              WHERE
+                uid = '$UID' AND
+                page = 'RUNES';
+            ";
+            $q = $this->db->query($s);
+            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
+                array_push($this->custom_filters, new Custom_Filter($this->db, $r["id"]));
+            }
             $this->title = "Runes - SWDB";
             $this->description = "Rune inventory";
             $this->canonical = $root . "runes/";
@@ -144,6 +168,38 @@
             return;
         }
 
+        /**
+         * Builds the query to the rune table using the a custom filter.
+         * 
+         * @param int $filter Custom filter ID.
+         * @return The query to be executed.
+         */
+        private function build_custom_query($filter){
+            global $UID;
+            $s = "
+              SELECT condition
+              FROM filter
+              WHERE
+                uid = '$UID' AND
+                id = $filter;
+            ";
+            $q = $this->db->query($s);
+            $r = $q->fetchArray(SQLITE3_ASSOC);
+            $cond = $r["condition"];
+            $s = "
+              SELECT
+                id,
+                assigned_to
+              FROM rune
+              WHERE uid = '$UID' AND (
+                $cond
+              )
+              ORDER BY  slot, type, stars DESC, original_quality DESC, quality DESC, level;
+            ";
+            error_log("CUSTOM QUERY: " . $s);
+            return $s;
+        }
+
         /**
          * Builds the query to the rune table using the selected or default
          * filters.

+ 53 - 0
application/view/inc/filter_runes.php

@@ -3,7 +3,60 @@
     global $RUNE_STAT;
     global $QUALITY;
 ?>
+<?php
+    if (count($custom_filters) > 0){
+?>
+        <script>
+            /**
+             * Changes the filter description.
+             *
+             * @param id Filter id.
+             */
+            function customFilterDescription(id){
+                switch (id){
+<?php
+                    foreach($custom_filters AS $filter){
+?>
+                        case '<?=$filter->id?>':
+                            document.getElementById("custom_filter_description").style.display = 'block';
+                            document.getElementById("custom_filter_description").innerHTML = '<?=$filter->description?>';
+                            break;
+<?php
+                    }
+?>
+                }
+            }
+        </script>
+        <form action='/<?=$UID?>/runes/' method='get' id='custom_filter_runes' class='custom_filter'>
+            <span class='custom_filter_title'>Custom filters:</span>
+            <select name='custom_filter' onChange='customFilterDescription(this.value);' onload='alert("LOAD");customFilterDescription("<?=$_GET["custom_filter"]?>");'>
+                <option> </option>
+<?php
+                 foreach($custom_filters AS $filter){
+                    if ($_GET["custom_filter"] == $filter->id){
+                        $selected = "selected='selected'";
+                    }
+                    else{
+                        $selected = "";
+                    }
+?>
+                    <option value='<?=$filter->id?>' <?=$selected?>><?=$filter->name?></option>
+<?php
+                 }
+?>
+            </select>
+            <input type='submit' value='Apply'/>
+            <p id='custom_filter_description'> </p>
+            <script>
+                customFilterDescription("<?=$_GET["custom_filter"]?>");
+            </script>
+        </form>
+<?php
+    }
+?>
+</form>
 <form action='/<?=$UID?>/runes/' method='get' id='filter_runes' class='filter'>
+    <div class='custom_filters'>
     <table>
         <tr>
             <td class='filter' rowspan='3'>

+ 1 - 0
application/view/runes.php

@@ -40,6 +40,7 @@
                 </h2>
 <?php
                 $filters = $page->filters;
+                $custom_filters = $page->custom_filters;
                 include __DIR__ . "/inc/filter_runes.php";
 ?>
 

+ 8 - 0
install_data/setup.sql

@@ -485,6 +485,14 @@ CREATE TABLE IF NOT EXISTS team_unit(
     unit INT NOT NULL REFERENCES unit(id),
     PRIMARY KEY(team, unit)
 );
+CREATE TABLE IF NOT EXISTS filter(
+    uid INT NOT NULL REFERENCES player(id),
+    id INT NOT NULL PRIMARY KEY,
+    page TEXT NOT NULL,
+    name INT NOT NULL,
+    description TEXT,
+    condition TEXT NOT NULL
+);
 PRAGMA foreign_keys = ON;
 
 INSERT INTO k_difficulty VALUES(0,'Easy');

+ 15 - 1
public/css/filters.css

@@ -10,9 +10,23 @@ section.filters{
     }
 }
 
+form.custom_filter{
+    border-bottom: 0.1em solid #ffffff;
+    padding-bottom: 0.5em;
+    margin: 0 1em 1em 1em;
+}
+
+form.custom_filter p#custom_filter_description{
+    display: none;
+    font-size: 70%;
+    margin: 0.2em;
+    background-color: #886644;
+    border-radius: 0.4em;
+    border: 0.1em solid #000000;
+}
+
 form.filter{
     text-align:center;
-    background-color: #aa8866;
 }
 
 form.filter table{