Project

General

Profile

Statistics
| Branch: | Revision:

colonymech / docs / www / colonyscout / internal / includes / class.queryboxdb.php @ f59acf11

History | View | Annotate | Download (4.07 KB)

1
<?php
2
        class QueryBoxDB {                
3
                
4
                public $limit = 10;
5
                public $items = array();
6
                /**
7
                 * renders QueryBox
8
                 */
9
                public function render($id='qbox',$queryURL=null){
10
                        if(!$queryURL){$queryURL=$_SERVER['PHP_SELF'].'?q=';};
11
                        echo $this->getMarkUp($id,$queryURL);
12
                }
13
                /**
14
                 * gets the HTML Markup for QueryBox.
15
                 */
16
                public function getMarkUp($id,$queryURL){
17
                        $output = 
18
                        '<script type="text/javascript">' .
19
                        'var '.$id.'         = new marumushi.widget.QueryBox("'.$queryURL.'","'.$id.'");' .
20
                        '</script>'.
21
                        '<div id="'.$id.'"></div>';
22
                        return $output;
23
                }
24
                /**
25
                 * Serves the result of a query in JSON format.
26
                 * Used to send back the results to the Dojo LiveSearchQueryBox
27
                 * */
28
                public function serve() {
29
                        $output = $this->getOutput();
30
                        //header("Content-type: application/json");
31
                        echo $output;
32
                }
33
                /**
34
                 * call this method to add items in the JSON result
35
                 */
36
                public function addItem($item) {
37
                        $this->items[] = $item;
38
                }
39
                /**
40
                 * gets the JSON result output
41
                 */
42
                private function getOutput() {
43
                        //write header
44
                        $out  = '{results:[';
45
                        $c=0;
46
                        //write content
47
                        foreach($this->items as $item) {
48
                                if($c++>0){
49
                                        $out.=',';
50
                                }
51
                                $out .= $item->getOutput();
52
                        }
53
                        //write footer
54
                        $out .= ']}';
55

    
56
                        return $out;
57
                }
58
                public function getResultsMarkup(){
59
                        $out ="<div class='QueryBox'><div class='ResultsPage'>";
60
                        foreach($this->items as $item) {
61
                                $out .= $item->getMarkup();
62
                        }
63
                        $out .="</div></div>";
64
                        return $out;
65
                }
66
                public function renderResultsMarkup(){
67
                        echo $this->getResultsMarkup();
68
                }
69
                /**
70
                 * For the sake of the demo, I've included all data querying in here.
71
                 * You probably want to abstract all this by using any of the super
72
                 * fancy PHP framworks out there.
73
                 * 
74
                 * The important thing is:
75
                 * 1. query the database
76
                 * 2. loop over the results
77
                 * 3. create one result object for each row you found in the DB
78
                 * 4. add that result object to this QueryBoxDB instance using $this->addItem($result);
79
                 * 5. when done call the serve() method to serve the results.
80
                 * 6. boila.
81
                 */
82
                function query($q){
83
                        $this->items = array();
84
                        $db         = mysqli_connect( "localhost","colony_scout_adm","sCH8FdUTs3jRTywC","colony_scout" );
85
                $q                 = mysqli_real_escape_string($db, $q);
86
                        $sql         = 'SELECT * FROM inventory WHERE Name LIKE "%'.$q.'%" OR Description LIKE "'.$q.'%" LIMIT '.$this->limit.';';
87
                        if ($rows = mysqli_query($db,$sql)){
88
                                while( $row = mysqli_fetch_assoc($rows) ){ 
89
                                        $result = new Result();
90
                                        $result->id                                = $row['ID'];
91
                                        $result->title                         = $row['Name']." ".$row['Description'];
92
                                        $result->description         = 'P/N: '.$row['VendorPartNo'];
93
                                        $result->link                         = 'inventory.php?tab=viewpart&q='.$row['VendorPartNo'];
94
                                        $result->category                 = $row['tags'];
95
                                        $this->addItem($result);
96
                                }
97
                                mysqli_free_result($rows); 
98
                        }
99
                        mysqli_close($db); 
100
                }
101
        
102
        }
103

    
104
        /**
105
         * Each result object that will be rendered as a JSON item.
106
         */
107
        class Result {
108
                
109
                public $title;
110
                public $link;
111
                public $description;
112
                public $id;        
113
                public $category;        
114
                /**
115
                 * gets the JSON encoded string representation for this object.
116
                 */
117
                function getOutput() {
118
                        $this->title                 = strip_tags($this->title);
119
                        $this->description         = strip_tags($this->description);
120
                        $this->title                 = $this->smart_trim($this->title,30);
121
                        $this->description         = $this->smart_trim($this->description,35);
122
                        //JSON encode self
123
                        return json_encode($this);
124
                }
125
                /**
126
                 * gets the HTML Markup for this object.
127
                 */
128
                function getMarkup(){
129
                        $str = "<div class='Result'>".
130
                        $str.= "<h3><a href=''>".$this->smart_trim(strip_tags($this->title),60)."</a></h3>";
131
                        $str.= "<p>".$this->smart_trim(strip_tags($this->description),100)."</p>";
132
                        $str.= "</div>";
133
                        return $str;
134
                }
135
                /**
136
                 * you probably don't need this here.
137
                 * Added for the sake of the Demo.
138
                 */
139
                function smart_trim($text, $max_len, $trim_chars = '...') {
140
                        $text = trim($text);
141
                        if (strlen($text) < $max_len) {
142
                                return $text;
143
                        }else{
144
                                $trimmed_text = substr($text, 0, $max_len);
145
                                $trimmed_text = trim($trimmed_text);
146
                                return $trimmed_text.$trim_chars;
147
                        }
148
                } 
149
        }