1
|
<?php
|
2
|
|
3
|
// $Id: class.order.php 19 2005-09-04 23:18:58Z stefan $
|
4
|
|
5
|
/*
|
6
|
|
7
|
Website Baker Project <http://www.websitebaker.org/>
|
8
|
Copyright (C) 2004-2005, Ryan Djurovich
|
9
|
|
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
|
if(!defined('WB_URL')) {
|
37
|
header('Location: ../index.php');
|
38
|
}
|
39
|
|
40
|
define('ORDERING_CLASS_LOADED', true);
|
41
|
|
42
|
// Load the other required class files if they are not already loaded
|
43
|
require_once(WB_PATH."/framework/class.database.php");
|
44
|
|
45
|
class order {
|
46
|
|
47
|
// Get the db values
|
48
|
function order($table, $order_field, $id_field = 'id', $common_field) {
|
49
|
$this->table = $table;
|
50
|
$this->order_field = $order_field;
|
51
|
$this->id_field = $id_field;
|
52
|
$this->common_field = $common_field;
|
53
|
}
|
54
|
|
55
|
// Move a row up
|
56
|
function move_up($id) {
|
57
|
global $database;
|
58
|
// Get current order
|
59
|
$query_order = "SELECT ".$this->order_field.",".$this->common_field." FROM ".$this->table." WHERE ".$this->id_field." = '$id'";
|
60
|
$get_order = $database->query($query_order);
|
61
|
$fetch_order = $get_order->fetchRow();
|
62
|
$order = $fetch_order[$this->order_field];
|
63
|
$parent = $fetch_order[$this->common_field];
|
64
|
// Find out what row is before current one
|
65
|
$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";
|
66
|
$get_previous = $database->query($query_previous);
|
67
|
if($get_previous->numRows() > 0) {
|
68
|
// Change the previous row to the current order
|
69
|
$fetch_previous = $get_previous->fetchRow();
|
70
|
$previous_id = $fetch_previous[$this->id_field];
|
71
|
$decremented_order = $fetch_previous[$this->order_field];
|
72
|
$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$order' WHERE ".$this->id_field." = '$previous_id' LIMIT 1";
|
73
|
$database->query($query);
|
74
|
// Change the row we want to the decremented order
|
75
|
$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$decremented_order' WHERE ".$this->id_field." = '$id' LIMIT 1";
|
76
|
$database->query($query);
|
77
|
|
78
|
if($database->is_error()) {
|
79
|
return false;
|
80
|
} else {
|
81
|
return true;
|
82
|
}
|
83
|
} else {
|
84
|
return false;
|
85
|
}
|
86
|
}
|
87
|
// Move a row up
|
88
|
function move_down($id) {
|
89
|
global $database;
|
90
|
// Get current order
|
91
|
$query_order = "SELECT ".$this->order_field.",".$this->common_field." FROM ".$this->table." WHERE ".$this->id_field." = '$id'";
|
92
|
$get_order = $database->query($query_order);
|
93
|
$fetch_order = $get_order->fetchRow();
|
94
|
$order = $fetch_order[$this->order_field];
|
95
|
$parent = $fetch_order[$this->common_field];
|
96
|
// Find out what row is before current one
|
97
|
$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";
|
98
|
$get_next = $database->query($query_next);
|
99
|
if($get_next->numRows() > 0) {
|
100
|
// Change the previous row to the current order
|
101
|
$fetch_next = $get_next->fetchRow();
|
102
|
$next_id = $fetch_next[$this->id_field];
|
103
|
$incremented_order = $fetch_next[$this->order_field];
|
104
|
$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$order' WHERE ".$this->id_field." = '$next_id' LIMIT 1";
|
105
|
$database->query($query);
|
106
|
// Change the row we want to the decremented order
|
107
|
$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$incremented_order' WHERE ".$this->id_field." = '$id' LIMIT 1";
|
108
|
$database->query($query);
|
109
|
if($database->is_error()) {
|
110
|
return false;
|
111
|
} else {
|
112
|
return true;
|
113
|
}
|
114
|
} else {
|
115
|
return false;
|
116
|
}
|
117
|
}
|
118
|
|
119
|
// Get new number for order
|
120
|
function get_new($cf_value) {
|
121
|
global $database;
|
122
|
$database = new database();
|
123
|
// Get last order
|
124
|
$query_last = "SELECT ".$this->order_field." FROM ".$this->table." WHERE ".$this->common_field." = '$cf_value' ORDER BY ".$this->order_field." DESC LIMIT 1";
|
125
|
$get_last = $database->query($query_last);
|
126
|
if($get_last->numRows() > 0) {
|
127
|
$fetch_last = $get_last->fetchRow();
|
128
|
$last_order = $fetch_last[$this->order_field];
|
129
|
return $last_order+1;
|
130
|
} else {
|
131
|
return 1;
|
132
|
}
|
133
|
}
|
134
|
|
135
|
// Clean ordering (should be called if a row in the middle has been deleted)
|
136
|
function clean($cf_value) {
|
137
|
global $database;
|
138
|
// Loop through all records and give new order
|
139
|
$query_all = "SELECT * FROM ".$this->table." WHERE ".$this->common_field." = '$cf_value' ORDER BY ".$this->order_field." ASC";
|
140
|
$get_all = $database->query($query_all);
|
141
|
if($get_all->numRows() > 0) {
|
142
|
$count = 1;
|
143
|
while($row = $get_all->fetchRow()) {
|
144
|
// Update row with new order
|
145
|
$database->query("UPDATE ".$this->table." SET ".$this->order_field." = '$count' WHERE ".$this->id_field." = '".$row[$this->id_field]."'");
|
146
|
$count = $count+1;
|
147
|
}
|
148
|
} else {
|
149
|
return true;
|
150
|
}
|
151
|
}
|
152
|
|
153
|
}
|
154
|
|
155
|
?>
|