Projekt

Obecné

Profil

Stáhnout (17.1 KB) Statistiky
| Větev: | Tag: | Revize:
1 6daefa8c Petr Lukašík
<?php
2
3
	/**
4
	 * Manage domains in a database
5
	 *
6
	 * $Id: domains.php,v 1.21 2005/10/18 03:45:16 chriskl Exp $
7
	 */
8
9
	// Include application functions
10
	include_once('./libraries/lib.inc.php');
11
	
12
	$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
13
	if (!isset($msg)) $msg = '';
14
	$PHP_SELF = $_SERVER['PHP_SELF'];
15
	
16
	/** 
17
	 * Function to save after altering a domain
18
	 */
19
	function doSaveAlter() {
20
		global $data, $lang;
21
		
22
		$status = $data->alterDomain($_POST['domain'], $_POST['domdefault'], 
23
			isset($_POST['domnotnull']), $_POST['domowner']);
24
		if ($status == 0)
25
			doProperties($lang['strdomainaltered']);
26
		else
27
			doAlter($lang['strdomainalteredbad']);
28
	}
29
30
	/**
31
	 * Allow altering a domain
32
	 */
33
	function doAlter($msg = '') {
34
		global $data, $misc;
35
		global $PHP_SELF, $lang;
36
	
37
		$misc->printTrail('domain');
38
		$misc->printTitle($lang['stralter'],'pg.domain.alter');
39
		$misc->printMsg($msg);
40
		
41
		// Fetch domain info
42
		$domaindata = $data->getDomain($_REQUEST['domain']);
43
		// Fetch all users
44
		$users = $data->getUsers();
45
		
46
		if ($domaindata->recordCount() > 0) {
47
			if (!isset($_POST['domname'])) {				
48
				$_POST['domtype'] = $domaindata->f['domtype'];
49
				$_POST['domdefault'] = $domaindata->f['domdef'];
50
				$domaindata->f['domnotnull'] = $data->phpBool($domaindata->f['domnotnull']);
51
				if ($domaindata->f['domnotnull']) $_POST['domnotnull'] = 'on';
52
				$_POST['domowner'] = $domaindata->f['domowner'];
53
			}
54
			
55
			// Display domain info
56
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
57
			echo "<table>\n";
58
			echo "<tr><th class=\"data left required\" width=\"70\">{$lang['strname']}</th>\n";
59
			echo "<td class=\"data1\">", $misc->printVal($domaindata->f['domname']), "</td></tr>\n";
60
			echo "<tr><th class=\"data left required\">{$lang['strtype']}</th>\n";
61
			echo "<td class=\"data1\">", $misc->printVal($domaindata->f['domtype']), "</td></tr>\n";
62
			echo "<tr><th class=\"data left\">{$lang['strnotnull']}</th>\n";
63
			echo "<td class=\"data1\"><input type=\"checkbox\" name=\"domnotnull\"", (isset($_POST['domnotnull']) ? ' checked="checked"' : ''), " /></td></tr>\n";
64
			echo "<tr><th class=\"data left\">{$lang['strdefault']}</th>\n";
65
			echo "<td class=\"data1\"><input name=\"domdefault\" size=\"32\" value=\"", 
66
				htmlspecialchars($_POST['domdefault']), "\" /></td></tr>\n";
67
			echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
68
			echo "<td class=\"data1\"><select name=\"domowner\">";
69
			while (!$users->EOF) {
70
				$uname = $users->f['usename'];
71
				echo "<option value=\"", htmlspecialchars($uname), "\"",
72
					($uname == $_POST['domowner']) ? ' selected="selected"' : '', ">", htmlspecialchars($uname), "</option>\n";
73
				$users->moveNext();
74
			}
75
			echo "</select></td></tr>\n";				
76
			echo "</table>\n";
77
			echo "<p><input type=\"hidden\" name=\"action\" value=\"save_alter\" />\n";
78
			echo "<input type=\"hidden\" name=\"domain\" value=\"", htmlspecialchars($_REQUEST['domain']), "\" />\n";
79
			echo $misc->form;
80
			echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
81
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
82
			echo "</form>\n";
83
		}
84
		else echo "<p>{$lang['strnodata']}</p>\n";
85
	}
86
	
87
	/**
88
	 * Confirm and then actually add a CHECK constraint
89
	 */
