Project

General

Profile

1
<?php
2

    
3
// $Id: class.order.php 10 2005-09-04 08:59:31Z ryan $
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_PATH')) { exit('Direct access to this file is not allowed'); }
37

    
38
define('ORDERING_CLASS_LOADED', true);
39

    
40
// Load the other required class files if they are not already loaded
41
require_once(WB_PATH."/framework/class.database.php");
42

    
43
class order {
44
	
45
	// Get the db values
46
	function order($table, $order_field, $id_field = 'id', $common_field) {
47
		$this->table = $table;
48
		$this->order_field = $order_field;
49
		$this->id_field = $id_field;
50
		$this->common_field = $common_field;
51
	}
52
	
53
	// Move a row up
54
	function move_up($id) {
55
		global $database;
56
		// Get current order
57
		$query_order = "SELECT ".$this->order_field.",".$this->common_field." FROM ".$this->table." WHERE ".$this->id_field." = '$id'";
58
		$get_order = $database->query($query_order);
59
		$fetch_order = $get_order->fetchRow();
60
		$order = $fetch_order[$this->order_field];
61
		$parent = $fetch_order[$this->common_field];
62
		// Find out what row is before current one
63
		$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";
64
		$get_previous = $database->query($query_previous);
65
		if($get_previous->numRows() > 0) {
66
			// Change the previous row to the current order
67
			$fetch_previous = $get_previous->fetchRow();
68
			$previous_id = $fetch_previous[$this->id_field];
69
			$decremented_order = $fetch_previous[$this->order_field];
70
			$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$order' WHERE ".$this->id_field." = '$previous_id' LIMIT 1";
71
			$database->query($query);
72
			// Change the row we want to the decremented order
73
			$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$decremented_order' WHERE ".$this->id_field." = '$id' LIMIT 1";
74
			$database->query($query);
75
			
76
			if($database->is_error()) {
77
				return false;
78
			} else {
79
				return true;
80
			}
81
		} else {
82
			return false;
83
		}
84
	}
85
	// Move a row up
86
	function move_down($id) {
87
		global $database;
88
		// Get current order
89
		$query_order = "SELECT ".$this->order_field.",".$this->common_field." FROM ".$this->table." WHERE ".$this->id_field." = '$id'";
90
		$get_order = $database->query($query_order);
91
		$fetch_order = $get_order->fetchRow();
92
		$order = $fetch_order[$this->order_field];
93
		$parent = $fetch_order[$this->common_field];
94
		// Find out what row is before current one
95
		$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";
96
		$get_next = $database->query($query_next);
97
		if($get_next->numRows() > 0) {
98
			// Change the previous row to the current order
99
			$fetch_next = $get_next->fetchRow();
100
			$next_id = $fetch_next[$this->id_field];
101
			$incremented_order = $fetch_next[$this->order_field];
102
			$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$order' WHERE ".$this->id_field." = '$next_id' LIMIT 1";
103
			$database->query($query);
104
			// Change the row we want to the decremented order
105
			$query = "UPDATE ".$this->table." SET ".$this->order_field." = '$incremented_order' WHERE ".$this->id_field." = '$id' LIMIT 1";
106
			$database->query($query);
107
			if($database->is_error()) {
108
				return false;
109
			} else {
110
				return true;
111
			}
112
		} else {
113
			return false;
114
		}
115
	}
116
	
117
	// Get new number for order
118
	function get_new($cf_value) {
119
		global $database;
120
		$database = new database();
121
		// Get last order
122
		$query_last = "SELECT ".$this->order_field." FROM ".$this->table." WHERE ".$this->common_field." = '$cf_value' ORDER BY ".$this->order_field." DESC LIMIT 1";
123
		$get_last = $database->query($query_last);
124
		if($get_last->numRows() > 0) {
125
			$fetch_last = $get_last->fetchRow();
126
			$last_order = $fetch_last[$this->order_field];
127
			return $last_order+1;
128
		} else {
129
			return 1;
130
		}
131
	}
132
	
133
	// Clean ordering (should be called if a row in the middle has been deleted)
134
	function clean($cf_value) {
135
		global $database;
136
		// Loop through all records and give new order
137
		$query_all = "SELECT * FROM ".$this->table." WHERE ".$this->common_field." = '$cf_value' ORDER BY ".$this->order_field." ASC";
138
		$get_all = $database->query($query_all);
139
		if($get_all->numRows() > 0) {
140
			$count = 1;
141
			while($row = $get_all->fetchRow()) {
142
				// Update row with new order
143
				$database->query("UPDATE ".$this->table." SET ".$this->order_field." = '$count' WHERE ".$this->id_field." = '".$row[$this->id_field]."'");
144
				$count = $count+1;
145
			}
146
		} else {
147
			 return true;
148
		}
149
	}
150
	
151
}
152

    
153
?>
(5-5/11)