Projekt

Obecné

Profil

Stáhnout (24.8 KB) Statistiky
| Větev: | Tag: | Revize:
1 6daefa8c Petr Lukašík
<?php
2
3
	/**
4
	 * Manage views in a database
5
	 *
6
	 * $Id: views.php,v 1.54 2005/10/18 03:45:16 chriskl Exp $
7
	 */
8
9
	// Include application functions
10
	include_once('./libraries/lib.inc.php');
11
	include_once('./classes/Gui.php');
12
	
13
	$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
14
	if (!isset($msg)) $msg = '';
15
	$PHP_SELF = $_SERVER['PHP_SELF'];
16
17
	/**
18
	 * Ask for select parameters and perform select
19
	 */
20
	function doSelectRows($confirm, $msg = '') {
21
		global $data, $misc;
22
		global $lang;
23
		global $PHP_SELF;
24
25
		if ($confirm) {
26
			$misc->printTrail('view');
27
			$misc->printTitle($lang['strselect'], 'pg.sql.select');
28
			$misc->printMsg($msg);
29
30
			$attrs = $data->getTableAttributes($_REQUEST['view']);
31
32
			echo "<form action=\"$PHP_SELF\" method=\"get\" name=\"selectform\">\n";
33
			if ($attrs->recordCount() > 0) {
34
				// JavaScript for select all feature
35
				echo "<script type=\"text/javascript\">\n";
36
				echo "<!--\n";
37
				echo "	function selectAll() {\n";
38
				echo "		for (var i=0; i<document.selectform.elements.length; i++) {\n";
39
				echo "			var e = document.selectform.elements[i];\n";
40
				echo "			if (e.name.indexOf('show') == 0) e.checked = document.selectform.selectall.checked;\n";
41
				echo "		}\n";
42
				echo "	}\n";
43
				echo "//-->\n";
44
				echo "</script>\n";
45
	
46
				echo "<table>\n<tr>";
47
48
				// Output table header
49
				echo "<tr><th class=\"data\">{$lang['strshow']}</th><th class=\"data\">{$lang['strcolumn']}</th>";
50
				echo "<th class=\"data\">{$lang['strtype']}</th><th class=\"data\">{$lang['stroperator']}</th>";
51
				echo "<th class=\"data\">{$lang['strvalue']}</th></tr>";
52
53
				$i = 0;
54
				while (!$attrs->EOF) {
55
					$attrs->f['attnotnull'] = $data->phpBool($attrs->f['attnotnull']);
56
					// Set up default value if there isn't one already
57
					if (!isset($_REQUEST['values'][$attrs->f['attname']]))
58
						$_REQUEST['values'][$attrs->f['attname']] = null;
59
					if (!isset($_REQUEST['ops'][$attrs->f['attname']]))
60
						$_REQUEST['ops'][$attrs->f['attname']] = null;
61
					// Continue drawing row
62
					$id = (($i % 2) == 0 ? '1' : '2');
63
					echo "<tr>\n";
64
					echo "<td class=\"data{$id}\" nowrap=\"nowrap\">";
65
					echo "<input type=\"checkbox\" name=\"show[", htmlspecialchars($attrs->f['attname']), "]\"",
66
						isset($_REQUEST['show'][$attrs->f['attname']]) ? ' checked="checked"' : '', " /></td>";
67
					echo "<td class=\"data{$id}\" nowrap=\"nowrap\">", $misc->printVal($attrs->f['attname']), "</td>";
68
					echo "<td class=\"data{$id}\" nowrap=\"nowrap\">", $misc->printVal($data->formatType($attrs->f['type'], $attrs->f['atttypmod'])), "</td>";
69
					echo "<td class=\"data{$id}\" nowrap=\"nowrap\">";
70
					echo "<select name=\"ops[{$attrs->f['attname']}]\">\n";
71
					foreach (array_keys($data->selectOps) as $v) {
72
						echo "<option value=\"", htmlspecialchars($v), "\"", ($v == $_REQUEST['ops'][$attrs->f['attname']]) ? ' selected="selected"' : '', 
73
						">", htmlspecialchars($v), "</option>\n";
74
					}
75
					echo "</select>\n";
76
					echo "<td class=\"data{$id}\" nowrap=\"nowrap\">", $data->printField("values[{$attrs->f['attname']}]",
77
						$_REQUEST['values'][$attrs->f['attname']], $attrs->f['type']), "</td>";
78
					echo "</tr>\n";
79
					$i++;
80
					$attrs->moveNext();
81
				}
82
				// Select all checkbox
83
				echo "<tr><td colspan=\"5\"><input type=\"checkbox\" name=\"selectall\" onClick=\"javascript:selectAll()\" />{$lang['strselectallfields']}</td>";
84
				echo "</table></p>\n";
85
			}
86
			else echo "<p>{$lang['strinvalidparam']}</p>\n";
87
88
			echo "<p><input type=\"hidden\" name=\"action\" value=\"selectrows\" />\n";
89
			echo "<input type=\"hidden\" name=\"view\" value=\"", htmlspecialchars($_REQUEST['view']), "\" />\n";
90
			echo "<input type=\"hidden\" name=\"subject\" value=\"view\" />\n";
91
			echo $misc->form;
92
			echo "<input type=\"submit\" name=\"select\" value=\"{$lang['strselect']}\" />\n";
93
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
94
			echo "</form>\n";
95
		}
96
		else {
97
			if (!isset($_GET['show'])) $_GET['show'] = array();
98
			if (!isset($_GET['values'])) $_GET['values'] = array();
99
			if (!isset($_GET['nulls'])) $_GET['nulls'] = array();
100
			
101
			// Verify that they haven't supplied a value for unary operators
102
			foreach ($_GET['ops'] as $k => $v) {
103
				if ($data->selectOps[$v] == 'p' && $_GET['values'][$k] != '') {
104
					doSelectRows(true, $lang['strselectunary']);
105
					return;
106
				}
107
			}
108
						
109
			if (sizeof($_GET['show']) == 0)
110
				doSelectRows(true, $lang['strselectneedscol']);			
111
			else {
112
				// Generate query SQL
113
				$query = $data->getSelectSQL($_REQUEST['view'], array_keys($_GET['show']),
114
					$_GET['values'], $_GET['ops']);
115
				$_REQUEST['query'] = $query;
116
				$_REQUEST['return_url'] = "views.php?action=confselectrows&{$misc->href}&view={$_REQUEST['view']}";
117
				$_REQUEST['return_desc'] = $lang['strback'];
118
119
				include('./display.php');
120
				exit;
121
			}
122
		}
123
124
	}
