1<?php
2
3	/**
4	 * List Columns properties in tables
5	 *
6	 * $Id: colproperties.php
7	 */
8
9	// Include application functions
10	include_once('./libraries/lib.inc.php');
11
12	$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
13	if (isset($_REQUEST['table']))
14		$tableName =& $_REQUEST['table'];
15	elseif (isset($_REQUEST['view']))
16		$tableName =& $_REQUEST['view'];
17	else
18		die($lang['strnotableprovided']);
19
20	/**
21	 * Displays a screen where they can alter a column
22	 */
23	function doAlter($msg = '') {
24		global $data, $misc, $_reload_browser;
25		global $lang;
26
27		if (!isset($_REQUEST['stage'])) $_REQUEST['stage'] = 1;
28
29		switch ($_REQUEST['stage']) {
30			case 1:
31				$misc->printTrail('column');
32				$misc->printTitle($lang['stralter'], 'pg.column.alter');
33				$misc->printMsg($msg);
34
35				echo "<script src=\"tables.js\" type=\"text/javascript\"></script>";
36				echo "<form action=\"colproperties.php\" method=\"post\">\n";
37
38				// Output table header
39				echo "<table>\n";
40				echo "<tr><th class=\"data required\">{$lang['strname']}</th>\n";
41				if ($data->hasAlterColumnType()) {
42					echo "<th class=\"data required\" colspan=\"2\">{$lang['strtype']}</th>\n";
43					echo "<th class=\"data\">{$lang['strlength']}</th>\n";
44				}
45				else {
46					echo "<th class=\"data required\">{$lang['strtype']}</th>\n";
47				}
48				echo "<th class=\"data\">{$lang['strnotnull']}</th>\n<th class=\"data\">{$lang['strdefault']}</th>\n<th class=\"data\">{$lang['strcomment']}</th></tr>\n";
49
50				$column = $data->getTableAttributes($_REQUEST['table'], $_REQUEST['column']);
51				$column->fields['attnotnull'] = $data->phpBool($column->fields['attnotnull']);
52
53				// Upon first drawing the screen, load the existing column information
54				// from the database.
55				if (!isset($_REQUEST['default'])) {
56					$_REQUEST['field'] = $column->fields['attname'];
57					$_REQUEST['type'] = $column->fields['base_type'];
58					// Check to see if its' an array type...
59					// XXX: HACKY
60					if (substr($column->fields['base_type'], strlen($column->fields['base_type']) - 2) == '[]') {
61						$_REQUEST['type'] = substr($column->fields['base_type'], 0, strlen($column->fields['base_type']) - 2);
62						$_REQUEST['array'] = '[]';
63					}
64					else {
65						$_REQUEST['type'] = $column->fields['base_type'];
66						$_REQUEST['array'] = '';
67					}
68					// To figure out the length, look in the brackets :(
69					// XXX: HACKY
70					if ($column->fields['type'] != $column->fields['base_type'] && preg_match('/\\(([0-9, ]*)\\)/', $column->fields['type'], $bits)) {
71						$_REQUEST['length'] = $bits[1];
72					}
73					else
74						$_REQUEST['length'] = '';
75					$_REQUEST['default'] = $_REQUEST['olddefault'] = $column->fields['adsrc'];
76					if ($column->fields['attnotnull']) $_REQUEST['notnull'] = 'YES';
77					$_REQUEST['comment'] = $column->fields['comment'];
78				}
79
80				// Column name
81				echo "<tr><td><input name=\"field\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
82					htmlspecialchars($_REQUEST['field']), "\" /></td>\n";
83
84				// Column type
85				$escaped_predef_types = array(); // the JS escaped array elements
86				if ($data->hasAlterColumnType()) {
87					// Fetch all available types
88					$types = $data->getTypes(true, false, true);
89					$types_for_js = array();
90
91					echo "<td><select name=\"type\" id=\"type\" onchange=\"checkLengths(document.getElementById('type').value,'');\">\n";
92					while (!$types->EOF) {
93						$typname = $types->fields['typname'];
94						$types_for_js[] = $typname;
95						echo "\t<option value=\"", htmlspecialchars($typname), "\"", ($typname == $_REQUEST['type']) ? ' selected="selected"' : '', ">",
96							$misc->printVal($typname), "</option>\n";
97						$types->moveNext();
98					}
99					echo "</select></td>\n";
100
101					// Output array type selector
102					echo "<td><select name=\"array\">\n";
103					echo "\t<option value=\"\"", ($_REQUEST['array'] == '') ? ' selected="selected"' : '', "></option>\n";
104					echo "\t<option value=\"[]\"", ($_REQUEST['array'] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n";
105					echo "</select></td>\n";
106					$predefined_size_types = array_intersect($data->predefined_size_types, $types_for_js);
107					foreach($predefined_size_types as $value) {
108						$escaped_predef_types[] = "'{$value}'";
109					}
110
111					echo "<td><input name=\"length\" id=\"lengths\" size=\"8\" value=\"",
112						htmlspecialchars($_REQUEST['length']), "\" /></td>\n";
113				} else {
114					// Otherwise draw the read-only type name
115					echo "<td>", $misc->printVal($data->formatType($column->fields['type'], $column->fields['atttypmod'])), "</td>\n";
116				}
117
118				echo "<td><input type=\"checkbox\" name=\"notnull\"", (isset($_REQUEST['notnull'])) ? ' checked="checked"' : '', " /></td>\n";
119				echo "<td><input name=\"default\" size=\"20\" value=\"",
120					htmlspecialchars($_REQUEST['default']), "\" /></td>\n";
121				echo "<td><input name=\"comment\" size=\"40\" value=\"",
122					htmlspecialchars($_REQUEST['comment']), "\" /></td></tr>\n";
123				echo "</table>\n";
124				echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n";
125				echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
126				echo $misc->form;
127				echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
128				echo "<input type=\"hidden\" name=\"column\" value=\"", htmlspecialchars($_REQUEST['column']), "\" />\n";
129				echo "<input type=\"hidden\" name=\"olddefault\" value=\"", htmlspecialchars($_REQUEST['olddefault']), "\" />\n";
130				if ($column->fields['attnotnull']) echo "<input type=\"hidden\" name=\"oldnotnull\" value=\"on\" />\n";
131				echo "<input type=\"hidden\" name=\"oldtype\" value=\"", htmlspecialchars($data->formatType($column->fields['type'], $column->fields['atttypmod'])), "\" />\n";
132				// Add hidden variables to suppress error notices if we don't support altering column type
133				if (!$data->hasAlterColumnType()) {
134					echo "<input type=\"hidden\" name=\"type\" value=\"", htmlspecialchars($_REQUEST['type']), "\" />\n";
135					echo "<input type=\"hidden\" name=\"length\" value=\"", htmlspecialchars($_REQUEST['length']), "\" />\n";
136					echo "<input type=\"hidden\" name=\"array\" value=\"", htmlspecialchars($_REQUEST['array']), "\" />\n";
137				}
138				echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n";
139				echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
140				echo "</form>\n";
141				echo "<script type=\"text/javascript\">predefined_lengths = new Array(". implode(",",$escaped_predef_types) .");checkLengths(document.getElementById('type').value,'');</script>\n";
142				break;
143			case 2:
144				// Check inputs
145				if (trim($_REQUEST['field']) == '') {
146					$_REQUEST['stage'] = 1;
147					doAlter($lang['strcolneedsname']);
148					return;
149				}
150				if (!isset($_REQUEST['length'])) $_REQUEST['length'] = '';
151				$status = $data->alterColumn($_REQUEST['table'], $_REQUEST['column'], $_REQUEST['field'],
152							     isset($_REQUEST['notnull']), isset($_REQUEST['oldnotnull']),
153							     $_REQUEST['default'], $_REQUEST['olddefault'],
154							     $_REQUEST['type'], $_REQUEST['length'], $_REQUEST['array'], $_REQUEST['oldtype'],
155							     $_REQUEST['comment']);
156				if ($status == 0) {
157					if ($_REQUEST['column'] != $_REQUEST['field']) {
158						$_REQUEST['column'] = $_REQUEST['field'];
159						$_reload_browser = true;
160					}
161					doDefault($lang['strcolumnaltered']);
162				}
163				else {
164					$_REQUEST['stage'] = 1;
165					doAlter($lang['strcolumnalteredbad']);
166					return;
167				}
168				break;
169			default:
170				echo "<p>{$lang['strinvalidparam']}</p>\n";
171		}
172	}
173
174	/**
175	 * Show default list of columns in the table
176	 */
177	function doDefault($msg = '', $isTable = true) {
178		global $data, $conf, $misc, $tableName;
179		global $lang;
180
181		function attPre(&$rowdata) {
182			global $data;
183			$rowdata->fields['+type'] = $data->formatType($rowdata->fields['type'], $rowdata->fields['atttypmod']);
184		}
185
186		if (empty($_REQUEST['column']))
187			$msg.= "<br/>{$lang['strnoobjects']}";
188
189			$misc->printTrail('column');
190			//$misc->printTitle($lang['strcolprop']);
191			$misc->printTabs('column','properties');
192			$misc->printMsg($msg);
193
194		if (! empty($_REQUEST['column'])) {
195			// Get table
196			$tdata = $data->getTable($tableName);
197			// Get columns
198			$attrs = $data->getTableAttributes($tableName, $_REQUEST['column']);
199
200			// Show comment if any
201			if ($attrs->fields['comment'] !== null)
202				echo "<p class=\"comment\">", $misc->printVal($attrs->fields['comment']), "</p>\n";
203
204			$column = array(
205				'column' => array(
206					'title' => $lang['strcolumn'],
207					'field' => field('attname'),
208				),
209				'type' => array(
210					'title' => $lang['strtype'],
211					'field' => field('+type'),
212				)
213			);
214
215			if ($isTable) {
216				$column['notnull'] = array(
217					'title' => $lang['strnotnull'],
218					'field' => field('attnotnull'),
219					'type'  => 'bool',
220					'params'=> array('true' => 'NOT NULL', 'false' => '')
221				);
222				$column['default'] = array(
223					'title' => $lang['strdefault'],
224					'field' => field('adsrc'),
225				);
226			}
227
228			$actions=array();
229			$misc->printTable($attrs, $column, $actions, 'colproperties-colproperties', null, 'attPre');
230
231			echo "<br />\n";
232
233			$f_attname = $_REQUEST['column'];
234			$f_table = $tableName;
235			$f_schema = $data->_schema;
236			$data->fieldClean($f_attname);
237			$data->fieldClean($f_table);
238			$data->fieldClean($f_schema);
239			$query = "SELECT \"{$f_attname}\", count(*) AS \"count\" FROM \"{$f_schema}\".\"{$f_table}\" GROUP BY \"{$f_attname}\" ORDER BY \"{$f_attname}\"";
240
241			if ($isTable) {
242
243				/* Browse link */
244				/* FIXME browsing a col should somehow be a action so we don't
245				 * send an ugly SQL in the URL */
246
247				$navlinks = array (
248					'browse' => array (
249						'attr'=> array (
250							'href' => array (
251								'url' => 'display.php',
252								'urlvars' => array (
253									'subject' => 'column',
254									'server' => $_REQUEST['server'],
255									'database' => $_REQUEST['database'],
256									'schema' => $_REQUEST['schema'],
257									'table' => $tableName,
258									'column' => $_REQUEST['column'],
259									'return' => 'column',
260									'query' => $query
261								)
262							)
263						),
264						'content' => $lang['strbrowse'],
265					),
266					'alter' => array (
267						'attr'=> array (
268							'href' => array (
269								'url' => 'colproperties.php',
270								'urlvars' => array (
271									'action' => 'properties',
272									'server' => $_REQUEST['server'],
273									'database' => $_REQUEST['database'],
274									'schema' => $_REQUEST['schema'],
275									'table' => $tableName,
276									'column' => $_REQUEST['column'],
277								)
278							)
279						),
280						'content' => $lang['stralter'],
281					),
282					'drop' => array (
283						'attr'=> array (
284							'href' => array (
285								'url' => 'tblproperties.php',
286								'urlvars' => array (
287									'action' => 'confirm_drop',
288									'server' => $_REQUEST['server'],
289									'database' => $_REQUEST['database'],
290									'schema' => $_REQUEST['schema'],
291									'table' => $tableName,
292									'column' => $_REQUEST['column'],
293								)
294							)
295						),
296						'content' => $lang['strdrop'],
297					)
298				);
299			}
300			else {
301				/* Browse link */
302				$navlinks = array (
303					'browse' => array (
304						'attr'=> array (
305							'href' => array (
306								'url' => 'display.php',
307								'urlvars' => array (
308									'subject' => 'column',
309									'server' => $_REQUEST['server'],
310									'database' => $_REQUEST['database'],
311									'schema' => $_REQUEST['schema'],
312									'view' => $tableName,
313									'column' => $_REQUEST['column'],
314									'return' => 'column',
315									'query' => $query
316								)
317							)
318						),
319						'content' => $lang['strbrowse']
320					)
321				);
322			}
323
324			$misc->printNavLinks($navlinks, 'colproperties-colproperties', get_defined_vars());
325		}
326	}
327
328	$misc->printHeader($lang['strtables'] . ' - ' . $tableName);
329	$misc->printBody();
330
331	if (isset($_REQUEST['view']))
332		doDefault(null, false);
333	else
334		switch ($action) {
335			case 'properties':
336				if (isset($_POST['cancel'])) doDefault();
337				else doAlter();
338				break;
339			default:
340				doDefault();
341				break;
342		}
343
344	$misc->printFooter();
345
346?>
347