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