125
	
126
	/**
127
	 * Show confirmation of drop and perform actual drop
128
	 */
129
	function doDrop($confirm) {
130
		global $data, $misc;
131
		global $PHP_SELF, $lang, $_reload_browser;
132
133
		if ($confirm) { 
134
			$misc->printTrail('view');
135
			$misc->printTitle($lang['strdrop'],'pg.view.drop');
136
			
137
			echo "<p>", sprintf($lang['strconfdropview'], $misc->printVal($_REQUEST['view'])), "</p>\n";
138
			
139
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
140
			echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
141
			echo "<input type=\"hidden\" name=\"view\" value=\"", htmlspecialchars($_REQUEST['view']), "\" />\n";
142
			echo $misc->form;
143
			// Show cascade drop option if supportd
144
			if ($data->hasDropBehavior()) {
145
				echo "<p><input type=\"checkbox\" name=\"cascade\" /> {$lang['strcascade']}</p>\n";
146
			}
147
			echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
148
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
149
			echo "</form>\n";
150
		}
151
		else {
152
			$status = $data->dropView($_POST['view'], isset($_POST['cascade']));
153
			if ($status == 0) {
154
				$_reload_browser = true;
155
				doDefault($lang['strviewdropped']);
156
			}
157
			else
158
				doDefault($lang['strviewdroppedbad']);
159
		}
160
		
161
	}
162
	
163
	/**
164
	 * Sets up choices for table linkage, and which fields to select for the view we're creating
165
	 */
