File: functions.php

package info (click to toggle)
opendb 0.81p18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,716 kB
  • ctags: 6,787
  • sloc: php: 50,213; sql: 3,098; sh: 272; makefile: 54; xml: 48
file content (305 lines) | stat: -rw-r--r-- 9,490 bytes parent folder | download
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
/* 	OpenDb - Open Media Lending Database
	Copyright (C) 2001,2002 by Jason Pell

	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.
*/

include_once("./functions/status_type.php");
include_once("./functions/user.php");

/*
* If more than one default_ind=Y record, then the one being updated,can be updated
* safely.
*/
function fetch_default_status_type_cnt()
{
	$query = "SELECT count('x') as count FROM s_status_type WHERE closed_ind <> 'Y' AND default_ind = 'Y'";
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		$found = mysql_fetch_array($result, MYSQL_ASSOC);
		mysql_free_result($result);
		if ($found!== FALSE)
			return $found['count'];
	}

	//else
	return FALSE;
}

function is_exists_items_with_status_type($s_status_type)
{
	$query = "SELECT 'x' FROM item_instance WHERE s_status_type = '".$s_status_type."'";
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		mysql_free_result($result);
		return TRUE;
	}

	//else
	return FALSE;
}

function is_exists_items_for_user_type_and_status_type($s_status_type, $user_type)
{
	$query = "SELECT 'x' ".
			"FROM item_instance ii, user u ".
			"WHERE ii.owner_id = u.user_id AND ".
			"ii.s_status_type = '$s_status_type' AND ".
			"IF(LENGTH(type)>0,u.type,'N') IN(".format_sql_in_clause(get_min_user_type_r($user_type)).")";
	
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		mysql_free_result($result);
		return TRUE;
	}

	//else
	return FALSE;
}

function is_exists_borrowed_items_for_status_type($s_status_type, $borrowed_items_only = FALSE)
{
	$query = "SELECT 'x' ".
			"FROM item_instance ii, borrowed_item bi ".
			"WHERE ii.item_id = bi.item_id AND ".
			"ii.instance_no = bi.instance_no AND ".
			"ii.s_status_type = '$s_status_type' ";

	if($borrowed_items_only)
	{
		$query .= "AND bi.status = 'B'";
	}
	
	$result = run_opendb_query($query);
	if($result && mysql_num_rows($result)>0)
	{
		mysql_free_result($result);
		return TRUE;
	}

	//else
	return FALSE;
}

function validate_ind_column($column, $options_r=NULL)
{
	$column = strtoupper($column);
	
	if(is_array($options_r) && array_search2($column, $options_r)!==FALSE)
		return $column;
	else if($column == 'Y')
		return 'Y';
	else
		return 'N';
}

/*
* A single user type is all that is allowed.
*/
function validate_user_type_column($column)
{
	if(strlen(trim($column))>0)
	{
		if(is_usertype_valid(trim($column)))
			return trim($column);
		else
			return FALSE;
	}
	else
		return "";
}

