1 |
4
|
ryan
|
<?php
|
2 |
|
|
|
3 |
19
|
stefan
|
// $Id$
|
4 |
4
|
ryan
|
|
5 |
|
|
/*
|
6 |
|
|
|
7 |
|
|
Website Baker Project <http://www.websitebaker.org/>
|
8 |
519
|
Ruebenwurz
|
Copyright (C) 2004-2008, Ryan Djurovich
|
9 |
4
|
ryan
|
|
10 |
|
|
Website Baker is free software; you can redistribute it and/or modify
|
11 |
|
|
it under the terms of the GNU General Public License as published by
|
12 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
13 |
|
|
(at your option) any later version.
|
14 |
|
|
|
15 |
|
|
Website Baker is distributed in the hope that it will be useful,
|
16 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
|
|
GNU General Public License for more details.
|
19 |
|
|
|
20 |
|
|
You should have received a copy of the GNU General Public License
|
21 |
|
|
along with Website Baker; if not, write to the Free Software
|
22 |
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23 |
|
|
|
24 |
|
|
*/
|
25 |
|
|
|
26 |
|
|
/*
|
27 |
|
|
|
28 |
|
|
Ordering class
|
29 |
|
|
|
30 |
|
|
This class will be used to change the order of an item in a table
|
31 |
|
|
which contains a special order field (type must be integer)
|
32 |
|
|
|
33 |
|
|
*/
|
34 |
|
|
|
35 |
|
|
// Stop this file from being accessed directly
|
36 |
19
|
stefan
|
if(!defined('WB_URL')) {
|
37 |
|
|
header('Location: ../index.php');
|
38 |
286
|
stefan
|
exit(0);
|
39 |
19
|
stefan
|
}
|
40 |
4
|
ryan
|
|
41 |
|
|
define('ORDERING_CLASS_LOADED', true);
|
42 |
|
|
|
43 |
|
|
// Load the other required class files if they are not already loaded
|
44 |
|
|
require_once(WB_PATH."/framework/class.database.php");
|
45 |
|
|
|
46 |
|
|
class order {
|
47 |
|
|
|
48 |
|
|
// Get the db values
|
49 |
|
|
function order($table, $order_field, $id_field = 'id', $common_field) {
|
50 |
|
|
$this->table = $table;
|
51 |
|
|
$this->order_field = $order_field;
|
52 |
|
|
$this->id_field = $id_field;
|
53 |
|
|
$this->common_field = $common_field;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
// Move a row up
|
57 |
|
|
function move_up($id) {
|
58 |
|
|
global $database;
|
59 |
|
|
// Get current order
|
60 |
|
|
$query_order = "SELECT ".$this->order_field.",".$this->common_field." FROM ".$this->table." WHERE ".$this->id_field." = '$id'";
|
61 |
|
|
$get_order = $database->query($query_order);
|
62 |
|
|
$fetch_order = $get_order->fetchRow();
|
63 |
|
|
$order = $fetch_order[$this->order_field];
|
64 |
|
|
$parent = $fetch_order[$this->common_field];
|
65 |
|
|
// Find out what row is before current one
|
66 |
|
|
$query_previous = "SELECT ".$this->id_field.",".$this->order_field." FROM ".$this->table." WHERE ".$this->order_field." < '$order' AND ".$this->common_field." = '$parent' ORDER BY ".$this->order_field." DESC LIMIT 1";
|
67 |
|
|
$get_previous = $database->query($query_previous);
|
68 |
|
|
if($get_previous->numRows() > 0) {
|
69 |
|
|
// Change the previous row to the current order
|
70 |
|
|
$fetch_previous = $get_previous->fetchRow();
|
71 |
|
|
$previous_id = $fetch_previous[$this->id_field];
|
72 |
|
|
$decremented_order = $fetch_previous[$this->order_field];
|
73 |
|
|
$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$order' WHERE ".$this->id_field." = '$previous_id' LIMIT 1";
|
74 |
|
|
$database->query($query);
|
75 |
|
|
// Change the row we want to the decremented order
|
76 |
|
|
$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$decremented_order' WHERE ".$this->id_field." = '$id' LIMIT 1";
|
77 |
|
|
$database->query($query);
|
78 |
|
|
|
79 |
|
|
if($database->is_error()) {
|
80 |
|
|
return false;
|
81 |
|
|
} else {
|
82 |
|
|
return true;
|
83 |
|
|
}
|
84 |
|
|
} else {
|
85 |
|
|
return false;
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
// Move a row up
|
89 |
|
|
function move_down($id) {
|
90 |
|
|
global $database;
|
91 |
|
|
// Get current order
|
92 |
|
|
$query_order = "SELECT ".$this->order_field.",".$this->common_field." FROM ".$this->table." WHERE ".$this->id_field." = '$id'";
|
93 |
|
|
$get_order = $database->query($query_order);
|
94 |
|
|
$fetch_order = $get_order->fetchRow();
|
95 |
|
|
$order = $fetch_order[$this->order_field];
|
96 |
|
|
$parent = $fetch_order[$this->common_field];
|
97 |
|
|
// Find out what row is before current one
|
98 |
|
|
$query_next = "SELECT $this->id_field,".$this->order_field." FROM ".$this->table." WHERE ".$this->order_field." > '$order' AND ".$this->common_field." = '$parent' ORDER BY ".$this->order_field." ASC LIMIT 1";
|
99 |
|
|
$get_next = $database->query($query_next);
|
100 |
|
|
if($get_next->numRows() > 0) {
|
101 |
|
|
// Change the previous row to the current order
|
102 |
|
|
$fetch_next = $get_next->fetchRow();
|
103 |
|
|
$next_id = $fetch_next[$this->id_field];
|
104 |
|
|
$incremented_order = $fetch_next[$this->order_field];
|
105 |
|
|
$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$order' WHERE ".$this->id_field." = '$next_id' LIMIT 1";
|
106 |
|
|
$database->query($query);
|
107 |
|
|
// Change the row we want to the decremented order
|
108 |
|
|
$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$incremented_order' WHERE ".$this->id_field." = '$id' LIMIT 1";
|
109 |
|
|
$database->query($query);
|
110 |
|
|
if($database->is_error()) {
|
111 |
|
|
return false;
|
112 |
|
|
} else {
|
113 |
|
|
return true;
|
114 |
|
|
}
|
115 |
|
|
} else {
|
116 |
|
|
return false;
|
117 |
|
|
}
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
// Get new number for order
|
121 |
|
|
function get_new($cf_value) {
|
122 |
|
|
global $database;
|
123 |
|
|
$database = new database();
|
124 |
|
|
// Get last order
|
125 |
|
|
$query_last = "SELECT ".$this->order_field." FROM ".$this->table." WHERE ".$this->common_field." = '$cf_value' ORDER BY ".$this->order_field." DESC LIMIT 1";
|
126 |
|
|
$get_last = $database->query($query_last);
|
127 |
|
|
if($get_last->numRows() > 0) {
|
128 |
|
|
$fetch_last = $get_last->fetchRow();
|
129 |
|
|
$last_order = $fetch_last[$this->order_field];
|
130 |
|
|
return $last_order+1;
|
131 |
|
|
} else {
|
132 |
|
|
return 1;
|
133 |
|
|
}
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
// Clean ordering (should be called if a row in the middle has been deleted)
|
137 |
|
|
function clean($cf_value) {
|
138 |
|
|
global $database;
|
139 |
|
|
// Loop through all records and give new order
|
140 |
|
|
$query_all = "SELECT * FROM ".$this->table." WHERE ".$this->common_field." = '$cf_value' ORDER BY ".$this->order_field." ASC";
|
141 |
|
|
$get_all = $database->query($query_all);
|
142 |
|
|
if($get_all->numRows() > 0) {
|
143 |
|
|
$count = 1;
|
144 |
|
|
while($row = $get_all->fetchRow()) {
|
145 |
|
|
// Update row with new order
|
146 |
|
|
$database->query("UPDATE ".$this->table." SET ".$this->order_field." = '$count' WHERE ".$this->id_field." = '".$row[$this->id_field]."'");
|
147 |
|
|
$count = $count+1;
|
148 |
|
|
}
|
149 |
|
|
} else {
|
150 |
|
|
return true;
|
151 |
|
|
}
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
?>
|