166
	function doSetParamsCreate($msg = '') {
167
		global $data, $misc;
168
		global $PHP_SELF, $lang;
169
		
170
		// Check that they've chosen tables for the view definition
171
		if (!isset($_POST['formTables']) ) doWizardCreate($lang['strviewneedsdef']);
172
		else {
173
			// Initialise variables
174
			if (!isset($_REQUEST['formView'])) $_REQUEST['formView'] = '';
175
			if (!isset($_REQUEST['formComment'])) $_REQUEST['formComment'] = '';
176
			
177
			$misc->printTrail('schema');
178
			$misc->printTitle($lang['strcreateviewwiz'], 'pg.view.create');
179
			$misc->printMsg($msg);
180
			
181
			$tblCount = sizeof($_POST['formTables']);
182
			//unserialize our schema/table information and store in arrSelTables
183
			for ($i = 0; $i < $tblCount; $i++) {
184
				$arrSelTables[] = unserialize($_POST['formTables'][$i]);
185
			}
186
			
187
			$linkCount = $tblCount;
188
			// If we can get foreign key info then get our linking keys
189
			if ($data->hasForeignKeysInfo()) {
190
				$rsLinkKeys = $data->getLinkingKeys($arrSelTables);
191
				$linkCount = $rsLinkKeys->recordCount() > $tblCount ? $rsLinkKeys->recordCount() : $tblCount;
192
			}
193
			
194
			$arrFields = array(); //array that will hold all our table/field names
195
			
196
			//if we have schemas we need to specify the correct schema for each table we're retrieiving
197
			//with getTableAttributes
198
			$curSchema = $data->hasSchemas() ? $data->_schema : NULL;
199
			for ($i = 0; $i < $tblCount; $i++) {
200
				if ($data->hasSchemas() && $data->_schema != $arrSelTables[$i]['schemaname']) {
201
					$data->setSchema($arrSelTables[$i]['schemaname']);
202
				}
203
				
204
				$attrs = $data->getTableAttributes($arrSelTables[$i]['tablename']);
205
				while (!$attrs->EOF) {
206
					if ($data->hasSchemas() ) {
207
						$arrFields["{$arrSelTables[$i]['schemaname']}.{$arrSelTables[$i]['tablename']}.{$attrs->f['attname']}"] = serialize(array('schemaname' => $arrSelTables[$i]['schemaname'], 'tablename' => $arrSelTables[$i]['tablename'], 'fieldname' => $attrs->f['attname']) );
208
					}
209
					else {
210
						$arrFields["{$arrSelTables[$i]['tablename']}.{$attrs->f['attname']}"] = serialize(array('schemaname' => NULL, 'tablename' => $arrSelTables[$i]['tablename'], 'fieldname' => $attrs->f['attname']) );
211
					}
212
					$attrs->moveNext();
213
				}
214
				
215
				//reset back to our original schema in case we switched from it
216
				if ($data->hasSchemas() ) {
217
					$data->setSchema($curSchema);
218
				}
219
			}
220
			asort($arrFields);
221
			
222
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
223
			echo "<table>\n";
224
			echo "<tr><th class=\"data\">{$lang['strviewname']}</th></tr>";
225
			echo "<tr>\n<td class=\"data1\">\n";
226
			// View name
227
			echo "<input name=\"formView\" id=\"formView\" value=\"", htmlspecialchars($_REQUEST['formView']), "\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" />\n";
228
			echo "</td>\n</tr>\n";
229
			echo "<tr><th class=\"data\">{$lang['strcomment']}</th></tr>";
230
			echo "<tr>\n<td class=\"data1\">\n";
231
			// View comments
232
			echo "<textarea name=\"formComment\" rows=\"3\" cols=\"32\" wrap=\"virtual\">", 
233
				htmlspecialchars($_REQUEST['formComment']), "</textarea>\n";
234
			echo "</td>\n</tr>\n";
235
			echo "</table>\n";
236
			
237
			// Output selector for fields to be retrieved from view
238
			echo "<table>\n";
239
			echo "<tr><th class=\"data\">{$lang['strcolumns']}</th></tr>";
240
			echo "<tr>\n<td class=\"data1\">\n";
241
			echo GUI::printCombo($arrFields, 'formFields[]', false, '', true);
242
			echo "</td>\n</tr>\n</table>\n<br />\n";
243
			
244
			// Output the Linking keys combo boxes
245
			echo "<table>\n";
246
			echo "<tr><th class=\"data\">{$lang['strviewlink']}</th></tr>";
247
			$rowClass = 'data1';
248
			for ($i = 0; $i < $linkCount; $i++) {
249
				// Initialise variables
250
				if (!isset($formLink[$i]['operator'])) $formLink[$i]['operator'] = 'INNER JOIN';
251
				echo "<tr>\n<td class=\"$rowClass\">\n";
252
				
253
				if ($data->hasForeignKeysInfo() && !$rsLinkKeys->EOF) {
254
					$curLeftLink = htmlspecialchars(serialize(array('schemaname' => $rsLinkKeys->f['p_schema'], 'tablename' => $rsLinkKeys->f['p_table'], 'fieldname' => $rsLinkKeys->f['p_field']) ) );
255
					$curRightLink = htmlspecialchars(serialize(array('schemaname' => $rsLinkKeys->f['f_schema'], 'tablename' => $rsLinkKeys->f['f_table'], 'fieldname' => $rsLinkKeys->f['f_field']) ) );
256
					$rsLinkKeys->moveNext();
257
				}
258
				else {
259
					$curLeftLink = '';
260
					$curRightLink = '';
261
				}
262
				
263
				echo GUI::printCombo($arrFields, "formLink[$i][leftlink]", true, $curLeftLink, false );
264
				echo GUI::printCombo($data->joinOps, "formLink[$i][operator]", true, $formLink[$i]['operator']);
265
				echo GUI::printCombo($arrFields, "formLink[$i][rightlink]", true, $curRightLink, false );
266
				echo "</td>\n</tr>\n";
267
				$rowClass = $rowClass == 'data1' ? 'data2' : 'data1';
268
			}
269
			echo "</table>\n<br />\n";
270
			
271
			// Build list of available operators (infix only)
272
			$arrOperators = array();
273
			foreach ($data->selectOps as $k => $v) {
274
				if ($v == 'i') $arrOperators[$k] = $k;
275
			}
276
277
			// Output additional conditions, note that this portion of the wizard treats the right hand side as literal values 
278
			//(not as database objects) so field names will be treated as strings, use the above linking keys section to perform joins
279
			echo "<table>\n";
280
			echo "<tr><th class=\"data\">{$lang['strviewconditions']}</th></tr>";
281
			$rowClass = 'data1';
282
			for ($i = 0; $i < $linkCount; $i++) {
283
				echo "<tr>\n<td class=\"$rowClass\">\n";
284
				echo GUI::printCombo($arrFields, "formCondition[$i][field]");
285
				echo GUI::printCombo($arrOperators, "formCondition[$i][operator]", false, false);
286
				echo "<input type=\"text\" name=\"formCondition[$i][txt]\" />\n";
287
				echo "</td>\n</tr>\n";
288
				$rowClass = $rowClass == 'data1' ? 'data2' : 'data1';
289
			}
290
			echo "</table>\n";
291
			echo "<p><input type=\"hidden\" name=\"action\" id=\"action\" value=\"save_create_wiz\" />\n";
292
			echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
293
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
294
			
295
			foreach ($arrSelTables AS $curTable) {
296
				echo "<input type=\"hidden\" name=\"formTables[]\" id=\"formTables[]\" value=\"" . htmlspecialchars(serialize($curTable) ) . "\" />\n";
297
			}
298
			
299
			echo $misc->form;
300
			echo "</form>\n";
301
		}
302
	}