90
	function addCheck($confirm, $msg = '') {
91
		global $PHP_SELF, $data, $data, $misc;
92
		global $lang;
93
94
		if (!isset($_POST['name'])) $_POST['name'] = '';
95
		if (!isset($_POST['definition'])) $_POST['definition'] = '';
96
97
		if ($confirm) {
98
			$misc->printTrail('domain');
99
			$misc->printTitle($lang['straddcheck'],'pg.constraint.check');
100
			$misc->printMsg($msg);
101
102
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
103
			echo "<table>\n";
104
			echo "<tr><th class=\"data\">{$lang['strname']}</th>\n";
105
			echo "<th class=\"data required\">{$lang['strdefinition']}</th></tr>\n";
106
107
			echo "<tr><td class=\"data1\"><input name=\"name\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
108
				htmlspecialchars($_POST['name']), "\" /></td>\n";
109
110
			echo "<td class=\"data1\">(<input name=\"definition\" size=\"32\" value=\"",
111
				htmlspecialchars($_POST['definition']), "\" />)</td></tr>\n";
112
			echo "</table>\n";
113
114
			echo "<input type=\"hidden\" name=\"action\" value=\"save_add_check\" />\n";
115
			echo "<input type=\"hidden\" name=\"domain\" value=\"", htmlspecialchars($_REQUEST['domain']), "\" />\n";
116
			echo $misc->form;
117
			echo "<p><input type=\"submit\" name=\"add\" value=\"{$lang['stradd']}\" />\n";
118
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
119
			echo "</form>\n";
120
121
		}
122
		else {
123
			if (trim($_POST['definition']) == '')
124
				addCheck(true, $lang['strcheckneedsdefinition']);
125
			else {
126
				$status = $data->addDomainCheckConstraint($_POST['domain'],
127
					$_POST['definition'], $_POST['name']);
128
				if ($status == 0)
129
					doProperties($lang['strcheckadded']);
130
				else
131
					addCheck(true, $lang['strcheckaddedbad']);
132
			}
133
		}
134
	}
135
136
	/**
137
	 * Show confirmation of drop constraint and perform actual drop
138
	 */
139
	function doDropConstraint($confirm, $msg = '') {
140
		global $data, $misc;
141
		global $PHP_SELF, $lang;
142
143
		if ($confirm) {
144
			$misc->printTrail('domain');
145
			$misc->printTitle($lang['strdrop'],'pg.constraint.drop');
146
			$misc->printMsg($msg);
147
			
148
			echo "<p>", sprintf($lang['strconfdropconstraint'], $misc->printVal($_REQUEST['constraint']), 
149
				$misc->printVal($_REQUEST['domain'])), "</p>\n";	
150
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
151
			echo "<input type=\"hidden\" name=\"action\" value=\"drop_con\" />\n";
152
			echo "<input type=\"hidden\" name=\"domain\" value=\"", htmlspecialchars($_REQUEST['domain']), "\" />\n";
153
			echo "<input type=\"hidden\" name=\"constraint\" value=\"", htmlspecialchars($_REQUEST['constraint']), "\" />\n";
154
			echo $misc->form;
155
			// Show cascade drop option if supportd
156
			if ($data->hasDropBehavior()) {
157
				echo "<p><input type=\"checkbox\" name=\"cascade\" /> {$lang['strcascade']}</p>\n";
158
			}
159
			echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
160
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
161
			echo "</form>\n";
162
		}
163
		else {
164
			$status = $data->dropDomainConstraint($_POST['domain'], $_POST['constraint'], isset($_POST['cascade']));
165
			if ($status == 0)
166
				doProperties($lang['strconstraintdropped']);
167
			else
168
				doDropConstraint(true, $lang['strconstraintdroppedbad']);
169
		}
170
		
171
	}
172
	
173
	/**
174
	 * Show properties for a domain.  Allow manipulating constraints as well.
175
	 */
