Refactored the tbl_select.php and tbl_zoom_select.php code in libraries/tbl_select.lib.php

Parts refactored are:
-> Setting titles
-> getting fields list
-> setting sub-tabs
-> setting table header of QBE display
-> displaying foreign data
-> search criteria input elements
This commit is contained in:
Ammar Yasir 2011-05-31 19:02:50 +05:30
parent e794690417
commit c7cef3b0ba
4 changed files with 254 additions and 188 deletions

View File

@ -22,16 +22,18 @@ $(document).ready(function() {
cache: 'false'
});
/*
* Handle events from zoom-search page
*
*/
/*if($('#id_flag').val()==2)
{
$('#tbl_search_form').hide();
$('#zoom_search_form').show();
$('#tableid_1').change(function() {
}*/
$('#tableid_0').change(function() {
$('#zoom_search_form').submit();
})
$('#tableid_2').change(function() {
$('#tableid_1').change(function() {
$('#zoom_search_form').submit();
})

View File

@ -9,6 +9,54 @@
* @package phpMyAdmin
*/
require_once 'url_generating.lib.php';
/**
* PMA_tbl_setTitle() sets the title for foreign keys display link
*
* @param $propertiesIconic Type of icon property
* @param $themeImage Icon Image
* @return string $str Value of the Title
*
*/
function PMA_tbl_setTitle($propertiesIconic,$pmaThemeImage){
if ($propertiesIconic == true) {
$str = '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
.'b_browse.png" alt="' . __('Browse foreign values') . '" title="'
. __('Browse foreign values') . '" />';
if ($propertiesIconic === 'both') {
$str .= __('Browse foreign values');
return $str;
}
} else {
return __('Browse foreign values');
}
}
/**
* PMA_tbl_getFields() gets all the fields of a table along with their types,collations and whether null or not.
*
* @uses PMA_DBI_query()
* @uses PMA_backquote()
* @uses PMA_DBI_num_rows()
* @uses PMA_DBI_fetch_assoc()
* @uses PMA_DBI_free_result()
* @uses preg_replace()
* @uses str_replace()
* @uses strncasecmp()
* @uses empty()
*
* @param $db Selected database
* @param $table Selected table
*
* @return array($fields_list,$fields_type,$fields_collation,$fields_null) Array containing the field list, field types, collations and null constatint
*
*/
function PMA_tbl_getFields($table,$db) {
// Gets the list and number of fields
@ -51,10 +99,179 @@ function PMA_tbl_getFields($table,$db) {
}
/* PMA_tbl_setTableHeader() sets the table header for displaying a table in query-by-example format
*
* @return HTML content, the tags and content for table header
*
*/
function PMA_tbl_setTableHeader(){
return '<thead>
<tr><th>' . __('Column') . '</th>
<th>' . __('Type') . '</th>
<th>' . __('Collation') . '</th>
<th>' . __('Operator') . '</th>
<th>' . __('Value') . '</th>
</tr>
</thead>';
}
/* PMA_tbl_getSubTabs() returns an array with necessary configrations to create sub-tabs(Table Search and Zoom Search) in the table_select page
*
* @return array $subtabs Array containing configuration (icon,text,link,id,args) of sub-tabs for Table Search and Zoom search
*
*/
function PMA_tbl_getSubTabs(){
$subtabs = array();
$subtabs['search']['icon'] = 'b_search.png';
$subtabs['search']['text'] = __('Table Search');
$subtabs['search']['link'] = 'tbl_select.php';
$subtabs['search']['id'] = 'tbl_search_id';
$subtabs['search']['args']['pos'] = 0;
$subtabs['zoom']['icon'] = 'b_props.png';
$subtabs['zoom']['link'] = 'tbl_zoom_select.php';
$subtabs['zoom']['text'] = __('Zoom Search');
$subtabs['zoom']['id'] = 'zoom_search_id';
return $subtabs;
}
/* PMA_tbl_getForeignFields_Values() creates the HTML content for: 1) Browsing foreign data for a field. 2) Creating elements for search criteria input on fields.
*
* @uses PMA_foreignDropdown
* @uses PMA_generate_common_url
* @uses isset()
* @uses is_array()
* @uses in_array()
* @uses urlencode()
* @uses str_replace()
* @uses stbstr()
*
* @param $foreigners Array of foreign keys
* @param $foreignData Foreign keys data
* @param $field Column name
* @param $tbl_fields_type Column type
* @param $i Column index
* @param $db Selected database
* @param $table Selected table
* @param $titles Selected title
* @param $foreignMaxLimit Max limit of displaying foreign elements
* @param $fields Array of search criteria inputs
*
* @return string $str HTML content for viewing foreing data and elements for search criteria input.
*
*/
function PMA_getForeignFields_Values($foreigners, $foreignData, $field, $tbl_fields_type, $i, $db, $table,$titles,$foreignMaxLimit, $fields){
$str = '';
if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
// f o r e i g n k e y s
$str .= ' <select name="fields[' . $i . ']">' . "\n";
// go back to first row
// here, the 4th parameter is empty because there is no current
// value of data for the dropdown (the search page initial values
// are displayed empty)
$str .= PMA_foreignDropdown($foreignData['disp_row'],
$foreignData['foreign_field'],
$foreignData['foreign_display'],
'', $foreignMaxLimit);
$str .= ' </select>' . "\n";
} elseif ($foreignData['foreign_link'] == true) {
$str .= '<input type="text" name="fields[' . $i . '] "';
'id="field_' . md5($field) . '[' . $i .']"
class="textfield" />' ; ?>
<?php $str .= '<script type="text/javascript">';
// <![CDATA[
$str .= <<<EOT
document.writeln('<a target="_blank" onclick="window.open(this.href, \'foreigners\', \'width=640,height=240,scrollbars=yes\'); return false" href="browse_foreigners.php?
EOT;
$str .= '' . PMA_generate_common_url($db, $table) . '&amp;field=' . urlencode($field) . '&amp;fieldkey=' . $i . '">' . str_replace("'", "\'", $titles['Browse']) . '</a>\');';
// ]]
$str .= '</script>';
?>
<?php
} elseif (strncasecmp($tbl_fields_type[$i], 'enum', 4) == 0) {
// e n u m s
$enum_value=explode(', ', str_replace("'", '', substr($tbl_fields_type[$i], 5, -1)));
$cnt_enum_value = count($enum_value);
$str .= ' <select name="fields[' . ($i) . '][]"'
.' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
for ($j = 0; $j < $cnt_enum_value; $j++) {
if(isset($fields[$i]) && is_array($fields[$i]) && in_array($enum_value[$j],$fields[$i])){
$str .= ' <option value="' . $enum_value[$j] . '" Selected>'
. $enum_value[$j] . '</option>';
}
else{
$str .= ' <option value="' . $enum_value[$j] . '">'
. $enum_value[$j] . '</option>';
}
} // end for
$str .= ' </select>' . "\n";
} else {
// o t h e r c a s e s
$the_class = 'textfield';
$type = $tbl_fields_type[$i];
if ($type == 'date') {
$the_class .= ' datefield';
} elseif ($type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
$the_class .= ' datetimefield';
}
if(isset($fields[$i]) && is_string($fields[$i])){
$str .= ' <input type="text" name="fields[' . $i . ']"'
.' size="40" class="' . $the_class . '" id="field_' . $i . '" value = "' . $fields[$i] . '"/>' . "\n";
}
else{
$str .= ' <input type="text" name="fields[' . $i . ']"'
.' size="40" class="' . $the_class . '" id="field_' . $i . '" />' . "\n";
}
};
return $str;
}
/* PMA_tbl_search_getWhereClause() Return the where clause for query generation based on the inputs provided.
*
* @uses PMA_backquote
* @uses PMA_sqlAddslashes
* @uses preg_match
* @uses isset()
* @uses in_array()
* @uses str_replace()
* @uses strpos()
* @uses explode()
* @uses trim()
*
* @param $fields Search criteria input
* @param $names Name of the field(column) on which search criteria is submitted
* @param $types Type of the field
* @param $collations Field collation
* @param $func_type Search fucntion/operator
* @param $unaryFlag Whether operator unary or not
*
* @return string $str HTML content for viewing foreing data and elements for search criteria input.
*
*/
function PMA_tbl_search_getWhereClause($fields, $names, $types, $collations, $func_type, $unaryFlag){
//Return the where clause for query generation based on the inputs provided in the tbl_select.php form
$w = '';
if($unaryFlag){
$fields = '';
$w = PMA_backquote($names) . ' ' . $func_type;
@ -128,24 +345,5 @@ function PMA_tbl_search_getWhereClause($fields, $names, $types, $collations, $fu
return $w;
}
function PMA_tbl_getSubTabs(){
$subtabs = array();
$subtabs['search']['icon'] = 'b_search.png';
$subtabs['search']['text'] = __('Table Search');
$subtabs['search']['link'] = 'tbl_select.php';
$subtabs['search']['id'] = 'tbl_search_id';
$subtabs['search']['args']['pos'] = 0;
$subtabs['zoom']['icon'] = 'b_props.png';
$subtabs['zoom']['link'] = 'tbl_zoom_select.php';
$subtabs['zoom']['text'] = __('Zoom Search');
$subtabs['zoom']['id'] = 'zoom_search_id';
return $subtabs;
}
?>

View File

@ -21,18 +21,8 @@ $GLOBALS['js_include'][] = 'sql.js';
$GLOBALS['js_include'][] = 'tbl_select.js';
$GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
$GLOBALS['js_include'][] = 'jquery/timepicker.js';
if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
$titles['Browse'] =
'<img class="icon" width="16" height="16" src="' . $pmaThemeImage
.'b_browse.png" alt="' . __('Browse foreign values') . '" title="'
. __('Browse foreign values') . '" />';
if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
$titles['Browse'] .= __('Browse foreign values');
}
} else {
$titles['Browse'] = __('Browse foreign values');
}
$titles['Browse'] = PMA_tbl_setTitle($GLOBALS['cfg']['PropertiesIconic'], $pmaThemeImage);
/**
* Not selection yet required -> displays the selection form
@ -72,7 +62,6 @@ if (! isset($param) || $param[0] == '') {
// check also foreigners even if relwork is FALSE (to get
// foreign keys from innodb)
$foreigners = PMA_getForeigners($db, $table);
$flag = 1;
?>
<fieldset id="fieldset_subtab">
@ -95,14 +84,7 @@ echo PMA_generate_html_tabs(PMA_tbl_getSubTabs(), $url_params);
<fieldset id="fieldset_table_qbe">
<legend><?php echo __('Do a "query by example" (wildcard: "%")') ?></legend>
<table class="data">
<thead>
<tr><th><?php echo __('Column'); ?></th>
<th><?php echo __('Type'); ?></th>
<th><?php echo __('Collation'); ?></th>
<th><?php echo __('Operator'); ?></th>
<th><?php echo __('Value'); ?></th>
</tr>
</thead>
<?php echo PMA_tbl_setTableHeader(); ?>
<tbody>
<?php
$odd_row = true;
@ -151,54 +133,9 @@ echo PMA_generate_html_tabs(PMA_tbl_getSubTabs(), $url_params);
$foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
// f o r e i g n k e y s
echo ' <select name="fields[' . $i . ']">' . "\n";
// go back to first row
// here, the 4th parameter is empty because there is no current
// value of data for the dropdown (the search page initial values
// are displayed empty)
echo PMA_foreignDropdown($foreignData['disp_row'],
$foreignData['foreign_field'],
$foreignData['foreign_display'],
'', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
echo ' </select>' . "\n";
} elseif ($foreignData['foreign_link'] == true) {
?>
<input type="text" name="fields[<?php echo $i; ?>]"
id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
class="textfield" />
<script type="text/javascript">
// <![CDATA[
document.writeln('<a target="_blank" onclick="window.open(this.href, \'foreigners\', \'width=640,height=240,scrollbars=yes\'); return false" href="browse_foreigners.php?<?php echo PMA_generate_common_url($db, $table); ?>&amp;field=<?php echo urlencode($field); ?>&amp;fieldkey=<?php echo $i; ?>"><?php echo str_replace("'", "\'", $titles['Browse']); ?></a>');
// ]]>
</script>
<?php
} elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
// e n u m s
$enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
$cnt_enum_value = count($enum_value);
echo ' <select name="fields[' . $i . '][]"'
.' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
for ($j = 0; $j < $cnt_enum_value; $j++) {
echo ' <option value="' . $enum_value[$j] . '">'
. $enum_value[$j] . '</option>';
} // end for
echo ' </select>' . "\n";
} else {
// o t h e r c a s e s
$the_class = 'textfield';
$type = $fields_type[$i];
if ($type == 'date') {
$the_class .= ' datefield';
} elseif ($type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
$the_class .= ' datetimefield';
}
echo ' <input type="text" name="fields[' . $i . ']"'
.' size="40" class="' . $the_class . '" id="field_' . $i . '" />' . "\n";
};
?>
echo PMA_getForeignFields_Values($foreigners, $foreignData, $field, $fields_type, $i, $db, $table, $titles,$GLOBALS['cfg']['ForeignKeyMaxLimit'], '' );
?>
<input type="hidden" name="names[<?php echo $i; ?>]"
value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
<input type="hidden" name="types[<?php echo $i; ?>]"
@ -285,7 +222,6 @@ echo PMA_generate_html_tabs(PMA_tbl_getSubTabs(), $url_params);
</fieldset>
<?php
print_r($inputs);
}

View File

@ -18,18 +18,9 @@ $GLOBALS['js_include'][] = 'sql.js';
$GLOBALS['js_include'][] = 'tbl_select.js';
$GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
$GLOBALS['js_include'][] = 'jquery/timepicker.js';
if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
$titles['Browse'] =
'<img class="icon" width="16" height="16" src="' . $pmaThemeImage
.'b_browse.png" alt="' . __('Browse foreign values') . '" title="'
. __('Browse foreign values') . '" />';
if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
$titles['Browse'] .= __('Browse foreign values');
}
} else {
$titles['Browse'] = __('Browse foreign values');
}
$titles['Browse'] = PMA_tbl_setTitle($GLOBALS['cfg']['PropertiesIconic'], $pmaThemeImage);
/**
* Not selection yet required -> displays the selection form
*/
@ -114,26 +105,19 @@ if(isset($inputs) && ($inputs[0] != __('pma_null') || $inputs[1] != __('pma_null
<fieldset id="zoom_fieldset_table_qbe">
<legend><?php echo __('Do a "query by example" (wildcard: "%") for two columns') ?></legend>
<table class="data">
<thead>
<tr><th><?php echo __('Column'); ?></th>
<th><?php echo __('Type'); ?></th>
<th><?php echo __('Collation'); ?></th>
<th><?php echo __('Operator'); ?></th>
<th><?php echo __('Value'); ?></th>
</tr>
</thead>
<tbody>
<?php echo PMA_tbl_setTableHeader();?>
<tbody-->
<?php
$odd_row = true;
for($i=1 ; $i<3 ; $i++){
for($i=0 ; $i<2 ; $i++){
?>
<tr class="noclick <?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
<th><select name="inputs[]" id=<?php echo 'tableid_' . $i?> >
<option value= <?php echo __('pma_null')?>><?php echo __('None'); ?> </option>
<?php
for ($j = 0; $j < $fields_cnt; $j++){
if(isset($inputs[$i-1]) && $inputs[$i-1]==htmlspecialchars($fields_list[$j])){?>
if(isset($inputs[$i]) && $inputs[$i]==htmlspecialchars($fields_list[$j])){?>
<option value=<?php echo htmlspecialchars($fields_list[$j]);?> Selected> <?php echo htmlspecialchars($fields_list[$j]);?></option>
<?php
}
@ -143,21 +127,21 @@ if(isset($inputs) && ($inputs[0] != __('pma_null') || $inputs[1] != __('pma_null
}
} ?>
</select></th>
<td><?php if(isset($tbl_fields_type[$i-1]))echo $tbl_fields_type[$i-1]; ?></td>
<td><?php if(isset($tbl_fields_collation[$i-1]))echo $tbl_fields_collation[$i-1]; ?></td>
<td><?php if(isset($tbl_fields_type[$i]))echo $tbl_fields_type[$i]; ?></td>
<td><?php if(isset($tbl_fields_collation[$i]))echo $tbl_fields_collation[$i]; ?></td>
<td>
<?php if(isset($inputs) && $inputs[$i-1] != __('pma_null')){ ?>
<?php if(isset($inputs) && $inputs[$i] != __('pma_null')){ ?>
<select name="zoomFunc[]">
<?php
if (strncasecmp($tbl_fields_type[$i-1], 'enum', 4) == 0) {
if (strncasecmp($tbl_fields_type[$i], 'enum', 4) == 0) {
foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
echo "\n" . ' '
. '<option value="' . htmlspecialchars($fc) . '">'
. htmlspecialchars($fc) . '</option>';
}
} elseif (preg_match('@char|blob|text|set@i', $tbl_fields_type[$i-1])) {
} elseif (preg_match('@char|blob|text|set@i', $tbl_fields_type[$i])) {
foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
echo "\n" . ' '
. '<option value="' . htmlspecialchars($fc) . '">'
@ -170,7 +154,7 @@ if(isset($inputs) && ($inputs[0] != __('pma_null') || $inputs[1] != __('pma_null
. htmlspecialchars($fc) . '</option>';
}
} // end if... else...
if ($tbl_fields_null[$i-1]) {
if ($tbl_fields_null[$i]) {
foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
echo "\n" . ' '
. '<option value="' . htmlspecialchars($fc) . '">'
@ -183,75 +167,21 @@ if(isset($inputs) && ($inputs[0] != __('pma_null') || $inputs[1] != __('pma_null
</td>
<td>
<?php
$field = $inputs[$i-1];
$field = $inputs[$i];
$foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
// f o r e i g n k e y s
echo ' <select name="fields[' . ($i-1) . ']">' . "\n";
// go back to first row
// here, the 4th parameter is empty because there is no current
// value of data for the dropdown (the search page initial values
// are displayed empty)
echo PMA_foreignDropdown($foreignData['disp_row'],
$foreignData['foreign_field'],
$foreignData['foreign_display'],
'', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
echo ' </select>' . "\n";
} elseif ($foreignData['foreign_link'] == true) {
?>
<input type="text" name="fields[<?php echo $i-1; ?>]"
id="field_<?php echo md5($field); ?>[<?php echo $i-1; ?>]"
class="textfield" />
<script type="text/javascript">
// <![CDATA[
document.writeln('<a target="_blank" onclick="window.open(this.href, \'foreigners\', \'width=640,height=240,scrollbars=yes\'); return false" href="browse_foreigners.php?<?php echo PMA_generate_common_url($db, $table); ?>&amp;field=<?php echo urlencode($field); ?>&amp;fieldkey=<?php echo $i; ?>"><?php echo str_replace("'", "\'", $titles['Browse']); ?></a>');
// ]]>
</script>
<?php
} elseif (strncasecmp($tbl_fields_type[$i-1], 'enum', 4) == 0) {
// e n u m s
$enum_value=explode(', ', str_replace("'", '', substr($tbl_fields_type[$i-1], 5, -1)));
$cnt_enum_value = count($enum_value);
echo ' <select name="fields[' . ($i-1) . '][]"'
.' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
for ($j = 0; $j < $cnt_enum_value; $j++) {
if(isset($fields[$i-1]) && is_array($fields[$i-1]) && in_array($enum_value[$j],$fields[$i-1])){
echo ' <option value="' . $enum_value[$j] . '" Selected>'
. $enum_value[$j] . '</option>';
}
else{
echo ' <option value="' . $enum_value[$j] . '">'
. $enum_value[$j] . '</option>';
}
} // end for
echo ' </select>' . "\n";
} else {
// o t h e r c a s e s
$the_class = 'textfield';
$type = $tbl_fields_type[$i-1];
if ($type == 'date') {
$the_class .= ' datefield';
} elseif ($type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
$the_class .= ' datetimefield';
}
if(isset($fields[$i-1]) && is_string($fields[$i-1])){
echo ' <input type="text" name="fields[' . ($i-1) . ']"'
.' size="40" class="' . $the_class . '" id="field_' . ($i-1) . '" value = "' . $fields[$i-1] . '"/>' . "\n";
}
else{
echo ' <input type="text" name="fields[' . ($i-1) . ']"'
.' size="40" class="' . $the_class . '" id="field_' . ($i-1) . '" />' . "\n";
}
};
}
if (isset($fields))
echo PMA_getForeignFields_Values($foreigners, $foreignData, $field, $tbl_fields_type, $i ,$db, $table, $titles, $GLOBALS['cfg']['ForeignKeyMaxLimit'], $fields);
else
echo PMA_getForeignFields_Values($foreigners, $foreignData, $field, $tbl_fields_type, $i ,$db, $table, $titles, $GLOBALS['cfg']['ForeignKeyMaxLimit'], '');
}
?>
<input type="hidden" name="types[<?php echo ($i-1); ?>]"
value="<?php if(isset($tbl_fields_type[$i-1]))echo $tbl_fields_type[$i-1]; ?>" />
<input type="hidden" name="types[<?php echo $i; ?>]"
value="<?php if(isset($tbl_fields_type[$i]))echo $tbl_fields_type[$i]; ?>" />
<input type="hidden" name="collations[<?php echo $i; ?>]"
value="<?php if(isset($tbl_fields_collation[$i-1]))echo $tbl_fields_collation[$i-1]; ?>" />
value="<?php if(isset($tbl_fields_collation[$i]))echo $tbl_fields_collation[$i]; ?>" />
</td>
</tr>