303
	
304
	/**
305
	 * Display a wizard where they can enter a new view
306
	 */
307
	function doWizardCreate($msg = '') {
308
		global $data, $misc;
309
		global $PHP_SELF, $lang;
310
		
311
		$tables = $data->getTables(true);
312
		
313
		$misc->printTrail('schema');
314
		$misc->printTitle($lang['strcreateviewwiz'], 'pg.view.create');
315
		$misc->printMsg($msg);
316
		
317
		echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
318
		echo "<table>\n";
319
		echo "<tr><th class=\"data\">{$lang['strtables']}</th></tr>";		
320
		echo "<tr>\n<td class=\"data1\">\n";		
321
		
322
		$arrTables = array();
323
		while (!$tables->EOF) {						
324
			$arrTmp = array();
325
			$arrTmp['schemaname'] = $tables->f['nspname'];
326
			$arrTmp['tablename'] = $tables->f['relname'];
327
			if ($data->hasSchemas() ) { //if schemas aren't available don't show them in the interface
328
				$arrTables[$tables->f['nspname'] . '.' . $tables->f['relname']] = serialize($arrTmp);
329
			}
330
			else {
331
				$arrTables[$tables->f['relname']] = serialize($arrTmp);
332
			}
333
			$tables->moveNext();
334
		}		
335
		echo GUI::printCombo($arrTables, 'formTables[]', false, '', true);			
336
		
337
		echo "</td>\n</tr>\n";		
338
		echo "</table>\n";		
339
		echo "<p><input type=\"submit\" value=\"{$lang['strnext']}\" />\n";
340
		echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
341
		echo "<input type=\"hidden\" name=\"action\" id=\"action\" value=\"set_params_create\" />\n";
342
		echo $misc->form;
343
		echo "</form>\n";
344
	}
