File: index.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 (297 lines) | stat: -rw-r--r-- 11,291 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
<?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("./admin/s_attribute_type/functions.php");
include_once("./admin/s_item_type/functions.php");

/*
* Get rid of spaces in s_attribute_type_lookup value fields, and update the
* item_attribute's that point to these problem value fields to the new format.
*/
function remove_lookup_whitespace()
{
	// Find any value columns which have space in them.
	$result = run_opendb_query("SELECT satl.s_attribute_type, satl.display, satl.value, sat.s_field_type FROM s_attribute_type sat, s_attribute_type_lookup satl WHERE sat.s_attribute_type = satl.s_attribute_type AND (satl.value LIKE '% %' OR satl.value LIKE '%	%')");
	if($result && mysql_num_rows($result)>0)
	{
		while($lookup_r = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			exec_patch_sql_statement("UPDATE s_attribute_type_lookup SET value = '".preg_replace("/[\s]*/", "", $lookup_r['value'])."', display = '".ifempty($lookup_r['display'],$lookup_r['value'])."' WHERE s_attribute_type = '".$lookup_r['s_attribute_type']."' AND value = '".$lookup_r['value']."'");
			if($lookup_r['s_field_type'] == 'CATEGORY')
			{
				$query = "SELECT i.id, i.category ".
					"FROM item i ".
					"WHERE ( UPPER(i.category) = '".str_replace('\_','_',$lookup_r['value'])."' OR ".
					"UPPER(i.category) LIKE '% ".$lookup_r['value']." %' OR ".
					"UPPER(i.category) LIKE '".$lookup_r['value']." %' OR ".
					"UPPER(i.category) LIKE '% ".$lookup_r['value']."') ";

				$result2 = run_opendb_query($query);
				if($result2)
				{		
					while($item_r = mysql_fetch_array($result2, MYSQL_ASSOC))
					{	
						print_r($attribute_val_r);
						exec_patch_sql_statement("UPDATE item SET category = '".str_replace($lookup_r['value'], preg_replace("/[\s]*/", "", $lookup_r['value']), $item_r['category'])."' ".
												"WHERE id = '".$item_r['id']."'");
					}
				}
			}
			else
			{
				$query = "SELECT ia.item_id, ia.s_attribute_type, ia.order_no, ia.attribute_val ".
					"FROM item_attribute ia ".
					"WHERE ( UPPER(ia.attribute_val) = '".str_replace('\_','_',$lookup_r['value'])."' OR ".
					"UPPER(ia.attribute_val) LIKE '% ".$lookup_r['value']." %' OR ".
					"UPPER(ia.attribute_val) LIKE '".$lookup_r['value']." %' OR ".
					"UPPER(ia.attribute_val) LIKE '% ".$lookup_r['value']."') ";

				$result2 = run_opendb_query($query);
				if($result2)
				{		
					while($attribute_val_r = mysql_fetch_array($result2, MYSQL_ASSOC))
					{	
						exec_patch_sql_statement("UPDATE item_attribute SET attribute_val = '".str_replace($lookup_r['value'], preg_replace("/[\s]*/", "", $lookup_r['value']), $attribute_val_r['attribute_val'])."' ".
												"WHERE item_id = '".$attribute_val_r['item_id']."' AND ".
												"s_attribute_type = '".$attribute_val_r['s_attribute_type']."' AND ".
												"order_no = '".$attribute_val_r['order_no']."'");
					}
				}
			}
		}
		mysql_free_result($result);
	}
	else
	{
		echo("<div class=\"upgrade_warning\">No Lookup records found with whitespace.</div>");
	}
}

function is_sat_from_siat_updated()
{
	$result = run_opendb_query("SELECT s_field_type FROM s_item_attribute_type LIMIT 0,1");
	if($result)
	{
		mysql_free_result($result);
		return FALSE;
	}
	else
		return TRUE;
}

function is_s_field_type_in_sat_table()
{
	$result = run_opendb_query("SELECT s_field_type FROM s_attribute_type LIMIT 0,1");
	if($result)
	{
		mysql_free_result($result);
		return FALSE;
	}
	else
		return TRUE;
}