176
	function doProperties($msg = '') {
177
		global $data, $misc;
178
		global $PHP_SELF, $lang;
179
	
180
		$misc->printTrail('domain');
181
		$misc->printTitle($lang['strproperties'],'pg.domain');
182
		$misc->printMsg($msg);
183
		
184
		$domaindata = $data->getDomain($_REQUEST['domain']);
185
		
186
		if ($domaindata->recordCount() > 0) {
187
			// Show comment if any
188
			if ($domaindata->f['domcomment'] !== null)
189
				echo "<p class=\"comment\">", $misc->printVal($domaindata->f['domcomment']), "</p>\n";
190
191
			// Display domain info
192
			$domaindata->f['domnotnull'] = $data->phpBool($domaindata->f['domnotnull']);
193
			echo "<table>\n";
194
			echo "<tr><th class=\"data left\" width=\"70\">{$lang['strname']}</th>\n";
195
			echo "<td class=\"data1\">", $misc->printVal($domaindata->f['domname']), "</td></tr>\n";
196
			echo "<tr><th class=\"data left\">{$lang['strtype']}</th>\n";
197
			echo "<td class=\"data1\">", $misc->printVal($domaindata->f['domtype']), "</td></tr>\n";
198
			echo "<tr><th class=\"data left\">{$lang['strnotnull']}</th>\n";
199
			echo "<td class=\"data1\">", ($domaindata->f['domnotnull'] ? 'NOT NULL' : ''), "</td></tr>\n";
200
			echo "<tr><th class=\"data left\">{$lang['strdefault']}</th>\n";
201
			echo "<td class=\"data1\">", $misc->printVal($domaindata->f['domdef']), "</td></tr>\n";
202
			echo "<tr><th class=\"data left\">{$lang['strowner']}</th>\n";
203
			echo "<td class=\"data1\">", $misc->printVal($domaindata->f['domowner']), "</td></tr>\n";
204
			echo "</table>\n";
205
			
206
			// Display domain constraints
207
			if ($data->hasDomainConstraints()) {
208
				$domaincons = $data->getDomainConstraints($_REQUEST['domain']);
209
				if ($domaincons->recordCount() > 0) {
210
					echo "<h3>{$lang['strconstraints']}</h3>\n";
211
					echo "<table>\n";
212
					echo "<tr><th class=\"data\">{$lang['strname']}</th><th class=\"data\">{$lang['strdefinition']}</th><th class=\"data\">{$lang['stractions']}</th>\n";
213
					$i = 0;
214
					
215
					while (!$domaincons->EOF) {
216
						$id = (($i % 2 ) == 0 ? '1' : '2');
217
						echo "<tr><td class=\"data{$id}\">", $misc->printVal($domaincons->f['conname']), "</td>";
218
						echo "<td class=\"data{$id}\">";
219
						echo $misc->printVal($domaincons->f['consrc']);
220
						echo "</td>";
221
						echo "<td class=\"opbutton{$id}\">";
222
						echo "<a href=\"$PHP_SELF?action=confirm_drop_con&amp;{$misc->href}&amp;constraint=", urlencode($domaincons->f['conname']),
223
							"&amp;domain=", urlencode($_REQUEST['domain']), "&amp;type=", urlencode($domaincons->f['contype']), "\">{$lang['strdrop']}</td></tr>\n";
224
		
225
						$domaincons->moveNext();
226
						$i++;
227
					}
228
					
229
					echo "</table>\n";
230
				}
231
			}
232
		}
233
		else echo "<p>{$lang['strnodata']}</p>\n";
234
		
235
		echo "<p><a class=\"navlink\" href=\"$PHP_SELF?{$misc->href}\">{$lang['strshowalldomains']}</a>\n";
236
		if ($data->hasDomainConstraints()) {
237
			echo "| <a class=\"navlink\" href=\"{$PHP_SELF}?action=add_check&amp;{$misc->href}&amp;domain=", urlencode($_REQUEST['domain']),
238
				"\">{$lang['straddcheck']}</a>\n";
239
			echo "| <a class=\"navlink\" href=\"$PHP_SELF?action=alter&amp;{$misc->href}&amp;domain=", 
240
				urlencode($_REQUEST['domain']), "\">{$lang['stralter']}</a>\n";
241
		}
242
		echo "</p>\n";
243
	}
244
	
245
	/**
246
	 * Show confirmation of drop and perform actual drop
247
	 */
248
	function doDrop($confirm) {
249
		global $data, $misc;
250
		global $PHP_SELF, $lang;
251
252
		if ($confirm) {
253
			$misc->printTrail('domain');
254
			$misc->printTitle($lang['strdrop'],'pg.domain.drop');
255
			
256
			echo "<p>", sprintf($lang['strconfdropdomain'], $misc->printVal($_REQUEST['domain'])), "</p>\n";	
257
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
258
			echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
259
			echo "<input type=\"hidden\" name=\"domain\" value=\"", htmlspecialchars($_REQUEST['domain']), "\" />\n";
260
			echo $misc->form;
261
			// Show cascade drop option if supportd
262
			if ($data->hasDropBehavior()) {
263
				echo "<p><input type=\"checkbox\" name=\"cascade\" /> {$lang['strcascade']}</p>\n";
264
			}
265
			echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
266
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
267
			echo "</form>\n";
268
		}
269
		else {
270
			$status = $data->dropDomain($_POST['domain'], isset($_POST['cascade']));
271
			if ($status == 0)
272
				doDefault($lang['strdomaindropped']);
273
			else
274
				doDefault($lang['strdomaindroppedbad']);
275
		}
276
		
277
	}
