1
0
mirror of https://github.com/phpmyadmin/phpmyadmin.git synced 2026-10-29 21:24:44 +03:00
Files
phpmyadmin/libraries/insert_edit.lib.php
2012-05-11 07:04:27 +05:30

494 lines
16 KiB
PHP

<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* set of functions with the insert/edit features in pma
*
* @package PhpMyAdmin
*/
if (! defined('PHPMYADMIN')) {
exit;
}
/**
* Retrieve form parameters for insert/edit form
*
* @param array $where_clauses
* @param array $where_clause_array
* @return array $_form_params
*/
function PMA_getFormParametersForInsertForm($paramArray, $where_clauses, $where_clause_array, $err_url)
{
list($table, $db) = $paramArray;
$_form_params = array(
'db' => $db,
'table' => $table,
'goto' => $GLOBALS['goto'],
'err_url' => $err_url,
'sql_query' => $_REQUEST['sql_query'],
);
if (isset($where_clauses)) {
foreach ($where_clause_array as $key_id => $where_clause) {
$_form_params['where_clause[' . $key_id . ']'] = trim($where_clause);
}
}
if (isset($_REQUEST['clause_is_unique'])) {
$_form_params['clause_is_unique'] = $_REQUEST['clause_is_unique'];
}
return $_form_params;
}
/**
* Retrieve the values for pma edit mode
*
* @param array $paramArray
* @param boolean $found_unique_key
* @return array
*/
function PMA_getValuesForEditMode($paramArray)
{
$found_unique_key = false;
list($table, $db) = $paramArray;
if (isset($_REQUEST['where_clause'])) {
$where_clause_array = PMA_getWhereClauseArray();
list($whereClauses, $resultArray, $rowsArray, $found_unique_key)
= PMA_analyzeWhereClauses($where_clause_array, $paramArray, $found_unique_key);
return array(false, $whereClauses, $resultArray, $rowsArray, $where_clause_array, $found_unique_key);
} else {
list($results, $row) = PMA_loadFirstRowInEditMode($paramArray);
return array(true, null, $results, $row, null, $found_unique_key);
}
}
/**
*
* @return whereClauseArray
*/
function PMA_getWhereClauseArray()
{
if(isset ($_REQUEST['where_clause'])) {
if (is_array($_REQUEST['where_clause'])) {
return $_REQUEST['where_clause'];
} else {
return array(0 => $_REQUEST['where_clause']);
}
}
}
/**
* Analysing where cluases array
*
* @param array $where_clause_array
* @param array $paramArray
* @param boolean $found_unique_key
* @return array $where_clauses, $result, $rows
*/
function PMA_analyzeWhereClauses($where_clause_array, $paramArray, $found_unique_key)
{
list($table, $db) = $paramArray;
var_dump($paramArray);
$rows = array();
$result = array();
$where_clauses = array();
foreach ($where_clause_array as $key_id => $where_clause) {
$local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table)
. ' WHERE ' . $where_clause . ';';
$result[$key_id] = PMA_DBI_query($local_query, null, PMA_DBI_QUERY_STORE);
$rows[$key_id] = PMA_DBI_fetch_assoc($result[$key_id]);
$where_clauses[$key_id] = str_replace('\\', '\\\\', $where_clause);
$found_unique_key = PMA_showEmptyResultMessageOrSetUniqueCondition($rows, $key_id, $where_clause_array, $local_query, $result, $found_unique_key);
}
return array($where_clauses, $result, $rows, $found_unique_key);
}
/**
* Show message for empty reult or set the unique_condition
*
* @param array $rows
* @param string $key_id
* @param array $where_clause_array
* @param string $local_query
* @param array $result
* @param boolean $found_unique_key
*/
function PMA_showEmptyResultMessageOrSetUniqueCondition($rows, $key_id, $where_clause_array, $local_query, $result, $found_unique_key)
{
// No row returned
if (! $rows[$key_id]) {
unset($rows[$key_id], $where_clause_array[$key_id]);
PMA_showMessage(__('MySQL returned an empty result set (i.e. zero rows).'), $local_query);
echo "\n";
include 'libraries/footer.inc.php';
} else {// end if (no row returned)
$meta = PMA_DBI_get_fields_meta($result[$key_id]);
list($unique_condition, $tmp_clause_is_unique)
= PMA_getUniqueCondition($result[$key_id], count($meta), $meta, $rows[$key_id], true);
if (! empty($unique_condition)) {
$found_unique_key = true;
}
unset($unique_condition, $tmp_clause_is_unique);
}
return $found_unique_key;
}
/**
* No primary key given, just load first row
*
* @param array $paramArray
* @return array
*/
function PMA_loadFirstRowInEditMode($paramArray )
{
list($table, $db) = $paramArray;
$result = PMA_DBI_query(
'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' LIMIT 1;',
null,
PMA_DBI_QUERY_STORE
);
$rows = array_fill(0, $GLOBALS['cfg']['InsertRows'], false);
return array($result, $rows);
}
/**
* Add some url parameters
*
* @param array $url_params
* @return array
*/
function PMA_urlParamsInEditMode($url_params)
{
if (isset($_REQUEST['where_clause'])) {
$url_params['where_clause'] = trim($_REQUEST['where_clause']);
}
if (! empty($_REQUEST['sql_query'])) {
$url_params['sql_query'] = $_REQUEST['sql_query'];
}
return $url_params;
}
/**
* Show function fields in data edit view in pma
*
* @param array $url_params
* @return string
*/
function PMA_showFunctionFieldsInEditMode($url_params, $showFuncFields)
{
$params = array();
if(! $showFuncFields) {
$params['ShowFunctionFields'] = 1;
} else {
$params['ShowFunctionFields'] = 0;
}
$params['ShowFieldTypesInDataEditView'] = $GLOBALS['cfg']['ShowFieldTypesInDataEditView'];
$params['goto'] = 'sql.php';
$this_url_params = array_merge($url_params, $params);
if(! $showFuncFields) {
return ' : <a href="tbl_change.php' . PMA_generate_common_url($this_url_params) . '">' . __('Function') . '</a>' . "\n";
}
return ' <th><a href="tbl_change.php' . PMA_generate_common_url($this_url_params) . '" title="' . __('Hide') . '">' . __('Function') . '</a></th>' . "\n";
}
/**
* Show field types in data edit view in pma
*
* @param array $url_params
* @return stirng
*/
function PMA_showColumnTypesInDataEditView($url_params, $showColumnType )
{
$params = array();
if(! $showColumnType) {
$params['ShowFieldTypesInDataEditView'] = 1;
} else {
$params['ShowFieldTypesInDataEditView'] = 0;
}
$params['ShowFunctionFields'] = $GLOBALS['cfg']['ShowFunctionFields'];
$params['goto'] = 'sql.php';
$this_other_url_params = array_merge($url_params, $params);
if(! $showColumnType) {
return ' : <a href="tbl_change.php' . PMA_generate_common_url($this_other_url_params) . '">' . __('Type') . '</a>' . "\n";
}
return ' <th><a href="tbl_change.php' . PMA_generate_common_url($this_other_url_params) . '" title="' . __('Hide') . '">' . __('Type') . '</a></th>' . "\n";
}
/**
* Retrieve the default for datetime data type
*
* @param array $table_fields
*/
function PMA_getDefaultForDatetime($field)
{
// d a t e t i m e
//
// Current date should not be set as default if the field is NULL
// for the current row, but do not put here the current datetime
// if there is a default value (the real default value will be set
// in the Default value logic below)
// Note: (tested in MySQL 4.0.16): when lang is some UTF-8,
// $field['Default'] is not set if it contains NULL:
// Array ([Field] => d [Type] => datetime [Null] => YES [Key] => [Extra] => [True_Type] => datetime)
// but, look what we get if we switch to iso: (Default is NULL)
// Array ([Field] => d [Type] => datetime [Null] => YES [Key] => [Default] => [Extra] => [True_Type] => datetime)
// so I force a NULL into it (I don't think it's possible
// to have an empty default value for DATETIME)
// then, the "if" after this one will work
if ($field['Type'] == 'datetime'
&& ! isset($field['Default'])
&& isset($field['Null'])
&& $field['Null'] == 'YES'
) {
$field['Default'] = null;
}
}
/**
* Analyze the table column array
*
* @param array $field
* @param array $comments_map
* @return type
*/
function PMA_analyzeTableColumnsArray($field, $comments_map, $timestamp_seen)
{
$field['Field_html'] = htmlspecialchars($field['Field']);
$field['Field_md5'] = md5($field['Field']);
// True_Type contains only the type (stops at first bracket)
$field['True_Type'] = preg_replace('@\(.*@s', '', $field['Type']);
PMA_getDefaultForDatetime($field);
$field['len'] = preg_match('@float|double@', $field['Type']) ? 100 : -1;
$field['Field_title'] = PMA_getColumnTitle($field, $comments_map);
$field['is_binary'] = PMA_isTableColumnBinary($field);
$field['is_blob'] = PMA_istableColumnBlob($field);
$field['is_char'] = PMA_isTableColumnChar($field);
list($field['pma_type'], $field['wrap'], $field['first_timestamp']) =
PMA_getEnumSetAndTimestampTableColumns($field, $timestamp_seen);
return $field;
}
/**
* Retrieve the column title
*
* @param array $field
* @param array $comments_map
* @return string
*/
function PMA_getColumnTitle($field, $comments_map)
{
if (isset($comments_map[$field['Field']])) {
return '<span style="border-bottom: 1px dashed black;" title="'
. htmlspecialchars($comments_map[$field['Field']]) . '">'
. $field['Field_html'] . '</span>';
} else {
return $field['Field_html'];
}
}
/**
* check is table column bainary
*
* @param array $field
* @return boolean
*/
function PMA_isTableColumnBinary($field)
{
// The type column.
// Fix for bug #3152931 'ENUM and SET cannot have "Binary" option'
// If check to ensure types such as "enum('one','two','binary',..)" or
// "enum('one','two','varbinary',..)" are not categorized as binary.
if (stripos($field['Type'], 'binary') === 0
|| stripos($field['Type'], 'varbinary') === 0
) {
return stristr($field['Type'], 'binary');
} else {
return false;
}
}
/**
* check is table column blob
*
* @param array $field
* @return boolean
*/
function PMA_istableColumnBlob($field)
{
// If check to ensure types such as "enum('one','two','blob',..)" or
// "enum('one','two','tinyblob',..)" etc. are not categorized as blob.
if (stripos($field['Type'], 'blob') === 0
|| stripos($field['Type'], 'tinyblob') === 0
|| stripos($field['Type'], 'mediumblob') === 0
|| stripos($field['Type'], 'longblob') === 0
) {
return stristr($field['Type'], 'blob');
} else {
return false;
}
}
/**
* check is table column char
*
* @param array $field
* @return boolean
*/
function PMA_isTableColumnChar($field)
{
// If check to ensure types such as "enum('one','two','char',..)" or
// "enum('one','two','varchar',..)" are not categorized as char.
if (stripos($field['Type'], 'char') === 0
|| stripos($field['Type'], 'varchar') === 0
) {
return stristr($field['Type'], 'char');
} else {
return false;
}
}
/**
* Retieve set, enum, timestamp table columns
*
* @param array $field
* @param int $timestamp_seen
*/
function PMA_getEnumSetAndTimestampTableColumns($field, $timestamp_seen)
{
$field['first_timestamp'] = false;
switch ($field['True_Type']) {
case 'set':
$field['pma_type'] = 'set';
$field['wrap'] = '';
break;
case 'enum':
$field['pma_type'] = 'enum';
$field['wrap'] = '';
break;
case 'timestamp':
if (!$timestamp_seen) { // can only occur once per table
$timestamp_seen = 1;
$field['first_timestamp'] = true;
}
$field['pma_type'] = $field['Type'];
$field['wrap'] = ' nowrap';
break;
default:
$field['pma_type'] = $field['Type'];
$field['wrap'] = ' nowrap';
break;
}
return array($field['pma_type'], $field['wrap'], $field['first_timestamp']);
}
/**
* The function column
* We don't want binary data to be destroyed
* Note: from the MySQL manual: "BINARY doesn't affect how the column is
* stored or retrieved" so it does not mean that the contents is binary
*
* @param array $params_for_function_column
* @return string $html_output
*/
function PMA_getFunctionColumn($params_for_function_column)
{
list($field, $is_upload, $field_name_appendix, $unnullify_trigger,
$no_support_types, $tabindex, $tabindex_for_function,
$idindex, $insert_mode) = $params_for_function_column;
$html_output = '';
if (($GLOBALS['cfg']['ProtectBinary'] && $field['is_blob'] && !$is_upload)
|| ($GLOBALS['cfg']['ProtectBinary'] == 'all' && $field['is_binary'])
|| ($GLOBALS['cfg']['ProtectBinary'] == 'noblob' && !$field['is_blob'])
) {
$html_output .= ' <td class="center">' . __('Binary') . '</td>' . "\n";
} elseif (strstr($field['True_Type'], 'enum')
|| strstr($field['True_Type'], 'set')
|| in_array($field['pma_type'], $no_support_types)
) {
$html_output .= ' <td class="center">--</td>' . "\n";
} else {
$html_output .= '<td>' . "\n";
$html_output .= '<select name="funcs' . $field_name_appendix . '"' . $unnullify_trigger
. 'tabindex="' . ($tabindex + $tabindex_for_function) . '" id="field_' . $idindex . '_1">';
$html_output .= PMA_getFunctionsForField($field, $insert_mode) . "\n";
$html_output .= '</select>' . "\n";
$html_output .= '</td>' . "\n";
}
return $html_output;
}
/**
* The null column
*
* @param array $params_for_null_column
* @return string $html_output
*/
function PMA_getNullColumn($params_for_null_column)
{
list($field, $field_name_appendix, $real_null_value, $tabindex, $tabindex_for_null,
$idindex, $vkey, $foreigners, $foreignData) = $params_for_null_column;
$html_output = '';
$html_output .= ' <td>' . "\n";
if ($field['Null'] == 'YES') {
$html_output .= ' <input type="hidden" name="fields_null_prev' . $field_name_appendix . '"';
if ($real_null_value && !$field['first_timestamp']) {
$html_output .= ' value="on"';
}
$html_output .= ' />' . "\n";
$html_output .= ' <input type="checkbox" class="checkbox_null" tabindex="' . ($tabindex + $tabindex_for_null) . '"'
. ' name="fields_null' . $field_name_appendix . '"';
if ($real_null_value && !$field['first_timestamp']) {
$html_output .= ' checked="checked"';
}
$html_output .= ' id="field_' . ($idindex) . '_2" />';
// nullify_code is needed by the js nullify() function
$nullify_code = PMA_getNullifyCodeForNullColumn($field, $foreigners, $foreignData);
// to be able to generate calls to nullify() in jQuery
$html_output .= '<input type="hidden" class="nullify_code" name="nullify_code'
. $field_name_appendix . '" value="' . $nullify_code . '" />';
$html_output .= '<input type="hidden" class="hashed_field" name="hashed_field'
. $field_name_appendix . '" value="' . $field['Field_md5'] . '" />';
$html_output .= '<input type="hidden" class="multi_edit" name="multi_edit'
. $field_name_appendix . '" value="' . PMA_escapeJsString($vkey) . '" />';
}
$html_output .= ' </td>' . "\n";
return $html_output;
}
/**
* Retrieve the nullify code for the null column
*
* @param array $field
* @param array $foreigners
* @param array $foreignData
* @return integer
*/
function PMA_getNullifyCodeForNullColumn($field, $foreigners, $foreignData)
{
if (strstr($field['True_Type'], 'enum')) {
if (strlen($field['Type']) > 20) {
$nullify_code = '1';
} else {
$nullify_code = '2';
}
} elseif (strstr($field['True_Type'], 'set')) {
$nullify_code = '3';
} elseif ($foreigners && isset($foreigners[$field['Field']]) && $foreignData['foreign_link'] == false) {
// foreign key in a drop-down
$nullify_code = '4';
} elseif ($foreigners && isset($foreigners[$field['Field']]) && $foreignData['foreign_link'] == true) {
// foreign key with a browsing icon
$nullify_code = '6';
} else {
$nullify_code = '5';
}
return $nullify_code;
}
?>