Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 2eab68b9

Přidáno uživatelem Tomáš Pašek před téměř 4 roky(ů)

Feature #8345 Implementce filterů dat - Server

vytvoření filtrů

Zobrazit rozdíly:

application/controller/TableController.php
2 2
require "../model/DB.php";
3 3
require "../model/Tag.php";
4 4
require "../model/Word.php";
5
require "../model/Lemma.php";
5 6
$DB = new DB();
6 7

  
7
// (B) SEARCH FOR USERS
8
$results = $DB->select(
9
    "SELECT w.id, w.context, w.date, w.description, w.description2, w.description3, w.ending, w.finished,
10
w.namedentity, w.position1, w.position2, w.positiondetail, w.word, w.tag_id, w.lemma_id, t.tag, l.lemma, l.pos FROM dd_wordform as w left join dd_tag as t on w.tag_id = t.id left join dd_lemma as l on w.lemma_id = l.id order by w.id LIMIT 100"
11
);
8
$parArray = Array();
9
$whereQuery = createWhereQuery($parArray);
12 10

  
13
var_dump($results);
11
if (array_key_exists("manuscript", $_POST) && $_POST['manuscript'] != "") {
12
    $results = $DB->select(
13
        "SELECT w.id, w.context, w.date, w.description, w.description2, w.description3, w.ending, w.finished,
14
w.namedentity, w.position1, w.position2, w.positiondetail, w.word, w.tag_id, w.lemma_id, t.tag, l.lemma, m.manuscript,
15
l.pos FROM dd_manuscript as m left join dd_wordform as w on m.wordform_id = w.id left join dd_tag as t on w.tag_id = t.id left join dd_lemma as l on w.lemma_id = l.id ".$whereQuery." order by w.id", $parArray);
16
} else {
17
    $results = $DB->select(
18
        "SELECT w.id, w.context, w.date, w.description, w.description2, w.description3, w.ending, w.finished,
19
w.namedentity, w.position1, w.position2, w.positiondetail, w.word, w.tag_id, w.lemma_id, t.tag, l.lemma,
20
l.pos FROM dd_wordform as w left join dd_tag as t on w.tag_id = t.id left join dd_lemma as l on w.lemma_id = l.id ".$whereQuery." order by w.id", $parArray);
21
}
14 22

  
15 23
$output = Array();
24
$temp_value = null;
16 25
foreach ($results as $res) {
17
    $tag = new Tag($res["tag_id"], $res["tag"]);
18
    $lemma = new Lemma($res["lemma_id"], $res["lemma"], $res["pos"]);
19
    $word = new Word($res["id"],  $res["context"], $res["date"], $res["description"], $res["description2"], $res["description3"], $res["ending"],
20
        $res["finished"], $res["namedentity"], $res["position1"], $res["position2"], $res["positiondetail"], $res["word"], $lemma, $tag);
21
    array_push($output, $word);
26
    if ($temp_value != null && $res["id"] != $temp_value->id) {
27
        array_push($output, $temp_value);
28
        $temp_value = null;
29
    }
30
    if ($temp_value == null) {
31
        $tag = new Tag($res["tag_id"], $res["tag"]);
32
        $lemma = new Lemma($res["lemma_id"], $res["lemma"], $res["pos"]);
33
        $word = new Word($res["id"],  $res["context"], $res["date"], $res["description"], $res["description2"], $res["description3"], $res["ending"],
34
            $res["finished"], $res["namedentity"], $res["position1"], $res["position2"], $res["positiondetail"], $res["word"], $lemma, $tag);
35
        if (array_key_exists("manuscript", $res)) {
36
            array_push($word->manuscript, $res["manuscript"]);
37
        }
38
        $temp_value = $word;
39
    } else {
40
        array_push($temp_value->manuscript, $res["manuscript"]);
41
    }
22 42
}
43
echo json_encode(count($output)==0 ? null : $output);
44

  
23 45

  
24
$result = json_encode(count($output)==0 ? null : $output);
25
echo $result;
46
function createWhereQuery(&$array) {
47
    $whereQuery = "WHERE";
48
    if (array_key_exists("lemma", $_POST) && $_POST['lemma'] != "") {
49
        $array['lemma'] = $_POST['lemma']."%";
50
        $whereQuery .= " l.lemma LIKE :lemma AND";
51
    }
52
    if (array_key_exists("word", $_POST) && $_POST['word'] != "") {
53
        $array['word'] = $_POST['word'];
54
        $whereQuery .= " w.word = :word AND";
55
    }
56
    if (array_key_exists("position1", $_POST) && $_POST['position1'] != "") {
57
        $array['position1'] = $_POST['position1'];
58
        $whereQuery .= " w.position1 = :position1 AND";
59
    }
60
    if (array_key_exists("position2", $_POST) && $_POST['position2'] != "") {
61
        $array['position2'] = $_POST['position2'];
62
        $whereQuery .= " w.position2 = :position2 AND";
63
    }
64
    if (array_key_exists("positiondetail", $_POST) && $_POST['positiondetail'] != "") {
65
        $array['positiondetail'] = $_POST['positiondetail'];
66
        $whereQuery .= " w.positiondetail = :positiondetail AND";
67
    }
68
    if (array_key_exists("manuscript", $_POST) && $_POST['manuscript'] != "") {
69
        $array['manuscript'] = Array();
70
        $nums = explode(",", $_POST['manuscript']);
71
        $whereQuery .= " m.manuscript in (";
72
        for ($x = 0; $x < count($nums); $x += 1) {
73
            $array['manuscript'][$x] = $nums[$x];
74
            $whereQuery .= ":manuscript".$x.",";
75
        }
76
        $whereQuery = substr_replace($whereQuery, "", -1, 1);
77
        $whereQuery .= ") AND";
78
    }
79
    $tag = "____________%";
80
    if (array_key_exists("tag_pos", $_POST) && $_POST['tag_pos'] != "") {
81
        $tag = substr_replace($tag, $_POST['tag_pos'], 0, 1);
82
    }
83
    if (array_key_exists("tag_case", $_POST) && $_POST['tag_case'] != "") {
84
        $tag = substr_replace($tag, $_POST['tag_case'], 1, 1);
85
    }
86
    if (array_key_exists("tag_number", $_POST) && $_POST['tag_number'] != "") {
87
        $tag = substr_replace($tag, $_POST['tag_number'], 2, 1);
88
    }
89
    if (array_key_exists("tag_gender", $_POST) && $_POST['tag_gender'] != "") {
90
        $tag = substr_replace($tag, $_POST['tag_gender'], 3, 1);
91
    }
92
    if (array_key_exists("tag_degree", $_POST) && $_POST['tag_degree'] != "") {
93
        $tag = substr_replace($tag, $_POST['tag_degree'], 4, 1);
94
    }
95
    if (array_key_exists("tag_shape", $_POST) && $_POST['tag_shape'] != "") {
96
        $tag = substr_replace($tag, $_POST['tag_shape'], 5, 1);
97
    }
98
    if (array_key_exists("tag_num", $_POST) && $_POST['tag_num'] != "") {
99
        $tag = substr_replace($tag, $_POST['tag_num'], 6, 1);
100
    }
101
    if (array_key_exists("tag_sentence", $_POST) && $_POST['tag_sentence'] != "") {
102
        $tag = substr_replace($tag, $_POST['tag_sentence'], 7, 1);
103
    }
104
    if (array_key_exists("tag_verb_person", $_POST) && $_POST['tag_verb_person'] != "") {
105
        $tag = substr_replace($tag, $_POST['tag_verb_person'], 8, 1);
106
    }
107
    if (array_key_exists("tag_verb_time", $_POST) && $_POST['tag_verb_time'] != "") {
108
        $tag = substr_replace($tag, $_POST['tag_verb_time'], 9, 1);
109
    }
110
    if (array_key_exists("tag_verb_degree", $_POST) && $_POST['tag_verb_degree'] != "") {
111
        $tag = substr_replace($tag, $_POST['tag_verb_degree'], 10, 1);
112
    }
113
    if (array_key_exists("tag_verb_aspect", $_POST) && $_POST['tag_verb_aspect'] != "") {
114
        $tag = substr_replace($tag, $_POST['tag_verb_aspect'], 11, 1);
115
    }
116
    $array['tag'] = $tag;
117
    $whereQuery .= " t.tag LIKE :tag";
118
    return $whereQuery;
119
}
application/model/DB.php
23 23
    }
24 24

  
25 25
    // (C) RUN A SELECT QUERY
26
    function select($sql, $cond=null){
26
    function select($sql, $params){
27 27
        $result = false;
28 28
        try {
29 29
            $this->stmt = $this->pdo->prepare($sql);
30
            $this->stmt->execute($cond);
30
            if (array_key_exists("lemma", $params)) {
31
                $this->stmt->bindParam(':lemma',$params['lemma'], PDO::PARAM_STR);
32
            }
33
            if (array_key_exists("word", $params)) {
34
                $this->stmt->bindParam(':word',$params['word'], PDO::PARAM_STR);
35
            }
36
            if (array_key_exists("position1", $params)) {
37
                $this->stmt->bindParam(':position1',$params['position1'], PDO::PARAM_INT);
38
            }
39
            if (array_key_exists("position2", $params)) {
40
                $this->stmt->bindParam(':position2',$params['position2'], PDO::PARAM_INT);
41
            }
42
            if (array_key_exists("positiondetail", $params)) {
43
                $this->stmt->bindParam(':positiondetail',$params['positiondetail'], PDO::PARAM_INT);
44
            }
45
            if (array_key_exists("tag", $params)) {
46
                $this->stmt->bindParam(':tag',$params['tag'], PDO::PARAM_STR);
47
            }
48
            if (array_key_exists("manuscript", $params)) {
49
                for ($x = 0; $x < count($params["manuscript"]); $x += 1) {
50
                    $this->stmt->bindParam(':manuscript'.$x,$params["manuscript"][$x], PDO::PARAM_INT);
51
                }
52
            }
53
            $this->stmt->execute();
31 54
            $result = $this->stmt->fetchAll();
32 55
            return $result;
33 56
        } catch (Exception $ex) {
......
39 62

  
40 63
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
41 64
define('DB_HOST', 'localhost');
42
define('DB_NAME', 'dalimil1');
65
define('DB_NAME', 'dalimil2');
43 66
define('DB_CHARSET', 'utf8');
44 67
define('DB_USER', 'postgres');
45 68
define('DB_PASSWORD', 'a');
application/model/Word.php
15 15
    public $word;
16 16
    public $lemma;
17 17
    public $tag;
18
    public $manuscript = Array();
18 19

  
19 20
    function __construct($id, $context, $date, $description, $description2, $description3, $ending, $finished, $namedentity, $position1, $position2, $positiondetail, $word, $lemma, $tag)
20 21
    {
application/view/Index.php
6 6
    function doSearch () {
7 7
        // (A1) GET SEARCH TERM
8 8
        var data = new FormData();
9
        // (A2) AJAX - USE HTTP:// NOT FILE://
9
        data.append("manuscript", "0,3"); //příklad pro filtraci manuscriptů, asi se ještě upraví
10
        // data.append("lemma", "ok");
11
        // data.append("tag_pos", "V");
12
        // data.append("tag_verb_aspect", "P");
13
        // data.append("tag_number", "S");
14
        // data.append("word", "okáza");
10 15
        var xhr = new XMLHttpRequest();
11 16
        xhr.open("POST", "../controller/TableController.php");
12 17
        xhr.onload = function(){
13 18
            let results = document.getElementById("results"),
14
                search = this.response;
19
                search = JSON.parse(this.response);
15 20
            results.innerHTML = "";
16 21
            console.log(search);
17
            let parsedJSON = JSON.parse(search);
18
            console.log(parsedJSON);
19
            if (parsedJSON != null) { for (let s of parsedJSON) {
20
                results.innerHTML += "<div>" + s.id + "</div>";
21
            }}
22 22
        };
23 23
        xhr.send(data);
24 24
        return false;

Také k dispozici: Unified diff