function insert_s_status_type($s_status_type, $description, $img, 
							$insert_ind, $update_ind, $delete_ind, $change_owner_ind, 
							$min_display_user_type,	$min_create_user_type,
							$new_owner_instance_ind, $new_not_owner_instance_ind,
							$borrow_ind, $status_comment_ind, $default_ind)
{
	global $HTTP_SESSION_VARS;
	
	$s_status_type = strtoupper(substr(trim($s_status_type),0,1));
	
	// this should never happen.
	if(strlen(trim($s_status_type))==0)
	{
		return FALSE;
	}
	
	$description = addslashes(trim(strip_tags($description)));

	// do this one first, as we need to validate the data for the others based on this one.
	$change_owner_ind = validate_ind_column($change_owner_ind);

	$insert_ind = validate_ind_column($insert_ind);
	$update_ind = validate_ind_column($update_ind);
	$delete_ind = validate_ind_column($delete_ind);
	
	$min_display_user_type = validate_user_type_column($min_display_user_type);
	
	if($change_owner_ind == 'Y')
		$min_create_user_type = 'N'; // normal user minimum
	else
		$min_create_user_type = validate_user_type_column($min_create_user_type);
	
	if($change_owner_ind == 'Y')
	{
		$new_owner_instance_ind = 'Y'; // this will change
		$new_not_owner_instance_ind = 'Y'; // this will change
	}
	else
	{
		$new_owner_instance_ind = validate_ind_column($new_owner_instance_ind);
		$new_not_owner_instance_ind = validate_ind_column($new_not_owner_instance_ind);
	}
	
	$borrow_ind = validate_ind_column($borrow_ind, array('Y','N','B','X'));
	
	$query = "INSERT INTO s_status_type ( s_status_type, description, img, insert_ind, update_ind, delete_ind, change_owner_ind, min_display_user_type, min_create_user_type, new_owner_instance_ind, new_not_owner_instance_ind, borrow_ind, status_comment_ind, default_ind, closed_ind )".
			"VALUES ('$s_status_type', '$description', '$img', '$insert_ind', '$update_ind', '$delete_ind', '$change_owner_ind', '$min_display_user_type', '$min_create_user_type', '$new_owner_instance_ind', '$new_not_owner_instance_ind', '$borrow_ind', '$status_comment_ind', '$default_ind', 'N')";

	$insert = run_opendb_query($query);
	// We should not treat updates that were not actually updated because value did not change as failures.
	$rows_affected = mysql_affected_rows();
	if($insert && $rows_affected !== -1)
	{
		if($rows_affected>0)
			opendb_log("Inserted s_status_type (s_status_type=$s_status_type, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to insert s_status_type (s_status_type=$s_status_type, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
	
	//else
	return FALSE;
}

/*
*/
function update_s_status_type($s_status_type, $description, $img, 
							$insert_ind, $update_ind, $delete_ind, $change_owner_ind,
							$min_display_user_type,	$min_create_user_type,
							$new_owner_instance_ind, $new_not_owner_instance_ind,
							$borrow_ind, $status_comment_ind, $default_ind,
							$closed_ind)
{
	global $HTTP_SESSION_VARS;
	
	$s_status_type = strtoupper($s_status_type);
	$description = addslashes(trim(strip_tags($description)));

	// do this one first, as we need to validate the data for the others based on this one.
	$change_owner_ind = validate_ind_column($change_owner_ind);
	
	$insert_ind = validate_ind_column($insert_ind);
	$update_ind = validate_ind_column($update_ind);
	$delete_ind = validate_ind_column($delete_ind);
	
	$min_display_user_type = validate_user_type_column($min_display_user_type);
	
	if($change_owner_ind == 'Y')
		$min_create_user_type = 'N'; // normal user minimum
	else
		$min_create_user_type = validate_user_type_column($min_create_user_type);
	
	if($change_owner_ind == 'Y')
	{
		$new_owner_instance_ind = 'Y'; // this will change
		$new_not_owner_instance_ind = 'Y'; // this will change
	}
	else
	{
		$new_owner_instance_ind = validate_ind_column($new_owner_instance_ind);
		$new_not_owner_instance_ind = validate_ind_column($new_not_owner_instance_ind);
	}
	
	$borrow_ind = validate_ind_column($borrow_ind, array('Y','N','B','X'));
		
	$status_comment_ind = validate_ind_column($status_comment_ind, array('Y','N','H'));
	$default_ind = validate_ind_column($default_ind);
	$closed_ind = validate_ind_column($closed_ind);
	
	$query = "UPDATE s_status_type ".
				"SET description = '$description', ".
				"img = '$img', ".
				"insert_ind = '$insert_ind', ".
				"update_ind = '$update_ind', ".
				"delete_ind = '$delete_ind', ".
				"change_owner_ind = '$change_owner_ind', ".
				"min_display_user_type = '$min_display_user_type', ".
				"min_create_user_type = '$min_create_user_type', ".
				"new_owner_instance_ind = '$new_owner_instance_ind', ".
				"new_not_owner_instance_ind = '$new_not_owner_instance_ind', ".
				"borrow_ind = '$borrow_ind', ".
				"status_comment_ind = '$status_comment_ind', ".
				"default_ind = '$default_ind', ".
				"closed_ind = '$closed_ind' ".
			" WHERE s_status_type = '$s_status_type'";

	$update = run_opendb_query($query);

	// We should not treat updates that were not actually updated because value did not change as failures.
	$rows_affected = mysql_affected_rows();
	if($update && $rows_affected !== -1)
	{
		if($rows_affected>0)
			opendb_log("Updated s_status_type (s_status_type=$s_status_type, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to update s_status_type (s_status_type=$s_status_type, update_who=".$HTTP_SESSION_VARS['user_id'].") [".mysql_error()."]");
		return FALSE;
	}
	
	//else
	return FALSE;
}

function delete_s_status_type($s_status_type)
{
	global $HTTP_SESSION_VARS;

	$s_status_type = strtoupper($s_status_type);
		
	$query = "DELETE FROM s_status_type "
			."WHERE s_status_type = '$s_status_type'";

	$delete = run_opendb_query($query);
	
	// We should not treat updates that were not actually updated because value did not change as failures.
	$rows_affected = mysql_affected_rows();
	if($delete && $rows_affected !== -1)
	{
		if($rows_affected>0)
			opendb_log("Deleted s_status_type (s_status_type=$s_status_type, update_who=".$HTTP_SESSION_VARS['user_id'].")");
		return TRUE;
	}
	else
	{
		opendb_log("Failed to delete s_status_type (s_status_type=$s_status_type, update_who=".$HTTP_SESSION_VARS['user_id']."). [".mysql_error()."]");
		return FALSE;
	}
	
	//else
	return FALSE;
}
?>