phpBMS

root/trunk/phpbms/advancedsort.php

Revision 769, 12.6 KB (checked in by brieb, 2 years ago)
  • Fixes #407 - Various typos and syntax warnings
  • Property svn:keywords set to LastChangedBy LastChangedDate LastChangedRevision
Line 
1<?php
2/*
3 $Rev$ | $LastChangedBy$
4 $LastChangedDate$
5 +-------------------------------------------------------------------------+
6 | Copyright (c) 2004 - 2010, Kreotek LLC                                  |
7 | All rights reserved.                                                    |
8 +-------------------------------------------------------------------------+
9 |                                                                         |
10 | Redistribution and use in source and binary forms, with or without      |
11 | modification, are permitted provided that the following conditions are  |
12 | met:                                                                    |
13 |                                                                         |
14 | - Redistributions of source code must retain the above copyright        |
15 |   notice, this list of conditions and the following disclaimer.         |
16 |                                                                         |
17 | - Redistributions in binary form must reproduce the above copyright     |
18 |   notice, this list of conditions and the following disclaimer in the   |
19 |   documentation and/or other materials provided with the distribution.  |
20 |                                                                         |
21 | - Neither the name of Kreotek LLC nor the names of its contributore may |
22 |   be used to endorse or promote products derived from this software     |
23 |   without specific prior written permission.                            |
24 |                                                                         |
25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS     |
26 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT       |
27 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
28 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      |
29 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   |
30 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT        |
31 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,   |
32 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY   |
33 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT     |
34 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   |
35 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    |
36 |                                                                         |
37 +-------------------------------------------------------------------------+
38*/
39    require("include/session.php");
40
41    class advancedSort{
42
43        var $db;
44        var $tabledefid;
45        var $tabledefuuid;
46
47        function advancedSort($db, $tabledefid){
48
49            $this->db = $db;
50            $this->tabledefid = $tabledefid;
51            $this->tabledefuuid = getUuid($this->db, "tbld:5c9d645f-26ab-5003-b98e-89e9049f8ac3", ((int) $tabledefid));
52
53            $querystatement = "
54                SELECT
55                    `prefix`
56                FROM
57                    `tabledefs`
58                WHERE
59                    `uuid` = '".$this->tabledefuuid."'
60            ";
61
62            $queryresult = $this->db->query($querystatement);
63
64            $therecord = $this->db->fetchArray($queryresult);
65            $this->prefix = $therecord["prefix"];
66
67        }//end function init
68
69
70        function load($id){
71
72            $querystatement = "
73                SELECT
74                    sqlclause
75                FROM
76                    usersearches
77                WHERE
78                    id = ".((int) $id);
79
80            $queryresult = $this->db->query($querystatement);
81
82            $therecord = $this->db->fetchArray($queryresult);
83
84            echo $therecord["sqlclause"];
85
86        }//end function load
87
88
89        function delete($id){
90
91            $deletestatement = "
92                DELETE FROM
93                    usersearches
94                WERE
95                    id = ".((int) $id);
96
97            $this->db->query($deletestatement);
98
99            echo "success";
100
101        }//end function delete
102
103
104        function showList($queryresult){
105
106            $numrows = $this->db->numRows($queryresult);
107
108            ?>
109            <select id="sortSavedList" name="sortSavedList" <?php if ($numrows<1) echo "disabled" ?> size="10" style="width:99%" onchange="sortSavedSelect(this)">
110            <?php
111
112                if($numrows<1){
113
114            ?>
115                <option value="NA">No Saved Sorts</option>
116            <?php
117
118                } else {
119
120                    $numglobal=0;
121                    while($therecord = $this->db->fetchArray($queryresult))
122                        if($therecord["userid"] == "")
123                            $numglobal++;
124
125                    $this->db->seek($queryresult,0);
126
127                    if($numglobal>0){
128
129                    ?>
130                        <option value="NA" style="font-style:italic;font-weight:bold"> -- global sorts ---------</option>
131                    <?php
132
133                    }//end if
134
135                    $userqueryline = true;
136
137                    while($therecord = $this->db->fetchArray($queryresult)){
138
139                        if ($therecord["userid"]> 0 and $userqueryline) {
140
141                            $userqueryline = false;
142                            ?><option value="NA" style="font-style:italic;font-weight:bold"> -- user sorts---------</option><?php
143
144                        }//end if
145
146                        ?><option value="<?php echo $therecord["id"]?>"><?php echo $therecord["name"]?></option><?php
147
148                    }// end while
149
150                }//end if
151
152            ?>
153            </select>
154            <?php
155
156        }//end function showList
157
158
159        function showSaved($userid){
160
161            $querystatement = "
162                SELECT
163                    id,
164                    name,
165                    userid
166                FROM
167                    usersearches
168                WHERE
169                    tabledefid = '".$this->tabledefuuid."'
170                    AND type = 'SRT'
171                    AND (userid = '' OR userid IS NULL OR userid = '".$userid."')
172                ORDER BY
173                    userid,
174                    name";
175
176            $queryresult = $this->db->query($querystatement);
177
178            ?>
179            <p>
180                <label for="sortSavedList">saved sorts</label><br />
181                <?php $this->showList($queryresult)?>
182            </p>
183            <p align="right" class="buttonsRight">
184                <input type="button" class="Buttons" style="width:75px;" id="sortSavedDeleteButton" value="delete" disabled="disabled" onclick="sortSavedDelete('<?php echo APP_PATH ?>')"/>
185                <input type="button" class="Buttons" style="width:75px;" id="sortSavedLoadButton" value="load" disabled="disabled" onclick="sortSavedLoad('<?php echo APP_PATH ?>')"/>
186                <input type="button" class="Buttons" style="width:75px;" id="sortSavedCancelButton" value="cancel" onclick="closeModal()"/>
187            </p>
188            <?php
189
190        }//end function showSaved
191
192
193        function save($name, $sqlclause, $userid){
194
195            $insertstatement = "
196                INSERT INTO
197                    `usersearches`
198                (
199                    userid,
200                    tabledefid,
201                    `name`,
202                    `type`,
203                    `sqlclause`,
204                    `uuid`
205                ) VALUES (
206                    '".mysql_real_escape_string($userid)."',
207                    '".mysql_real_escape_string($this->tabledefuuid)."',
208                    '".mysql_real_escape_string($name)."',
209                    'SRT',
210                    '".mysql_real_escape_string($sqlclause)."',
211                    '".uuid($this->prefix.":")."'
212                )";
213
214            $this->db->query($insertstatement);
215
216            echo "success";
217
218        }//end function save
219
220
221        function showUI(){
222
223            //First, grab table name from id
224            $querystatement = "
225                SELECT
226                    querytable
227                FROM
228                    tabledefs
229                WHERE
230                    uuid = '".$this->tabledefuuid."'";
231
232            $queryresult = $this->db->query($querystatement);
233
234            if(!$queryresult)
235                $error = new appError(500,"Cannot retrieve Table Information");
236
237            $thetabledef = $this->db->fetchArray($queryresult);
238
239            //Grab query for all columns
240            $querystatement = "
241                SELECT
242                    *
243                FROM
244                    ".$thetabledef["querytable"]."
245                LIMIT 1";
246
247                $queryresult = $this->db->query($querystatement);
248
249                if(!$queryresult)
250                    $error = new appError(500,"Cannot retrieve Table Information");
251
252                $numfields = $this->db->numFields($queryresult);
253
254                for ($i=0;$i<$numfields;$i++)
255                    $fieldlist[] = $this->db->fieldTable($queryresult,$i).".".$this->db->fieldName($queryresult,$i);
256
257                ?>
258                <table border="0" cellspacing="0" cellpadding="0">
259                    <tr>
260                        <td valign=top width="99%">
261                            <div id="theSorts">
262                                <div id="Sort1">
263                                    <select id="Sort1Field" onchange="updateSort()">
264                                        <?php
265                                            foreach($fieldlist as $field){
266
267                                                echo '<option value="'.$field.'" >'.$field."</option>\n";
268
269                                            }//endforeach?>
270                                    </select>
271                                    <select id="Sort1Order" onchange="updateSort()">
272                                        <option value="ASC" selected="selected">Ascending</option>
273                                        <option value="DESC">Descending</option>
274                                    </select>
275                                    <button type="button" id="Sort1Up" class="graphicButtons buttonUpDisabled" onclick="sortMove(this,'up')"><span>up</span></button>
276                                    <button type="button" id="Sort1Down" class="graphicButtons buttonDownDisabled" onclick="sortMove(this,'down')"><span>down</span></button>
277                                    <button type="button" id="Sort1Minus" class="graphicButtons buttonMinusDisabled" onclick="sortRemoveLine(this)"><span>-</span></button>
278                                    <button type="button" id="Sort1Plus" class="graphicButtons buttonPlus" onclick="sortAddLine()"><span>+</span></button>
279                                </div>
280                            </div>
281                            <p>
282                                sql order by clause<br/>
283                                <textarea id="sortSQL" style="width:98%;height:75px;" cols="57" rows="4" onkeyup="sortEnableButtons(this)" ></textarea>
284                            </p>
285                        </td>
286                        <td valign=top>
287                            <div style="float:right">
288                                <br/>
289                                <p><input id="sortRunSort" type="button" onclick="performAdvancedSort(this)" class="Buttons" disabled="disabled" value="run sort" style="width:90px;" /></p>
290                                <p><input id="sortLoadSort" type="button" onclick="sortAskLoad('<?php echo APP_PATH?>')" class="Buttons" value="load sort..." style="width:90px;" /></p>
291                                <p><input id="sortSaveSort" type="button" onclick="sortAskSaveName('<?php echo APP_PATH?>')" class="Buttons" disabled="disabled" value="save sort..." style="width:90px;" /></p>
292                                <p><input id="sortClearSort" type="button" onclick="clearSort()" class="Buttons" disabled="disabled" value="clear sort" style="width:90px;" /></p>
293                            </div>
294                        </td>
295                    </tr>
296                </table>
297                <?php
298
299        }//end function showUI
300
301    }//end class advancedSort
302
303
304
305
306/*
307 * PROCESSOR
308 * =============================================================================
309 */
310    if(isset($_GET["cmd"])){
311
312        if(!isset($_GET["tid"]))
313            $_GET["tid"] = "tbld:5c9d645f-26ab-5003-b98e-89e9049f8ac3";
314
315        $sorts = new advancedSort($db, $_GET["tid"]);
316
317        switch($_GET["cmd"]){
318
319            case "show":
320                $sorts->showUI();
321                break;
322
323            case "save":
324                $sorts->save($_GET["name"], $_GET["clause"], $_SESSION["userinfo"]["uuid"]);
325                break;
326
327            case "showSaved":
328                $sorts->showSaved($_SESSION["userinfo"]["uuid"]);
329                break;
330
331            case "deleteSaved":
332                $sorts->delete($_GET["id"]);
333                break;
334
335            case "loadSaved":
336                $sorts->load($_GET["id"]);
337                break;
338
339        }//end switch
340
341    }//endif
342?>
Note: See TracBrowser for help on using the browser.
Scanned by Orvant Copyright © 2010 Kreotek, LLC. All Rights reserved.