summaryrefslogtreecommitdiffstats
path: root/lib/class/view.class.php
blob: 4f58677ac042db8c873f753667e47c11a5123f5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/*

 Copyright (c) 2001 - 2006 Ampache.org
 All rights reserved.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

/*!
	@header View Object of crappyness
	View object that is thrown into their session

*/


class View {

	//Basic Componets
	var $base_sql;
	var $offset;
	var $offset_limit;
	var $sort_order; //asc or desc
	var $sort_type;
	var $action;
	var $total_items;

	//generate a new view
	function View($base_sql=0,$script=0,$sort_type=0,$total_items=0,$offset_limit=0) {
		global $conf;

		// If we don't have a base sql, stop here
		if (!is_string($base_sql)) {
			return true;
		}

		//Convert all 's into "s 
		$base_sql = str_replace("'",'"',$base_sql);	

		$this->base_sql = $base_sql;
		if ($offset_limit) { $this->offset_limit = $offset_limit; }
		else { $this->offset_limit = $_SESSION['offset_limit']; }
		if ($this->offset_limit < '1') { $this->offset_limit = '50'; }
		$this->script = $script;
		$this->sort_type = $sort_type;
		$this->sort_order = "ASC";
		$this->offset = 0;		
		$this->total_items = $total_items;

		// Set the session
		$_SESSION['view_offset_limit']  = $this->offset_limit;		
		$_SESSION['view_sort_type']	= $this->sort_type;
		$_SESSION['view_offset'] 	= $this->offset;
		$_SESSION['view_base_sql']	= $this->base_sql;
		$_SESSION['view_sort_order']	= $this->sort_order;
		$_SESSION['view_script']	= $this->script;
		$_SESSION['view_total_items']	= $this->total_items;
		$this->sql = $this->generate_sql();

	} //constructor

	//takes all the parts and makes a full blown sql statement
	function generate_sql() {
		global $conf;
			
		$sql = $this->base_sql . " ORDER BY " . $this->sort_type ." ". $this->sort_order ." LIMIT " . $this->offset . "," . $this->offset_limit;
	
		return $sql;
	
	} //generate_sql

	//change the sort order from asc to desc or vise versa
	function change_sort($new_sort=0) {
		global $conf;

		if ($new_sort) {
			$this->sort_order = $new_sort;
		}
		elseif ($this->sort_order == "DESC") {
			$this->sort_order = "ASC";
		}
		else {
			$this->sort_order = "DESC";
		}
		
		$_SESSION['view_sort_order'] = $this->sort_order;

		$this->sql = $this->generate_sql();

	return;

	} //change_sort

	//change the base sql
	function change_sql($base_sql) {
		global $conf;

		//Convert all 's into "s 
		$base_sql = str_replace("'",'"',$base_sql);	

		$this->base_sql = $base_sql;

		$_SESSION['view_base_sql'] = $this->base_sql;
	
		$this->sql = $this->generate_sql();

	} //change_sql

	//change offset
	function change_offset($offset=0) {
		global $conf;

		if (isset($offset)) {
			$this->offset = $offset;
		}
		else {
			$this->offset = $this->offset + $this->offset_limit;
		}

		$_SESSION['view_offset'] = $this->offset;

		$this->sql = $this->generate_sql();

	} //change_offset

	//change sort_type
	function change_sort_type($sort_type) {

		$this->sort_type = $sort_type;

		$_SESSION['view_sort_type'] = $this->sort_type;

		$this->sql = $this->generate_sql();

	} //change_sort_type

	/*!
		@function change_offset_limit
		@discussion changes the offset limit, sets the session
			    var and generates the sql statement
	*/
	function change_offset_limit($offset_limit) {

		$this->offset_limit = $offset_limit;

		$_SESSION['view_offset_limit'] = $this->offset_limit;

		$this->sql = $this->generate_sql();

	} // change_offset_limit

	/*!
		@function initialize
		@discussion initializes the view object, checks $_REQUEST
			    for changes to the view object
	*/
	function initialize($sql='') {

		/* From time to time we need to change the SQL statement while 
		 * maintaining the paging 
		 */
		if ($sql) { 
			$this->change_sql($sql);
		}

		if ($_REQUEST['sort_type']) {
			$this->change_sort_type($_REQUEST['sort_type']);
		}

		if (isset($_REQUEST['offset'])) {
			$this->change_offset($_REQUEST['offset']);
		}

		if ($_REQUEST['base_sql']) {
			$this->change_sql($_REQUEST['base_sql']);
		}

		if (isset($_REQUEST['sort_order'])) {
			$this->change_sort($_REQUEST['sort_order']);
		}

		if ($_REQUEST['offset_limit']) {
			$this->change_offset_limit($_REQUEST['offset_limit']);
		}

	} // initialize


	/*!
		@function import_session_view
		@discussion this imports the view from the session for use..
			    this keeps us from having to globalize anything
			    wohoo!
	*/
	function import_session_view() {

		$this->sort_type 	= $_SESSION['view_sort_type'];
		$this->offset		= $_SESSION['view_offset'];
		$this->base_sql		= $_SESSION['view_base_sql'];
		$this->sort_order	= $_SESSION['view_sort_order'];
		$this->script		= $_SESSION['view_script'];
		$this->total_items	= $_SESSION['view_total_items'];


		if ($_SESSION['view_offset_limit']) {
			$this->offset_limit	= $_SESSION['view_offset_limit'];
		} 
		else {
			$this->offset_limit	= $_SESSION['offset_limit'];
		}


		$this->sql = $this->generate_sql();

	} // import_session_view

		

} //end class
?>