345
	
346
	/**
347
	 * Displays a screen where they can enter a new view
348
	 */
349
	function doCreate($msg = '') {
350
		global $data, $misc, $conf;
351
		global $PHP_SELF, $lang;
352
		
353
		if (!isset($_REQUEST['formView'])) $_REQUEST['formView'] = '';
354
		if (!isset($_REQUEST['formDefinition'])) $_REQUEST['formDefinition'] = 'SELECT ';
355
		if (!isset($_REQUEST['formComment'])) $_REQUEST['formComment'] = '';
356
		
357
		$misc->printTrail('schema');
358
		$misc->printTitle($lang['strcreateview'], 'pg.view.create');
359
		$misc->printMsg($msg);
360
		
361
		echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
362
		echo "<table width=\"100%\">\n";
363
		echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
364
		echo "\t<td class=\"data1\"><input name=\"formView\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", 
365
			htmlspecialchars($_REQUEST['formView']), "\" /></td>\n\t</tr>\n";
366
		echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strdefinition']}</th>\n";
367
		echo "\t<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"10\" cols=\"50\" name=\"formDefinition\" wrap=\"virtual\">", 
368
			htmlspecialchars($_REQUEST['formDefinition']), "</textarea></td>\n\t</tr>\n";
369
		echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
370
		echo "\t\t<td class=\"data1\"><textarea name=\"formComment\" rows=\"3\" cols=\"32\" wrap=\"virtual\">", 
371
			htmlspecialchars($_REQUEST['formComment']), "</textarea></td>\n\t</tr>\n";
372
		echo "</table>\n";
373
		echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
374
		echo $misc->form;
375
		echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
376
		echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
377
		echo "</form>\n";
378
	}
379
	
380
	/**
381
	 * Actually creates the new view in the database
382
	 */
383
	function doSaveCreate() {
384
		global $data, $lang, $_reload_browser;
385
		
386
		// Check that they've given a name and a definition
387
		if ($_POST['formView'] == '') doCreate($lang['strviewneedsname']);
388
		elseif ($_POST['formDefinition'] == '') doCreate($lang['strviewneedsdef']);
389
		else {		 
390
			$status = $data->createView($_POST['formView'], $_POST['formDefinition'], false, $_POST['formComment']);
391
			if ($status == 0) {
392
				$_reload_browser = true;
393
				doDefault($lang['strviewcreated']);
394
			}
395
			else
396
				doCreate($lang['strviewcreatedbad']);
397
		}
398
	}	
399
400
  	/**
401
	 * Actually creates the new wizard view in the database
402
	 */