278
	
279
	/**
280
	 * Displays a screen where they can enter a new domain
281
	 */
282
	function doCreate($msg = '') {
283
		global $data, $misc;
284
		global $PHP_SELF, $lang;
285
		
286
		if (!isset($_POST['domname'])) $_POST['domname'] = '';
287
		if (!isset($_POST['domtype'])) $_POST['domtype'] = '';
288
		if (!isset($_POST['domlength'])) $_POST['domlength'] = '';
289
		if (!isset($_POST['domarray'])) $_POST['domarray'] = '';
290
		if (!isset($_POST['domdefault'])) $_POST['domdefault'] = '';
291
		if (!isset($_POST['domcheck'])) $_POST['domcheck'] = '';
292
293
		$types = $data->getTypes(true);
294
		
295
		$misc->printTrail('schema');
296
		$misc->printTitle($lang['strcreatedomain'],'pg.domain.create');
297
		$misc->printMsg($msg);
298
299
		echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
300
		echo "<table>\n";
301
		echo "<tr><th class=\"data left required\" width=\"70\">{$lang['strname']}</th>\n";
302
		echo "<td class=\"data1\"><input name=\"domname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", 
303
			htmlspecialchars($_POST['domname']), "\" /></td></tr>\n";
304
		echo "<tr><th class=\"data left required\">{$lang['strtype']}</th>\n";
305
		echo "<td class=\"data1\">\n";
306
		// Output return type list		
307
		echo "<select name=\"domtype\">\n";
308
		while (!$types->EOF) {
309
			echo "<option value=\"", htmlspecialchars($types->f['typname']), "\"", 
310
				($types->f['typname'] == $_POST['domtype']) ? ' selected="selected"' : '', ">",
311
				$misc->printVal($types->f['typname']), "</option>\n";
312
			$types->moveNext();
313
		}
314
		echo "</select>\n";
315
		
316
		// Type length
317
		echo "<input type=\"text\" size=\"4\" name=\"domlength\" value=\"", htmlspecialchars($_POST['domlength']), "\" />";
318
319
		// Output array type selector
320
		echo "<select name=\"domarray\">\n";
321
		echo "<option value=\"\"", ($_POST['domarray'] == '') ? ' selected="selected"' : '', "></option>\n";
322
		echo "<option value=\"[]\"", ($_POST['domarray'] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n";
323
		echo "</select></td></tr>\n";
324
325
		echo "<tr><th class=\"data left\">{$lang['strnotnull']}</th>\n";
326
		echo "<td class=\"data1\"><input type=\"checkbox\" name=\"domnotnull\"", 
327
			(isset($_POST['domnotnull']) ? ' checked="checked"' : ''), " /></td></tr>\n";
328
		echo "<tr><th class=\"data left\">{$lang['strdefault']}</th>\n";
329
		echo "<td class=\"data1\"><input name=\"domdefault\" size=\"32\" value=\"", 
330
			htmlspecialchars($_POST['domdefault']), "\" /></td></tr>\n";
331
		if ($data->hasDomainConstraints()) {
332
			echo "<tr><th class=\"data left\">{$lang['strconstraints']}</th>\n";
333
			echo "<td class=\"data1\">CHECK (<input name=\"domcheck\" size=\"32\" value=\"", 
334
				htmlspecialchars($_POST['domcheck']), "\" />)</td></tr>\n";
335
		}
336
		echo "</table>\n";
337
		echo "<input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
338
		echo $misc->form;
339
		echo "<p><input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
340
		echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
341
		echo "</form>\n";
342
	}
343
	
344
	/**
345
	 * Actually creates the new domain in the database
346
	 */
347
	function doSaveCreate() {
348
		global $data, $lang;
349
		
350
		if (!isset($_POST['domcheck'])) $_POST['domcheck'] = '';
351
352
		// Check that they've given a name and a definition
353
		if ($_POST['domname'] == '') doCreate($lang['strdomainneedsname']);
354
		else {		 
355
			$status = $data->createDomain($_POST['domname'], $_POST['domtype'], $_POST['domlength'], $_POST['domarray'] != '',
356
																isset($_POST['domnotnull']), $_POST['domdefault'], $_POST['domcheck']);
357
			if ($status == 0)
358
				doDefault($lang['strdomaincreated']);
359
			else
360
				doCreate($lang['strdomaincreatedbad']);
361
		}
362
	}	
363
364
	/**
365
	 * Show default list of domains in the database
366
	 */
367
	function doDefault($msg = '') {
368
		global $data, $conf, $misc;
369
		global $PHP_SELF, $lang;
370
		
371
		$misc->printTrail('schema');
372
		$misc->printTabs('schema','domains');
373
		$misc->printMsg($msg);
374
		
375
		$domains = $data->getDomains();
376
		
377
		$columns = array(
378
			'domain' => array(
379
				'title' => $lang['strdomain'],
380
				'field' => 'domname',
381
			),
382
			'type' => array(
383
				'title' => $lang['strtype'],
384
				'field' => 'domtype',
385
			),
386
			'notnull' => array(
387
				'title' => $lang['strnotnull'],
388
				'field' => 'domnotnull',
389
				'type'  => 'bool',
390
				'params'=> array('true' => 'NOT NULL', 'false' => ''),
391
			),
392
			'default' => array(
393
				'title' => $lang['strdefault'],
394
				'field' => 'domdef',
395
			),
396
			'owner' => array(
397
				'title' => $lang['strowner'],
398
				'field' => 'domowner',
399
			),
400
			'actions' => array(
401
				'title' => $lang['stractions'],
402
			),
403
			'comment' => array(
404
				'title' => $lang['strcomment'],
405
				'field' => 'domcomment',
406
			),
407
		);
408
		
409
		$actions = array(
410
			'properties' => array(
411
				'title' => $lang['strproperties'],
412
				'url'	=> "{$PHP_SELF}?action=properties&amp;{$misc->href}&amp;",
413
				'vars'	=> array('domain' => 'domname'),
414
			),
415
			'drop' => array(
416
				'title'	=> $lang['strdrop'],
417
				'url'	=> "{$PHP_SELF}?action=confirm_drop&amp;{$misc->href}&amp;",
418
				'vars'	=> array('domain' => 'domname'),
419
			),
420
		);
421
		
422
		$misc->printTable($domains, $columns, $actions, $lang['strnodomains']);
423
		
424
		echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=create&amp;{$misc->href}\">{$lang['strcreatedomain']}</a></p>\n";
425
426
	}
427
	
428
	/**
429
	 * Generate XML for the browser tree.
430
	 */
431
	function doTree() {
432
		global $misc, $data, $PHP_SELF;
433
		
434
		$domains = $data->getDomains();
435
		
436
		$reqvars = $misc->getRequestVars('domain');
437
		
438
		$attrs = array(
439
			'text'   => field('domname'),
440
			'icon'   => 'domains',
441
			'toolTip'=> field('domcomment'),
442
			'action' => url('domains.php',
443
							$reqvars,
444
							array(
445
								'action' => 'properties',
446
								'domain' => field('domname')
447
							)
448
						)
449
		);
450
		
451
		$misc->printTreeXML($domains, $attrs);
452
		exit;
453
	}
454
	
455
	if ($action == 'tree') doTree();
456
457
	$misc->printHeader($lang['strdomains']);
458
	$misc->printBody();
459
460
	switch ($action) {
461
		case 'add_check':
462
			addCheck(true);
463
			break;
464
		case 'save_add_check':
465
			if (isset($_POST['cancel'])) doProperties();
466
			else addCheck(false);
467
			break;
468
		case 'drop_con':
469
			if (isset($_POST['drop'])) doDropConstraint(false);
470
			else doProperties();
471
			break;
472
		case 'confirm_drop_con':
473
			doDropConstraint(true);
474
			break;			
475
		case 'save_create':
476
			if (isset($_POST['cancel'])) doDefault();
477
			else doSaveCreate();
478
			break;
479
		case 'create':
480
			doCreate();
481
			break;
482
		case 'drop':
483
			if (isset($_POST['drop'])) doDrop(false);
484
			else doDefault();
485
			break;
486
		case 'confirm_drop':
487
			doDrop(true);
488
			break;			
489
		case 'save_alter':
490
			if (isset($_POST['alter'])) doSaveAlter();
491
			else doProperties();
492
			break;
493
		case 'alter':
494
			doAlter();
495
			break;
496
		case 'properties':
497
			doProperties();
498
			break;
499
		default:
500
			doDefault();
501
			break;
502
	}	
503
504
	$misc->printFooter();
505
	
506
?>