Skip to content

Commit

Permalink
Add automagic enable/disable of length attribute based on selected ty…
Browse files Browse the repository at this point in the history
…pe in table creation / alter screens.

Patch from John Jawed for Google SoC, with some cleanup and modifications from me.
  • Loading branch information
xzilla committed Aug 18, 2006
1 parent f58a041 commit 10ff95e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
5 changes: 4 additions & 1 deletion classes/database/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* A class that implements the DB interface for Postgres
* Note: This class uses ADODB and returns RecordSets.
*
* $Id: Postgres.php,v 1.289 2006/08/13 15:31:13 xzilla Exp $
* $Id: Postgres.php,v 1.290 2006/08/18 21:02:41 xzilla Exp $
*/

// @@@ THOUGHT: What about inherits? ie. use of ONLY???
Expand Down Expand Up @@ -150,6 +150,9 @@ class Postgres extends ADODB_base {

// Help sub pages
var $help_page;

// Predefined size types
var $predefined_size_types = array('abstime','aclitem','bigserial','boolean','bytea','cid','cidr','circle','date','float4','float8','gtsvector','inet','int2','int4','int8','macaddr','money','oid','path','polygon','refcursor','regclass','regoper','regoperator','regproc','regprocedure','regtype','reltime','serial','smgr','text','tid','tinterval','tsquery','tsvector','varbit','void','xid');

/**
* Constructor
Expand Down
18 changes: 18 additions & 0 deletions tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var predefined_lengths = null;
var sizesLength = false;

function checkLengths(sValue,idx) {
if(predefined_lengths) {
if(sizesLength==false) {
sizesLength = predefined_lengths.length;
}
for(var i=0;i<sizesLength;i++) {
if(sValue.toString().toUpperCase()==predefined_lengths[i].toString().toUpperCase()) {
document.getElementById("lengths"+idx).value='';
document.getElementById("lengths"+idx).disabled='on';
return;
}
}
document.getElementById("lengths"+idx).disabled='';
}
}
28 changes: 21 additions & 7 deletions tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* List tables in a database
*
* $Id: tables.php,v 1.81 2006/08/04 20:42:24 xzilla Exp $
* $Id: tables.php,v 1.82 2006/08/18 21:02:41 xzilla Exp $
*/

// Include application functions
Expand Down Expand Up @@ -98,11 +98,13 @@ function doCreate($msg = '') {
}

$types = $data->getTypes(true, false, true);

$types_for_js = array();

$misc->printTrail('schema');
$misc->printTitle($lang['strcreatetable'], 'pg.table.create');
$misc->printMsg($msg);

echo "<script src=\"tables.js\" type=\"text/javascript\"></script>";
echo "<form action=\"$PHP_SELF\" method=\"post\">\n";

// Output table header
Expand All @@ -121,30 +123,40 @@ function doCreate($msg = '') {
echo "\t<tr>\n\t\t<td>", $i + 1, ".&nbsp;</td>\n";
echo "\t\t<td><input name=\"field[{$i}]\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
htmlspecialchars($_REQUEST['field'][$i]), "\" /></td>\n";
echo "\t\t<td>\n\t\t\t<select name=\"type[{$i}]\">\n";
echo "\t\t<td>\n\t\t\t<select name=\"type[{$i}]\" id=\"types{$i}\" onchange=\"checkLengths(this.options[this.selectedIndex].value,{$i});\">\n";
// Output any "magic" types
foreach ($data->extraTypes as $v) {
$types_for_js[strtolower($v)] = 1;
echo "\t\t\t\t<option value=\"", htmlspecialchars($v), "\"",
(isset($_REQUEST['type'][$i]) && $v == $_REQUEST['type'][$i]) ? ' selected="selected"' : '', ">",
$misc->printVal($v), "</option>\n";
}
$types->moveFirst();
while (!$types->EOF) {
$typname = $types->f['typname'];
$types_for_js[$typname] = 1;
echo "\t\t\t\t<option value=\"", htmlspecialchars($typname), "\"",
(isset($_REQUEST['type'][$i]) && $typname == $_REQUEST['type'][$i]) ? ' selected="selected"' : '', ">",
$misc->printVal($typname), "</option>\n";
$types->moveNext();
}
echo "\t\t\t</select>\n\t\t</td>\n";
echo "\t\t\t</select>\n\t\t\n";
if($i==0) { // only define js types array once
$predefined_size_types = array_intersect($data->predefined_size_types,array_keys($types_for_js));
$escaped_predef_types = array(); // the JS escaped array elements
foreach($predefined_size_types as $value) {
$escaped_predef_types[] = "'{$value}'";
}
echo "<script type=\"text/javascript\">predefined_lengths = new Array(". implode(",",$escaped_predef_types) .");</script>\n\t</td>";
}

// Output array type selector
echo "\t\t<td>\n\t\t\t<select name=\"array[{$i}]\">\n";
echo "\t\t\t\t<option value=\"\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '') ? ' selected="selected"' : '', "></option>\n";
echo "\t\t\t\t<option value=\"[]\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n";
echo "\t\t\t</select>\n\t\t</td>\n";

echo "\t\t<td><input name=\"length[{$i}]\" size=\"10\" value=\"",
echo "\t\t<td><input name=\"length[{$i}]\" id=\"lengths{$i}\" size=\"10\" value=\"",
htmlspecialchars($_REQUEST['length'][$i]), "\" /></td>\n";
echo "\t\t<td><input type=\"checkbox\" name=\"notnull[{$i}]\"", (isset($_REQUEST['notnull'][$i])) ? ' checked="checked"' : '', " /></td>\n";
echo "\t\t<td align=\"center\"><input type=\"checkbox\" name=\"uniquekey[{$i}]\""
Expand All @@ -155,7 +167,9 @@ function doCreate($msg = '') {
echo "\t\t<td><input name=\"default[{$i}]\" size=\"20\" value=\"",
htmlspecialchars($_REQUEST['default'][$i]), "\" /></td>\n";
echo "\t\t<td><input name=\"colcomment[{$i}]\" size=\"40\" value=\"",
htmlspecialchars($_REQUEST['colcomment'][$i]), "\" /></td>\n\t</tr>\n";
htmlspecialchars($_REQUEST['colcomment'][$i]), "\" />
<script type=\"text/javascript\">checkLengths(document.getElementById('types{$i}').value,{$i});</script>
</td>\n\t</tr>\n";
}
echo "</table>\n";
echo "<p><input type=\"hidden\" name=\"action\" value=\"create\" />\n";
Expand Down Expand Up @@ -445,7 +459,7 @@ function doInsertRow($confirm, $msg = '') {
}
echo "</p>\n";
echo "</form>\n";
echo "<script>rEB(document.getElementById('no_ac').checked);</script>";
echo "<script type=\"text/javascript\">rEB(document.getElementById('no_ac').checked);</script>";
}
else {
if (!isset($_POST['values'])) $_POST['values'] = array();
Expand Down
42 changes: 31 additions & 11 deletions tblproperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* List tables in a database
*
* $Id: tblproperties.php,v 1.71 2006/08/03 19:03:32 xzilla Exp $
* $Id: tblproperties.php,v 1.72 2006/08/18 21:02:41 xzilla Exp $
*/

// Include application functions
Expand Down Expand Up @@ -241,11 +241,12 @@ function doAddColumn($msg = '') {

// Fetch all available types
$types = $data->getTypes(true, false, true);
$types_for_js = array();

$misc->printTrail('table');
$misc->printTitle($lang['straddcolumn'], 'pg.column.add');
$misc->printMsg($msg);

echo "<script src=\"tables.js\" type=\"text/javascript\"></script>";
echo "<form action=\"$PHP_SELF\" method=\"post\">\n";

// Output table header
Expand All @@ -257,17 +258,19 @@ function doAddColumn($msg = '') {

echo "<tr><td><input name=\"field\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
htmlspecialchars($_POST['field']), "\" /></td>";
echo "<td><select name=\"type\">\n";
echo "<td><select name=\"type\" id=\"type\" onchange=\"checkLengths(document.getElementById('type').value,'');\">\n";
// Output any "magic" types. This came in with the alter column type so we'll check that
if ($data->hasAlterColumnType()) {
foreach ($data->extraTypes as $v) {
$types_for_js[strtolower($v)] = 1;
echo "<option value=\"", htmlspecialchars($v), "\"",
($v == $_POST['type']) ? ' selected="selected"' : '', ">",
$misc->printVal($v), "</option>\n";
}
}
while (!$types->EOF) {
$typname = $types->f['typname'];
$types_for_js[$typname] = 1;
echo "<option value=\"", htmlspecialchars($typname), "\"", ($typname == $_POST['type']) ? ' selected="selected"' : '', ">",
$misc->printVal($typname), "</option>\n";
$types->moveNext();
Expand All @@ -278,9 +281,15 @@ function doAddColumn($msg = '') {
echo "<td><select name=\"array\">\n";
echo "<option value=\"\"", ($_POST['array'] == '') ? ' selected="selected"' : '', "></option>\n";
echo "<option value=\"[]\"", ($_POST['array'] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n";
echo "</select></td>\n";
echo "</select>";
$predefined_size_types = array_intersect($data->predefined_size_types,array_keys($types_for_js));
$escaped_predef_types = array(); // the JS escaped array elements
foreach($predefined_size_types as $value) {
$escaped_predef_types[] = "'{$value}'";
}
echo "\t</td>\n";

echo "<td><input name=\"length\" size=\"8\" value=\"",
echo "<td><input name=\"length\" id=\"lengths\" size=\"8\" value=\"",
htmlspecialchars($_POST['length']), "\" /></td>";
// Support for adding column with not null and default
if ($data->hasAlterColumnType()) {
Expand All @@ -302,7 +311,7 @@ function doAddColumn($msg = '') {
echo "<p><input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
echo "</form>\n";

echo "<script type=\"text/javascript\">predefined_lengths = new Array(". implode(",",$escaped_predef_types) .");checkLengths(document.getElementById('type').value,'');</script>\n";
break;
case 2:
global $data, $lang;
Expand Down Expand Up @@ -348,11 +357,12 @@ function doProperties($msg = '') {
$misc->printTrail('column');
$misc->printTitle($lang['straltercolumn'], 'pg.column.alter');
$misc->printMsg($msg);
echo "<script src=\"tables.js\" type=\"text/javascript\"></script>";

echo "<form action=\"$PHP_SELF\" method=\"post\">\n";

// Output table header
echo "<table>\n<tr>";
echo "<table>\n";
echo "<tr><th class=\"data required\">{$lang['strname']}</th>";
if ($data->hasAlterColumnType()) {
echo "<th class=\"data required\" colspan=\"2\">{$lang['strtype']}</th>";
Expand Down Expand Up @@ -401,16 +411,19 @@ function doProperties($msg = '') {
if ($data->hasAlterColumnType()) {
// Fetch all available types
$types = $data->getTypes(true, false, true);
$types_for_js = array();

echo "<td><select name=\"type\">\n";
echo "<td><select name=\"type\" id=\"type\" onchange=\"checkLengths(document.getElementById('type').value,'');\">\n";
// Output any "magic" types. This came in with Alter Column Type so we don't need to check that
foreach ($data->extraTypes as $v) {
$types_for_js[strtolower($v)] = 1;
echo "<option value=\"", htmlspecialchars($v), "\"",
($v == $_REQUEST['type']) ? ' selected="selected"' : '', ">",
$misc->printVal($v), "</option>\n";
}
while (!$types->EOF) {
$typname = $types->f['typname'];
$types_for_js[$typname] = 1;
echo "<option value=\"", htmlspecialchars($typname), "\"", ($typname == $_REQUEST['type']) ? ' selected="selected"' : '', ">",
$misc->printVal($typname), "</option>\n";
$types->moveNext();
Expand All @@ -421,9 +434,15 @@ function doProperties($msg = '') {
echo "<td><select name=\"array\">\n";
echo "<option value=\"\"", ($_REQUEST['array'] == '') ? ' selected="selected"' : '', "></option>\n";
echo "<option value=\"[]\"", ($_REQUEST['array'] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n";
echo "</select></td>\n";

echo "<td><input name=\"length\" size=\"8\" value=\"",
echo "</select>";
$predefined_size_types = array_intersect($data->predefined_size_types,array_keys($types_for_js));
$escaped_predef_types = array(); // the JS escaped array elements
foreach($predefined_size_types as $value) {
$escaped_predef_types[] = "'{$value}'";
}
echo "\t</td>\n";

echo "<td><input name=\"length\" id=\"lengths\" size=\"8\" value=\"",
htmlspecialchars($_REQUEST['length']), "\" /></td>";
} else {
// Otherwise draw the read-only type name
Expand Down Expand Up @@ -454,6 +473,7 @@ function doProperties($msg = '') {
echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n";
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
echo "</form>\n";
echo "<script type=\"text/javascript\">predefined_lengths = new Array(". implode(",",$escaped_predef_types) .");checkLengths(document.getElementById('type').value,'');</script>\n";

break;
case 2:
Expand Down

0 comments on commit 10ff95e

Please sign in to comment.