403
	function doSaveCreateWiz() {
404
		global $data, $lang, $_reload_browser;
405
		
406
		// Check that they've given a name and fields they want to select		
407
	
408
		if (!strlen($_POST['formView']) ) doSetParamsCreate($lang['strviewneedsname']);
409
		else if (!isset($_POST['formFields']) || !count($_POST['formFields']) ) doSetParamsCreate($lang['strviewneedsfields']);
410
		else {						
411
			$selFields = '';						
412
			foreach ($_POST['formFields'] AS $curField) {
413
				$arrTmp = unserialize($curField);
414
				if ($data->hasSchemas() ) {					
415
					$selFields .= strlen($selFields) ? ", \"{$arrTmp['schemaname']}\".\"{$arrTmp['tablename']}\".\"{$arrTmp['fieldname']}\"" : "\"{$arrTmp['schemaname']}\".\"{$arrTmp['tablename']}\".\"{$arrTmp['fieldname']}\"";
416
				}
417
				else {
418
					$selFields .= strlen($selFields) ? ", \"{$arrTmp['tablename']}\".\"{$arrTmp['fieldname']}\"" : "\"{$arrTmp['tablename']}\".\"{$arrTmp['fieldname']}\"";
419
				}				
420
			}			
421
			
422
			$linkFields = '';
423
424
			// If we have links, out put the JOIN ... ON statements
425
			if (is_array($_POST['formLink']) ) {
426
				// Filter out invalid/blank entries for our links
427
				$arrLinks = array();
428
				foreach ($_POST['formLink'] AS $curLink) {
429
					if (strlen($curLink['leftlink']) && strlen($curLink['rightlink']) && strlen($curLink['operator'])) {
430
						$arrLinks[] = $curLink;
431
					}
432
				}				
433
				// We must perform some magic to make sure that we have a valid join order
434
				$count = sizeof($arrLinks);
435
				$arrJoined = array();
436
				$arrUsedTbls = array();								
437
438
				// If we have at least one join condition, output it
439
				if ($count > 0) {
440
					$j = 0;
441
					while ($j < $count) {					
442
						foreach ($arrLinks AS $curLink) {
443
							
444
							$arrLeftLink = unserialize($curLink['leftlink']);
445
							$arrRightLink = unserialize($curLink['rightlink']);
446
							
447
							$tbl1 = $data->hasSchemas() ? "\"{$arrLeftLink['schemaname']}\".\"{$arrLeftLink['tablename']}\"" : $arrLeftLink['tablename'];
448
							$tbl2 = $data->hasSchemas() ? "\"{$arrRightLink['schemaname']}\".\"{$arrRightLink['tablename']}\"" : $arrRightLink['tablename'];
449
							
450
							if ( (!in_array($curLink, $arrJoined) && in_array($tbl1, $arrUsedTbls)) || !count($arrJoined) ) {
451
								
452
								// Make sure for multi-column foreign keys that we use a table alias tables joined to more than once
453
								// This can (and should be) more optimized for multi-column foreign keys
454
								$adj_tbl2 = in_array($tbl2, $arrUsedTbls) ? "$tbl2 AS alias_ppa_" . mktime() : $tbl2;
455
								
456
								if ($data->hasSchemas() ) {
457
									$linkFields .= strlen($linkFields) ? "{$curLink['operator']} $adj_tbl2 ON (\"{$arrLeftLink['schemaname']}\".\"{$arrLeftLink['tablename']}\".\"{$arrLeftLink['fieldname']}\" = \"{$arrRightLink['schemaname']}\".\"{$arrRightLink['tablename']}\".\"{$arrRightLink['fieldname']}\") " : "$tbl1 {$curLink['operator']} $adj_tbl2 ON (\"{$arrLeftLink['schemaname']}\".\"{$arrLeftLink['tablename']}\".\"{$arrLeftLink['fieldname']}\" = \"{$arrRightLink['schemaname']}\".\"{$arrRightLink['tablename']}\".\"{$arrRightLink['fieldname']}\") ";
458
								}
459
								else {
460
									$linkFields .= strlen($linkFields) ? "{$curLink['operator']} $adj_tbl2 ON (\"{$arrLeftLink['tablename']}\".\"{$arrLeftLink['fieldname']}\" = \"{$arrRightLink['tablename']}\".\"{$arrRightLink['fieldname']}\") " : "$tbl1 {$curLink['operator']} $adj_tbl2 ON (\"{$arrLeftLink['tablename']}\".\"{$arrLeftLink['fieldname']}\" = \"{$arrRightLink['tablename']}\".\"{$arrRightLink['fieldname']}\") ";
461
								}
462
								
463
								$arrJoined[] = $curLink;
464
								if (!in_array($tbl1, $arrUsedTbls) )  $arrUsedTbls[] = $tbl1;
465
								if (!in_array($tbl2, $arrUsedTbls) )  $arrUsedTbls[] = $tbl2;
466
							}
467
						}
468
						$j++;
469
					}
470
				}
471
			}
472
			
473
			//if linkfields has no length then either _POST['formLink'] was not set, or there were no join conditions 
474
			//just select from all seleted tables - a cartesian join do a
475
			if (!strlen($linkFields) ) {
476
				foreach ($_POST['formTables'] AS $curTable) {
477
					$arrTmp = unserialize($curTable);
478
					if ($data->hasSchemas() ) {
479
						$linkFields .= strlen($linkFields) ? ", \"{$arrTmp['schemaname']}\".\"{$arrTmp['tablename']}\"" : "\"{$arrTmp['schemaname']}\".\"{$arrTmp['tablename']}\"";
480
					}
481
					else {
482
						$linkFields .= strlen($linkFields) ? ", \"{$arrTmp['tablename']}\"" : "\"{$arrTmp['tablename']}\"";
483
					}
484
				}
485
			}
486
			
487
			$addConditions = '';
488
			if (is_array($_POST['formCondition']) ) {
489
				foreach ($_POST['formCondition'] AS $curCondition) {
490
					if (strlen($curCondition['field']) && strlen($curCondition['txt']) ) {
491
						$arrTmp = unserialize($curCondition['field']);
492
						if ($data->hasSchemas() ) {
493
							$addConditions .= strlen($addConditions) ? " AND \"{$arrTmp['schemaname']}\".\"{$arrTmp['tablename']}\".\"{$arrTmp['fieldname']}\" {$curCondition['operator']} '{$curCondition['txt']}' " : " \"{$arrTmp['schemaname']}\".\"{$arrTmp['tablename']}\".\"{$arrTmp['fieldname']}\" {$curCondition['operator']} '{$curCondition['txt']}' ";
494
						}
495
						else {
496
							$addConditions .= strlen($addConditions) ? " AND \"{$arrTmp['tablename']}\".\"{$arrTmp['fieldname']}\" {$curCondition['field']} {$curCondition['operator']} '{$curCondition['txt']}' " : " \"{$arrTmp['tablename']}\".\"{$arrTmp['fieldname']}\" {$curCondition['operator']} '{$curCondition['txt']}' ";
497
						}
498
					}
499
				}
500
			}
501
			
502
			$viewQuery = "SELECT $selFields FROM $linkFields ";
503
			
504
			//add where from additional conditions
505
			if (strlen($addConditions) ) $viewQuery .= ' WHERE ' . $addConditions;
506
			
507
			$status = $data->createView($_POST['formView'], $viewQuery, false, $_POST['formComment']);
508
			if ($status == 0) {
509
				$_reload_browser = true;
510
				doDefault($lang['strviewcreated']);
511
			}
512
			else
513
				doSetParamsCreate($lang['strviewcreatedbad']);
514
		}
515
	}