function update_sat_from_siat()
{
	$result = run_opendb_query("SELECT DISTINCT s_attribute_type, s_field_type FROM s_item_attribute_type WHERE length(ifnull(s_field_type,''))>0");
	if($result)
	{
		while($s_item_attr_type_r = mysql_fetch_array($result))
		{
			// Not sure whether this will be a problem in reality, but it is a problem for me.
			if($s_item_attr_type_r['s_field_type'] == 'STATUS_IND')
				$s_item_attr_type_r['s_field_type'] = 'STATUSTYPE';
				
			exec_patch_sql_statement("UPDATE s_attribute_type SET s_field_type = '".$s_item_attr_type_r['s_field_type']."' WHERE s_attribute_type = '".$s_item_attr_type_r['s_attribute_type']."'");
		}
		mysql_free_result($result);
	}
}

function is_reviews_updated()
{
	$result = run_opendb_query("SELECT sequence_number FROM review LIMIT 0,1");
	if($result)
	{
		mysql_free_result($result);
		return TRUE;
	}
	else
		return FALSE;
}

function update_reviews()
{
	// If not yet converted.
	if(!is_reviews_updated())
	{
		if(exec_patch_sql_statement("RENAME TABLE review TO review_old"))
		{
			// Create new table definition.
			$query = "CREATE TABLE review (".
					"\n\tsequence_number	integer(10) unsigned NOT NULL auto_increment,".
  					"\n\tauthor_id		varchar(20) NOT NULL,".
					"\n\titem_id		integer(10) unsigned NOT NULL,".
					"\n\tcomment		text,".
					"\n\trating		varchar(1) NOT NULL,".
					"\n\tupdate_on		timestamp(14) NOT NULL,".
					"\n\tPRIMARY KEY ( sequence_number ),".
					"\n\tKEY author_item ( author_id, item_id )".
					"\n) TYPE=MyISAM COMMENT='Item Reviews'";
			
			if(exec_patch_sql_statement($query))
			{
				$query = "SELECT author_id, item_id, comment, rating, update_on FROM review_old ORDER BY update_on ASC";
				$result = run_opendb_query($query);
				if($result)
				{
					while($review_r = mysql_fetch_array($result))
					{
						exec_patch_sql_statement("INSERT INTO review (item_id,author_id,comment,rating,update_on) ".
										"VALUES ('".$review_r['item_id']."','".$review_r['author_id']."','".addslashes($review_r['comment'])."','".$review_r['rating']."','".$review_r['update_on']."')");
					}
					mysql_free_result($result);
				}
				
				exec_patch_sql_statement("DROP TABLE review_old");
			}
		}
	}
}