516
	
517
	/**
518
	 * Show default list of views in the database
519
	 */
520
	function doDefault($msg = '') {
521
		global $data, $misc, $conf;
522
		global $PHP_SELF, $lang;
523
		
524
		$misc->printTrail('schema');
525
		$misc->printTabs('schema','views');
526
		$misc->printMsg($msg);
527
		
528
		$views = $data->getViews();
529
		
530
		$columns = array(
531
			'view' => array(
532
				'title'	=> $lang['strview'],
533
				'field'	=> 'relname',
534
			),
535
			'owner' => array(
536
				'title'	=> $lang['strowner'],
537
				'field'	=> 'relowner',
538
			),
539
			'actions' => array(
540
				'title'	=> $lang['stractions'],
541
			),
542
			'comment' => array(
543
				'title'	=> $lang['strcomment'],
544
				'field'	=> 'relcomment',
545
			),
546
		);
547
		
548
		$actions = array(
549
			'properties' => array(
550
				'title' => $lang['strproperties'],
551
				'url'	=> "redirect.php?subject=view&amp;{$misc->href}&amp;",
552
				'vars'	=> array('view' => 'relname'),
553
			),
554
			'browse' => array(
555
				'title'	=> $lang['strbrowse'],
556
				'url'	=> "display.php?{$misc->href}&amp;subject=view&amp;return_url=".urlencode("views.php?{$misc->href}")."&amp;return_desc=".urlencode($lang['strback'])."&amp;",
557
				'vars'	=> array('view' => 'relname'),
558
			),
559
			'select' => array(
560
				'title'	=> $lang['strselect'],
561
				'url'	=> "{$PHP_SELF}?action=confselectrows&amp;{$misc->href}&amp;",
562
				'vars'	=> array('view' => 'relname'),
563
			),
564
			
565
// Insert is possible if the relevant rule for the view has been created.
566
//			'insert' => array(
567
//				'title'	=> $lang['strinsert'],
568
//				'url'	=> "{$PHP_SELF}?action=confinsertrow&amp;{$misc->href}&amp;",
569
//				'vars'	=> array('view' => 'relname'),
570
//			),
571
572
			'drop' => array(
573
				'title'	=> $lang['strdrop'],
574
				'url'	=> "{$PHP_SELF}?action=confirm_drop&amp;{$misc->href}&amp;",
575
				'vars'	=> array('view' => 'relname'),
576
			),
577
		);
578
		
579
		$misc->printTable($views, $columns, $actions, $lang['strnoviews']);
580
		
581
		echo "<p><a class=\"navlink\" href=\"$PHP_SELF?action=create&amp;{$misc->href}\">{$lang['strcreateview']}</a> |\n";
582
		echo "<a class=\"navlink\" href=\"$PHP_SELF?action=wiz_create&amp;{$misc->href}\">{$lang['strcreateviewwiz']}</a></p>\n";
583
584
	}
585
	
586
	/**
587
	 * Generate XML for the browser tree.
588
	 */
589
	function doTree() {
590
		global $misc, $data;
591
		
592
		$views = $data->getViews();
593
		
594
		$reqvars = $misc->getRequestVars('view');
595
		
596
		$attrs = array(
597
			'text'   => field('relname'),
598
			'icon'   => 'views',
599
			'toolTip'=> field('relcomment'),
600
			'action' => url('redirect.php',
601
							$reqvars,
602
							array('view' => field('relname'))
603
						)
604
		);
605
		
606
		$misc->printTreeXML($views, $attrs);
607
		exit;
608
	}
609
	
610
	if ($action == 'tree') doTree();
611
	
612
	$misc->printHeader($lang['strviews']);
613
	$misc->printBody();
614
615
	switch ($action) {
616
		case 'selectrows':
617
			if (!isset($_REQUEST['cancel'])) doSelectRows(false);
618
			else doDefault();
619
			break;
620
		case 'confselectrows':
621
			doSelectRows(true);
622
			break;
623
		case 'save_create_wiz':
624
			if (isset($_REQUEST['cancel'])) doDefault();
625
			else doSaveCreateWiz();
626
			break;
627
		case 'wiz_create':
628
			doWizardCreate();
629
			break;
630
		case 'set_params_create':
631
			if (isset($_POST['cancel'])) doDefault();
632
			else doSetParamsCreate();
633
  			break;
634
		case 'save_create':
635
			if (isset($_REQUEST['cancel'])) doDefault();
636
			else doSaveCreate();
637
			break;
638
		case 'create':
639
			doCreate();
640
			break;
641
		case 'drop':
642
			if (isset($_POST['drop'])) doDrop(false);
643
			else doDefault();
644
			break;
645
		case 'confirm_drop':
646
			doDrop(true);
647
			break;			
648
		default:
649
			doDefault();
650
			break;
651
	}	
652
653
	$misc->printFooter();
654
	
655
?>