function update_item_types()
{
	global $HTTP_SESSION_VARS;
	
	// Use '*', in case title_prompt has already been dropped.
	$query = "SELECT * FROM s_item_type";
	$result = run_opendb_query($query);
	if($result)
	{
		while($item_type_r = mysql_fetch_array($result))
		{
			echo ("<p class=\"querytitle\">Update Item Type (".$item_type_r['s_item_type'].") Structure</p>");
	
			if(!fetch_sfieldtype_item_attribute_type($item_type_r['s_item_type'], 'CATEGORY'))
			{				
				echo("<div class=\"upgrade_failure\">Item attribute type for s_field_type CATEGORY is missing.  Use System Admin 'Types' tool to rectify after upgrade.</div>");
			}
			
			if(!is_exists_item_attribute_type($item_type_r['s_item_type'], 'S_TITLE'))
			{
				// Insert new S_TITLE attribute.
				if(insert_s_item_attribute_type($item_type_r['s_item_type'], 'S_TITLE', '1', $item_type_r['title_prompt'], 'Y', $HTTP_SESSION_VARS['user_id']))
					echo("<div class=\"upgrade_success\">Item attribute type S_TITLE inserted</div>");
				else
					echo("<div class=\"upgrade_failure\">Item attribute type S_TITLE <b>not</b> inserted</div>");
			}
			else
			{
				echo("<div class=\"upgrade_warning\">Item attribute type S_TITLE already inserted.</div>");
			}
			
			if(!is_exists_item_attribute_type($item_type_r['s_item_type'], 'S_STATUS'))
			{		
				// Insert new S_STATUS attribute.
				if(insert_s_item_attribute_type($item_type_r['s_item_type'], 'S_STATUS', '254', NULL, 'N', $HTTP_SESSION_VARS['user_id']))
					echo("<div class=\"upgrade_success\">Item attribute type S_STATUS inserted</div>");
				else
					echo("<div class=\"upgrade_failure\">Item attribute type S_STATUS <b>not</b> inserted</div>");
			}
			else
			{
				echo("<div class=\"upgrade_warning\">Item attribute type S_STATUS already inserted.</div>");
			}
			
			if(!is_exists_item_attribute_type($item_type_r['s_item_type'], 'S_STATCMNT'))
			{		
				// Insert new S_STATUS attribute.
				if(insert_s_item_attribute_type($item_type_r['s_item_type'], 'S_STATCMNT', '255', NULL, 'N', $HTTP_SESSION_VARS['user_id']))
					echo("<div class=\"upgrade_success\">Item attribute type S_STATCMNT inserted</div>");
				else
					echo("<div class=\"upgrade_failure\">Item attribute type S_STATCMNT <b>not</b> inserted</div>");
			}
			else
			{
				echo("<div class=\"upgrade_warning\">Item attribute type S_STATCMNT already inserted.</div>");
			}

			if(!is_exists_item_attribute_type($item_type_r['s_item_type'], 'S_DURATION'))
			{				
				// Insert new S_DURATION attribute.
				if(insert_s_item_attribute_type($item_type_r['s_item_type'], 'S_DURATION', '200', NULL, 'N', $HTTP_SESSION_VARS['user_id']))
					echo("<div class=\"upgrade_success\">Item attribute type S_DURATION inserted</div>");
				else
					echo("<div class=\"upgrade_failure\">Item attribute type S_DURATION <b>not</b> inserted</div>");
			}
			else
			{
				echo("<div class=\"upgrade_warning\">Item attribute type S_DURATION already inserted.</div>");
			}
		}
		mysql_free_result($result);
	}
}

if($CONFIG_VARS['patch.enable']===TRUE)
{
	if($HTTP_VARS['op'] == "menu")
	{
		echo("<p>
		</p>");

		echo("<DIV ALIGN=\"CENTER\">");
		echo("<FORM action=\"$PHP_SELF\" method=\"get\" target=\"main\">");
		print_button("patch_before", "1. Execute patch_before.sql", FALSE);
		echo("&nbsp;");
		print_button("update_siat", "2. Copy S_FIELD_TYPE values to Attribute Type", is_sat_from_siat_updated());
		echo("&nbsp;");
		print_button("update_types", "3. Update Item Types", FALSE);
		echo("&nbsp;");
		print_button("update_reviews", "4. Convert Reviews", is_reviews_updated());
		echo("&nbsp;");
		print_button("remove_lookup_whitespace", "5. Lookup Record(s) whitespace removal", is_s_field_type_in_sat_table());
		echo("&nbsp;");
		print_button("patch_after", "6. Execute patch_after.sql", FALSE);
		echo("<INPUT TYPE=\"hidden\" name=\"op\" value=\"result\">");
		echo("<INPUT TYPE=\"hidden\" name=\"from\" value=\"".$HTTP_VARS['from']."\">");
		echo("</FORM>");
		echo("</DIV>");
		
		echo("<p class=\"colortext\">Please ensure that you complete ALL steps.  The steps must be completed
		in order, or it is possible that not everything will be updated correctly.  If you experience problems
		with Adding/Editing Items, such as 'Invalid Item Type' errors, then it is quite possible that this patch
		was not completed successfully.</b></p>");
	}
	else 
	{
		if(isset($HTTP_VARS['update_types']))
			update_item_types();	
		else if(isset($HTTP_VARS['update_siat']))
			update_sat_from_siat();
		else if(isset($HTTP_VARS['update_reviews']))
			update_reviews();
		else if(isset($HTTP_VARS['remove_lookup_whitespace']))
			remove_lookup_whitespace();
		else if(isset($HTTP_VARS['patch_before'] ))
			exec_patch_sql_file($HTTP_VARS['from'], "patch_before.sql", TRUE);
		else if(isset($HTTP_VARS['patch_after'] ))
			exec_patch_sql_file($HTTP_VARS['from'], "patch_after.sql", TRUE);
	}
}
?>